Skip to content

Commit ee61120

Browse files
use YARL to build Url
1 parent ba881e9 commit ee61120

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

packages/service-library/requirements/_base.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ repro-zipfile
3030
tenacity
3131
toolz
3232
tqdm
33+
yarl

packages/service-library/requirements/_base.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ wrapt==1.16.0
274274
# opentelemetry-instrumentation-redis
275275
yarl==1.12.1
276276
# via
277+
# -r requirements/_base.in
277278
# aio-pika
278279
# aiohttp
279280
# aiormq

packages/service-library/src/servicelib/aiohttp/tracing.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from opentelemetry.sdk.trace.export import BatchSpanProcessor
2121
from servicelib.logging_utils import log_context
2222
from settings_library.tracing import TracingSettings
23+
from yarl import URL
2324

2425
_logger = logging.getLogger(__name__)
2526
try:
@@ -54,7 +55,7 @@ def setup_tracing(
5455
"""
5556
_ = app
5657
opentelemetry_collector_endpoint = (
57-
tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_ENDPOINT
58+
f"{tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_ENDPOINT}"
5859
)
5960
opentelemetry_collector_port = tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_PORT
6061
if not opentelemetry_collector_endpoint and not opentelemetry_collector_port:
@@ -72,9 +73,9 @@ def setup_tracing(
7273
resource = Resource(attributes={"service.name": service_name})
7374
trace.set_tracer_provider(TracerProvider(resource=resource))
7475
tracer_provider: trace.TracerProvider = trace.get_tracer_provider()
75-
tracing_destination: str = (
76-
f"{opentelemetry_collector_endpoint}:{opentelemetry_collector_port}/v1/traces"
77-
)
76+
77+
opentelemetry_collector_path = "/v1/traces"
78+
tracing_destination: str = f"{URL(opentelemetry_collector_endpoint).with_port(opentelemetry_collector_port).with_path(opentelemetry_collector_path)}"
7879

7980
_logger.info(
8081
"Trying to connect service %s to tracing collector at %s.",

packages/service-library/src/servicelib/fastapi/tracing.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from opentelemetry.sdk.trace.export import BatchSpanProcessor
1818
from servicelib.logging_utils import log_context
1919
from settings_library.tracing import TracingSettings
20+
from yarl import URL
2021

2122
_logger = logging.getLogger(__name__)
2223

@@ -75,7 +76,14 @@ def setup_tracing(
7576
trace.set_tracer_provider(TracerProvider(resource=resource))
7677
global_tracer_provider = trace.get_tracer_provider()
7778
assert isinstance(global_tracer_provider, TracerProvider) # nosec
78-
tracing_destination: str = f"{tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_ENDPOINT}:{tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_PORT}/v1/traces"
79+
80+
opentelemetry_collector_endpoint: str = (
81+
f"{tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_ENDPOINT}"
82+
)
83+
84+
opentelemetry_collector_path = "/v1/traces"
85+
tracing_destination: str = f"{URL(opentelemetry_collector_endpoint).with_port(tracing_settings.TRACING_OPENTELEMETRY_COLLECTOR_PORT).with_path(opentelemetry_collector_path)}"
86+
7987
_logger.info(
8088
"Trying to connect service %s to opentelemetry tracing collector at %s.",
8189
service_name,

0 commit comments

Comments
 (0)