Skip to content

Commit 6f620ee

Browse files
authored
Refactoring custom header collection API for consistency (#1064)
1 parent fedf944 commit 6f620ee

File tree

7 files changed

+82
-30
lines changed
  • instrumentation
    • opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django
    • opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon
    • opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask
    • opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid
    • opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado
    • opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi

7 files changed

+82
-30
lines changed

CHANGELOG.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.11.1-0.30b1...HEAD)
99

1010
### Fixed
11-
- `opentelemetry-instrumentation-aws-lambda` Fixed an issue - in some rare cases (API GW proxy integration test)
11+
- `opentelemetry-instrumentation-aws-lambda` Fixed an issue - in some rare cases (API GW proxy integration test)
1212
headers are set to None, breaking context propagators.
1313
([#1055](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1055))
14+
- Refactoring custom header collection API for consistency
15+
([#1064](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1064))
1416

1517
## [1.11.1-0.30b1](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.11.1-0.30b1) - 2022-04-21
1618

instrumentation/opentelemetry-instrumentation-django/src/opentelemetry/instrumentation/django/middleware.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
_start_internal_or_server_span,
2929
extract_attributes_from_object,
3030
)
31+
from opentelemetry.instrumentation.wsgi import add_response_attributes
3132
from opentelemetry.instrumentation.wsgi import (
32-
add_custom_request_headers as wsgi_add_custom_request_headers,
33+
collect_custom_request_headers_attributes as wsgi_collect_custom_request_headers_attributes,
3334
)
3435
from opentelemetry.instrumentation.wsgi import (
35-
add_custom_response_headers as wsgi_add_custom_response_headers,
36+
collect_custom_response_headers_attributes as wsgi_collect_custom_response_headers_attributes,
3637
)
37-
from opentelemetry.instrumentation.wsgi import add_response_attributes
3838
from opentelemetry.instrumentation.wsgi import (
3939
collect_request_attributes as wsgi_collect_request_attributes,
4040
)
@@ -231,7 +231,11 @@ def process_request(self, request):
231231
)
232232
else:
233233
if span.is_recording() and span.kind == SpanKind.SERVER:
234-
wsgi_add_custom_request_headers(span, carrier)
234+
custom_attributes = (
235+
wsgi_collect_custom_request_headers_attributes(carrier)
236+
)
237+
if len(custom_attributes) > 0:
238+
span.set_attributes(custom_attributes)
235239

236240
for key, value in attributes.items():
237241
span.set_attribute(key, value)
@@ -309,7 +313,13 @@ def process_response(self, request, response):
309313
response.items(),
310314
)
311315
if span.is_recording() and span.kind == SpanKind.SERVER:
312-
wsgi_add_custom_response_headers(span, response.items())
316+
custom_attributes = (
317+
wsgi_collect_custom_response_headers_attributes(
318+
response.items()
319+
)
320+
)
321+
if len(custom_attributes) > 0:
322+
span.set_attributes(custom_attributes)
313323

314324
propagator = get_global_response_propagator()
315325
if propagator:

instrumentation/opentelemetry-instrumentation-falcon/src/opentelemetry/instrumentation/falcon/__init__.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,11 @@ def __call__(self, env, start_response):
268268
for key, value in attributes.items():
269269
span.set_attribute(key, value)
270270
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
271-
otel_wsgi.add_custom_request_headers(span, env)
271+
custom_attributes = (
272+
otel_wsgi.collect_custom_request_headers_attributes(env)
273+
)
274+
if len(custom_attributes) > 0:
275+
span.set_attributes(custom_attributes)
272276

273277
activation = trace.use_span(span, end_on_exit=True)
274278
activation.__enter__()
@@ -382,9 +386,13 @@ def process_response(
382386
response_headers = resp.headers
383387

384388
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
385-
otel_wsgi.add_custom_response_headers(
386-
span, response_headers.items()
389+
custom_attributes = (
390+
otel_wsgi.collect_custom_response_headers_attributes(
391+
response_headers.items()
392+
)
387393
)
394+
if len(custom_attributes) > 0:
395+
span.set_attributes(custom_attributes)
388396
except ValueError:
389397
pass
390398

instrumentation/opentelemetry-instrumentation-flask/src/opentelemetry/instrumentation/flask/__init__.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,11 @@ def _start_response(status, response_headers, *args, **kwargs):
208208
span.is_recording()
209209
and span.kind == trace.SpanKind.SERVER
210210
):
211-
otel_wsgi.add_custom_response_headers(
212-
span, response_headers
211+
custom_attributes = otel_wsgi.collect_custom_response_headers_attributes(
212+
response_headers
213213
)
214+
if len(custom_attributes) > 0:
215+
span.set_attributes(custom_attributes)
214216
else:
215217
_logger.warning(
216218
"Flask environ's OpenTelemetry span "
@@ -259,9 +261,13 @@ def _before_request():
259261
for key, value in attributes.items():
260262
span.set_attribute(key, value)
261263
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
262-
otel_wsgi.add_custom_request_headers(
263-
span, flask_request_environ
264+
custom_attributes = (
265+
otel_wsgi.collect_custom_request_headers_attributes(
266+
flask_request_environ
267+
)
264268
)
269+
if len(custom_attributes) > 0:
270+
span.set_attributes(custom_attributes)
265271

266272
activation = trace.use_span(span, end_on_exit=True)
267273
activation.__enter__() # pylint: disable=E1101

instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/callbacks.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,13 @@ def _before_traversal(event):
105105
for key, value in attributes.items():
106106
span.set_attribute(key, value)
107107
if span.kind == trace.SpanKind.SERVER:
108-
otel_wsgi.add_custom_request_headers(span, request_environ)
108+
custom_attributes = (
109+
otel_wsgi.collect_custom_request_headers_attributes(
110+
request_environ
111+
)
112+
)
113+
if len(custom_attributes) > 0:
114+
span.set_attributes(custom_attributes)
109115

110116
activation = trace.use_span(span, end_on_exit=True)
111117
activation.__enter__() # pylint: disable=E1101
@@ -178,9 +184,13 @@ def trace_tween(request):
178184
)
179185

180186
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
181-
otel_wsgi.add_custom_response_headers(
182-
span, getattr(response, "headerlist", None)
187+
custom_attributes = (
188+
otel_wsgi.collect_custom_response_headers_attributes(
189+
getattr(response, "headerlist", None)
190+
)
183191
)
192+
if len(custom_attributes) > 0:
193+
span.set_attributes(custom_attributes)
184194

185195
propagator = get_global_response_propagator()
186196
if propagator and hasattr(response, "headers"):

instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/__init__.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def _log_exception(tracer, func, handler, args, kwargs):
315315
return func(*args, **kwargs)
316316

317317

318-
def _add_custom_request_headers(span, request_headers):
318+
def _collect_custom_request_headers_attributes(request_headers):
319319
custom_request_headers_name = get_custom_headers(
320320
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST
321321
)
@@ -325,10 +325,10 @@ def _add_custom_request_headers(span, request_headers):
325325
if header_values:
326326
key = normalise_request_header_name(header_name.lower())
327327
attributes[key] = [header_values]
328-
span.set_attributes(attributes)
328+
return attributes
329329

330330

331-
def _add_custom_response_headers(span, response_headers):
331+
def _collect_custom_response_headers_attributes(response_headers):
332332
custom_response_headers_name = get_custom_headers(
333333
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE
334334
)
@@ -338,7 +338,7 @@ def _add_custom_response_headers(span, response_headers):
338338
if header_values:
339339
key = normalise_response_header_name(header_name.lower())
340340
attributes[key] = [header_values]
341-
span.set_attributes(attributes)
341+
return attributes
342342

343343

344344
def _get_attributes_from_request(request):
@@ -392,7 +392,11 @@ def _start_span(tracer, handler, start_time) -> _TraceContext:
392392
span.set_attribute(key, value)
393393
span.set_attribute("tornado.handler", _get_full_handler_name(handler))
394394
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
395-
_add_custom_request_headers(span, handler.request.headers)
395+
custom_attributes = _collect_custom_request_headers_attributes(
396+
handler.request.headers
397+
)
398+
if len(custom_attributes) > 0:
399+
span.set_attributes(custom_attributes)
396400

397401
activation = trace.use_span(span, end_on_exit=True)
398402
activation.__enter__() # pylint: disable=E1101
@@ -448,7 +452,11 @@ def _finish_span(tracer, handler, error=None):
448452
)
449453
)
450454
if ctx.span.is_recording() and ctx.span.kind == trace.SpanKind.SERVER:
451-
_add_custom_response_headers(ctx.span, handler._headers)
455+
custom_attributes = _collect_custom_response_headers_attributes(
456+
handler._headers
457+
)
458+
if len(custom_attributes) > 0:
459+
ctx.span.set_attributes(custom_attributes)
452460

453461
ctx.activation.__exit__(*finish_args) # pylint: disable=E1101
454462
if ctx.token:

instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,8 @@ def collect_request_attributes(environ):
266266
return result
267267

268268

269-
def add_custom_request_headers(span, environ):
270-
"""Adds custom HTTP request headers into the span which are configured by the user
269+
def collect_custom_request_headers_attributes(environ):
270+
"""Returns custom HTTP request headers which are configured by the user
271271
from the PEP3333-conforming WSGI environ to be used as span creation attributes as described
272272
in the specification https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers"""
273273
attributes = {}
@@ -280,11 +280,11 @@ def add_custom_request_headers(span, environ):
280280
if header_values:
281281
key = normalise_request_header_name(header_name)
282282
attributes[key] = [header_values]
283-
span.set_attributes(attributes)
283+
return attributes
284284

285285

286-
def add_custom_response_headers(span, response_headers):
287-
"""Adds custom HTTP response headers into the sapn which are configured by the user from the
286+
def collect_custom_response_headers_attributes(response_headers):
287+
"""Returns custom HTTP response headers which are configured by the user from the
288288
PEP3333-conforming WSGI environ as described in the specification
289289
https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers"""
290290
attributes = {}
@@ -301,7 +301,7 @@ def add_custom_response_headers(span, response_headers):
301301
if header_values:
302302
key = normalise_response_header_name(header_name)
303303
attributes[key] = [header_values]
304-
span.set_attributes(attributes)
304+
return attributes
305305

306306

307307
def add_response_attributes(
@@ -365,7 +365,11 @@ def _create_start_response(span, start_response, response_hook):
365365
def _start_response(status, response_headers, *args, **kwargs):
366366
add_response_attributes(span, status, response_headers)
367367
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
368-
add_custom_response_headers(span, response_headers)
368+
custom_attributes = collect_custom_response_headers_attributes(
369+
response_headers
370+
)
371+
if len(custom_attributes) > 0:
372+
span.set_attributes(custom_attributes)
369373
if response_hook:
370374
response_hook(status, response_headers)
371375
return start_response(status, response_headers, *args, **kwargs)
@@ -388,7 +392,11 @@ def __call__(self, environ, start_response):
388392
attributes=collect_request_attributes(environ),
389393
)
390394
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
391-
add_custom_request_headers(span, environ)
395+
custom_attributes = collect_custom_request_headers_attributes(
396+
environ
397+
)
398+
if len(custom_attributes) > 0:
399+
span.set_attributes(custom_attributes)
392400

393401
if self.request_hook:
394402
self.request_hook(span, environ)

0 commit comments

Comments
 (0)