Skip to content

Commit fadb7d3

Browse files
fix(dsm): move DSM logic from bindStart to start for google-cloud-pubsub
Move DSM checkpoint logic from bindStart to start in both consumer and producer plugins so DSM context is properly bound in the async store. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent ce11a98 commit fadb7d3

2 files changed

Lines changed: 32 additions & 17 deletions

File tree

packages/datadog-plugin-google-cloud-pubsub/src/consumer.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ class GoogleCloudPubsubConsumerPlugin extends ConsumerPlugin {
9696
})
9797
}
9898

99+
start (ctx) {
100+
if (!this.config.dsmEnabled) return
101+
const { message } = ctx
102+
if (!message?.attributes) return
103+
104+
const { span } = ctx.currentStore
105+
const subscription = message._subscriber._subscription
106+
const topic = subscription?.metadata?.topic || message.attributes?.['pubsub.topic']
107+
const payloadSize = getMessageSize(message)
108+
this.tracer.decodeDataStreamsContext(message.attributes)
109+
this.tracer
110+
.setCheckpoint(['direction:in', `topic:${topic}`, 'type:google-pubsub'], span, payloadSize)
111+
}
112+
99113
bindStart (ctx) {
100114
const { message } = ctx
101115
const subscription = message._subscriber._subscription
@@ -181,13 +195,6 @@ class GoogleCloudPubsubConsumerPlugin extends ConsumerPlugin {
181195
}
182196
}
183197

184-
if (this.config.dsmEnabled && message?.attributes) {
185-
const payloadSize = getMessageSize(message)
186-
this.tracer.decodeDataStreamsContext(message.attributes)
187-
this.tracer
188-
.setCheckpoint(['direction:in', `topic:${topic}`, 'type:google-pubsub'], span, payloadSize)
189-
}
190-
191198
return ctx.currentStore
192199
}
193200

packages/datadog-plugin-google-cloud-pubsub/src/producer.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ class GoogleCloudPubsubProducerPlugin extends ProducerPlugin {
3636
if (topicName) attributes['pubsub.topic'] = topicName
3737
}
3838

39+
start (ctx) {
40+
if (!this.config.dsmEnabled) return
41+
const { request } = ctx
42+
const messages = request.messages || []
43+
const topic = request.topic
44+
const { span } = ctx.currentStore
45+
46+
for (const msg of messages) {
47+
const attributes = msg.attributes ??= {}
48+
const dataStreamsContext = this.tracer.setCheckpoint(
49+
['direction:out', `topic:${topic}`, 'type:google-pubsub'],
50+
span,
51+
getHeadersSize(msg)
52+
)
53+
DsmPathwayCodec.encode(dataStreamsContext, attributes)
54+
}
55+
}
56+
3957
bindStart (ctx) {
4058
const { request, api, projectId } = ctx
4159
if (api !== 'publish') return
@@ -113,7 +131,6 @@ class GoogleCloudPubsubProducerPlugin extends ProducerPlugin {
113131

114132
const messageCountStr = String(messageCount)
115133
const startTimeStr = String(Math.floor(batchSpan._startTime))
116-
const dsmEnabled = this.config.dsmEnabled
117134

118135
for (let i = 0; i < messageCount; i++) {
119136
const msg = messages[i]
@@ -137,15 +154,6 @@ class GoogleCloudPubsubProducerPlugin extends ProducerPlugin {
137154
if (batchTraceIdUpper) {
138155
attributes['_dd.pubsub_request.p.tid'] = batchTraceIdUpper
139156
}
140-
141-
if (dsmEnabled) {
142-
const dataStreamsContext = this.tracer.setCheckpoint(
143-
['direction:out', `topic:${topic}`, 'type:google-pubsub'],
144-
batchSpan,
145-
getHeadersSize(msg)
146-
)
147-
DsmPathwayCodec.encode(dataStreamsContext, attributes)
148-
}
149157
}
150158

151159
ctx.batchSpan = batchSpan

0 commit comments

Comments
 (0)