Skip to content

Commit

Permalink
chore(span): deprecate Span.pprint (DataDog#3213)
Browse files Browse the repository at this point in the history
Introduces Span._pprint() to internalize Span.pprint().

Span.pprint will be removed in v1.0.

## Checklist
- [ ] Added to the correct milestone.
- [ ] Tests provided or description of manual testing performed is included in the code or PR.
- [ ] Library documentation is updated.
- [ ] [Corp site](https://github.com/DataDog/documentation/) documentation is updated (link to the PR).
  • Loading branch information
mabdinur authored Feb 4, 2022
1 parent a17276c commit 5a07399
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 39 deletions.
5 changes: 5 additions & 0 deletions ddtrace/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from .internal.compat import stringify
from .internal.compat import time_ns
from .internal.logger import get_logger
from .internal.utils.deprecation import deprecated


if TYPE_CHECKING:
Expand Down Expand Up @@ -490,7 +491,11 @@ def _remove_exc_info(self):
self._remove_tag(ERROR_TYPE)
self._remove_tag(ERROR_STACK)

@deprecated(message="Span.pprint will be removed.", version="1.0.0")
def pprint(self):
return self._pprint()

def _pprint(self):
# type: () -> str
"""Return a human readable version of the span."""
data = [
Expand Down
4 changes: 2 additions & 2 deletions ddtrace/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ def _on_span_finish(self, span):
p.on_span_finish(span)

if self.log.isEnabledFor(logging.DEBUG):
self.log.debug("finishing span %s (enabled:%s)", span.pprint(), self.enabled)
self.log.debug("finishing span %s (enabled:%s)", span._pprint(), self.enabled)

def _initialize_span_processors(self, appsec_enabled=asbool(get_env("appsec", "enabled", default=False))):
# type: (Optional[bool]) -> None
Expand Down Expand Up @@ -805,7 +805,7 @@ def write(self, spans):
if self.log.isEnabledFor(logging.DEBUG):
self.log.debug("writing %s spans (enabled:%s)", len(spans), self.enabled)
for span in spans:
self.log.debug("\n%s", span.pprint())
self.log.debug("\n%s", span._pprint())

if not self.enabled:
return
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
deprecations:
- |
:py:meth:`ddtrace.Span.pprint` is deprecated and will be removed in v1.0.
26 changes: 13 additions & 13 deletions tests/contrib/cherrypy/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_success(self):
self.assertHeader("Content-Type", "text/html;charset=utf-8")
self.assertBody("Hello world!")

assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand All @@ -138,7 +138,7 @@ def test_alias(self):
self.assertHeader("Content-Type", "text/html;charset=utf-8")
self.assertBody("alias")

assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand All @@ -154,7 +154,7 @@ def test_handleme(self):
self.assertErrorPage(418, message="handled")

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand Down Expand Up @@ -210,7 +210,7 @@ def test_unicode(self):
self.assertBody(b"\xc3\xbc\xc5\x8b\xc3\xaf\xc4\x89\xc3\xb3\xc4\x91\xc4\x93")

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand All @@ -227,7 +227,7 @@ def test_404(self):
self.assertStatus("404 Not Found")

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand All @@ -252,7 +252,7 @@ def test_propagation(self):
self.assertHeader("Content-Type", "text/html;charset=utf-8")
self.assertBody("Hello world!")

assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand All @@ -279,7 +279,7 @@ def test_disabled_distributed_tracing_config(self):
self.assertBody("Hello world!")

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand Down Expand Up @@ -308,7 +308,7 @@ def test_disabled_distributed_tracing_middleware(self):
self.assertBody("Hello world!")

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand All @@ -327,7 +327,7 @@ def test_custom_span(self):
self.assertBody("hiya")

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand Down Expand Up @@ -376,7 +376,7 @@ def test_variable_resource(self):
self.assertBody("dispatch with abc123")

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand All @@ -397,7 +397,7 @@ def test_post(self):
self.assertBody("Hello world!")

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand All @@ -417,7 +417,7 @@ def test_service_configuration_config(self):
self.assertBody("Hello world!")

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand All @@ -439,7 +439,7 @@ def test_service_configuration_middleware(self):
self.assertBody("Hello world!")

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand Down
20 changes: 10 additions & 10 deletions tests/contrib/flask/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_success(self):
assert rv.data == b"hello"

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand All @@ -129,7 +129,7 @@ def test_template(self):
assert rv.data == b"hello earth"

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 2
by_name = {s.name: s for s in spans}
Expand Down Expand Up @@ -158,7 +158,7 @@ def test_handleme(self):
assert rv.data == b"handled"

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand All @@ -181,7 +181,7 @@ def test_template_err(self):
end = time.time()

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
by_name = {s.name: s for s in spans}
Expand All @@ -205,7 +205,7 @@ def test_template_render_err(self):
end = time.time()

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 2
by_name = {s.name: s for s in spans}
Expand Down Expand Up @@ -284,7 +284,7 @@ def test_unicode(self):
assert rv.data == b"\xc3\xbc\xc5\x8b\xc3\xaf\xc4\x89\xc3\xb3\xc4\x91\xc4\x93"

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand All @@ -306,7 +306,7 @@ def test_404(self):
assert rv.status_code == 404

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand All @@ -330,7 +330,7 @@ def test_propagation(self):
assert rv.data == b"hello"

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand All @@ -344,7 +344,7 @@ def test_custom_span(self):
rv = self.client.get("/custom_span")
assert rv.status_code == 200
# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 1
s = spans[0]
Expand All @@ -368,7 +368,7 @@ def test_success_200_ot(self):
assert rv.data == b"hello"

# ensure trace worked
assert not self.tracer.current_span(), self.tracer.current_span().pprint()
assert not self.tracer.current_span()
spans = self.pop_spans()
assert len(spans) == 2
ot_span, dd_span = spans
Expand Down
4 changes: 2 additions & 2 deletions tests/contrib/pymongo/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ def test_insert_find(self):
assert span.span_type == "mongodb"
assert span.meta.get("mongodb.collection") == "teams"
assert span.meta.get("mongodb.db") == "testdb"
assert span.meta.get("out.host"), span.pprint()
assert span.metrics.get("out.port"), span.pprint()
assert span.meta.get("out.host")
assert span.metrics.get("out.port")
assert span.start > start
assert span.duration < end - start

Expand Down
10 changes: 5 additions & 5 deletions tests/tracer/test_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ def test_span_pprint():
root.set_tag("t", "v")
root.set_metric("m", 1.0)
root.finish()
actual = root.pprint()
actual = root._pprint()
assert "name='test.span'" in actual
assert "service='s'" in actual
assert "resource='r'" in actual
Expand All @@ -647,22 +647,22 @@ def test_span_pprint():
assert re.search("end=[0-9.]+", actual) is not None

root = Span(None, "test.span", service="s", resource="r", span_type=SpanTypes.WEB)
actual = root.pprint()
actual = root._pprint()
assert "duration=None" in actual
assert "end=None" in actual

root = Span(None, "test.span", service="s", resource="r", span_type=SpanTypes.WEB)
root.error = 1
actual = root.pprint()
actual = root._pprint()
assert "error=1" in actual

root = Span(None, "test.span", service="s", resource="r", span_type=SpanTypes.WEB)
root.set_tag(u"😌", u"😌")
actual = root.pprint()
actual = root._pprint()
assert (u"tags={'😌': '😌'}" if six.PY3 else "tags={u'\\U0001f60c': u'\\U0001f60c'}") in actual

root = Span(None, "test.span", service=object())
actual = root.pprint()
actual = root._pprint()
assert "service=<object object at" in actual


Expand Down
7 changes: 0 additions & 7 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,13 +793,6 @@ def test_case(self):
spans[i].assert_matches(parent_id=self.span_id, trace_id=self.trace_id, _parent=self)
spans[i].assert_structure(root, _children)

def pprint(self):
parts = [super(TestSpanNode, self).pprint()]
for child in self._children:
parts.append("-" * 20)
parts.append(child.pprint())
return "\r\n".join(parts)


def assert_dict_issuperset(a, b):
assert set(a.items()).issuperset(set(b.items())), "{a} is not a superset of {b}".format(a=a, b=b)
Expand Down

0 comments on commit 5a07399

Please sign in to comment.