From ee61120da6ca6620101462cdc36d87c5c19e8fb9 Mon Sep 17 00:00:00 2001 From: Giancarlo Romeo Date: Fri, 22 Nov 2024 11:31:19 +0100 Subject: [PATCH 1/2] use YARL to build Url --- packages/service-library/requirements/_base.in | 1 + packages/service-library/requirements/_base.txt | 1 + .../service-library/src/servicelib/aiohttp/tracing.py | 9 +++++---- .../service-library/src/servicelib/fastapi/tracing.py | 10 +++++++++- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/service-library/requirements/_base.in b/packages/service-library/requirements/_base.in index 4e4ecd929e4..5a51d883b74 100644 --- a/packages/service-library/requirements/_base.in +++ b/packages/service-library/requirements/_base.in @@ -30,3 +30,4 @@ repro-zipfile tenacity toolz tqdm +yarl diff --git a/packages/service-library/requirements/_base.txt b/packages/service-library/requirements/_base.txt index 4ed45283e9e..7bfebd08206 100644 --- a/packages/service-library/requirements/_base.txt +++ b/packages/service-library/requirements/_base.txt @@ -274,6 +274,7 @@ wrapt==1.16.0 # opentelemetry-instrumentation-redis yarl==1.12.1 # via + # -r requirements/_base.in # aio-pika # aiohttp # aiormq diff --git a/packages/service-library/src/servicelib/aiohttp/tracing.py b/packages/service-library/src/servicelib/aiohttp/tracing.py index 3da3b28e3b3..2303bf2b5e0 100644 --- a/packages/service-library/src/servicelib/aiohttp/tracing.py +++ b/packages/service-library/src/servicelib/aiohttp/tracing.py @@ -20,6 +20,7 @@ from opentelemetry.sdk.trace.export import BatchSpanProcessor from servicelib.logging_utils import log_context from settings_library.tracing import TracingSettings +from yarl import URL _logger = logging.getLogger(__name__) try: @@ -54,7 +55,7 @@ def setup_tracing( """ _ = app opentelemetry_collector_endpoint = ( - tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_ENDPOINT + f"{tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_ENDPOINT}" ) opentelemetry_collector_port = tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_PORT if not opentelemetry_collector_endpoint and not opentelemetry_collector_port: @@ -72,9 +73,9 @@ def setup_tracing( resource = Resource(attributes={"service.name": service_name}) trace.set_tracer_provider(TracerProvider(resource=resource)) tracer_provider: trace.TracerProvider = trace.get_tracer_provider() - tracing_destination: str = ( - f"{opentelemetry_collector_endpoint}:{opentelemetry_collector_port}/v1/traces" - ) + + opentelemetry_collector_path = "/v1/traces" + tracing_destination: str = f"{URL(opentelemetry_collector_endpoint).with_port(opentelemetry_collector_port).with_path(opentelemetry_collector_path)}" _logger.info( "Trying to connect service %s to tracing collector at %s.", diff --git a/packages/service-library/src/servicelib/fastapi/tracing.py b/packages/service-library/src/servicelib/fastapi/tracing.py index 36e9b06fa12..4fdd9258c03 100644 --- a/packages/service-library/src/servicelib/fastapi/tracing.py +++ b/packages/service-library/src/servicelib/fastapi/tracing.py @@ -17,6 +17,7 @@ from opentelemetry.sdk.trace.export import BatchSpanProcessor from servicelib.logging_utils import log_context from settings_library.tracing import TracingSettings +from yarl import URL _logger = logging.getLogger(__name__) @@ -75,7 +76,14 @@ def setup_tracing( trace.set_tracer_provider(TracerProvider(resource=resource)) global_tracer_provider = trace.get_tracer_provider() assert isinstance(global_tracer_provider, TracerProvider) # nosec - tracing_destination: str = f"{tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_ENDPOINT}:{tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_PORT}/v1/traces" + + opentelemetry_collector_endpoint: str = ( + f"{tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_ENDPOINT}" + ) + + opentelemetry_collector_path = "/v1/traces" + tracing_destination: str = f"{URL(opentelemetry_collector_endpoint).with_port(tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_PORT).with_path(opentelemetry_collector_path)}" + _logger.info( "Trying to connect service %s to opentelemetry tracing collector at %s.", service_name, From ce223cbb309de266aa861f7466724ca98dd4160c Mon Sep 17 00:00:00 2001 From: Giancarlo Romeo Date: Fri, 22 Nov 2024 12:14:45 +0100 Subject: [PATCH 2/2] remove var --- packages/service-library/src/servicelib/aiohttp/tracing.py | 3 +-- packages/service-library/src/servicelib/fastapi/tracing.py | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/service-library/src/servicelib/aiohttp/tracing.py b/packages/service-library/src/servicelib/aiohttp/tracing.py index 2303bf2b5e0..199f4836976 100644 --- a/packages/service-library/src/servicelib/aiohttp/tracing.py +++ b/packages/service-library/src/servicelib/aiohttp/tracing.py @@ -74,8 +74,7 @@ def setup_tracing( trace.set_tracer_provider(TracerProvider(resource=resource)) tracer_provider: trace.TracerProvider = trace.get_tracer_provider() - opentelemetry_collector_path = "/v1/traces" - tracing_destination: str = f"{URL(opentelemetry_collector_endpoint).with_port(opentelemetry_collector_port).with_path(opentelemetry_collector_path)}" + tracing_destination: str = f"{URL(opentelemetry_collector_endpoint).with_port(opentelemetry_collector_port).with_path('/v1/traces')}" _logger.info( "Trying to connect service %s to tracing collector at %s.", diff --git a/packages/service-library/src/servicelib/fastapi/tracing.py b/packages/service-library/src/servicelib/fastapi/tracing.py index 4fdd9258c03..ee0258d93f4 100644 --- a/packages/service-library/src/servicelib/fastapi/tracing.py +++ b/packages/service-library/src/servicelib/fastapi/tracing.py @@ -81,8 +81,7 @@ def setup_tracing( f"{tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_ENDPOINT}" ) - opentelemetry_collector_path = "/v1/traces" - tracing_destination: str = f"{URL(opentelemetry_collector_endpoint).with_port(tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_PORT).with_path(opentelemetry_collector_path)}" + tracing_destination: str = f"{URL(opentelemetry_collector_endpoint).with_port(tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_PORT).with_path('/v1/traces')}" _logger.info( "Trying to connect service %s to opentelemetry tracing collector at %s.",