Refactoring custom header collection API for consistency (#1064)
This commit is contained in:
parent
fedf9448c0
commit
6f620ee142
|
|
@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
- `opentelemetry-instrumentation-aws-lambda` Fixed an issue - in some rare cases (API GW proxy integration test)
|
- `opentelemetry-instrumentation-aws-lambda` Fixed an issue - in some rare cases (API GW proxy integration test)
|
||||||
headers are set to None, breaking context propagators.
|
headers are set to None, breaking context propagators.
|
||||||
([#1055](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1055))
|
([#1055](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1055))
|
||||||
|
- Refactoring custom header collection API for consistency
|
||||||
|
([#1064](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1064))
|
||||||
|
|
||||||
## [1.11.1-0.30b1](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.11.1-0.30b1) - 2022-04-21
|
## [1.11.1-0.30b1](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.11.1-0.30b1) - 2022-04-21
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -28,13 +28,13 @@ from opentelemetry.instrumentation.utils import (
|
||||||
_start_internal_or_server_span,
|
_start_internal_or_server_span,
|
||||||
extract_attributes_from_object,
|
extract_attributes_from_object,
|
||||||
)
|
)
|
||||||
from opentelemetry.instrumentation.wsgi import (
|
|
||||||
add_custom_request_headers as wsgi_add_custom_request_headers,
|
|
||||||
)
|
|
||||||
from opentelemetry.instrumentation.wsgi import (
|
|
||||||
add_custom_response_headers as wsgi_add_custom_response_headers,
|
|
||||||
)
|
|
||||||
from opentelemetry.instrumentation.wsgi import add_response_attributes
|
from opentelemetry.instrumentation.wsgi import add_response_attributes
|
||||||
|
from opentelemetry.instrumentation.wsgi import (
|
||||||
|
collect_custom_request_headers_attributes as wsgi_collect_custom_request_headers_attributes,
|
||||||
|
)
|
||||||
|
from opentelemetry.instrumentation.wsgi import (
|
||||||
|
collect_custom_response_headers_attributes as wsgi_collect_custom_response_headers_attributes,
|
||||||
|
)
|
||||||
from opentelemetry.instrumentation.wsgi import (
|
from opentelemetry.instrumentation.wsgi import (
|
||||||
collect_request_attributes as wsgi_collect_request_attributes,
|
collect_request_attributes as wsgi_collect_request_attributes,
|
||||||
)
|
)
|
||||||
|
|
@ -231,7 +231,11 @@ class _DjangoMiddleware(MiddlewareMixin):
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
if span.is_recording() and span.kind == SpanKind.SERVER:
|
if span.is_recording() and span.kind == SpanKind.SERVER:
|
||||||
wsgi_add_custom_request_headers(span, carrier)
|
custom_attributes = (
|
||||||
|
wsgi_collect_custom_request_headers_attributes(carrier)
|
||||||
|
)
|
||||||
|
if len(custom_attributes) > 0:
|
||||||
|
span.set_attributes(custom_attributes)
|
||||||
|
|
||||||
for key, value in attributes.items():
|
for key, value in attributes.items():
|
||||||
span.set_attribute(key, value)
|
span.set_attribute(key, value)
|
||||||
|
|
@ -309,7 +313,13 @@ class _DjangoMiddleware(MiddlewareMixin):
|
||||||
response.items(),
|
response.items(),
|
||||||
)
|
)
|
||||||
if span.is_recording() and span.kind == SpanKind.SERVER:
|
if span.is_recording() and span.kind == SpanKind.SERVER:
|
||||||
wsgi_add_custom_response_headers(span, response.items())
|
custom_attributes = (
|
||||||
|
wsgi_collect_custom_response_headers_attributes(
|
||||||
|
response.items()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if len(custom_attributes) > 0:
|
||||||
|
span.set_attributes(custom_attributes)
|
||||||
|
|
||||||
propagator = get_global_response_propagator()
|
propagator = get_global_response_propagator()
|
||||||
if propagator:
|
if propagator:
|
||||||
|
|
|
||||||
|
|
@ -268,7 +268,11 @@ class _InstrumentedFalconAPI(getattr(falcon, _instrument_app)):
|
||||||
for key, value in attributes.items():
|
for key, value in attributes.items():
|
||||||
span.set_attribute(key, value)
|
span.set_attribute(key, value)
|
||||||
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
|
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
|
||||||
otel_wsgi.add_custom_request_headers(span, env)
|
custom_attributes = (
|
||||||
|
otel_wsgi.collect_custom_request_headers_attributes(env)
|
||||||
|
)
|
||||||
|
if len(custom_attributes) > 0:
|
||||||
|
span.set_attributes(custom_attributes)
|
||||||
|
|
||||||
activation = trace.use_span(span, end_on_exit=True)
|
activation = trace.use_span(span, end_on_exit=True)
|
||||||
activation.__enter__()
|
activation.__enter__()
|
||||||
|
|
@ -382,9 +386,13 @@ class _TraceMiddleware:
|
||||||
response_headers = resp.headers
|
response_headers = resp.headers
|
||||||
|
|
||||||
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
|
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
|
||||||
otel_wsgi.add_custom_response_headers(
|
custom_attributes = (
|
||||||
span, response_headers.items()
|
otel_wsgi.collect_custom_response_headers_attributes(
|
||||||
|
response_headers.items()
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
if len(custom_attributes) > 0:
|
||||||
|
span.set_attributes(custom_attributes)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -208,9 +208,11 @@ def _rewrapped_app(wsgi_app, response_hook=None, excluded_urls=None):
|
||||||
span.is_recording()
|
span.is_recording()
|
||||||
and span.kind == trace.SpanKind.SERVER
|
and span.kind == trace.SpanKind.SERVER
|
||||||
):
|
):
|
||||||
otel_wsgi.add_custom_response_headers(
|
custom_attributes = otel_wsgi.collect_custom_response_headers_attributes(
|
||||||
span, response_headers
|
response_headers
|
||||||
)
|
)
|
||||||
|
if len(custom_attributes) > 0:
|
||||||
|
span.set_attributes(custom_attributes)
|
||||||
else:
|
else:
|
||||||
_logger.warning(
|
_logger.warning(
|
||||||
"Flask environ's OpenTelemetry span "
|
"Flask environ's OpenTelemetry span "
|
||||||
|
|
@ -259,9 +261,13 @@ def _wrapped_before_request(
|
||||||
for key, value in attributes.items():
|
for key, value in attributes.items():
|
||||||
span.set_attribute(key, value)
|
span.set_attribute(key, value)
|
||||||
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
|
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
|
||||||
otel_wsgi.add_custom_request_headers(
|
custom_attributes = (
|
||||||
span, flask_request_environ
|
otel_wsgi.collect_custom_request_headers_attributes(
|
||||||
|
flask_request_environ
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
if len(custom_attributes) > 0:
|
||||||
|
span.set_attributes(custom_attributes)
|
||||||
|
|
||||||
activation = trace.use_span(span, end_on_exit=True)
|
activation = trace.use_span(span, end_on_exit=True)
|
||||||
activation.__enter__() # pylint: disable=E1101
|
activation.__enter__() # pylint: disable=E1101
|
||||||
|
|
|
||||||
|
|
@ -105,7 +105,13 @@ def _before_traversal(event):
|
||||||
for key, value in attributes.items():
|
for key, value in attributes.items():
|
||||||
span.set_attribute(key, value)
|
span.set_attribute(key, value)
|
||||||
if span.kind == trace.SpanKind.SERVER:
|
if span.kind == trace.SpanKind.SERVER:
|
||||||
otel_wsgi.add_custom_request_headers(span, request_environ)
|
custom_attributes = (
|
||||||
|
otel_wsgi.collect_custom_request_headers_attributes(
|
||||||
|
request_environ
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if len(custom_attributes) > 0:
|
||||||
|
span.set_attributes(custom_attributes)
|
||||||
|
|
||||||
activation = trace.use_span(span, end_on_exit=True)
|
activation = trace.use_span(span, end_on_exit=True)
|
||||||
activation.__enter__() # pylint: disable=E1101
|
activation.__enter__() # pylint: disable=E1101
|
||||||
|
|
@ -178,9 +184,13 @@ def trace_tween_factory(handler, registry):
|
||||||
)
|
)
|
||||||
|
|
||||||
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
|
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
|
||||||
otel_wsgi.add_custom_response_headers(
|
custom_attributes = (
|
||||||
span, getattr(response, "headerlist", None)
|
otel_wsgi.collect_custom_response_headers_attributes(
|
||||||
|
getattr(response, "headerlist", None)
|
||||||
)
|
)
|
||||||
|
)
|
||||||
|
if len(custom_attributes) > 0:
|
||||||
|
span.set_attributes(custom_attributes)
|
||||||
|
|
||||||
propagator = get_global_response_propagator()
|
propagator = get_global_response_propagator()
|
||||||
if propagator and hasattr(response, "headers"):
|
if propagator and hasattr(response, "headers"):
|
||||||
|
|
|
||||||
|
|
@ -315,7 +315,7 @@ def _log_exception(tracer, func, handler, args, kwargs):
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def _add_custom_request_headers(span, request_headers):
|
def _collect_custom_request_headers_attributes(request_headers):
|
||||||
custom_request_headers_name = get_custom_headers(
|
custom_request_headers_name = get_custom_headers(
|
||||||
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST
|
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST
|
||||||
)
|
)
|
||||||
|
|
@ -325,10 +325,10 @@ def _add_custom_request_headers(span, request_headers):
|
||||||
if header_values:
|
if header_values:
|
||||||
key = normalise_request_header_name(header_name.lower())
|
key = normalise_request_header_name(header_name.lower())
|
||||||
attributes[key] = [header_values]
|
attributes[key] = [header_values]
|
||||||
span.set_attributes(attributes)
|
return attributes
|
||||||
|
|
||||||
|
|
||||||
def _add_custom_response_headers(span, response_headers):
|
def _collect_custom_response_headers_attributes(response_headers):
|
||||||
custom_response_headers_name = get_custom_headers(
|
custom_response_headers_name = get_custom_headers(
|
||||||
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE
|
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE
|
||||||
)
|
)
|
||||||
|
|
@ -338,7 +338,7 @@ def _add_custom_response_headers(span, response_headers):
|
||||||
if header_values:
|
if header_values:
|
||||||
key = normalise_response_header_name(header_name.lower())
|
key = normalise_response_header_name(header_name.lower())
|
||||||
attributes[key] = [header_values]
|
attributes[key] = [header_values]
|
||||||
span.set_attributes(attributes)
|
return attributes
|
||||||
|
|
||||||
|
|
||||||
def _get_attributes_from_request(request):
|
def _get_attributes_from_request(request):
|
||||||
|
|
@ -392,7 +392,11 @@ def _start_span(tracer, handler, start_time) -> _TraceContext:
|
||||||
span.set_attribute(key, value)
|
span.set_attribute(key, value)
|
||||||
span.set_attribute("tornado.handler", _get_full_handler_name(handler))
|
span.set_attribute("tornado.handler", _get_full_handler_name(handler))
|
||||||
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
|
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
|
||||||
_add_custom_request_headers(span, handler.request.headers)
|
custom_attributes = _collect_custom_request_headers_attributes(
|
||||||
|
handler.request.headers
|
||||||
|
)
|
||||||
|
if len(custom_attributes) > 0:
|
||||||
|
span.set_attributes(custom_attributes)
|
||||||
|
|
||||||
activation = trace.use_span(span, end_on_exit=True)
|
activation = trace.use_span(span, end_on_exit=True)
|
||||||
activation.__enter__() # pylint: disable=E1101
|
activation.__enter__() # pylint: disable=E1101
|
||||||
|
|
@ -448,7 +452,11 @@ def _finish_span(tracer, handler, error=None):
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
if ctx.span.is_recording() and ctx.span.kind == trace.SpanKind.SERVER:
|
if ctx.span.is_recording() and ctx.span.kind == trace.SpanKind.SERVER:
|
||||||
_add_custom_response_headers(ctx.span, handler._headers)
|
custom_attributes = _collect_custom_response_headers_attributes(
|
||||||
|
handler._headers
|
||||||
|
)
|
||||||
|
if len(custom_attributes) > 0:
|
||||||
|
ctx.span.set_attributes(custom_attributes)
|
||||||
|
|
||||||
ctx.activation.__exit__(*finish_args) # pylint: disable=E1101
|
ctx.activation.__exit__(*finish_args) # pylint: disable=E1101
|
||||||
if ctx.token:
|
if ctx.token:
|
||||||
|
|
|
||||||
|
|
@ -266,8 +266,8 @@ def collect_request_attributes(environ):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def add_custom_request_headers(span, environ):
|
def collect_custom_request_headers_attributes(environ):
|
||||||
"""Adds custom HTTP request headers into the span which are configured by the user
|
"""Returns custom HTTP request headers which are configured by the user
|
||||||
from the PEP3333-conforming WSGI environ to be used as span creation attributes as described
|
from the PEP3333-conforming WSGI environ to be used as span creation attributes as described
|
||||||
in the specification https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers"""
|
in the specification https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers"""
|
||||||
attributes = {}
|
attributes = {}
|
||||||
|
|
@ -280,11 +280,11 @@ def add_custom_request_headers(span, environ):
|
||||||
if header_values:
|
if header_values:
|
||||||
key = normalise_request_header_name(header_name)
|
key = normalise_request_header_name(header_name)
|
||||||
attributes[key] = [header_values]
|
attributes[key] = [header_values]
|
||||||
span.set_attributes(attributes)
|
return attributes
|
||||||
|
|
||||||
|
|
||||||
def add_custom_response_headers(span, response_headers):
|
def collect_custom_response_headers_attributes(response_headers):
|
||||||
"""Adds custom HTTP response headers into the sapn which are configured by the user from the
|
"""Returns custom HTTP response headers which are configured by the user from the
|
||||||
PEP3333-conforming WSGI environ as described in the specification
|
PEP3333-conforming WSGI environ as described in the specification
|
||||||
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers"""
|
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers"""
|
||||||
attributes = {}
|
attributes = {}
|
||||||
|
|
@ -301,7 +301,7 @@ def add_custom_response_headers(span, response_headers):
|
||||||
if header_values:
|
if header_values:
|
||||||
key = normalise_response_header_name(header_name)
|
key = normalise_response_header_name(header_name)
|
||||||
attributes[key] = [header_values]
|
attributes[key] = [header_values]
|
||||||
span.set_attributes(attributes)
|
return attributes
|
||||||
|
|
||||||
|
|
||||||
def add_response_attributes(
|
def add_response_attributes(
|
||||||
|
|
@ -365,7 +365,11 @@ class OpenTelemetryMiddleware:
|
||||||
def _start_response(status, response_headers, *args, **kwargs):
|
def _start_response(status, response_headers, *args, **kwargs):
|
||||||
add_response_attributes(span, status, response_headers)
|
add_response_attributes(span, status, response_headers)
|
||||||
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
|
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
|
||||||
add_custom_response_headers(span, response_headers)
|
custom_attributes = collect_custom_response_headers_attributes(
|
||||||
|
response_headers
|
||||||
|
)
|
||||||
|
if len(custom_attributes) > 0:
|
||||||
|
span.set_attributes(custom_attributes)
|
||||||
if response_hook:
|
if response_hook:
|
||||||
response_hook(status, response_headers)
|
response_hook(status, response_headers)
|
||||||
return start_response(status, response_headers, *args, **kwargs)
|
return start_response(status, response_headers, *args, **kwargs)
|
||||||
|
|
@ -388,7 +392,11 @@ class OpenTelemetryMiddleware:
|
||||||
attributes=collect_request_attributes(environ),
|
attributes=collect_request_attributes(environ),
|
||||||
)
|
)
|
||||||
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
|
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
|
||||||
add_custom_request_headers(span, environ)
|
custom_attributes = collect_custom_request_headers_attributes(
|
||||||
|
environ
|
||||||
|
)
|
||||||
|
if len(custom_attributes) > 0:
|
||||||
|
span.set_attributes(custom_attributes)
|
||||||
|
|
||||||
if self.request_hook:
|
if self.request_hook:
|
||||||
self.request_hook(span, environ)
|
self.request_hook(span, environ)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue