You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.7KB

  1. import base64
  2. import warnings
  3. from scribe import scribe
  4. from thrift.transport import TTransport, TSocket
  5. from thrift.protocol import TBinaryProtocol
  6. from eventlet import GreenPile
  7. CATEGORY = 'zipkin'
  8. class ZipkinClient(object):
  9. def __init__(self, host='127.0.0.1', port=9410):
  10. """
  11. :param host: zipkin collector IP addoress (default '127.0.0.1')
  12. :param port: zipkin collector port (default 9410)
  13. """
  14. self.host = host
  15. self.port = port
  16. self.pile = GreenPile(1)
  17. self._connect()
  18. def _connect(self):
  19. socket = TSocket.TSocket(self.host, self.port)
  20. self.transport = TTransport.TFramedTransport(socket)
  21. protocol = TBinaryProtocol.TBinaryProtocol(self.transport,
  22. False, False)
  23. self.scribe_client = scribe.Client(protocol)
  24. try:
  25. self.transport.open()
  26. except TTransport.TTransportException as e:
  27. warnings.warn(e.message)
  28. def _build_message(self, thrift_obj):
  29. trans = TTransport.TMemoryBuffer()
  30. protocol = TBinaryProtocol.TBinaryProtocolAccelerated(trans=trans)
  31. thrift_obj.write(protocol)
  32. return base64.b64encode(trans.getvalue())
  33. def send_to_collector(self, span):
  34. self.pile.spawn(self._send, span)
  35. def _send(self, span):
  36. log_entry = scribe.LogEntry(CATEGORY, self._build_message(span))
  37. try:
  38. self.scribe_client.Log([log_entry])
  39. except Exception as e:
  40. msg = 'ZipkinClient send error %s' % str(e)
  41. warnings.warn(msg)
  42. self._connect()
  43. def close(self):
  44. self.transport.close()