Commit 6ae9763
feat(dsm): add kafka_cluster_id to confluent-kafka-go (#4470)
## Summary
Most DSM tracer instrumentations for Kafka enrich a kafka_cluster_id. This is useful because numerous generated metrics such as `data_streams.latency` and `data_streams.kafka.lag_messages` will automatically add this metric if present.
In situations where people have the same topic names across multiple kafka clusters, the above metric will become incorrect without the cluster id tag. This PR adds feature parity for Go and collects this metric tag when DSM is enabled.
This is implemented by:
* Launching a goroutine on consumer/producer creation to initiate the admin API and query for cluster id
* Enriching metrics with this cluster id after we obtain it
* Signalling a stop for an inflight cluster id request if the consumer/producer closes early
* Enabling this only for DSM
For context, the other tracers implement this as follows:
1. Java (doesn't block, intercepts the metadata response to enrich cluster id going forwards, see [here](https://github.com/DataDog/dd-trace-java/blob/master/dd-java-agent/instrumentation/kafka/kafka-clients-3.8/src/main/java17/datadog/trace/instrumentation/kafka_clients38/ProducerAdvice.java#L44) and [here](https://github.com/DataDog/dd-trace-java/blob/master/dd-java-agent/instrumentation/kafka/kafka-clients-0.11/src/main/java/datadog/trace/instrumentation/kafka_clients/MetadataInstrumentation.java#L82))
2. Node (blocks, see [here](https://github.com/DataDog/dd-trace-js/blob/master/packages/datadog-instrumentations/src/kafkajs.js#L234) and [here](https://github.com/DataDog/dd-trace-py/blob/main/ddtrace/contrib/internal/kafka/patch.py#L183))
3. Python (blocks with a 1 second timeout, see [here](https://github.com/DataDog/dd-trace-py/blob/main/ddtrace/contrib/internal/kafka/patch.py#L360) and [here](https://github.com/DataDog/dd-trace-py/blob/main/ddtrace/contrib/internal/kafka/patch.py#L183))
## Test plan
- Add to existing tests
- Test manually
Screenshots:
Spans have `kafka_cluster_id`:
<img width="876" height="447" alt="Screenshot 2026-02-25 at 4 25 51 pm" src="https://github.com/user-attachments/assets/57cacf9d-ba8f-480b-97c6-60fffa9f66a2" />
<img width="822" height="483" alt="Screenshot 2026-02-25 at 4 26 04 pm" src="https://github.com/user-attachments/assets/3308be4b-ae79-4de4-8a30-6b7d3b0e6e26" />
Metric now adds `kafka_cluster_id`:
<img width="2385" height="980" alt="Screenshot 2026-02-25 at 4 49 09 pm" src="https://github.com/user-attachments/assets/3ca95243-a3e2-4e2e-9a4b-8c97f25d3b3a" />
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-authored-by: genesor <[email protected]>
Co-authored-by: hannahs.kim <[email protected]>1 parent 6f4b819 commit 6ae9763
15 files changed
Lines changed: 360 additions & 30 deletions
File tree
- contrib/confluentinc/confluent-kafka-go
- kafka.v2
- kafkatrace
- kafka
- ddtrace
- ext
- tracer
- internal/datastreams
Lines changed: 62 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
55 | 83 | | |
56 | 84 | | |
57 | 85 | | |
58 | | - | |
59 | | - | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
60 | 89 | | |
61 | 90 | | |
62 | 91 | | |
| |||
67 | 96 | | |
68 | 97 | | |
69 | 98 | | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
70 | 110 | | |
71 | 111 | | |
72 | 112 | | |
73 | 113 | | |
74 | 114 | | |
75 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
76 | 120 | | |
77 | 121 | | |
78 | 122 | | |
| |||
159 | 203 | | |
160 | 204 | | |
161 | 205 | | |
| 206 | + | |
162 | 207 | | |
163 | 208 | | |
164 | 209 | | |
| |||
170 | 215 | | |
171 | 216 | | |
172 | 217 | | |
173 | | - | |
174 | | - | |
| 218 | + | |
| 219 | + | |
175 | 220 | | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
176 | 230 | | |
177 | 231 | | |
178 | 232 | | |
| |||
185 | 239 | | |
186 | 240 | | |
187 | 241 | | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
188 | 246 | | |
189 | 247 | | |
190 | 248 | | |
| |||
Lines changed: 46 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
170 | 170 | | |
171 | 171 | | |
172 | 172 | | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
173 | 176 | | |
174 | | - | |
175 | | - | |
| 177 | + | |
| 178 | + | |
176 | 179 | | |
177 | 180 | | |
178 | 181 | | |
| |||
181 | 184 | | |
182 | 185 | | |
183 | 186 | | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
184 | 220 | | |
185 | 221 | | |
186 | 222 | | |
| |||
361 | 397 | | |
362 | 398 | | |
363 | 399 | | |
| 400 | + | |
364 | 401 | | |
365 | 402 | | |
366 | 403 | | |
| |||
399 | 436 | | |
400 | 437 | | |
401 | 438 | | |
| 439 | + | |
402 | 440 | | |
403 | 441 | | |
404 | 442 | | |
| |||
428 | 466 | | |
429 | 467 | | |
430 | 468 | | |
431 | | - | |
432 | | - | |
433 | | - | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
434 | 475 | | |
435 | 476 | | |
436 | 477 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
52 | 53 | | |
53 | 54 | | |
54 | 55 | | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
55 | 83 | | |
56 | 84 | | |
57 | 85 | | |
58 | | - | |
59 | | - | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
60 | 89 | | |
61 | 90 | | |
62 | 91 | | |
| |||
67 | 96 | | |
68 | 97 | | |
69 | 98 | | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
70 | 110 | | |
71 | 111 | | |
72 | 112 | | |
73 | 113 | | |
74 | 114 | | |
75 | 115 | | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
76 | 120 | | |
77 | 121 | | |
78 | 122 | | |
| |||
159 | 203 | | |
160 | 204 | | |
161 | 205 | | |
| 206 | + | |
162 | 207 | | |
163 | 208 | | |
164 | 209 | | |
| |||
170 | 215 | | |
171 | 216 | | |
172 | 217 | | |
173 | | - | |
174 | | - | |
| 218 | + | |
| 219 | + | |
175 | 220 | | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
176 | 230 | | |
177 | 231 | | |
178 | 232 | | |
| |||
185 | 239 | | |
186 | 240 | | |
187 | 241 | | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
188 | 246 | | |
189 | 247 | | |
190 | 248 | | |
| |||
0 commit comments