Skip to content

Commit eccec01

Browse files
authored
Include domain in message for outgoing HTTP requests: fix for old semconv (#909)
1 parent 4725329 commit eccec01

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

logfire/_internal/exporters/processor_wrapper.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from dataclasses import dataclass
4+
from typing import Any
45
from urllib.parse import parse_qs, urlparse
56

67
from opentelemetry import context
@@ -217,11 +218,17 @@ def _tweak_http_spans(span: ReadableSpanDict):
217218
message_target = target
218219
if span['kind'] == SpanKind.CLIENT:
219220
# For outgoing requests, we also want the domain, not just the path.
220-
server_name = (
221+
server_name: Any = (
221222
attributes.get(SpanAttributes.SERVER_ADDRESS)
222223
or attributes.get(SpanAttributes.HTTP_SERVER_NAME)
223224
or attributes.get(SpanAttributes.HTTP_HOST)
224225
)
226+
if not server_name:
227+
try:
228+
server_name = urlparse(url).hostname # type: ignore
229+
except Exception: # pragma: no cover
230+
pass
231+
server_name = server_name or url
225232
if server_name and isinstance(server_name, str): # pragma: no branch
226233
message_target = server_name + message_target
227234
messages.append(message_target)

tests/otel_integrations/test_httpx.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from dirty_equals import IsStr
1111
from httpx import Request
1212
from inline_snapshot import snapshot
13+
from opentelemetry.instrumentation._semconv import _OpenTelemetrySemanticConventionStability # type: ignore
1314
from opentelemetry.instrumentation.httpx import RequestInfo, ResponseInfo
1415
from opentelemetry.trace.span import Span
1516

@@ -98,6 +99,39 @@ def test_httpx_client_instrumentation(exporter: TestExporter):
9899
)
99100

100101

102+
def test_httpx_client_instrumentation_old_semconv(exporter: TestExporter):
103+
with mock.patch.dict('os.environ', {'OTEL_SEMCONV_STABILITY_OPT_IN': ''}):
104+
with httpx.Client(transport=create_transport()) as client:
105+
# Pick up the new value of OTEL_SEMCONV_STABILITY_OPT_IN
106+
_OpenTelemetrySemanticConventionStability._initialized = False # type: ignore
107+
108+
logfire.instrument_httpx(client)
109+
client.get('https://example.org:8080/foo')
110+
111+
# Now let other tests get the original value set in conftest.py
112+
_OpenTelemetrySemanticConventionStability._initialized = False # type: ignore
113+
114+
assert exporter.exported_spans_as_dict() == snapshot(
115+
[
116+
{
117+
'name': 'GET',
118+
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
119+
'parent': None,
120+
'start_time': 1000000000,
121+
'end_time': 2000000000,
122+
'attributes': {
123+
'http.method': 'GET',
124+
'http.url': 'https://example.org:8080/foo',
125+
'logfire.span_type': 'span',
126+
'logfire.msg': 'GET example.org/foo',
127+
'http.status_code': 200,
128+
'http.target': '/foo',
129+
},
130+
}
131+
]
132+
)
133+
134+
101135
async def test_async_httpx_client_instrumentation(exporter: TestExporter):
102136
with check_traceparent_header() as checker:
103137
async with httpx.AsyncClient(transport=create_transport()) as client:

0 commit comments

Comments
 (0)