Skip to content

Commit

Permalink
fix(opentracer/Span): return self in methods (#3378)
Browse files Browse the repository at this point in the history
As pointed out in #3365 the opentracing Span object returns itself in
its set_tag and set_operation_name methods and our implementation should
match.
  • Loading branch information
Kyle-Verhoog authored Mar 4, 2022
1 parent 45fccce commit 2204b68
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 4 additions & 2 deletions ddtrace/opentracer/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,10 @@ def get_baggage_item(self, key):
return self.context.get_baggage_item(key)

def set_operation_name(self, operation_name):
# type: (str) -> None
# type: (str) -> Span
"""Set the operation name."""
self._dd_span.name = operation_name
return self

def log_kv(self, key_values, timestamp=None):
# type: (Dict[_TagNameType, Any], Optional[float]) -> Span
Expand Down Expand Up @@ -133,7 +134,7 @@ def log_kv(self, key_values, timestamp=None):
return self

def set_tag(self, key, value):
# type: (_TagNameType, Any) -> None
# type: (_TagNameType, Any) -> Span
"""Set a tag on the span.
This sets the tag on the underlying datadog span.
Expand All @@ -152,6 +153,7 @@ def set_tag(self, key, value):
self._dd_span.context.sampling_priority = value
else:
self._dd_span.set_tag(key, value)
return self

def _get_tag(self, key):
# type: (_TagNameType) -> Optional[Text]
Expand Down
5 changes: 5 additions & 0 deletions releasenotes/notes/opentracer-span-1abe3738df78d3c1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
opentracer: update ``set_tag`` and ``set_operation_name`` to return a
reference to the span to match the OpenTracing spec.
6 changes: 4 additions & 2 deletions tests/opentracer/core/test_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ def test_init(self, nop_tracer, nop_span_ctx):

def test_tags(self, nop_span):
"""Set a tag and get it back."""
nop_span.set_tag("test", 23)
r = nop_span.set_tag("test", 23)
assert nop_span._get_metric("test") == 23
assert r is nop_span

def test_set_baggage(self, nop_span):
"""Test setting baggage."""
Expand Down Expand Up @@ -96,8 +97,9 @@ def test_log_dd_kv(self, nop_span):
def test_operation_name(self, nop_span):
"""Sanity check for setting the operation name."""
# just try setting the operation name
nop_span.set_operation_name("new_op_name")
r = nop_span.set_operation_name("new_op_name")
assert nop_span._dd_span.name == "new_op_name"
assert r is nop_span

def test_context_manager(self, nop_span):
"""Test the span context manager."""
Expand Down

0 comments on commit 2204b68

Please sign in to comment.