Skip to content

Commit

Permalink
ignore pyramid request
Browse files Browse the repository at this point in the history
  • Loading branch information
acer618 committed Nov 14, 2024
1 parent c328e36 commit c3dc6e8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
11 changes: 5 additions & 6 deletions swagger_zipkin/otel_decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from swagger_zipkin.decorate_client import Resource

if TYPE_CHECKING:
import pyramid.request.Request # noqa: F401
import pyramid.request.Request # type: ignore # noqa: F401

T = TypeVar('T', covariant=True)
P = ParamSpec('P')
Expand Down Expand Up @@ -48,7 +48,7 @@ def with_headers(self, call_name: str, *args: Any, **kwargs: Any) -> Any:

# what is the right way to get the Request object. can we use contruct_request
# https://github.com/Yelp/bravado/blob/master/bravado/client.py#L283C5-L283C22
# this would create a bravado dependency.
# this would create a bravado dependency.
request = get_pyramid_current_request()
http_route = getattr(request, "matched_route", "")
http_request_method = getattr(request, "method", "")
Expand All @@ -61,8 +61,8 @@ def with_headers(self, call_name: str, *args: Any, **kwargs: Any) -> Any:
self.inject_otel_headers(kwargs, span)
self.inject_zipkin_headers(kwargs, span, parent_span)

# ideally the exception should be scoped for self.with_headers
# handle_exception should handle general excetion and the specific exception HTTPError
# ideally the exception should be scoped for self.with_headers
# handle_exception should handle general excetion and the specific exception HTTPError
# But this would create a dependency on bravado package. Is this ok?
# Assuming resource.operation does throw HTTPError
# https://github.com/Yelp/bravado/blob/master/bravado/exception.py
Expand Down Expand Up @@ -100,7 +100,6 @@ def inject_otel_headers(
carrier = kwargs['_request_options']["headers"]
propagator.inject(carrier=carrier, context=trace.set_span_in_context(current_span))


def inject_zipkin_headers(
self, kwargs: dict[str, Any], current_span: trace.Span, parent_span: trace.Span
) -> None:
Expand All @@ -114,7 +113,7 @@ def inject_zipkin_headers(
if parent_span is not None and parent_span.is_recording():
parent_span_context = parent_span.get_span_context()
kwargs["_request_options"]["headers"]["X-B3-ParentSpanId"] = format_span_id(
parent_span_context.span_id)
parent_span_context.span_id)

kwargs["_request_options"]["headers"]["X-B3-Sampled"] = (
"1"
Expand Down
8 changes: 3 additions & 5 deletions tests/otel_decorator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import pytest
from opentelemetry import trace
from opentelemetry.trace import SpanKind
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter
from opentelemetry.trace import SpanKind
from opentelemetry.trace.span import format_span_id
from opentelemetry.trace.span import format_trace_id

Expand Down Expand Up @@ -50,7 +50,7 @@ def create_request_options(parent_span: trace.Span, exported_span: trace.Span):
}
if parent_span is not None:
headers['headers']['X-B3-ParentSpanId'] = format_span_id(parent_span.get_span_context().span_id)

return headers


Expand Down Expand Up @@ -112,8 +112,6 @@ def test_client_request(mock_request, get_request, setup):
assert exported_span.attributes["http.response.status_code"] == "200"




@mock.patch(
"swagger_zipkin.otel_decorator.get_pyramid_current_request", autospec=True
)
Expand Down Expand Up @@ -185,5 +183,5 @@ def test_with_headers_exception(mock_request, get_request, setup):
assert exported_span.attributes["client.namespace"] == client_identifier
assert exported_span.attributes["peer.service"] == smartstack_namespace
assert exported_span.attributes["server.namespace"] == smartstack_namespace
assert exported_span.attributes["error.type"] == "Exception"
assert exported_span.attributes["error.type"] == "Exception"
assert exported_span.attributes["http.response.status_code"] == "500"

0 comments on commit c3dc6e8

Please sign in to comment.