Skip to content

Commit b8d513a

Browse files
fix: set messaging.operation in createAttributes (#2030)
* apply messaging operation to createAttributes * fix: Set messaging.operation in createAttributes * chore: linting updates for src/telemetry-tracing.ts after the recent 5.0 merge * chore: linting updates for test/telemetry-tracing.ts after the recent 5.0 merge --------- Co-authored-by: Megan Potter <[email protected]>
1 parent 66cc717 commit b8d513a

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/telemetry-tracing.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@ export class PubsubSpans {
305305
params: AttributeParams,
306306
message?: PubsubMessage,
307307
caller?: string,
308+
operation?: string,
308309
): SpanAttributes {
309310
const destinationName = params.topicName ?? params.subName;
310311
const destinationId = params.topicId ?? params.subId;
@@ -351,6 +352,9 @@ export class PubsubSpans {
351352
if (message.ackId) {
352353
spanAttributes['messaging.gcp_pubsub.message.ack_id'] = message.ackId;
353354
}
355+
if (operation) {
356+
spanAttributes['messaging.operation'] = operation;
357+
}
354358
}
355359

356360
return spanAttributes;
@@ -368,11 +372,15 @@ export class PubsubSpans {
368372
const topicInfo = getTopicInfo(topicName);
369373
const span: Span = getTracer().startSpan(`${topicName} create`, {
370374
kind: SpanKind.PRODUCER,
371-
attributes: PubsubSpans.createAttributes(topicInfo, message, caller),
375+
attributes: PubsubSpans.createAttributes(
376+
topicInfo,
377+
message,
378+
caller,
379+
'create',
380+
),
372381
});
373382
if (topicInfo.topicId) {
374383
span.updateName(`${topicInfo.topicId} create`);
375-
span.setAttribute('messaging.operation', 'create');
376384
span.setAttribute('messaging.destination.name', topicInfo.topicId);
377385
}
378386

@@ -404,10 +412,14 @@ export class PubsubSpans {
404412

405413
const subInfo = getSubscriptionInfo(subName);
406414
const name = `${subInfo.subId ?? subName} subscribe`;
407-
const attributes = this.createAttributes(subInfo, message, caller);
415+
const attributes = this.createAttributes(
416+
subInfo,
417+
message,
418+
caller,
419+
'receive',
420+
);
408421
if (subInfo.subId) {
409422
attributes['messaging.destination.name'] = subInfo.subId;
410-
attributes['messaging.operation'] = 'receive';
411423
}
412424

413425
if (context) {
@@ -473,6 +485,7 @@ export class PubsubSpans {
473485
getTopicInfo(topicName),
474486
undefined,
475487
caller,
488+
'create',
476489
);
477490
const links: Link[] = messages
478491
.filter(m => m.parentSpan && isSampled(m.parentSpan))
@@ -515,6 +528,7 @@ export class PubsubSpans {
515528
subInfo,
516529
undefined,
517530
caller,
531+
'receive',
518532
);
519533
const links: Link[] = messageSpans
520534
.filter(m => m && isSampled(m))
@@ -531,7 +545,6 @@ export class PubsubSpans {
531545
);
532546

533547
span?.setAttribute('messaging.batch.message_count', messageSpans.length);
534-
span?.setAttribute('messaging.operation', 'receive');
535548

536549
if (span) {
537550
// Also attempt to link from the subscribe span(s) back to the publish RPC span.
@@ -563,6 +576,7 @@ export class PubsubSpans {
563576
subInfo,
564577
undefined,
565578
caller,
579+
'receive',
566580
);
567581
const links: Link[] = messageSpans
568582
.filter(m => m && isSampled(m))
@@ -579,7 +593,6 @@ export class PubsubSpans {
579593
);
580594

581595
span?.setAttribute('messaging.batch.message_count', messageSpans.length);
582-
span?.setAttribute('messaging.operation', 'receive');
583596

584597
if (span) {
585598
// Also attempt to link from the subscribe span(s) back to the publish RPC span.

test/telemetry-tracing.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ describe('OpenTelemetryTracer', () => {
212212
topicInfo,
213213
message,
214214
'tests',
215+
'create',
215216
);
216217
assert.deepStrictEqual(topicAttrs, {
217218
'messaging.system': 'gcp_pubsub',
@@ -222,6 +223,7 @@ describe('OpenTelemetryTracer', () => {
222223
'messaging.gcp_pubsub.message.exactly_once_delivery':
223224
message.isExactlyOnceDelivery,
224225
'messaging.gcp_pubsub.message.ack_id': message.ackId,
226+
'messaging.operation': 'create',
225227
'code.function': 'tests',
226228
});
227229

@@ -235,10 +237,12 @@ describe('OpenTelemetryTracer', () => {
235237
topicInfo,
236238
message,
237239
'tests',
240+
'create',
238241
);
239242
assert.deepStrictEqual(topicAttrs2, {
240243
'messaging.system': 'gcp_pubsub',
241244
'messaging.destination.name': topicInfo.topicId,
245+
'messaging.operation': 'create',
242246
'gcp.project_id': topicInfo.projectId,
243247
'messaging.message.envelope.size': message.data?.length,
244248
'code.function': 'tests',
@@ -283,6 +287,7 @@ describe('OpenTelemetryTracer', () => {
283287
const firstSpan = spans.pop();
284288
assert.ok(firstSpan);
285289
assert.strictEqual(firstSpan.name, `${tests.topicInfo.topicId} create`);
290+
assert.strictEqual(firstSpan.attributes['messaging.operation'], 'create');
286291
assert.strictEqual(
287292
firstSpan.attributes['messaging.destination.name'],
288293
tests.topicInfo.topicId,
@@ -312,7 +317,6 @@ describe('OpenTelemetryTracer', () => {
312317
const firstSpan = spans.pop();
313318
assert.ok(firstSpan);
314319
assert.strictEqual(firstSpan.name, 'other create');
315-
316320
assert.strictEqual(
317321
firstSpan.attributes['messaging.destination.name'],
318322
'other',
@@ -346,6 +350,10 @@ describe('OpenTelemetryTracer', () => {
346350
childReadSpan.attributes['messaging.destination.name'],
347351
'sub',
348352
);
353+
assert.strictEqual(
354+
childReadSpan.attributes['messaging.operation'],
355+
'receive',
356+
);
349357
assert.strictEqual(childReadSpan.kind, SpanKind.CONSUMER);
350358
assert.ok(childReadSpan.parentSpanId);
351359
});

0 commit comments

Comments
 (0)