Created
May 31, 2017 14:10
-
-
Save mainroach/496c40134ba99809f09713f6b8edf9c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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