Skip to content

Commit 295e0ef

Browse files
committed
Slight factor of headers to use _possible_headers to comply with wsgi spec like what the propagation/http.py file is currently doing
1 parent cece303 commit 295e0ef

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

ddtrace/_trace/_inferred_proxy.py

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@
99
from ddtrace.ext import SpanTypes
1010
from ddtrace.ext import http
1111
from ddtrace.internal.constants import COMPONENT
12+
from ddtrace.propagation.http import _extract_header_value
13+
from ddtrace.propagation.http import _possible_header
1214

1315

1416
log = logging.getLogger(__name__)
1517

16-
PROXY_HEADER_SYSTEM = "x-dd-proxy"
17-
PROXY_HEADER_START_TIME_MS = "x-dd-proxy-request-time-ms"
18-
PROXY_HEADER_PATH = "x-dd-proxy-path"
19-
PROXY_HEADER_HTTPMETHOD = "x-dd-proxy-httpmethod"
20-
PROXY_HEADER_DOMAIN = "x-dd-proxy-domain-name"
21-
PROXY_HEADER_STAGE = "x-dd-proxy-stage"
18+
# Checking lower case and upper case versions per WSGI spec following ddtrace/propagation/http.py's logic to extract http headers
19+
POSSIBLE_PROXY_HEADER_SYSTEM = _possible_header("x-dd-proxy")
20+
POSSIBLE_PROXY_HEADER_START_TIME_MS = _possible_header("x-dd-proxy-request-time-ms")
21+
POSSIBLE_PROXY_HEADER_PATH = _possible_header("x-dd-proxy-path")
22+
POSSIBLE_PROXY_HEADER_HTTPMETHOD = _possible_header("x-dd-proxy-httpmethod")
23+
POSSIBLE_PROXY_HEADER_DOMAIN = _possible_header("x-dd-proxy-domain-name")
24+
POSSIBLE_PROXY_HEADER_STAGE = _possible_header("x-dd-proxy-stage")
2225

2326
supported_proxies: Dict[str, Dict[str, str]] = {
2427
"aws-apigateway": {"span_name": "aws.apigateway", "component": "aws-apigateway"}
@@ -74,22 +77,37 @@ def set_inferred_proxy_span_tags(span, proxy_context) -> Span:
7477

7578

7679
def extract_inferred_proxy_context(headers) -> Union[None, Dict[str, str]]:
77-
if PROXY_HEADER_START_TIME_MS not in headers:
80+
proxy_header_system = str(_extract_header_value(POSSIBLE_PROXY_HEADER_SYSTEM, headers))
81+
proxy_header_start_time_ms = str(_extract_header_value(POSSIBLE_PROXY_HEADER_START_TIME_MS, headers))
82+
proxy_header_path = str(_extract_header_value(POSSIBLE_PROXY_HEADER_PATH, headers))
83+
proxy_header_httpmethod = str(_extract_header_value(POSSIBLE_PROXY_HEADER_HTTPMETHOD, headers))
84+
proxy_header_domain = str(_extract_header_value(POSSIBLE_PROXY_HEADER_DOMAIN, headers))
85+
proxy_header_stage = str(_extract_header_value(POSSIBLE_PROXY_HEADER_STAGE, headers))
86+
87+
# Exit if any of the required headers are not present
88+
if (
89+
not proxy_header_system
90+
or not proxy_header_start_time_ms
91+
or not proxy_header_path
92+
or not proxy_header_httpmethod
93+
or not proxy_header_domain
94+
or not proxy_header_stage
95+
):
7896
return None
7997

80-
if not (PROXY_HEADER_SYSTEM in headers and headers[PROXY_HEADER_SYSTEM] in supported_proxies):
98+
if not (proxy_header_system and proxy_header_system in supported_proxies):
8199
log.debug(
82100
"Received headers to create inferred proxy span but headers include an unsupported proxy type", headers
83101
)
84102
return None
85103

86104
return {
87-
"request_time": headers[PROXY_HEADER_START_TIME_MS] if headers[PROXY_HEADER_START_TIME_MS] else "0",
88-
"method": headers[PROXY_HEADER_HTTPMETHOD],
89-
"path": headers[PROXY_HEADER_PATH],
90-
"stage": headers[PROXY_HEADER_STAGE],
91-
"domain_name": headers[PROXY_HEADER_DOMAIN],
92-
"proxy_system_name": headers[PROXY_HEADER_SYSTEM],
105+
"request_time": proxy_header_start_time_ms,
106+
"method": proxy_header_httpmethod,
107+
"path": proxy_header_path,
108+
"stage": proxy_header_stage,
109+
"domain_name": proxy_header_domain,
110+
"proxy_system_name": proxy_header_system,
93111
}
94112

95113

tests/contrib/flask/test_request.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import pytest
1313

1414
from ddtrace.constants import ERROR_MSG
15-
1615
from ddtrace.constants import USER_KEEP
1716
from ddtrace.contrib.internal.flask.patch import flask_version
1817
from ddtrace.ext import http

0 commit comments

Comments
 (0)