Skip to content

Commit 6d81403

Browse files
Fix runtime-id and system.pid tags not being set on distributed traces (#2562)
* Add regression test * Fix runtime id tag not being set on distributed traces As per title. * Use same logic for pid * Update releasenotes/notes/runtimeid-39dfdf1e0c1af45d.yaml Co-authored-by: Brett Langdon <[email protected]> * Fix spelling, grpc snapshot Co-authored-by: Brett Langdon <[email protected]> Co-authored-by: Brett Langdon <[email protected]>
1 parent 852214d commit 6d81403

7 files changed

+61
-34
lines changed

ddtrace/tracer.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,6 @@ def _start_span(
550550
on_finish=[self._on_span_finish],
551551
)
552552
span._local_root = span
553-
span.metrics[system.PID] = self._pid or getpid()
554-
span.meta["runtime-id"] = get_runtime_id()
555553
if config.report_hostname:
556554
span.meta[HOSTNAME_KEY] = hostname.get_hostname()
557555
span.sampled = self.sampler.sample(span)
@@ -581,6 +579,10 @@ def _start_span(
581579
# We must always mark the span as sampled so it is forwarded to the agent
582580
span.sampled = True
583581

582+
if not span._parent:
583+
span.meta["runtime-id"] = get_runtime_id()
584+
span.metrics[system.PID] = self._pid
585+
584586
# Apply default global tags.
585587
if self.tags:
586588
span.set_tags(self.tags)

docs/spelling_wordlist.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ mysqldb
8585
namespace
8686
opentracer
8787
opentracing
88+
pid
8889
plugin
8990
posix
9091
postgres
@@ -107,6 +108,7 @@ repo
107108
respawn
108109
runnable
109110
runtime
111+
runtime-id
110112
sanic
111113
sql
112114
sqlalchemy
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
fixes:
3+
- |
4+
Fix runtime-id and system.pid tags not being set on distributed traces.

tests/snapshots/tests.contrib.grpc.test_grpc.test_method_service.snap

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
"span_id" 0
77
"trace_id" 0
88
"parent_id" nil
9-
"start" 1619744633550844100
10-
"duration" 1911900
11-
"meta" {"runtime-id" "04d67a3bd550461295cfd98eedc69d99"
12-
"grpc.port" "55945"
9+
"start" 1624047273666117000
10+
"duration" 2052000
11+
"meta" {"runtime-id" "d8bca53d2d0942c69b9144e37b3626c6"
12+
"grpc.port" "59087"
1313
"grpc.host" "::"
1414
"grpc.method.kind" "unary"
1515
"span.kind" "client"
@@ -20,7 +20,7 @@
2020
"metrics" {"_dd.agent_psr" 1.0
2121
"_dd.measured" 1
2222
"_sampling_priority_v1" 1
23-
"system.pid" 714
23+
"system.pid" 18581
2424
"_dd.tracer_kr" 1.0}}
2525
{"name" "grpc"
2626
"service" "grpc-server"
@@ -30,14 +30,16 @@
3030
"span_id" 1
3131
"trace_id" 0
3232
"parent_id" 0
33-
"start" 1619744633552296900
34-
"duration" 48200
35-
"meta" {"grpc.method.path" "/Servicer/Handler"
33+
"start" 1624047273667894000
34+
"duration" 43000
35+
"meta" {"runtime-id" "d8bca53d2d0942c69b9144e37b3626c6"
36+
"grpc.method.path" "/Servicer/Handler"
3637
"grpc.method.service" "Servicer"
3738
"grpc.method.name" "Handler"
3839
"grpc.method.kind" "unary"
3940
"span.kind" "server"}
40-
"metrics" {"_dd.measured" 1}}]
41+
"metrics" {"_dd.measured" 1
42+
"system.pid" 18581}}]
4143
[{"name" "grpc"
4244
"service" "grpc-client"
4345
"resource" "/pkg.Servicer/Handler"
@@ -46,10 +48,10 @@
4648
"span_id" 0
4749
"trace_id" 1
4850
"parent_id" nil
49-
"start" 1619744633553123900
50-
"duration" 1079700
51-
"meta" {"runtime-id" "04d67a3bd550461295cfd98eedc69d99"
52-
"grpc.port" "55945"
51+
"start" 1624047273668507000
52+
"duration" 601000
53+
"meta" {"runtime-id" "d8bca53d2d0942c69b9144e37b3626c6"
54+
"grpc.port" "59087"
5355
"grpc.method.package" "pkg"
5456
"grpc.host" "::"
5557
"grpc.method.kind" "unary"
@@ -61,7 +63,7 @@
6163
"metrics" {"_dd.agent_psr" 1.0
6264
"_dd.measured" 1
6365
"_sampling_priority_v1" 1
64-
"system.pid" 714
66+
"system.pid" 18581
6567
"_dd.tracer_kr" 1.0}}
6668
{"name" "grpc"
6769
"service" "grpc-server"
@@ -71,12 +73,14 @@
7173
"span_id" 1
7274
"trace_id" 1
7375
"parent_id" 0
74-
"start" 1619744633553812700
75-
"duration" 42900
76-
"meta" {"grpc.method.path" "/pkg.Servicer/Handler"
76+
"start" 1624047273668929000
77+
"duration" 29000
78+
"meta" {"runtime-id" "d8bca53d2d0942c69b9144e37b3626c6"
79+
"grpc.method.path" "/pkg.Servicer/Handler"
7780
"grpc.method.package" "pkg"
7881
"grpc.method.service" "Servicer"
7982
"grpc.method.name" "Handler"
8083
"grpc.method.kind" "unary"
8184
"span.kind" "server"}
82-
"metrics" {"_dd.measured" 1}}]]
85+
"metrics" {"_dd.measured" 1
86+
"system.pid" 18581}}]]

tests/snapshots/tests.integration.test_integration_snapshots.test_tracer_trace_across_fork.snap

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
"span_id" 0
66
"trace_id" 0
77
"parent_id" nil
8-
"start" 1618357801277945181
9-
"duration" 107575512
10-
"meta" {"runtime-id" "cb4a438bb8364fa5ac0fec9f7df34c89"}
8+
"start" 1624045258581163000
9+
"duration" 22147000
10+
"meta" {"runtime-id" "afc32eb3be454c03b8a140291d5fd696"}
1111
"metrics" {"_dd.agent_psr" 1.0
1212
"_sampling_priority_v1" 1
13-
"system.pid" 8786
13+
"system.pid" 12507
1414
"_dd.tracer_kr" 1.0}}
1515
{"name" "child"
1616
"service" nil
@@ -19,7 +19,9 @@
1919
"span_id" 1
2020
"trace_id" 0
2121
"parent_id" 0
22-
"start" 1618357801293690837
23-
"duration" 76002
22+
"start" 1624045258591089000
23+
"duration" 53000
24+
"meta" {"runtime-id" "dc30b84ca01142239a7f2a51d9c7725e"}
2425
"metrics" {"_sampling_priority_v1" 1
26+
"system.pid" 12517
2527
"_dd.tracer_kr" 1.0}}]]

tests/snapshots/tests.integration.test_integration_snapshots.test_tracer_trace_across_multiple_forks.snap

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
"span_id" 0
66
"trace_id" 0
77
"parent_id" nil
8-
"start" 1618357412996234998
9-
"duration" 425047115
10-
"meta" {"runtime-id" "31d11c17a6c547ccb9169d1347b1379e"}
8+
"start" 1624045173599735000
9+
"duration" 51650000
10+
"meta" {"runtime-id" "120a8105b0c542d889b939d7c91d71fa"}
1111
"metrics" {"_dd.agent_psr" 1.0
1212
"_sampling_priority_v1" 1
13-
"system.pid" 6435
13+
"system.pid" 12266
1414
"_dd.tracer_kr" 1.0}}
1515
{"name" "child1"
1616
"service" nil
@@ -19,9 +19,11 @@
1919
"span_id" 1
2020
"trace_id" 0
2121
"parent_id" 0
22-
"start" 1618357413012622804
23-
"duration" 315314377
22+
"start" 1624045173606288000
23+
"duration" 32138000
24+
"meta" {"runtime-id" "8fba36822c6846d895a13f5d043543be"}
2425
"metrics" {"_sampling_priority_v1" 1
26+
"system.pid" 12294
2527
"_dd.tracer_kr" 1.0}}
2628
{"name" "child2"
2729
"service" nil
@@ -30,7 +32,9 @@
3032
"span_id" 2
3133
"trace_id" 0
3234
"parent_id" 1
33-
"start" 1618357413236058471
34-
"duration" 75980
35+
"start" 1624045173612023000
36+
"duration" 86000
37+
"meta" {"runtime-id" "859c494affcb4933a1e15b6c4d2561b8"}
3538
"metrics" {"_sampling_priority_v1" 1
39+
"system.pid" 12295
3640
"_dd.tracer_kr" 1.0}}]]

tests/tracer/test_tracer.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,15 @@ def task(tracer, q):
910910
assert children_tag != span.get_tag("runtime-id")
911911

912912

913+
def test_tracer_runtime_tags_cross_execution(tracer):
914+
ctx = Context(trace_id=12, span_id=21)
915+
tracer.context_provider.activate(ctx)
916+
with tracer.trace("span") as span:
917+
pass
918+
assert span.get_tag("runtime-id") is not None
919+
assert span.get_metric(system.PID) is not None
920+
921+
913922
def test_start_span_hooks():
914923
t = ddtrace.Tracer()
915924

0 commit comments

Comments
 (0)