Skip to content

Commit 4b8ffac

Browse files
github-actions[bot]zarirhamzamabdinur
authored
chore(tracing): adds out.host tag for celery [backport 1.14] (#6056)
Backport e876003 from #6006 to 1.14. Adds `out.host` tag for celery as a precursor tag for `peer.service` Testing is updated to check for new tag by calling hostname for local machine ## Checklist - [x] Change(s) are motivated and described in the PR description. - [x] Testing strategy is described if automated tests are not included in the PR. - [x] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment. Co-authored-by: Zarir Hamza <[email protected]> Co-authored-by: Munir Abdinur <[email protected]>
1 parent f6b740e commit 4b8ffac

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

ddtrace/contrib/celery/signals.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from celery import registry
2+
from celery.utils import nodenames
23

34
from ddtrace import Pin
45
from ddtrace import config
@@ -11,6 +12,7 @@
1112
from ...constants import SPAN_MEASURED_KEY
1213
from ...ext import SpanKind
1314
from ...ext import SpanTypes
15+
from ...ext import net
1416
from ...internal.logger import get_logger
1517
from ...propagation.http import HTTPPropagator
1618
from .utils import attach_span
@@ -124,6 +126,9 @@ def trace_before_publish(*args, **kwargs):
124126
span.set_tag_str(c.TASK_TAG_KEY, c.TASK_APPLY_ASYNC)
125127
span.set_tag_str("celery.id", task_id)
126128
set_tags_from_context(span, kwargs)
129+
if kwargs.get("headers") is not None:
130+
# required to extract hostname from origin header on `celery>=4.0`
131+
set_tags_from_context(span, kwargs["headers"])
127132

128133
# Note: adding tags from `traceback` or `state` calls will make an
129134
# API call to the backend for the properties so we should rely
@@ -156,6 +161,11 @@ def trace_after_publish(*args, **kwargs):
156161
if span is None:
157162
return
158163
else:
164+
nodename = span.get_tag("celery.hostname")
165+
if nodename is not None:
166+
_, host = nodenames.nodesplit(nodename)
167+
span.set_tag_str(net.TARGET_HOST, host)
168+
159169
span.finish()
160170
detach_span(task, task_id, is_publish=True)
161171

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
celery: Resolves an issue where hostname tags were not set in spans generated by ``celery>4.0``.

tests/contrib/celery/test_integration.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from collections import Counter
22
import os
3+
import socket
34
import subprocess
45
from time import sleep
56

@@ -191,6 +192,7 @@ def fn_task_parameters(user, force_logout=False):
191192
assert async_span.get_tag("celery.routing_key") == "celery"
192193
assert async_span.get_tag("component") == "celery"
193194
assert async_span.get_tag("span.kind") == "producer"
195+
assert async_span.get_tag("out.host") == socket.gethostname()
194196
else:
195197
assert 1 == len(traces)
196198
assert 1 == len(traces[0])
@@ -234,6 +236,7 @@ def fn_task_parameters(user, force_logout=False):
234236
assert async_span.get_tag("celery.routing_key") == "celery"
235237
assert async_span.get_tag("component") == "celery"
236238
assert async_span.get_tag("span.kind") == "producer"
239+
assert async_span.get_tag("out.host") == socket.gethostname()
237240
else:
238241
assert 1 == len(traces)
239242
assert 1 == len(traces[0])
@@ -682,6 +685,7 @@ def fn_task_parameters(user, force_logout=False):
682685
assert async_span.get_tag("celery.routing_key") == "celery"
683686
assert async_span.get_tag("component") == "celery"
684687
assert async_span.get_tag("span.kind") == "producer"
688+
assert async_span.get_tag("out.host") == socket.gethostname()
685689

686690
run_span = self.find_span(name="celery.run")
687691
assert run_span.name == "celery.run"

0 commit comments

Comments
 (0)