Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/http/httpx/kiota_http/middleware/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,16 @@ async def send(self, request, transport):
return await self.next.send(request, transport)

def _create_observability_span(self, request, span_name: str) -> trace.Span:
"""Gets the parent_span from the request options and creates a new span.
If no parent_span is found, we try to get the current span."""
"""Gets the parent_span from the request options and creates a new span.
If no parent_span is found in the request, uses the parent_span in the
object. If parent_span is None, current context will be used."""
_span = None
if options := getattr(request, "options", None):
if parent_span := options.get("parent_span", None):
self.parent_span = parent_span
_context = trace.set_span_in_context(parent_span)
_span = tracer.start_span(span_name, _context)
if _span is None:
_span = trace.get_current_span()
_context = trace.set_span_in_context(self.parent_span)
_span = tracer.start_span(span_name, _context)
return _span
5 changes: 5 additions & 0 deletions packages/http/httpx/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ def mock_no_content_response(sample_headers):
tracer = trace.get_tracer(__name__)


@pytest.fixture
def otel_tracer():
return tracer


@pytest.fixture
def mock_otel_span():
return tracer.start_span("mock")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,21 @@ def test_next_is_none():
assert middleware.next is None


def test_span_created(request_info):
"""Ensures the current span is returned and the parent_span is not set."""
def test_span_returned(request_info):
"""Ensures a span is returned and the parent_span is not set."""
middleware = BaseMiddleware()
span = middleware._create_observability_span(request_info, "test_span_created")
span = middleware._create_observability_span(request_info, "test_span_returned")
span.end()
assert isinstance(span, trace.Span)
assert middleware.parent_span is None


def test_span_created(request_info, otel_tracer):
"""Ensures the span returned does not end an outer context managed span"""
with otel_tracer.start_as_current_span("outside-span") as outside_span:
before_state = outside_span.is_recording()
middleware = BaseMiddleware()
span = middleware._create_observability_span(request_info, "test_span_created")
span.end()
after_state = outside_span.is_recording()
assert(before_state == after_state)