Skip to content

Commit 85855e3

Browse files
committed
Span Status: only set description for ERROR status code.
According to the spec: > Description MUST only be used with the Error StatusCode value. > An empty Description is equivalent with a not present one. https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#set-status This commit updates the `Status` implementation to comply with the spec.
1 parent 1af9f87 commit 85855e3

File tree

7 files changed

+50
-23
lines changed

7 files changed

+50
-23
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v0.18b0...HEAD)
8+
- Status now only sets `description` when `status_code` is set to `StatusCode.ERROR`
9+
([#1673])(https://github.com/open-telemetry/opentelemetry-python/pull/1673)
810

911
### Added
1012
- Add `max_attr_value_length` support to Jaeger exporter

exporter/opentelemetry-exporter-jaeger/tests/test_jaeger_exporter_protobuf.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def test_translate_to_jaeger(self):
192192
otel_spans[1].end(end_time=end_times[1])
193193

194194
otel_spans[2].start(start_time=start_times[2])
195-
otel_spans[2].set_status(Status(StatusCode.OK, "Example description"))
195+
otel_spans[2].set_status(Status(StatusCode.OK))
196196
otel_spans[2].end(end_time=end_times[2])
197197

198198
translate = Translate(otel_spans)
@@ -352,11 +352,6 @@ def test_translate_to_jaeger(self):
352352
v_type=model_pb2.ValueType.STRING,
353353
v_str="OK",
354354
),
355-
model_pb2.KeyValue(
356-
key="otel.status_description",
357-
v_type=model_pb2.ValueType.STRING,
358-
v_str="Example description",
359-
),
360355
model_pb2.KeyValue(
361356
key="span.kind",
362357
v_type=model_pb2.ValueType.STRING,

exporter/opentelemetry-exporter-jaeger/tests/test_jaeger_exporter_thrift.py

+1-6
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def test_translate_to_jaeger(self):
274274
otel_spans[1].end(end_time=end_times[1])
275275

276276
otel_spans[2].start(start_time=start_times[2])
277-
otel_spans[2].set_status(Status(StatusCode.OK, "Example description"))
277+
otel_spans[2].set_status(Status(StatusCode.OK))
278278
otel_spans[2].end(end_time=end_times[2])
279279

280280
translate = Translate(otel_spans)
@@ -396,11 +396,6 @@ def test_translate_to_jaeger(self):
396396
vType=jaeger.TagType.STRING,
397397
vStr="OK",
398398
),
399-
jaeger.Tag(
400-
key="otel.status_description",
401-
vType=jaeger.TagType.STRING,
402-
vStr="Example description",
403-
),
404399
jaeger.Tag(
405400
key="span.kind",
406401
vType=jaeger.TagType.STRING,

exporter/opentelemetry-exporter-opencensus/tests/test_otcollector_trace_exporter.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,7 @@ def test_translate_to_collector(self):
147147
otel_spans[0].set_attribute("key_float", 111.22)
148148
otel_spans[0].set_attribute("key_int", 333)
149149
otel_spans[0].set_status(
150-
trace_api.Status(
151-
trace_api.status.StatusCode.OK, "test description",
152-
)
150+
trace_api.Status(trace_api.status.StatusCode.OK)
153151
)
154152
otel_spans[0].end(end_time=end_times[0])
155153
otel_spans[1].start(start_time=start_times[1])
@@ -198,7 +196,6 @@ def test_translate_to_collector(self):
198196
self.assertEqual(
199197
output_spans[0].status.code, trace_api.status.StatusCode.OK.value,
200198
)
201-
self.assertEqual(output_spans[0].status.message, "test description")
202199
self.assertEqual(len(output_spans[0].tracestate.entries), 1)
203200
self.assertEqual(output_spans[0].tracestate.entries[0].key, "testkey")
204201
self.assertEqual(

opentelemetry-api/src/opentelemetry/trace/status.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,18 @@ def __init__(
4848
):
4949
self._status_code = status_code
5050
self._description = None
51+
5152
if description is not None and not isinstance(description, str):
5253
logger.warning("Invalid status description type, expected str")
53-
else:
54-
self._description = description
54+
return
55+
56+
if status_code != StatusCode.ERROR:
57+
logger.warning(
58+
"description should only be set when status_code is set to StatusCode.ERROR"
59+
)
60+
return
61+
62+
self._description = description
5563

5664
@property
5765
def status_code(self) -> StatusCode:

opentelemetry-api/tests/trace/test_status.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,40 @@ def test_constructor(self):
2929
self.assertEqual(status.description, "unavailable")
3030

3131
def test_invalid_description(self):
32-
with self.assertLogs(level=WARNING):
32+
with self.assertLogs(level=WARNING) as warning:
3333
status = Status(description={"test": "val"}) # type: ignore
3434
self.assertIs(status.status_code, StatusCode.UNSET)
3535
self.assertEqual(status.description, None)
36+
self.assertIn(
37+
"Invalid status description type, expected str",
38+
warning.output[0],
39+
)
40+
41+
def test_description_and_non_error_status(self):
42+
with self.assertLogs(level=WARNING) as warning:
43+
status = Status(
44+
status_code=StatusCode.OK, description="status description"
45+
)
46+
self.assertIs(status.status_code, StatusCode.OK)
47+
self.assertEqual(status.description, None)
48+
self.assertIn(
49+
"description should only be set when status_code is set to StatusCode.ERROR",
50+
warning.output[0],
51+
)
52+
53+
with self.assertLogs(level=WARNING) as warning:
54+
status = Status(
55+
status_code=StatusCode.UNSET, description="status description"
56+
)
57+
self.assertIs(status.status_code, StatusCode.UNSET)
58+
self.assertEqual(status.description, None)
59+
self.assertIn(
60+
"description should only be set when status_code is set to StatusCode.ERROR",
61+
warning.output[0],
62+
)
63+
64+
status = Status(
65+
status_code=StatusCode.ERROR, description="status description"
66+
)
67+
self.assertIs(status.status_code, StatusCode.ERROR)
68+
self.assertEqual(status.description, "status description")

opentelemetry-sdk/tests/trace/test_trace.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -951,13 +951,10 @@ def test_override_error_status(self):
951951
def error_status_test(context):
952952
with self.assertRaises(AssertionError):
953953
with context as root:
954-
root.set_status(
955-
trace_api.status.Status(StatusCode.OK, "OK")
956-
)
954+
root.set_status(trace_api.status.Status(StatusCode.OK))
957955
raise AssertionError("unknown")
958956

959957
self.assertIs(root.status.status_code, StatusCode.OK)
960-
self.assertEqual(root.status.description, "OK")
961958

962959
error_status_test(
963960
trace.TracerProvider().get_tracer(__name__).start_span("root")

0 commit comments

Comments
 (0)