Skip to content

Commit 17d5ea2

Browse files
committed
Improved HTTP propagator: Also support HTTP_ style headers
1 parent a42bcf1 commit 17d5ea2

File tree

1 file changed

+31
-13
lines changed

1 file changed

+31
-13
lines changed

instana/http_propagator.py

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,39 @@
33
from basictracer.context import SpanContext
44
from instana import util, log
55

6-
prefix_tracer_state = 'X-Instana-'
7-
field_name_trace_id = prefix_tracer_state + 'T'
8-
field_name_span_id = prefix_tracer_state + 'S'
9-
field_count = 2
6+
7+
# The carrier can be a dict or a list.
8+
# Using the trace header as an example, it can be in the following forms
9+
# for extraction:
10+
# X-Instana-T
11+
# HTTP_X_INSTANA_T
12+
#
13+
# The second form above is found in places like Django middleware for
14+
# incoming requests.
15+
#
16+
# For injection, we only support the standard format:
17+
# X-Instana-T
1018

1119

1220
class HTTPPropagator():
1321
"""A Propagator for Format.HTTP_HEADERS. """
1422

23+
HEADER_KEY_T = 'X-Instana-T'
24+
HEADER_KEY_S = 'X-Instana-S'
25+
ALT_HEADER_KEY_T = 'HTTP_X_INSTANA_T'
26+
ALT_HEADER_KEY_S = 'HTTP_X_INSTANA_S'
27+
1528
def inject(self, span_context, carrier):
1629
try:
1730
trace_id = util.id_to_header(span_context.trace_id)
1831
span_id = util.id_to_header(span_context.span_id)
32+
1933
if type(carrier) is dict or hasattr(carrier, "__dict__"):
20-
carrier[field_name_trace_id] = trace_id
21-
carrier[field_name_span_id] = span_id
34+
carrier[self.HEADER_KEY_T] = trace_id
35+
carrier[self.HEADER_KEY_S] = span_id
2236
elif type(carrier) is list:
23-
trace_header = (field_name_trace_id, trace_id)
24-
carrier.append(trace_header)
25-
span_header = (field_name_span_id, span_id)
26-
carrier.append(span_header)
37+
carrier.append((self.HEADER_KEY_T, trace_id))
38+
carrier.append((self.HEADER_KEY_S, span_id))
2739
else:
2840
raise Exception("Unsupported carrier type", type(carrier))
2941

@@ -39,9 +51,15 @@ def extract(self, carrier): # noqa
3951
else:
4052
raise ot.SpanContextCorruptedException()
4153

42-
if field_name_trace_id in dc and field_name_span_id in dc:
43-
trace_id = util.header_to_id(dc[field_name_trace_id])
44-
span_id = util.header_to_id(dc[field_name_span_id])
54+
# Look for standard X-Instana-T/S format
55+
if self.HEADER_KEY_T in dc and self.header_key_s in dc:
56+
trace_id = util.header_to_id(dc[self.HEADER_KEY_T])
57+
span_id = util.header_to_id(dc[self.header_key_s])
58+
59+
# Alternatively check for HTTP_X_INSTANA_T/S style
60+
elif self.ALT_HEADER_KEY_T in dc and self.ALT_HEADER_KEY_S in dc:
61+
trace_id = util.header_to_id(dc[self.ALT_HEADER_KEY_T])
62+
span_id = util.header_to_id(dc[self.ALT_HEADER_KEY_S])
4563

4664
return SpanContext(span_id=span_id,
4765
trace_id=trace_id,

0 commit comments

Comments
 (0)