Skip to content

Instantly share code, notes, and snippets.

@mainroach
Created May 31, 2017 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mainroach/496c40134ba99809f09713f6b8edf9c5 to your computer and use it in GitHub Desktop.
Save mainroach/496c40134ba99809f09713f6b8edf9c5 to your computer and use it in GitHub Desktop.
class Span(object):
def __init__(self, name, kind='SPAN_KIND_UNSPECIFIED'):
self.id = str(random.getrandbits(64))
self.name = name
self.kind = kind
def start(self):
logging.info("CREATED SPAN")
self.start = datetime.now()
def finish(self):
self.end = datetime.now()
if hasattr(_trace_context, 'spans'):
_trace_context.spans.append(self)
# Helpers for scope-specific use cases
def __enter__(self):
self.start()
return self
def __exit__(self, type, value, tb):
self.finish()
def json(self):
"""Format as a dictionary of the correct shape for sending to the Cloud
Trace REST API as a JSON object"""
j = {
'kind': self.kind,
'name': self.name,
'spanId': self.id,
'startTime': self.start.isoformat() + 'Z',
'endTime': self.end.isoformat() + 'Z',
}
return j
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment