From a34d593b6ab490359ce09cae2b1572e16015fd11 Mon Sep 17 00:00:00 2001 From: Ivan Zubenko Date: Wed, 20 Nov 2024 22:11:21 +0200 Subject: [PATCH] upgrade neuro-logging to 24.4.0 (#888) --- .../templates/deployment.yml | 12 ++-- charts/platform-registry/values.yaml | 6 +- platform_registry_api/api.py | 49 +-------------- platform_registry_api/config.py | 45 -------------- setup.cfg | 2 +- tests/unit/test_config.py | 60 ------------------- 6 files changed, 11 insertions(+), 163 deletions(-) diff --git a/charts/platform-registry/templates/deployment.yml b/charts/platform-registry/templates/deployment.yml index a2cf1f83..1dfa55dc 100644 --- a/charts/platform-registry/templates/deployment.yml +++ b/charts/platform-registry/templates/deployment.yml @@ -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 }} diff --git a/charts/platform-registry/values.yaml b/charts/platform-registry/values.yaml index 3a775426..bec71e3c 100644 --- a/charts/platform-registry/values.yaml +++ b/charts/platform-registry/values.yaml @@ -39,8 +39,8 @@ ingress: service: annotations: {} -zipkin: {} - -sentry: {} +sentry: + appName: platform-registry + sampleRate: 0.002 priorityClassName: "" diff --git a/platform_registry_api/api.py b/platform_registry_api/api.py index 8710c6bd..e692cca6 100644 --- a/platform_registry_api/api.py +++ b/platform_registry_api/api.py @@ -31,6 +31,7 @@ from aiohttp.web import ( Application, HTTPBadRequest, + HTTPForbidden, HTTPNotFound, HTTPUnauthorized, Request, @@ -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 @@ -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() @@ -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), ) ) @@ -967,16 +951,12 @@ 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 @@ -984,7 +964,6 @@ async def on_request_redirect( await setup_security( app=app, auth_client=auth_client, auth_scheme=AuthScheme.BASIC ) - setup_tracing(config, exclude) yield @@ -999,32 +978,9 @@ 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() @@ -1032,6 +988,7 @@ def main() -> None: 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) diff --git a/platform_registry_api/config.py b/platform_registry_api/config.py index ef93a764..b6af5721 100644 --- a/platform_registry_api/config.py +++ b/platform_registry_api/config.py @@ -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: @@ -158,36 +141,10 @@ 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( @@ -195,6 +152,4 @@ def create(self) -> Config: upstream_registry=upstream_registry_config, auth=auth_config, cluster_name=cluster_name, - zipkin=zipkin_config, - sentry=sentry_config, ) diff --git a/setup.cfg b/setup.cfg index 36669b98..5c25dae6 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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 diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py index 706e6ac6..66305e0d 100644 --- a/tests/unit/test_config.py +++ b/tests/unit/test_config.py @@ -4,11 +4,9 @@ AuthConfig, Config, EnvironConfigFactory, - SentryConfig, ServerConfig, UpstreamRegistryConfig, UpstreamType, - ZipkinConfig, ) @@ -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( @@ -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 @@ -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, - )