Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

62 řádky
1.7KB

  1. import warnings
  2. import six
  3. from eventlet.green import httplib
  4. from eventlet.zipkin import api
  5. # see https://twitter.github.io/zipkin/Instrumenting.html
  6. HDR_TRACE_ID = 'X-B3-TraceId'
  7. HDR_SPAN_ID = 'X-B3-SpanId'
  8. HDR_PARENT_SPAN_ID = 'X-B3-ParentSpanId'
  9. HDR_SAMPLED = 'X-B3-Sampled'
  10. if six.PY2:
  11. __org_endheaders__ = httplib.HTTPConnection.endheaders
  12. __org_begin__ = httplib.HTTPResponse.begin
  13. def _patched_endheaders(self):
  14. if api.is_tracing():
  15. trace_data = api.get_trace_data()
  16. new_span_id = api.generate_span_id()
  17. self.putheader(HDR_TRACE_ID, hex_str(trace_data.trace_id))
  18. self.putheader(HDR_SPAN_ID, hex_str(new_span_id))
  19. self.putheader(HDR_PARENT_SPAN_ID, hex_str(trace_data.span_id))
  20. self.putheader(HDR_SAMPLED, int(trace_data.sampled))
  21. api.put_annotation('Client Send')
  22. __org_endheaders__(self)
  23. def _patched_begin(self):
  24. __org_begin__(self)
  25. if api.is_tracing():
  26. api.put_annotation('Client Recv (%s)' % self.status)
  27. def patch():
  28. if six.PY2:
  29. httplib.HTTPConnection.endheaders = _patched_endheaders
  30. httplib.HTTPResponse.begin = _patched_begin
  31. if six.PY3:
  32. warnings.warn("Since current Python thrift release \
  33. doesn't support Python 3, eventlet.zipkin.http \
  34. doesn't also support Python 3 (http.client)")
  35. def unpatch():
  36. if six.PY2:
  37. httplib.HTTPConnection.endheaders = __org_endheaders__
  38. httplib.HTTPResponse.begin = __org_begin__
  39. if six.PY3:
  40. pass
  41. def hex_str(n):
  42. """
  43. Thrift uses a binary representation of trace and span ids
  44. HTTP headers use a hexadecimal representation of the same
  45. """
  46. return '%0.16x' % (n,)