|
9 | 9 | from ddtrace.ext import SpanTypes
|
10 | 10 | from ddtrace.ext import http
|
11 | 11 | from ddtrace.internal.constants import COMPONENT
|
| 12 | +from ddtrace.propagation.http import _extract_header_value |
| 13 | +from ddtrace.propagation.http import _possible_header |
12 | 14 |
|
13 | 15 |
|
14 | 16 | log = logging.getLogger(__name__)
|
15 | 17 |
|
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") |
22 | 25 |
|
23 | 26 | supported_proxies: Dict[str, Dict[str, str]] = {
|
24 | 27 | "aws-apigateway": {"span_name": "aws.apigateway", "component": "aws-apigateway"}
|
@@ -74,22 +77,37 @@ def set_inferred_proxy_span_tags(span, proxy_context) -> Span:
|
74 | 77 |
|
75 | 78 |
|
76 | 79 | 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 | + ): |
78 | 96 | return None
|
79 | 97 |
|
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): |
81 | 99 | log.debug(
|
82 | 100 | "Received headers to create inferred proxy span but headers include an unsupported proxy type", headers
|
83 | 101 | )
|
84 | 102 | return None
|
85 | 103 |
|
86 | 104 | 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, |
93 | 111 | }
|
94 | 112 |
|
95 | 113 |
|
|
0 commit comments