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

Commit 5831ba9

Browse files
victoraugustollsc24t
authored andcommitted
Add set_status to span, update pymongo integration (#738)
1 parent dfd6d4f commit 5831ba9

File tree

16 files changed

+209
-46
lines changed

16 files changed

+209
-46
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changelog
22

33
## Unreleased
4+
- Added `set_status` to `span`
5+
([#738](https://github.com/census-instrumentation/opencensus-python/pull/738))
46

57
## 0.7.0
68
Released 2019-07-31

contrib/opencensus-ext-flask/tests/test_flask_middleware.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,10 @@ def test_teardown_include_exception(self):
316316
exported_spandata = mock_exporter.export.call_args[0][0][0]
317317
self.assertIsInstance(exported_spandata, span_data.SpanData)
318318
self.assertIsInstance(exported_spandata.status, status.Status)
319-
self.assertEqual(exported_spandata.status.code, code_pb2.UNKNOWN)
320-
self.assertEqual(exported_spandata.status.message, 'error')
319+
self.assertEqual(
320+
exported_spandata.status.canonical_code, code_pb2.UNKNOWN
321+
)
322+
self.assertEqual(exported_spandata.status.description, 'error')
321323

322324
def test_teardown_include_exception_and_traceback(self):
323325
mock_exporter = mock.MagicMock()
@@ -331,8 +333,10 @@ def test_teardown_include_exception_and_traceback(self):
331333
exported_spandata = mock_exporter.export.call_args[0][0][0]
332334
self.assertIsInstance(exported_spandata, span_data.SpanData)
333335
self.assertIsInstance(exported_spandata.status, status.Status)
334-
self.assertEqual(exported_spandata.status.code, code_pb2.UNKNOWN)
335-
self.assertEqual(exported_spandata.status.message, 'error')
336+
self.assertEqual(
337+
exported_spandata.status.canonical_code, code_pb2.UNKNOWN
338+
)
339+
self.assertEqual(exported_spandata.status.description, 'error')
336340
self.assertIsInstance(
337341
exported_spandata.stack_trace, stack_trace.StackTrace
338342
)

contrib/opencensus-ext-grpc/tests/test_server_interceptor.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,10 @@ def test_intercept_handler_exception(self):
147147

148148
# check that the status obj is attached to the current span
149149
self.assertIsNotNone(current_span.status)
150-
self.assertEqual(current_span.status.code, code_pb2.UNKNOWN)
151-
self.assertEqual(current_span.status.message, 'Test')
150+
self.assertEqual(
151+
current_span.status.canonical_code, code_pb2.UNKNOWN
152+
)
153+
self.assertEqual(current_span.status.description, 'Test')
152154

153155
@mock.patch(
154156
'opencensus.trace.execution_context.get_opencensus_tracer')

contrib/opencensus-ext-jaeger/opencensus/ext/jaeger/trace_exporter/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,12 @@ def translate_to_jaeger(self, span_datas):
184184
tags.append(jaeger.Tag(
185185
key='status.code',
186186
vType=jaeger.TagType.LONG,
187-
vLong=status.code))
187+
vLong=status.canonical_code))
188188

189189
tags.append(jaeger.Tag(
190190
key='status.message',
191191
vType=jaeger.TagType.STRING,
192-
vStr=status.message))
192+
vStr=status.description))
193193

194194
refs = _extract_refs_from_span(span)
195195
logs = _extract_logs_from_span(span)

contrib/opencensus-ext-ocagent/opencensus/ext/ocagent/trace_exporter/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ def translate_to_trace_proto(span_data):
4343
span_data.start_time),
4444
end_time=ocagent_utils.proto_ts_from_datetime_str(span_data.end_time),
4545
status=trace_pb2.Status(
46-
code=span_data.status.code,
47-
message=span_data.status.message)
46+
code=span_data.status.canonical_code,
47+
message=span_data.status.description,
48+
)
4849
if span_data.status is not None else None,
4950
same_process_as_parent_span=BoolValue(
5051
value=span_data.same_process_as_parent_span)

contrib/opencensus-ext-pymongo/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changelog
22

33
## Unreleased
4+
- Changed attributes names to make it compatible with [OpenTelemetry](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md), maintaining OpenCensus specs fidelity
5+
([#738](https://github.com/census-instrumentation/opencensus-python/pull/738))
46

57
## 0.1.3
68
Released 2019-05-31

contrib/opencensus-ext-pymongo/opencensus/ext/pymongo/trace.py

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616

1717
from pymongo import monitoring
1818

19+
from google.rpc import code_pb2
20+
1921
from opencensus.trace import execution_context
2022
from opencensus.trace import span as span_module
23+
from opencensus.trace import status as status_module
2124

2225

2326
log = logging.getLogger(__name__)
@@ -34,7 +37,6 @@ def trace_integration(tracer=None):
3437

3538

3639
class MongoCommandListener(monitoring.CommandListener):
37-
3840
def __init__(self, tracer=None):
3941
self._tracer = tracer
4042

@@ -44,30 +46,47 @@ def tracer(self):
4446

4547
def started(self, event):
4648
span = self.tracer.start_span(
47-
name='{}.{}.{}.{}'.format(MODULE_NAME,
48-
event.database_name,
49-
event.command.get(event.command_name),
50-
event.command_name))
49+
name='{}.{}.{}.{}'.format(
50+
MODULE_NAME,
51+
event.database_name,
52+
event.command.get(event.command_name),
53+
event.command_name,
54+
)
55+
)
5156
span.span_kind = span_module.SpanKind.CLIENT
5257

58+
self.tracer.add_attribute_to_current_span('component', 'mongodb')
59+
self.tracer.add_attribute_to_current_span('db.type', 'mongodb')
60+
self.tracer.add_attribute_to_current_span(
61+
'db.instance', event.database_name
62+
)
63+
self.tracer.add_attribute_to_current_span(
64+
'db.statement', event.command.get(event.command_name)
65+
)
66+
5367
for attr in COMMAND_ATTRIBUTES:
5468
_attr = event.command.get(attr)
5569
if _attr is not None:
5670
self.tracer.add_attribute_to_current_span(attr, str(_attr))
5771

5872
self.tracer.add_attribute_to_current_span(
59-
'request_id', event.request_id)
73+
'request_id', event.request_id
74+
)
6075

6176
self.tracer.add_attribute_to_current_span(
62-
'connection_id', str(event.connection_id))
77+
'connection_id', str(event.connection_id)
78+
)
6379

6480
def succeeded(self, event):
65-
self._stop('succeeded')
81+
self._stop(code_pb2.OK)
6682

6783
def failed(self, event):
68-
self._stop('failed')
69-
70-
def _stop(self, status):
71-
self.tracer.add_attribute_to_current_span('status', status)
72-
84+
self._stop(code_pb2.UNKNOWN, 'MongoDB error', event.failure)
85+
86+
def _stop(self, code, message='', details=None):
87+
span = self.tracer.current_span()
88+
status = status_module.Status(
89+
code=code, message=message, details=details
90+
)
91+
span.set_status(status)
7392
self.tracer.end_span()

contrib/opencensus-ext-pymongo/tests/test_pymongo_trace.py

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def test_started(self):
4949
}
5050

5151
expected_attrs = {
52+
'component': 'mongodb',
53+
'db.type': 'mongodb',
54+
'db.instance': 'database_name',
55+
'db.statement': 'find',
5256
'filter': 'filter',
5357
'sort': 'sort',
5458
'limit': 'limit',
@@ -63,8 +67,8 @@ def test_started(self):
6367
trace.MongoCommandListener().started(
6468
event=MockEvent(command_attrs))
6569

66-
self.assertEqual(mock_tracer.current_span.attributes, expected_attrs)
67-
self.assertEqual(mock_tracer.current_span.name, expected_name)
70+
self.assertEqual(mock_tracer.span.attributes, expected_attrs)
71+
self.assertEqual(mock_tracer.span.name, expected_name)
6872

6973
def test_succeed(self):
7074
mock_tracer = MockTracer()
@@ -74,12 +78,16 @@ def test_succeed(self):
7478
'opencensus.trace.execution_context.get_opencensus_tracer',
7579
return_value=mock_tracer)
7680

77-
expected_attrs = {'status': 'succeeded'}
81+
expected_status = {
82+
'code': 0,
83+
'message': '',
84+
'details': None
85+
}
7886

7987
with patch:
8088
trace.MongoCommandListener().succeeded(event=MockEvent(None))
8189

82-
self.assertEqual(mock_tracer.current_span.attributes, expected_attrs)
90+
self.assertEqual(mock_tracer.span.status, expected_status)
8391
mock_tracer.end_span.assert_called_with()
8492

8593
def test_failed(self):
@@ -90,12 +98,16 @@ def test_failed(self):
9098
'opencensus.trace.execution_context.get_opencensus_tracer',
9199
return_value=mock_tracer)
92100

93-
expected_attrs = {'status': 'failed'}
101+
expected_status = {
102+
'code': 2,
103+
'message': 'MongoDB error',
104+
'details': 'failure'
105+
}
94106

95107
with patch:
96108
trace.MongoCommandListener().failed(event=MockEvent(None))
97109

98-
self.assertEqual(mock_tracer.current_span.attributes, expected_attrs)
110+
self.assertEqual(mock_tracer.span.status, expected_status)
99111
mock_tracer.end_span.assert_called_with()
100112

101113

@@ -115,17 +127,31 @@ def __getattr__(self, item):
115127
return item
116128

117129

130+
class MockSpan(object):
131+
def __init__(self):
132+
self.status = None
133+
134+
def set_status(self, status):
135+
self.status = {
136+
'code': status.canonical_code,
137+
'message': status.description,
138+
'details': status.details,
139+
}
140+
141+
118142
class MockTracer(object):
119143
def __init__(self):
120-
self.current_span = None
144+
self.span = MockSpan()
121145
self.end_span = mock.Mock()
122146

123147
def start_span(self, name=None):
124-
span = mock.Mock()
125-
span.name = name
126-
span.attributes = {}
127-
self.current_span = span
128-
return span
148+
self.span.name = name
149+
self.span.attributes = {}
150+
self.span.status = {}
151+
return self.span
129152

130153
def add_attribute_to_current_span(self, key, value):
131-
self.current_span.attributes[key] = value
154+
self.span.attributes[key] = value
155+
156+
def current_span(self):
157+
return self.span

opencensus/trace/base_span.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ def add_link(self, link):
8080
"""
8181
raise NotImplementedError
8282

83+
def set_status(self, status):
84+
"""Sets span status.
85+
86+
:type code: :class: `~opencensus.trace.status.Status`
87+
:param code: A Status object.
88+
"""
89+
raise NotImplementedError
90+
8391
def start(self):
8492
"""Set the start time for a span."""
8593
raise NotImplementedError

opencensus/trace/blank_span.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ def add_link(self, link):
136136
"""
137137
pass
138138

139+
def set_status(self, status):
140+
"""No-op implementation of this method.
141+
142+
:type code: :class: `~opencensus.trace.status.Status`
143+
:param code: A Status object.
144+
"""
145+
pass
146+
139147
def start(self):
140148
"""No-op implementation of this method."""
141149
pass

0 commit comments

Comments
 (0)