Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix span batching to stackdriver #425

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions opencensus/trace/exporters/stackdriver_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,15 @@ def emit(self, span_datas):
for sd in span_datas:
trace_span_map[sd.context.trace_id] += [sd]

stackdriver_spans = []
# Write spans to Stackdriver
for _, sds in trace_span_map.items():
# convert to the legacy trace json for easier refactoring
# TODO: refactor this to use the span data directly
trace = span_data.format_legacy_trace_json(sds)
stackdriver_spans = self.translate_to_stackdriver(trace)
self.client.batch_write_spans(project, stackdriver_spans)
stackdriver_spans.extend(self.translate_to_stackdriver(trace))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity: why change this to a generator if you're just appending the results?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to avoid intermediate lists


self.client.batch_write_spans(project, {'spans': stackdriver_spans})

def export(self, span_datas):
"""
Expand All @@ -247,7 +249,6 @@ def translate_to_stackdriver(self, trace):
set_attributes(trace)
spans_json = trace.get('spans')
trace_id = trace.get('traceId')
spans_list = []

for span in spans_json:
span_name = 'projects/{}/traces/{}/spans/{}'.format(
Expand All @@ -272,10 +273,7 @@ def translate_to_stackdriver(self, trace):
parent_span_id = str(span.get('parentSpanId'))
span_json['parentSpanId'] = parent_span_id

spans_list.append(span_json)

spans = {'spans': spans_list}
return spans
yield span_json

def map_attributes(self, attribute_map):
if attribute_map is None:
Expand Down
90 changes: 38 additions & 52 deletions tests/unit/trace/exporters/test_stackdriver_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,63 +200,49 @@ def test_translate_to_stackdriver(self, mr_mock):
exporter = stackdriver_exporter.StackdriverExporter(
client=client, project_id=project_id)

spans = exporter.translate_to_stackdriver(trace)
spans = list(exporter.translate_to_stackdriver(trace))

expected_traces = {
'spans': [{
'name':
'projects/{}/traces/{}/spans/{}'.format(
expected_traces = [{
'name': 'projects/{}/traces/{}/spans/{}'.format(
project_id, trace_id, span_id),
'displayName': {
'value': span_name,
'truncated_byte_count': 0
},
'attributes': {
'attributeMap': {
'g.co/agent': {
'string_value': {
'truncated_byte_count':
0,
'value':
'displayName': {
'value': span_name,
'truncated_byte_count': 0
},
'attributes': {
'attributeMap': {
'g.co/agent': {
'string_value': {
'truncated_byte_count': 0,
'value':
'opencensus-python [{}]'.format(__version__)
}
},
'key': {
'string_value': {
'truncated_byte_count': 0,
'value': 'value'
}
},
'/http/host': {
'string_value': {
'truncated_byte_count': 0,
'value': 'host'
}
}
},
'key': {
'string_value': {
'truncated_byte_count': 0,
'value': 'value'
}
},
'/http/host': {
'string_value': {
'truncated_byte_count': 0,
'value': 'host'
}
}
},
'spanId':
str(span_id),
'startTime':
start_time,
'endTime':
end_time,
'parentSpanId':
str(parent_span_id),
'status':
None,
'links':
None,
'stackTrace':
None,
'timeEvents':
None,
'childSpanCount':
0,
'sameProcessAsParentSpan':
None
}]
}
}
},
'spanId': str(span_id),
'startTime': start_time,
'endTime': end_time,
'parentSpanId': str(parent_span_id),
'status': None,
'links': None,
'stackTrace': None,
'timeEvents': None,
'childSpanCount': 0,
'sameProcessAsParentSpan': None
}]

self.assertEqual(spans, expected_traces)

Expand Down