Skip to content

Commit 45448f8

Browse files
FastAPI: capture custom request response headers in span attributes (#1032)
1 parent 0fdc3c1 commit 45448f8

File tree

4 files changed

+423
-2
lines changed

4 files changed

+423
-2
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1515
([#999])(https://github.com/open-telemetry/opentelemetry-python-contrib/pull/999)
1616

1717
### Added
18-
18+
- `opentelemetry-instrumentation-fastapi` Capture custom request/response headers in span attributes
19+
([#1032])(https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1032)
1920
- `opentelemetry-instrumentation-django` Capture custom request/response headers in span attributes
2021
([#1024])(https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1024)
2122
- `opentelemetry-instrumentation-asgi` Capture custom request/response headers in span attributes

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

+51
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,57 @@ def client_response_hook(span: Span, message: dict):
9191
9292
OpenTelemetryMiddleware().(application, server_request_hook=server_request_hook, client_request_hook=client_request_hook, client_response_hook=client_response_hook)
9393
94+
Capture HTTP request and response headers
95+
*****************************************
96+
You can configure the agent to capture predefined HTTP headers as span attributes, according to the `semantic convention <https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers>`_.
97+
98+
Request headers
99+
***************
100+
To capture predefined HTTP request headers as span attributes, set the environment variable ``OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST``
101+
to a comma-separated list of HTTP header names.
102+
103+
For example,
104+
105+
::
106+
107+
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST="content-type,custom_request_header"
108+
109+
will extract ``content-type`` and ``custom_request_header`` from request headers and add them as span attributes.
110+
111+
It is recommended that you should give the correct names of the headers to be captured in the environment variable.
112+
Request header names in ASGI are case insensitive. So, giving header name as ``CUStom-Header`` in environment variable will be able capture header with name ``custom-header``.
113+
114+
The name of the added span attribute will follow the format ``http.request.header.<header_name>`` where ``<header_name>`` being the normalized HTTP header name (lowercase, with - characters replaced by _ ).
115+
The value of the attribute will be single item list containing all the header values.
116+
117+
Example of the added span attribute,
118+
``http.request.header.custom_request_header = ["<value1>,<value2>"]``
119+
120+
Response headers
121+
****************
122+
To capture predefined HTTP response headers as span attributes, set the environment variable ``OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE``
123+
to a comma-separated list of HTTP header names.
124+
125+
For example,
126+
127+
::
128+
129+
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE="content-type,custom_response_header"
130+
131+
will extract ``content-type`` and ``custom_response_header`` from response headers and add them as span attributes.
132+
133+
It is recommended that you should give the correct names of the headers to be captured in the environment variable.
134+
Response header names captured in ASGI are case insensitive. So, giving header name as ``CUStomHeader`` in environment variable will be able capture header with name ``customheader``.
135+
136+
The name of the added span attribute will follow the format ``http.response.header.<header_name>`` where ``<header_name>`` being the normalized HTTP header name (lowercase, with - characters replaced by _ ).
137+
The value of the attribute will be single item list containing all the header values.
138+
139+
Example of the added span attribute,
140+
``http.response.header.custom_response_header = ["<value1>,<value2>"]``
141+
142+
Note:
143+
Environment variable names to caputre http headers are still experimental, and thus are subject to change.
144+
94145
API
95146
---
96147
"""

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

+52
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,58 @@ def client_response_hook(span: Span, message: dict):
7474
7575
FastAPIInstrumentor().instrument(server_request_hook=server_request_hook, client_request_hook=client_request_hook, client_response_hook=client_response_hook)
7676
77+
78+
Capture HTTP request and response headers
79+
*****************************************
80+
You can configure the agent to capture predefined HTTP headers as span attributes, according to the `semantic convention <https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#http-request-and-response-headers>`_.
81+
82+
Request headers
83+
***************
84+
To capture predefined HTTP request headers as span attributes, set the environment variable ``OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST``
85+
to a comma-separated list of HTTP header names.
86+
87+
For example,
88+
89+
::
90+
91+
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST="content-type,custom_request_header"
92+
93+
will extract ``content-type`` and ``custom_request_header`` from request headers and add them as span attributes.
94+
95+
It is recommended that you should give the correct names of the headers to be captured in the environment variable.
96+
Request header names in FastAPI are case insensitive. So, giving header name as ``CUStom-Header`` in environment variable will be able capture header with name ``custom-header``.
97+
98+
The name of the added span attribute will follow the format ``http.request.header.<header_name>`` where ``<header_name>`` being the normalized HTTP header name (lowercase, with - characters replaced by _ ).
99+
The value of the attribute will be single item list containing all the header values.
100+
101+
Example of the added span attribute,
102+
``http.request.header.custom_request_header = ["<value1>,<value2>"]``
103+
104+
Response headers
105+
****************
106+
To capture predefined HTTP response headers as span attributes, set the environment variable ``OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE``
107+
to a comma-separated list of HTTP header names.
108+
109+
For example,
110+
111+
::
112+
113+
export OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE="content-type,custom_response_header"
114+
115+
will extract ``content-type`` and ``custom_response_header`` from response headers and add them as span attributes.
116+
117+
It is recommended that you should give the correct names of the headers to be captured in the environment variable.
118+
Response header names captured in FastAPI are case insensitive. So, giving header name as ``CUStomHeader`` in environment variable will be able capture header with name ``customheader``.
119+
120+
The name of the added span attribute will follow the format ``http.response.header.<header_name>`` where ``<header_name>`` being the normalized HTTP header name (lowercase, with - characters replaced by _ ).
121+
The value of the attribute will be single item list containing all the header values.
122+
123+
Example of the added span attribute,
124+
``http.response.header.custom_response_header = ["<value1>,<value2>"]``
125+
126+
Note:
127+
Environment variable names to caputre http headers are still experimental, and thus are subject to change.
128+
77129
API
78130
---
79131
"""

0 commit comments

Comments
 (0)