Skip to content

Commit b394f07

Browse files
authored
feat(tracing): add Span.get_tags and Span.get_metrics (#3402)
1 parent 32a9462 commit b394f07

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+157
-151
lines changed

ddtrace/span.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ def get_tag(self, key):
373373
"""Return the given tag or None if it doesn't exist."""
374374
return self._meta.get(key, None)
375375

376-
def _get_tags(self):
376+
def get_tags(self):
377377
# type: () -> _MetaDictType
378378
"""Return all tags."""
379379
return self._meta.copy()
@@ -462,10 +462,12 @@ def set_metrics(self, metrics):
462462

463463
def get_metric(self, key):
464464
# type: (_TagNameType) -> Optional[NumericType]
465+
"""Return the given metric or None if it doesn't exist."""
465466
return self._metrics.get(key)
466467

467-
def _get_metrics(self):
468+
def get_metrics(self):
468469
# type: () -> _MetricDictType
470+
"""Return all metrics."""
469471
return self._metrics.copy()
470472

471473
@remove(message="Use getters for ``Span`` attributes instead.", version="1.0.0")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
features:
3+
- |
4+
Add ``Span.get_tags`` and ``Span.get_metrics``.

tests/contrib/aiohttp/test_middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ async def test_param_handler(app_tracer, aiohttp_client, loop, query_string, tra
7676
if app[CONFIG_KEY].get("trace_query_string"):
7777
assert query_string == span.get_tag(http.QUERY_STRING)
7878
else:
79-
assert http.QUERY_STRING not in span._get_tags()
79+
assert http.QUERY_STRING not in span.get_tags()
8080

8181

8282
async def test_404_handler(app_tracer, aiohttp_client):

tests/contrib/aredis/test_aredis.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ async def test_meta_override(tracer, test_spans):
109109
test_spans.assert_trace_count(1)
110110
test_spans.assert_span_count(1)
111111
assert test_spans.spans[0].service == "redis"
112-
assert "cheese" in test_spans.spans[0]._get_tags() and test_spans.spans[0].get_tag("cheese") == "camembert"
112+
assert "cheese" in test_spans.spans[0].get_tags() and test_spans.spans[0].get_tag("cheese") == "camembert"
113113

114114

115115
@pytest.mark.asyncio

tests/contrib/bottle/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def hi(name):
7171
if ddtrace.config.bottle.trace_query_string:
7272
assert s.get_tag(http.QUERY_STRING) == query_string
7373
else:
74-
assert http.QUERY_STRING not in s._get_tags()
74+
assert http.QUERY_STRING not in s.get_tags()
7575

7676
def test_query_string(self):
7777
return self.test_200("foo=bar")

tests/contrib/celery/test_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def test_tags_from_context(self):
3232

3333
span = Span(None, "test")
3434
set_tags_from_context(span, context)
35-
metas = span._get_tags()
36-
metrics = span._get_metrics()
35+
metas = span.get_tags()
36+
metrics = span.get_metrics()
3737
sentinel = object()
3838
assert metas["celery.correlation_id"] == "44b7f305"
3939
assert metas["celery.delivery_info"] == '{"eager": "True"}'
@@ -56,7 +56,7 @@ def test_tags_from_context_empty_keys(self):
5656
"timelimit": (None, None),
5757
"retries": 0,
5858
}
59-
tags = span._get_tags()
59+
tags = span.get_tags()
6060

6161
set_tags_from_context(span, context)
6262
assert {} == tags

tests/contrib/cherrypy/test_middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def test_fatal(self):
195195
assert_span_http_status_code(s, 500)
196196
assert s.error == 1
197197
assert s.get_tag(http.METHOD) == "GET"
198-
assert "ZeroDivisionError" in s.get_tag(ERROR_TYPE), s._get_tags()
198+
assert "ZeroDivisionError" in s.get_tag(ERROR_TYPE), s.get_tags()
199199
assert "by zero" in s.get_tag(ERROR_MSG)
200200
assert re.search('File ".*/contrib/cherrypy/web.py", line [0-9]+, in fatal', s.get_tag(ERROR_STACK))
201201

tests/contrib/django/test_django.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def test_django_v2XX_request_root_span(client, test_spans):
7575
if django.VERSION >= (2, 2, 0):
7676
meta["http.route"] = "^$"
7777

78-
assert http.QUERY_STRING not in root._get_tags()
78+
assert http.QUERY_STRING not in root.get_tags()
7979
root.assert_matches(
8080
name="django.request",
8181
service="django",
@@ -116,7 +116,7 @@ def test_django_v2XX_alter_root_resource(client, test_spans):
116116
if django.VERSION >= (2, 2, 0):
117117
meta["http.route"] = "^alter-resource/$"
118118

119-
assert http.QUERY_STRING not in root._get_tags()
119+
assert http.QUERY_STRING not in root.get_tags()
120120
root.assert_matches(
121121
name="django.request",
122122
service="django",
@@ -729,7 +729,7 @@ def test_cache_get(test_spans):
729729
"django.cache.key": "missing_key",
730730
}
731731

732-
assert_dict_issuperset(span._get_tags(), expected_meta)
732+
assert_dict_issuperset(span.get_tags(), expected_meta)
733733

734734

735735
def test_cache_set(test_spans):
@@ -754,7 +754,7 @@ def test_cache_set(test_spans):
754754
"django.cache.key": "a_new_key",
755755
}
756756

757-
assert_dict_issuperset(span._get_tags(), expected_meta)
757+
assert_dict_issuperset(span.get_tags(), expected_meta)
758758

759759

760760
def test_cache_delete(test_spans):
@@ -778,7 +778,7 @@ def test_cache_delete(test_spans):
778778
"django.cache.key": "an_existing_key",
779779
}
780780

781-
assert_dict_issuperset(span._get_tags(), expected_meta)
781+
assert_dict_issuperset(span.get_tags(), expected_meta)
782782

783783

784784
@pytest.mark.skipif(django.VERSION >= (2, 1, 0), reason="")
@@ -813,8 +813,8 @@ def test_cache_incr_1XX(test_spans):
813813
"django.cache.key": "value",
814814
}
815815

816-
assert_dict_issuperset(span_get._get_tags(), expected_meta)
817-
assert_dict_issuperset(span_incr._get_tags(), expected_meta)
816+
assert_dict_issuperset(span_get.get_tags(), expected_meta)
817+
assert_dict_issuperset(span_incr.get_tags(), expected_meta)
818818

819819

820820
@pytest.mark.skipif(django.VERSION < (2, 1, 0), reason="")
@@ -843,7 +843,7 @@ def test_cache_incr_2XX(test_spans):
843843
"django.cache.key": "value",
844844
}
845845

846-
assert_dict_issuperset(span_incr._get_tags(), expected_meta)
846+
assert_dict_issuperset(span_incr.get_tags(), expected_meta)
847847

848848

849849
@pytest.mark.skipif(django.VERSION >= (2, 1, 0), reason="")
@@ -884,9 +884,9 @@ def test_cache_decr_1XX(test_spans):
884884
"django.cache.key": "value",
885885
}
886886

887-
assert_dict_issuperset(span_get._get_tags(), expected_meta)
888-
assert_dict_issuperset(span_incr._get_tags(), expected_meta)
889-
assert_dict_issuperset(span_decr._get_tags(), expected_meta)
887+
assert_dict_issuperset(span_get.get_tags(), expected_meta)
888+
assert_dict_issuperset(span_incr.get_tags(), expected_meta)
889+
assert_dict_issuperset(span_decr.get_tags(), expected_meta)
890890

891891

892892
@pytest.mark.skipif(django.VERSION < (2, 1, 0), reason="")
@@ -921,8 +921,8 @@ def test_cache_decr_2XX(test_spans):
921921
"django.cache.key": "value",
922922
}
923923

924-
assert_dict_issuperset(span_incr._get_tags(), expected_meta)
925-
assert_dict_issuperset(span_decr._get_tags(), expected_meta)
924+
assert_dict_issuperset(span_incr.get_tags(), expected_meta)
925+
assert_dict_issuperset(span_decr.get_tags(), expected_meta)
926926

927927

928928
def test_cache_get_many(test_spans):
@@ -960,7 +960,7 @@ def test_cache_get_many(test_spans):
960960
"django.cache.key": str(["missing_key", "another_key"]),
961961
}
962962

963-
assert_dict_issuperset(span_get_many._get_tags(), expected_meta)
963+
assert_dict_issuperset(span_get_many.get_tags(), expected_meta)
964964

965965

966966
def test_cache_set_many(test_spans):
@@ -1081,8 +1081,8 @@ def test_cached_view(client, test_spans):
10811081
"django.cache.key": "views.decorators.cache.cache_header..03cdc1cc4aab71b038a6764e5fcabb82.en-us",
10821082
}
10831083

1084-
assert span_view._get_tags() == expected_meta_view
1085-
assert span_header._get_tags() == expected_meta_header
1084+
assert span_view.get_tags() == expected_meta_view
1085+
assert span_header.get_tags() == expected_meta_header
10861086

10871087

10881088
@pytest.mark.django_db
@@ -1118,7 +1118,7 @@ def test_cached_template(client, test_spans):
11181118
"django.cache.key": "template.cache.users_list.d41d8cd98f00b204e9800998ecf8427e",
11191119
}
11201120

1121-
assert span_template_cache._get_tags() == expected_meta
1121+
assert span_template_cache.get_tags() == expected_meta
11221122

11231123

11241124
"""
@@ -1494,7 +1494,7 @@ def test_user_name_excluded(client, test_spans):
14941494

14951495
# user name should not be present in root span tags
14961496
root = test_spans.get_root_span()
1497-
assert "django.user.name" not in root._get_tags()
1497+
assert "django.user.name" not in root.get_tags()
14981498
assert root.get_tag("django.user.is_authenticated") == "True"
14991499

15001500

@@ -1772,7 +1772,7 @@ def start_response(status, headers):
17721772
if django.VERSION >= (2, 2, 0):
17731773
meta["http.route"] = "^$"
17741774

1775-
assert http.QUERY_STRING not in root._get_tags()
1775+
assert http.QUERY_STRING not in root.get_tags()
17761776
root.assert_matches(
17771777
name="django.request",
17781778
service="django",

tests/contrib/falcon/test_suite.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_404(self):
4040
assert span.resource == "GET 404"
4141
assert_span_http_status_code(span, 404)
4242
assert span.get_tag(httpx.URL) == "http://falconframework.org/fake_endpoint"
43-
assert httpx.QUERY_STRING not in span._get_tags()
43+
assert httpx.QUERY_STRING not in span.get_tags()
4444
assert span.parent_id is None
4545
assert span.error == 0
4646

@@ -86,7 +86,7 @@ def test_200(self, query_string="", trace_query_string=False):
8686
if config.falcon.trace_query_string:
8787
assert span.get_tag(httpx.QUERY_STRING) == query_string
8888
else:
89-
assert httpx.QUERY_STRING not in span._get_tags()
89+
assert httpx.QUERY_STRING not in span.get_tags()
9090
assert span.parent_id is None
9191
assert span.span_type == "web"
9292
assert span.error == 0

tests/contrib/flask/test_blueprint.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def test():
103103
self.assertEqual(span.service, "flask")
104104
self.assertEqual(span.name, "bp.test")
105105
self.assertEqual(span.resource, "/")
106-
self.assertEqual(span._get_tags(), dict())
106+
self.assertEqual(span.get_tags(), dict())
107107

108108
def test_blueprint_request_pin_override(self):
109109
"""
@@ -130,7 +130,7 @@ def test():
130130
self.assertEqual(span.service, "flask-bp")
131131
self.assertEqual(span.name, "bp.test")
132132
self.assertEqual(span.resource, "/")
133-
self.assertEqual(span._get_tags(), dict())
133+
self.assertEqual(span.get_tags(), dict())
134134

135135
def test_blueprint_request_pin_disabled(self):
136136
"""

0 commit comments

Comments
 (0)