Skip to content
This repository was archived by the owner on Jun 16, 2025. It is now read-only.

Make root span trace IDs unique #505

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
Use `UnknownMetricFamily` for `SumData` instead of `UntypedMetricFamily`.
Check if label keys and values match before exporting.
- Remove min and max from Distribution.
- Fix a bug that caused root spans from the same tracer to share a trace ID
([#505](https://github.com/census-instrumentation/opencensus-python/pull/505)

## 0.2.0
Released 2019-01-18
Expand Down
5 changes: 4 additions & 1 deletion opencensus/trace/tracers/context_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
import threading

from opencensus.trace import execution_context
from opencensus.trace.span_context import SpanContext
from opencensus.trace import span as trace_span
from opencensus.trace import span_data as span_data_module
from opencensus.trace.exporters import print_exporter
from opencensus.trace.span_context import SpanContext
from opencensus.trace.span_context import generate_trace_id
from opencensus.trace.tracers import base


Expand Down Expand Up @@ -116,6 +117,8 @@ def end_span(self, *args, **kwargs):
execution_context.set_current_span(cur_span.parent_span)
else:
execution_context.set_current_span(None)
self.span_context.trace_id = generate_trace_id()
self.trace_id = self.span_context.trace_id
Copy link
Member Author

Choose a reason for hiding this comment

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

tracer.trace_id is unused, and I don't know why it's included on the tracer. Removing it is a problem for another PR.


with self._spans_list_condition:
if cur_span in self._spans_list:
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/trace/tracers/test_context_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,21 @@ def test_end_span_active(self, mock_current_span):
mock_span.attributes = {}
mock_span.__iter__ = mock.Mock(return_value=iter([mock_span]))
parent_span_id = '6e0c63257de34c92'
mock_span.parent_span = mock.Mock(spec=span.Span)
mock_span.parent_span.span_id = parent_span_id
mock_current_span.return_value = mock_span
old_trace_id = tracer.trace_id
self.assertEqual(tracer.trace_id, tracer.span_context.trace_id)
tracer.end_span()

self.assertTrue(mock_span.finish.called)
self.assertEqual(tracer.span_context.span_id, parent_span_id)
self.assertFalse(tracer.exporter.export.called)

# Check that we don't change the trace ID if the parent span exists
self.assertEqual(tracer.trace_id, old_trace_id)
self.assertEqual(tracer.trace_id, tracer.span_context.trace_id)

@mock.patch.object(context_tracer.ContextTracer, 'current_span')
def test_end_span_without_parent(self, mock_current_span):
from opencensus.trace.execution_context import get_current_span
Expand All @@ -159,11 +166,17 @@ def test_end_span_without_parent(self, mock_current_span):
mock_span.attributes = {}
mock_span.__iter__ = mock.Mock(return_value=iter([mock_span]))
mock_current_span.return_value = mock_span
old_trace_id = tracer.trace_id
self.assertEqual(tracer.trace_id, tracer.span_context.trace_id)
tracer.end_span()

cur_span = get_current_span()
self.assertIsNone(cur_span)

# Check that ending the root span generates a new trace ID
self.assertNotEqual(tracer.trace_id, old_trace_id)
self.assertEqual(tracer.trace_id, tracer.span_context.trace_id)

def test_end_span_batch_export(self):
exporter = mock.Mock()
tracer = context_tracer.ContextTracer(exporter=exporter)
Expand Down