Skip to content

Commit

Permalink
Set status for ended spans
Browse files Browse the repository at this point in the history
Fixes #292
  • Loading branch information
ocelotl committed Nov 19, 2019
1 parent fb3e715 commit 4411184
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
from pymongo import monitoring

from opentelemetry.trace import SpanKind
from opentelemetry.trace.status import Status, StatusCanonicalCode
from opentelemetry.trace.status import (
Status, StatusCanonicalCode, OkStatus, UnknownStatus
)

DATABASE_TYPE = "mongodb"
COMMAND_ATTRIBUTES = ["filter", "sort", "skip", "limit", "pipeline"]
Expand Down Expand Up @@ -82,7 +84,7 @@ def succeeded(self, event: monitoring.CommandSucceededEvent):
span.set_attribute(
"db.mongo.duration_micros", event.duration_micros
)
span.set_status(Status(StatusCanonicalCode.OK, event.reply))
span.set_status(OkStatus())
span.end()
self._remove_span(event)

Expand All @@ -92,7 +94,7 @@ def failed(self, event: monitoring.CommandFailedEvent):
span.set_attribute(
"db.mongo.duration_micros", event.duration_micros
)
span.set_status(Status(StatusCanonicalCode.UNKNOWN, event.failure))
span.set_status(UnknownStatus())
span.end()
self._remove_span(event)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ def test_succeeded(self):
self.assertIs(
span.status.canonical_code, trace_api.status.StatusCanonicalCode.OK
)
self.assertEqual(span.status.description, "reply")
self.assertEqual(
span.status.description,
"Not an error, returned on success."
)
self.assertIsNotNone(span.end_time)

def test_failed(self):
Expand All @@ -100,7 +103,9 @@ def test_failed(self):
span.status.canonical_code,
trace_api.status.StatusCanonicalCode.UNKNOWN,
)
self.assertEqual(span.status.description, "failure")
self.assertEqual(
span.status.description, trace_api.status.UnknownStatus.__doc__
)
self.assertIsNotNone(span.end_time)

def test_multiple_commands(self):
Expand Down
33 changes: 29 additions & 4 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
import typing
from contextlib import contextmanager

from opentelemetry.trace.status import Status
from opentelemetry.trace.status import Status, UnknownStatus
from opentelemetry.util import loader, types

# TODO: quarantine
Expand Down Expand Up @@ -124,10 +124,12 @@ class SpanKind(enum.Enum):
https://github.com/open-telemetry/opentelemetry-specification/pull/226.
"""

#: Default value. Indicates that the span is used internally in the application.
#: Default value. Indicates that the span is used internally in the
# application.
INTERNAL = 0

#: Indicates that the span describes an operation that handles a remote request.
#: Indicates that the span describes an operation that handles a remote
# request.
SERVER = 1

#: Indicates that the span describes a request to some remote service.
Expand All @@ -144,9 +146,13 @@ class SpanKind(enum.Enum):
CONSUMER = 4


class Span:
class Span(object):
"""A span represents a single operation within a trace."""

def __init__(self, auto_update_status=False):
# FIXME what is the standard to follow for documentation of attributes?
self._auto_update_status = auto_update_status

def start(self, start_time: typing.Optional[int] = None) -> None:
"""Sets the current time as the span's start time.
Expand Down Expand Up @@ -237,8 +243,27 @@ def __exit__(
exc_tb: typing.Optional[python_types.TracebackType],
) -> None:
"""Ends context manager and calls `end` on the `Span`."""

self.end()

if (
exc_val is not None and
self.status is None and
self._auto_update_status
):
if isinstance(exc_val, Status):
self.set_status(exc_val.status)

else:
self.set_status(UnknownStatus)

# return False
# return True

# FIXME https://docs.python.org/3.7/reference/datamodel.html?highlight=__exit__#object.__exit__
# says that a boolean value should be returned. Is the lack of the
# return of a boolean value here intentional?


class TraceOptions(int):
"""A bitmask that represents options specific to the trace.
Expand Down
Loading

0 comments on commit 4411184

Please sign in to comment.