Skip to content

Commit 8624bd8

Browse files
committed
add headers
1 parent 3154dbc commit 8624bd8

File tree

7 files changed

+48
-5
lines changed

7 files changed

+48
-5
lines changed

sentry_sdk/integrations/_wsgi_common.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import sentry_sdk
55
from sentry_sdk.scope import should_send_default_pii
6-
from sentry_sdk.utils import AnnotatedValue, logger
6+
from sentry_sdk.utils import AnnotatedValue, logger, SENSITIVE_DATA_SUBSTITUTE
77

88
try:
99
from django.http.request import RawPostDataException
@@ -221,6 +221,22 @@ def _filter_headers(headers):
221221
}
222222

223223

224+
def _request_headers_to_span_attributes(headers):
225+
# type: (dict[str, str]) -> dict[str, str]
226+
attributes = {}
227+
228+
headers = _filter_headers(headers)
229+
230+
for header, value in headers.items():
231+
if isinstance(value, AnnotatedValue):
232+
value = SENSITIVE_DATA_SUBSTITUTE
233+
else:
234+
value = value.lower()
235+
attributes[f"http.request.header.{header}"] = value
236+
237+
return attributes
238+
239+
224240
def _in_http_status_code_range(code, code_ranges):
225241
# type: (object, list[HttpStatusCodeRange]) -> bool
226242
for target in code_ranges:

sentry_sdk/integrations/aiohttp.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from sentry_sdk.sessions import track_session
1414
from sentry_sdk.integrations._wsgi_common import (
1515
_filter_headers,
16+
_request_headers_to_span_attributes,
1617
request_body_within_bounds,
1718
)
1819
from sentry_sdk.tracing import (
@@ -394,4 +395,6 @@ def _prepopulate_attributes(request):
394395
if request.query_string:
395396
attributes["url.full"] = f"{url}?{request.query_string}"
396397

398+
attributes.update(_request_headers_to_span_attributes(dict(request.headers)))
399+
397400
return attributes

sentry_sdk/integrations/asgi.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
)
2222
from sentry_sdk.integrations._wsgi_common import (
2323
DEFAULT_HTTP_METHODS_TO_CAPTURE,
24+
_request_headers_to_span_attributes,
2425
)
2526
from sentry_sdk.sessions import track_session
2627
from sentry_sdk.tracing import (
@@ -363,4 +364,6 @@ def _prepopulate_attributes(scope):
363364

364365
attributes["url.full"] = full_url
365366

367+
attributes.update(_request_headers_to_span_attributes(_get_headers(scope)))
368+
366369
return attributes

sentry_sdk/integrations/aws_lambda.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
reraise,
2121
)
2222
from sentry_sdk.integrations import Integration
23-
from sentry_sdk.integrations._wsgi_common import _filter_headers
23+
from sentry_sdk.integrations._wsgi_common import (
24+
_filter_headers,
25+
_request_headers_to_span_attributes,
26+
)
2427

2528
from typing import TYPE_CHECKING
2629

@@ -162,7 +165,7 @@ def sentry_handler(aws_event, aws_context, *args, **kwargs):
162165
name=aws_context.function_name,
163166
source=TRANSACTION_SOURCE_COMPONENT,
164167
origin=AwsLambdaIntegration.origin,
165-
attributes=_prepopulate_attributes(aws_event, aws_context),
168+
attributes=_prepopulate_attributes(aws_event, aws_context, headers),
166169
):
167170
try:
168171
return handler(aws_event, aws_context, *args, **kwargs)
@@ -487,10 +490,15 @@ def _prepopulate_attributes(aws_event, aws_context):
487490
url += f"?{aws_event['queryStringParameters']}"
488491
attributes["url.full"] = url
489492

490-
headers = aws_event.get("headers") or {}
493+
headers = {}
494+
if aws_event.get("headers") and isinstance(aws_event["headers"], dict):
495+
headers = aws_event["headers"]
496+
491497
if headers.get("X-Forwarded-Proto"):
492498
attributes["network.protocol.name"] = headers["X-Forwarded-Proto"]
493499
if headers.get("Host"):
494500
attributes["server.address"] = headers["Host"]
495501

502+
attributes.update(_request_headers_to_span_attributes(headers))
503+
496504
return attributes

sentry_sdk/integrations/gcp.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import sentry_sdk
88
from sentry_sdk.consts import OP
99
from sentry_sdk.integrations import Integration
10-
from sentry_sdk.integrations._wsgi_common import _filter_headers
10+
from sentry_sdk.integrations._wsgi_common import (
11+
_filter_headers,
12+
_request_headers_to_span_attributes,
13+
)
1114
from sentry_sdk.scope import should_send_default_pii
1215
from sentry_sdk.tracing import TRANSACTION_SOURCE_COMPONENT
1316
from sentry_sdk.utils import (
@@ -249,4 +252,8 @@ def _prepopulate_attributes(gcp_event):
249252
if getattr(gcp_event, key, None):
250253
attributes[attr] = getattr(gcp_event, key)
251254

255+
if hasattr(gcp_event, "headers"):
256+
headers = gcp_event.headers
257+
attributes.update(_request_headers_to_span_attributes(headers))
258+
252259
return attributes

sentry_sdk/integrations/tornado.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
RequestExtractor,
2323
_filter_headers,
2424
_is_json_content_type,
25+
_request_headers_to_span_attributes,
2526
)
2627
from sentry_sdk.integrations.logging import ignore_logger
2728

@@ -257,4 +258,6 @@ def _prepopulate_attributes(request):
257258
with capture_internal_exceptions():
258259
attributes["url.full"] = request.full_url()
259260

261+
attributes.update(_request_headers_to_span_attributes(request.headers))
262+
260263
return attributes

sentry_sdk/integrations/wsgi.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from sentry_sdk.integrations._wsgi_common import (
1010
DEFAULT_HTTP_METHODS_TO_CAPTURE,
1111
_filter_headers,
12+
_request_headers_to_span_attributes,
1213
)
1314
from sentry_sdk.sessions import track_session
1415
from sentry_sdk.tracing import Transaction, TRANSACTION_SOURCE_ROUTE
@@ -344,4 +345,6 @@ def _prepopulate_attributes(wsgi_environ, use_x_forwarded_for=False):
344345
query = wsgi_environ.get("QUERY_STRING")
345346
attributes["url.full"] = f"{url}?{query}"
346347

348+
attributes.update(_request_headers_to_span_attributes(_get_headers(wsgi_environ)))
349+
347350
return attributes

0 commit comments

Comments
 (0)