Skip to content

Commit

Permalink
Include domain in message for outgoing HTTP requests: fix for old sem…
Browse files Browse the repository at this point in the history
…conv (#909)
  • Loading branch information
alexmojaki authored Mar 5, 2025
1 parent 4725329 commit eccec01
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
9 changes: 8 additions & 1 deletion logfire/_internal/exporters/processor_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Any
from urllib.parse import parse_qs, urlparse

from opentelemetry import context
Expand Down Expand Up @@ -217,11 +218,17 @@ def _tweak_http_spans(span: ReadableSpanDict):
message_target = target
if span['kind'] == SpanKind.CLIENT:
# For outgoing requests, we also want the domain, not just the path.
server_name = (
server_name: Any = (
attributes.get(SpanAttributes.SERVER_ADDRESS)
or attributes.get(SpanAttributes.HTTP_SERVER_NAME)
or attributes.get(SpanAttributes.HTTP_HOST)
)
if not server_name:
try:
server_name = urlparse(url).hostname # type: ignore
except Exception: # pragma: no cover
pass
server_name = server_name or url
if server_name and isinstance(server_name, str): # pragma: no branch
message_target = server_name + message_target
messages.append(message_target)
Expand Down
34 changes: 34 additions & 0 deletions tests/otel_integrations/test_httpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from dirty_equals import IsStr
from httpx import Request
from inline_snapshot import snapshot
from opentelemetry.instrumentation._semconv import _OpenTelemetrySemanticConventionStability # type: ignore
from opentelemetry.instrumentation.httpx import RequestInfo, ResponseInfo
from opentelemetry.trace.span import Span

Expand Down Expand Up @@ -98,6 +99,39 @@ def test_httpx_client_instrumentation(exporter: TestExporter):
)


def test_httpx_client_instrumentation_old_semconv(exporter: TestExporter):
with mock.patch.dict('os.environ', {'OTEL_SEMCONV_STABILITY_OPT_IN': ''}):
with httpx.Client(transport=create_transport()) as client:
# Pick up the new value of OTEL_SEMCONV_STABILITY_OPT_IN
_OpenTelemetrySemanticConventionStability._initialized = False # type: ignore

logfire.instrument_httpx(client)
client.get('https://example.org:8080/foo')

# Now let other tests get the original value set in conftest.py
_OpenTelemetrySemanticConventionStability._initialized = False # type: ignore

assert exporter.exported_spans_as_dict() == snapshot(
[
{
'name': 'GET',
'context': {'trace_id': 1, 'span_id': 1, 'is_remote': False},
'parent': None,
'start_time': 1000000000,
'end_time': 2000000000,
'attributes': {
'http.method': 'GET',
'http.url': 'https://example.org:8080/foo',
'logfire.span_type': 'span',
'logfire.msg': 'GET example.org/foo',
'http.status_code': 200,
'http.target': '/foo',
},
}
]
)


async def test_async_httpx_client_instrumentation(exporter: TestExporter):
with check_traceparent_header() as checker:
async with httpx.AsyncClient(transport=create_transport()) as client:
Expand Down

0 comments on commit eccec01

Please sign in to comment.