Skip to content

Commit

Permalink
upgrade neuro-logging to 24.4.0 (#888)
Browse files Browse the repository at this point in the history
  • Loading branch information
zubenkoivan authored Nov 20, 2024
1 parent 581e2b0 commit a34d593
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 163 deletions.
12 changes: 4 additions & 8 deletions charts/platform-registry/templates/deployment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,13 @@ spec:
{{- if .Values.platform.token }}
{{ toYaml .Values.platform.token | indent 10 }}
{{- end }}
{{- if .Values.zipkin }}
- name: NP_ZIPKIN_URL
value: {{ .Values.zipkin.url }}
- name: NP_ZIPKIN_SAMPLE_RATE
value: {{ .Values.zipkin.sampleRate | default 0 | quote }}
{{- end }}
{{- if .Values.sentry }}
- name: NP_SENTRY_DSN
- name: SENTRY_DSN
value: {{ .Values.sentry.dsn }}
- name: NP_SENTRY_CLUSTER_NAME
- name: SENTRY_CLUSTER_NAME
value: {{ .Values.sentry.clusterName }}
- name: SENTRY_APP_NAME
value: {{ .Values.sentry.appName }}
- name: NP_SENTRY_SAMPLE_RATE
value: {{ .Values.sentry.sampleRate | default 0 | quote }}
{{- end }}
Expand Down
6 changes: 3 additions & 3 deletions charts/platform-registry/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ ingress:
service:
annotations: {}

zipkin: {}

sentry: {}
sentry:
appName: platform-registry
sampleRate: 0.002

priorityClassName: ""
49 changes: 3 additions & 46 deletions platform_registry_api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from aiohttp.web import (
Application,
HTTPBadRequest,
HTTPForbidden,
HTTPNotFound,
HTTPUnauthorized,
Request,
Expand All @@ -44,11 +45,7 @@
from neuro_auth_client.security import AuthScheme, setup_security
from neuro_logging import (
init_logging,
make_sentry_trace_config,
make_zipkin_trace_config,
setup_sentry,
setup_zipkin,
setup_zipkin_tracer,
trace,
)
from yarl import URL
Expand Down Expand Up @@ -914,18 +911,6 @@ async def add_version_to_header(request: Request, response: StreamResponse) -> N
response.headers["X-Service-Version"] = f"platform-registry-api/{package_version}"


def make_tracing_trace_configs(config: Config) -> list[aiohttp.TraceConfig]:
trace_configs = []

if config.zipkin:
trace_configs.append(make_zipkin_trace_config())

if config.sentry:
trace_configs.append(make_sentry_trace_config())

return trace_configs


async def create_app(config: Config) -> aiohttp.web.Application:
app = aiohttp.web.Application()

Expand All @@ -946,7 +931,6 @@ async def on_request_redirect(

session = await exit_stack.enter_async_context(
aiohttp.ClientSession(
trace_configs=[trace_config] + make_tracing_trace_configs(config),
connector=aiohttp.TCPConnector(force_close=True),
)
)
Expand All @@ -967,24 +951,19 @@ async def on_request_redirect(
)

if is_aws:
client = app["v2_app"]["upstream"]._client
exclude = [client.exceptions.RepositoryNotFoundException]
else:
exclude = []
app["v2_app"]["upstream"]._client

auth_client = await exit_stack.enter_async_context(
AuthClient(
url=config.auth.server_endpoint_url,
token=config.auth.service_token,
trace_configs=make_tracing_trace_configs(config),
)
)
app["v2_app"]["auth_client"] = auth_client

await setup_security(
app=app, auth_client=auth_client, auth_scheme=AuthScheme.BASIC
)
setup_tracing(config, exclude)

yield

Expand All @@ -999,39 +978,17 @@ async def on_request_redirect(

app.on_response_prepare.append(add_version_to_header)

if config.zipkin:
setup_zipkin(app)

return app


def setup_tracing(config: Config, exclude: list[type[BaseException]]) -> None:
if config.zipkin:
setup_zipkin_tracer(
config.zipkin.app_name,
config.server.host,
config.server.port,
config.zipkin.url,
config.zipkin.sample_rate,
)

if config.sentry:
setup_sentry(
config.sentry.dsn,
app_name=config.sentry.app_name,
cluster_name=config.sentry.cluster_name,
sample_rate=config.sentry.sample_rate,
exclude=exclude,
)


def main() -> None:
init_logging()

loop = asyncio.get_event_loop()

config = EnvironConfigFactory().create()
logger.info("Loaded config: %r", config)
setup_sentry(ignore_errors=[HTTPUnauthorized, HTTPForbidden])
app = loop.run_until_complete(create_app(config))
aiohttp.web.run_app(app, host=config.server.host, port=config.server.port)

Expand Down
45 changes: 0 additions & 45 deletions platform_registry_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,12 @@ def is_oauth(self) -> bool:
return self.type == UpstreamType.OAUTH


@dataclass(frozen=True)
class ZipkinConfig:
url: URL
app_name: str = "platform-registry"
sample_rate: float = 0


@dataclass(frozen=True)
class SentryConfig:
dsn: URL
cluster_name: str
app_name: str = "platform-registry"
sample_rate: float = 0


@dataclass(frozen=True)
class Config:
server: ServerConfig
upstream_registry: UpstreamRegistryConfig
auth: AuthConfig
cluster_name: str
zipkin: ZipkinConfig | None = None
sentry: SentryConfig | None = None


class EnvironConfigFactory:
Expand Down Expand Up @@ -158,43 +141,15 @@ def create_auth(self) -> AuthConfig:
token = self._environ["NP_REGISTRY_AUTH_TOKEN"]
return AuthConfig(server_endpoint_url=url, service_token=token)

def create_zipkin(self) -> ZipkinConfig | None:
if "NP_ZIPKIN_URL" not in self._environ:
return None

url = URL(self._environ["NP_ZIPKIN_URL"])
app_name = self._environ.get("NP_ZIPKIN_APP_NAME", ZipkinConfig.app_name)
sample_rate = float(
self._environ.get("NP_ZIPKIN_SAMPLE_RATE", ZipkinConfig.sample_rate)
)
return ZipkinConfig(url=url, app_name=app_name, sample_rate=sample_rate)

def create_sentry(self) -> SentryConfig | None:
if "NP_SENTRY_DSN" not in self._environ:
return None

return SentryConfig(
dsn=URL(self._environ["NP_SENTRY_DSN"]),
cluster_name=self._environ["NP_SENTRY_CLUSTER_NAME"],
app_name=self._environ.get("NP_SENTRY_APP_NAME", SentryConfig.app_name),
sample_rate=float(
self._environ.get("NP_SENTRY_SAMPLE_RATE", SentryConfig.sample_rate)
),
)

def create(self) -> Config:
server_config = self.create_server()
upstream_registry_config = self.create_upstream_registry()
auth_config = self.create_auth()
zipkin_config = self.create_zipkin()
sentry_config = self.create_sentry()
cluster_name = self._environ["NP_CLUSTER_NAME"]
assert cluster_name
return Config(
server=server_config,
upstream_registry=upstream_registry_config,
auth=auth_config,
cluster_name=cluster_name,
zipkin=zipkin_config,
sentry=sentry_config,
)
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ install_requires =
# have it fixed. See the issue above for testing instructions.
uvloop==0.19.0
aiobotocore==2.12.3
neuro-logging==21.12.2
neuro-logging==24.4.0
trafaret==2.1.1
yarl==1.9.4

Expand Down
60 changes: 0 additions & 60 deletions tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
AuthConfig,
Config,
EnvironConfigFactory,
SentryConfig,
ServerConfig,
UpstreamRegistryConfig,
UpstreamType,
ZipkinConfig,
)


Expand Down Expand Up @@ -64,9 +62,6 @@ def test_oauth(self) -> None:
"NP_REGISTRY_UPSTREAM_TOKEN_REGISTRY_SCOPE": "",
"NP_REGISTRY_UPSTREAM_TOKEN_REPO_SCOPE_ACTIONS": "push,pull",
"NP_CLUSTER_NAME": "test-cluster",
"NP_ZIPKIN_URL": "http://zipkin.io:9411/",
"NP_SENTRY_DSN": "https://sentry",
"NP_SENTRY_CLUSTER_NAME": "test",
}
config = EnvironConfigFactory(environ=environ).create()
assert config == Config(
Expand All @@ -88,8 +83,6 @@ def test_oauth(self) -> None:
service_token="test_auth_token",
),
cluster_name="test-cluster",
zipkin=ZipkinConfig(URL("http://zipkin.io:9411/")),
sentry=SentryConfig(dsn=URL("https://sentry"), cluster_name="test"),
)
assert config.upstream_registry.is_oauth

Expand Down Expand Up @@ -179,56 +172,3 @@ def test_basic(self) -> None:
)
assert config.upstream_registry.is_basic
assert not config.upstream_registry.is_oauth

def test_create_zipkin_none(self) -> None:
result = EnvironConfigFactory({}).create_zipkin()

assert result is None

def test_create_zipkin_default(self) -> None:
env = {"NP_ZIPKIN_URL": "https://zipkin:9411"}
result = EnvironConfigFactory(env).create_zipkin()

assert result == ZipkinConfig(url=URL("https://zipkin:9411"))

def test_create_zipkin_custom(self) -> None:
env = {
"NP_ZIPKIN_URL": "https://zipkin:9411",
"NP_ZIPKIN_APP_NAME": "api",
"NP_ZIPKIN_SAMPLE_RATE": "1",
}
result = EnvironConfigFactory(env).create_zipkin()

assert result == ZipkinConfig(
url=URL("https://zipkin:9411"), app_name="api", sample_rate=1
)

def test_create_sentry_none(self) -> None:
result = EnvironConfigFactory({}).create_sentry()

assert result is None

def test_create_sentry_default(self) -> None:
env = {
"NP_SENTRY_DSN": "https://sentry",
"NP_SENTRY_CLUSTER_NAME": "test",
}
result = EnvironConfigFactory(env).create_sentry()

assert result == SentryConfig(dsn=URL("https://sentry"), cluster_name="test")

def test_create_sentry_custom(self) -> None:
env = {
"NP_SENTRY_DSN": "https://sentry",
"NP_SENTRY_APP_NAME": "api",
"NP_SENTRY_CLUSTER_NAME": "test",
"NP_SENTRY_SAMPLE_RATE": "1",
}
result = EnvironConfigFactory(env).create_sentry()

assert result == SentryConfig(
dsn=URL("https://sentry"),
app_name="api",
cluster_name="test",
sample_rate=1,
)

0 comments on commit a34d593

Please sign in to comment.