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: ensure status is always string #640

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def translate_to_collector(spans: Sequence[Span]):
code=span.status.canonical_code.value,
message=span.status.description,
)

collector_span = trace_pb2.Span(
name=trace_pb2.TruncatableString(value=span.name),
kind=utils.get_collector_span_kind(span.kind),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ def test_translate_to_collector(self):
)
otel_spans[0].end(end_time=end_times[0])
otel_spans[1].start(start_time=start_times[1])
otel_spans[1].set_status(
trace_api.Status(
trace_api.status.StatusCanonicalCode.INTERNAL, {"test", "val"},
)
)
otel_spans[1].end(end_time=end_times[1])
otel_spans[2].start(start_time=start_times[2])
otel_spans[2].end(end_time=end_times[2])
Expand Down Expand Up @@ -263,6 +268,11 @@ def test_translate_to_collector(self):
output_spans[0].links.link[0].type,
trace_pb2.Span.Link.Type.TYPE_UNSPECIFIED,
)
self.assertEqual(
output_spans[1].status.code,
trace_api.status.StatusCanonicalCode.INTERNAL.value,
)
self.assertEqual(output_spans[1].status.message, "")

Choose a reason for hiding this comment

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

I think this particular test should not be included here as it's testing something implemented in a different place.

self.assertEqual(
output_spans[2].links.link[0].type,
trace_pb2.Span.Link.Type.PARENT_LINKED_SPAN,
Expand Down
9 changes: 8 additions & 1 deletion opentelemetry-api/src/opentelemetry/trace/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
# limitations under the License.

import enum
import logging
import typing

logger = logging.getLogger(__name__)


class StatusCanonicalCode(enum.Enum):
"""Represents the canonical set of status codes of a finished Span."""
Expand Down Expand Up @@ -167,7 +170,11 @@ def __init__(
description: typing.Optional[str] = None,
):
self._canonical_code = canonical_code
self._description = description
self._description = None
if description is not None and not isinstance(description, str):
logger.warning("Invalid status description type, expected str")
else:
self._description = description

@property
def canonical_code(self) -> StatusCanonicalCode:
Expand Down
37 changes: 37 additions & 0 deletions opentelemetry-api/tests/trace/test_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
from logging import WARNING

from opentelemetry.trace.status import Status, StatusCanonicalCode


class TestStatus(unittest.TestCase):
def test_constructor(self):
status = Status()
self.assertEqual(status.canonical_code, StatusCanonicalCode.OK)
codeboten marked this conversation as resolved.
Show resolved Hide resolved
self.assertIsNone(status.description)

status = Status(StatusCanonicalCode.UNAVAILABLE, "unavailable")
self.assertEqual(
codeboten marked this conversation as resolved.
Show resolved Hide resolved
status.canonical_code, StatusCanonicalCode.UNAVAILABLE
)
self.assertEqual(status.description, "unavailable")

def test_invalid_description(self):
with self.assertLogs(level=WARNING):
status = Status(description={"test": "val"}) # type: ignore
self.assertEqual(status.canonical_code, StatusCanonicalCode.OK)
codeboten marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(status.description, "")