Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

code changes to resolve conditional server span creation for WSGI and adding 'attributes' parameter to util function #903

Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.9.1-0.28b1...HEAD)

- `opentelemetry-instrumentation-wsgi` WSGI: Conditionally create SERVER spans
([#903](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/903))

### Fixed

- `opentelemetry-instrumentation-logging` retrieves service name defensively.
Expand All @@ -21,7 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `opentelemetry-instrumentation-pika` requires `packaging` dependency

- `opentelemetry-instrumentation-tornado` Tornado: Conditionally create SERVER spans
([#867](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/889))
([#889](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/889))

## [1.9.0-0.28b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.9.0-0.28b0) - 2022-01-26

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,11 +280,17 @@ def __call__(self, environ, start_response):
start_response: The WSGI start_response callable.
"""

token = context.attach(extract(environ, getter=wsgi_getter))
owais marked this conversation as resolved.
Show resolved Hide resolved
token = ctx = None
span_kind = trace.SpanKind.INTERNAL
if trace.get_current_span() is trace.INVALID_SPAN:
ctx = extract(environ, getter=wsgi_getter)
token = context.attach(ctx)
span_kind = trace.SpanKind.SERVER

span = self.tracer.start_span(
get_default_span_name(environ),
kind=trace.SpanKind.SERVER,
context=ctx,
kind=span_kind,
attributes=collect_request_attributes(environ),
)

Expand All @@ -308,7 +314,8 @@ def __call__(self, environ, start_response):
if span.is_recording():
span.set_status(Status(StatusCode.ERROR, str(ex)))
span.end()
context.detach(token)
if token is not None:
context.detach(token)
raise


Expand All @@ -324,7 +331,8 @@ def _end_span_after_iterating(iterable, span, tracer, token):
if close:
close()
span.end()
context.detach(token)
if token is not None:
context.detach(token)


# TODO: inherit from opentelemetry.instrumentation.propagators.Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,5 +414,35 @@ def test_basic_wsgi_call(self):
self.validate_response(response, exporter)


class TestWsgiMiddlewareWrappedWithAnotherFramework(WsgiTestBase):
def test_mark_span_internal_in_presence_of_span_from_other_framework(self):
tracer_provider, exporter = TestBase.create_tracer_provider()
tracer = tracer_provider.get_tracer(__name__)

with tracer.start_as_current_span(
"test", kind=trace_api.SpanKind.SERVER
) as parent_span:
app = otel_wsgi.OpenTelemetryMiddleware(
simple_wsgi, tracer_provider=tracer_provider
)
response = app(self.environ, self.start_response)
while True:
try:
value = next(response)
self.assertEqual(value, b"*")
except StopIteration:
break

span_list = exporter.get_finished_spans()

self.assertEqual(trace_api.SpanKind.INTERNAL, span_list[0].kind)
self.assertEqual(trace_api.SpanKind.SERVER, parent_span.kind)

# internal span should be child of the parent span we have provided
self.assertEqual(
parent_span.context.span_id, span_list[0].parent.span_id
)


if __name__ == "__main__":
unittest.main()