codekingpro/portable-devtools
114k
1import warnings2 3import six4from eventlet.green import httplib5from eventlet.zipkin import api6 7 8# see https://twitter.github.io/zipkin/Instrumenting.html9HDR_TRACE_ID = 'X-B3-TraceId'10HDR_SPAN_ID = 'X-B3-SpanId'11HDR_PARENT_SPAN_ID = 'X-B3-ParentSpanId'12HDR_SAMPLED = 'X-B3-Sampled'13 14 15if six.PY2:16 __org_endheaders__ = httplib.HTTPConnection.endheaders17 __org_begin__ = httplib.HTTPResponse.begin18 19 def _patched_endheaders(self):20 if api.is_tracing():21 trace_data = api.get_trace_data()22 new_span_id = api.generate_span_id()23 self.putheader(HDR_TRACE_ID, hex_str(trace_data.trace_id))24 self.putheader(HDR_SPAN_ID, hex_str(new_span_id))25 self.putheader(HDR_PARENT_SPAN_ID, hex_str(trace_data.span_id))26 self.putheader(HDR_SAMPLED, int(trace_data.sampled))27 api.put_annotation('Client Send')28 29 __org_endheaders__(self)30 31 def _patched_begin(self):32 __org_begin__(self)33 34 if api.is_tracing():35 api.put_annotation('Client Recv (%s)' % self.status)36 37 38def patch():39 if six.PY2:40 httplib.HTTPConnection.endheaders = _patched_endheaders41 httplib.HTTPResponse.begin = _patched_begin42 if six.PY3:43 warnings.warn("Since current Python thrift release \44 doesn't support Python 3, eventlet.zipkin.http \45 doesn't also support Python 3 (http.client)")46 47 48def unpatch():49 if six.PY2:50 httplib.HTTPConnection.endheaders = __org_endheaders__51 httplib.HTTPResponse.begin = __org_begin__52 if six.PY3:53 pass54 55 56def hex_str(n):57 """58 Thrift uses a binary representation of trace and span ids59 HTTP headers use a hexadecimal representation of the same60 """61 return '%0.16x' % (n,)62 