Skip to content

Commit 4181619

Browse files
authored
fix(tracing): fix usage of mapped_service when creating a new span (backport #4782) (#4789)
Backport of #4782 ## Description After getting the remapped service name we were not using it to determine if the `DD_VERSION` tag should be set, or when keeping track of all the service names seen by the tracer ## Relevant issue(s) <!-- Link the pull request to any issues related to the fix. Use keywords for links to automate closing the issues once the pull request is merged. --> ## Testing strategy Added two tests to validate that when setting `DD_SERVICE_MAPPING` we get the expected results for `version` tag and the collected list of services seen by the tracer. ## Reviewer Checklist - [ ] Title is accurate. - [ ] Description motivates each change. - [ ] No unnecessary changes were introduced in this PR. - [ ] Avoid breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [ ] Tests provided or description of manual testing performed is included in the code or PR. - [ ] Release note has been added and follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/contributing.html#Release-Note-Guidelines), or else `changelog/no-changelog` label added. - [ ] All relevant GitHub issues are correctly linked. - [ ] Backports are identified and tagged with Mergifyio.
1 parent 9358ef8 commit 4181619

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

ddtrace/tracer.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,8 @@ def _start_span(
632632
else:
633633
service = config.service
634634

635-
mapped_service = config.service_mapping.get(service, service)
635+
# Update the service name based on any mapping
636+
service = config.service_mapping.get(service, service)
636637

637638
if trace_id:
638639
# child_of a non-empty context, so either a local child span or from a remote context
@@ -641,7 +642,7 @@ def _start_span(
641642
context=context,
642643
trace_id=trace_id,
643644
parent_id=parent_id,
644-
service=mapped_service,
645+
service=service,
645646
resource=resource,
646647
span_type=span_type,
647648
on_finish=[self._on_span_finish],
@@ -660,7 +661,7 @@ def _start_span(
660661
span = Span(
661662
name=name,
662663
context=context,
663-
service=mapped_service,
664+
service=service,
664665
resource=resource,
665666
span_type=span_type,
666667
on_finish=[self._on_span_finish],
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
tracing: This fix resolves an issue where the ``DD_SERVICE_MAPPING`` mapped service names were not used
5+
when updating span metadata with the ``DD_VERSION`` set version string.

tests/tracer/test_tracer.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,17 @@ def test_adding_services(self):
473473
pass
474474
assert self.tracer._services == set(["one", "two"])
475475

476+
@run_in_subprocess(env_overrides=dict(DD_SERVICE_MAPPING="two:three"))
477+
def test_adding_mapped_services(self):
478+
assert self.tracer._services == set()
479+
with self.start_span("root", service="one") as root:
480+
assert self.tracer._services == set(["one"])
481+
482+
# service "two" gets remapped to "three"
483+
with self.start_span("child", service="two", child_of=root):
484+
pass
485+
assert self.tracer._services == set(["one", "three"])
486+
476487
def test_configure_dogstatsd_url_host_port(self):
477488
tracer = Tracer()
478489
tracer.configure(dogstatsd_url="foo:1234")
@@ -901,6 +912,32 @@ def test_version_service(self):
901912
assert child2.service == "mysql"
902913
assert VERSION_KEY not in child2.get_tags()
903914

915+
@run_in_subprocess(env_overrides=dict(DD_SERVICE="django", DD_VERSION="0.1.2", DD_SERVICE_MAPPING="mysql:django"))
916+
def test_version_service_mapping(self):
917+
"""When using DD_SERVICE_MAPPING we properly add version tag to appropriate spans"""
918+
919+
# Our app is called django, we provide DD_SERVICE=django and DD_VERSION=0.1.2
920+
# mysql spans will get remapped to django via DD_SERVICE_MAPPING=mysql:django
921+
922+
with self.trace("django.request") as root:
923+
# Root span should be tagged
924+
assert root.service == "django"
925+
assert VERSION_KEY in root.get_tags() and root.get_tag(VERSION_KEY) == "0.1.2"
926+
927+
# Child spans should be tagged
928+
with self.trace("") as child1:
929+
assert child1.service == "django"
930+
assert VERSION_KEY in child1.get_tags() and child1.get_tag(VERSION_KEY) == "0.1.2"
931+
932+
# Service name gets remapped to django and we get version tag applied
933+
with self.trace("mysql.query", service="mysql") as span:
934+
assert span.service == "django"
935+
assert VERSION_KEY in span.get_tags() and span.get_tag(VERSION_KEY) == "0.1.2"
936+
# Child should also have a version
937+
with self.trace("") as child2:
938+
assert child2.service == "django"
939+
assert VERSION_KEY in child2.get_tags() and child2.get_tag(VERSION_KEY) == "0.1.2"
940+
904941
@run_in_subprocess(env_overrides=dict(AWS_LAMBDA_FUNCTION_NAME="my-func"))
905942
def test_detect_agentless_env_with_lambda(self):
906943
assert _in_aws_lambda()

0 commit comments

Comments
 (0)