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

Add setStatus in Span #213

Merged
merged 37 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
b837c9f
Span add override parameters
hectorhdzg Sep 26, 2019
a12fe5d
Make lint happy
hectorhdzg Sep 26, 2019
276ecb4
Addressing comments
hectorhdzg Sep 27, 2019
2b90351
Addressing comments
hectorhdzg Sep 27, 2019
eccef1a
Allowing 0 as start and end time
hectorhdzg Sep 28, 2019
a187fec
Fix lint issues
hectorhdzg Sep 28, 2019
a43c980
Merge remote-tracking branch 'upstream/master'
hectorhdzg Oct 2, 2019
8dfb44e
Merge remote-tracking branch 'upstream/master'
hectorhdzg Oct 8, 2019
77d3649
Add code coverage
hectorhdzg Oct 8, 2019
f3af20f
Revert latest commit
hectorhdzg Oct 9, 2019
1229bc7
Merge remote-tracking branch 'upstream/master'
hectorhdzg Oct 9, 2019
e54a053
Adding setStatus in Span
hectorhdzg Oct 9, 2019
174ac90
Fixing lint issues
hectorhdzg Oct 9, 2019
2015944
Addressing comments
hectorhdzg Oct 10, 2019
437b5a5
Fixing mypy issues
hectorhdzg Oct 10, 2019
8ae0532
Fixing old python ver issue
hectorhdzg Oct 10, 2019
798551b
Fixing formatting issue
hectorhdzg Oct 10, 2019
35a93d4
Fixing issue with trailing whitespace
hectorhdzg Oct 10, 2019
db45017
Addressing comments
hectorhdzg Oct 11, 2019
7fe57a1
Fixing lint issues
hectorhdzg Oct 11, 2019
57ebc24
Fixing formatting issues
hectorhdzg Oct 11, 2019
27df21b
Fixing lint issues
hectorhdzg Oct 11, 2019
4ccc142
Fixing issues generating docs
hectorhdzg Oct 12, 2019
d08c3db
Addressing comments
hectorhdzg Oct 15, 2019
5c39740
Merge branch 'master' into spanStatus
hectorhdzg Oct 15, 2019
7265b92
Fixing lint issue after merge
hectorhdzg Oct 15, 2019
98960ef
Lint fix
hectorhdzg Oct 15, 2019
a714d20
Fix tests
hectorhdzg Oct 15, 2019
ac7cf0d
Enum docstring fixes
c24t Oct 15, 2019
1863412
Docstring tweaks
c24t Oct 15, 2019
27ba6a4
Fix wrap
c24t Oct 15, 2019
5e17bb7
Merge pull request #1 from c24t/spanStatus-docs-fixes
hectorhdzg Oct 15, 2019
d8f8c26
Addressing comments
hectorhdzg Oct 15, 2019
673b5be
Merge
hectorhdzg Oct 15, 2019
c5e71eb
Addressing comments
hectorhdzg Oct 16, 2019
ce3552b
Fix mangled comment wrapping
c24t Oct 16, 2019
04709bd
Wrap some text
c24t Oct 16, 2019
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
31 changes: 31 additions & 0 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,33 @@ class SpanKind(enum.Enum):
CONSUMER = 4


class SpanStatus:
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

This consists only of getters. I think this is a bit un-pythonic, should we make them @propertys?

"""Represents the status of a finished Span """

def get_canonical_code(self) -> int:
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
"""Gets the status CanonicalCode.
StatusCanonicalCode represents the canonical set of status codes of a finished Span, following the Standard GRPC codes
https://github.com/grpc/grpc/blob/master/doc/statuscodes.md
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved

Returns:
A number representing the CanonicalCode
"""

def get_description(self) -> str:
"""Gets the status description.

Returns:
Returns the description of the Status.
"""

def get_is_ok(self) -> bool:
"""Gets the status is ok flag.
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved

Returns:
Returns false if this Status represents an error, else returns true.
"""


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

Expand Down Expand Up @@ -226,6 +253,10 @@ def is_recording_events(self) -> bool:
events with the add_event operation and attributes using set_attribute.
"""

def set_status(self, status: SpanStatus) -> None:
"""Sets the Status of the Span. If used, this will override the default Span status, which is OK.
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
"""


class TraceOptions(int):
"""A bitmask that represents options specific to the trace.
Expand Down
31 changes: 31 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,32 @@ def shutdown(self) -> None:
sp.shutdown()


class SpanStatus(trace_api.SpanStatus):
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
"""See `opentelemetry.trace.SpanStatus`.

Represents the status of a finished Span

Args:
canonical_code: Represents the canonical set of status codes of a finished Span, following the Standard GRPC codes
description: Description of this Status
is_ok: Use to determine if the span was an error or not
"""

def __init__(self, canonical_code=0, description=None, is_ok=True):
Copy link
Member

Choose a reason for hiding this comment

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

I'm confused, I thought is_ok is deduced from the code?

Copy link
Member

Choose a reason for hiding this comment

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

I created open-telemetry/opentelemetry-specification#297 in the spec, but I think the is_ok getter of the code should just use canonical_code.

self.canonical_code = canonical_code
self.description = description
self.is_ok = is_ok

def get_canonical_code(self) -> bool:
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
return self.canonical_code

def get_description(self) -> bool:
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
return self.description

def get_is_ok(self) -> bool:
return self.is_ok


class Span(trace_api.Span):
"""See `opentelemetry.trace.Span`.

Expand Down Expand Up @@ -132,6 +158,7 @@ def __init__(
links: typing.Sequence[trace_api.Link] = None, # TODO
kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL,
span_processor: SpanProcessor = SpanProcessor(),
status: SpanStatus = SpanStatus(),
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
) -> None:

self.name = name
Expand All @@ -141,6 +168,7 @@ def __init__(
self.trace_config = trace_config
self.resource = resource
self.kind = kind
self.status = status

self.span_processor = span_processor
self._lock = threading.Lock()
Expand Down Expand Up @@ -285,6 +313,9 @@ def update_name(self, name: str) -> None:
def is_recording_events(self) -> bool:
return True

def set_status(self, status: trace_api.SpanStatus) -> None:
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
self.status = status
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved


def generate_span_id() -> int:
"""Get a new random span ID.
Expand Down
10 changes: 10 additions & 0 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,16 @@ def test_start_span(self):
span.start()
self.assertEqual(start_time, span.start_time)

# default status
self.assertEqual(span.status.get_is_ok(), True)
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(span.status.get_canonical_code(), 0)
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(span.status.get_description(), None)
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved

# status
new_status = trace.SpanStatus(2, "Test description", False)
span.set_status(new_status)
self.assertEqual(span.status, new_status)
hectorhdzg marked this conversation as resolved.
Show resolved Hide resolved

def test_span_override_start_and_end_time(self):
"""Span sending custom start_time and end_time values"""
span = trace.Span("name", mock.Mock(spec=trace_api.SpanContext))
Expand Down