Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/instrumentation-kafkajs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ These attributes are added to both spans and metrics, where possible.
| ------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `messaging.system` | An identifier for the messaging system being used (i.e. `"kafka"`). |
| `messaging.destination.name` | The message destination name. |
| `messaging.consumer.group.name` | The name of the consumer group of the consumer that received or processed the message. **Note:** only available on consumer spans. |
| `messaging.operation.type` | A string identifying the type of messaging operation. |
| `messaging.operation.name` | The system-specific name of the messaging operation. |
| `messaging.operation.name` | The system-specific name of the messaging operation. |
Expand Down
36 changes: 30 additions & 6 deletions packages/instrumentation-kafkajs/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { EVENT_LISTENERS_SET } from './internal-types';
import { bufferTextMapGetter } from './propagator';
import {
ATTR_MESSAGING_BATCH_MESSAGE_COUNT,
ATTR_MESSAGING_CONSUMER_GROUP_NAME,
ATTR_MESSAGING_DESTINATION_NAME,
ATTR_MESSAGING_DESTINATION_PARTITION_ID,
ATTR_MESSAGING_KAFKA_CLUSTER_ID,
Expand Down Expand Up @@ -279,6 +280,10 @@ export class KafkaJsInstrumentation extends InstrumentationBase<KafkaJsInstrumen
...args: Parameters<kafkaJs.Kafka['consumer']>
) {
const newConsumer: Consumer = original.apply(this, args);
const groupId = args[0]?.groupId;
const consumerGroupAttributes: Attributes = groupId
? { [ATTR_MESSAGING_CONSUMER_GROUP_NAME]: groupId }
: {};

if (isWrapped(newConsumer.run)) {
instrumentation._unwrap(newConsumer, 'run');
Expand All @@ -287,7 +292,7 @@ export class KafkaJsInstrumentation extends InstrumentationBase<KafkaJsInstrumen
instrumentation._wrap(
newConsumer,
'run',
instrumentation._getConsumerRunPatch()
instrumentation._getConsumerRunPatch(consumerGroupAttributes)
);

instrumentation._setKafkaEventListeners(newConsumer);
Expand Down Expand Up @@ -366,7 +371,7 @@ export class KafkaJsInstrumentation extends InstrumentationBase<KafkaJsInstrumen
};
}

private _getConsumerRunPatch() {
private _getConsumerRunPatch(consumerGroupAttributes: Attributes) {
const instrumentation = this;
return (original: Consumer['run']) => {
return function run(
Expand All @@ -381,7 +386,10 @@ export class KafkaJsInstrumentation extends InstrumentationBase<KafkaJsInstrumen
instrumentation._wrap(
config,
'eachMessage',
instrumentation._getConsumerEachMessagePatch(this)
instrumentation._getConsumerEachMessagePatch(
this,
consumerGroupAttributes
)
);
}
if (config?.eachBatch) {
Expand All @@ -391,15 +399,21 @@ export class KafkaJsInstrumentation extends InstrumentationBase<KafkaJsInstrumen
instrumentation._wrap(
config,
'eachBatch',
instrumentation._getConsumerEachBatchPatch(this)
instrumentation._getConsumerEachBatchPatch(
this,
consumerGroupAttributes
)
);
}
return original.call(this, config);
};
};
}

private _getConsumerEachMessagePatch(kafkaClient?: unknown) {
private _getConsumerEachMessagePatch(
kafkaClient: unknown,
consumerGroupAttributes: Attributes
) {
const instrumentation = this;
return (original: ConsumerRunConfig['eachMessage']) => {
return function eachMessage(
Expand All @@ -419,6 +433,7 @@ export class KafkaJsInstrumentation extends InstrumentationBase<KafkaJsInstrumen
ctx: propagatedContext,
kafkaClient,
attributes: {
...consumerGroupAttributes,
[ATTR_MESSAGING_DESTINATION_PARTITION_ID]: String(
payload.partition
),
Expand All @@ -436,6 +451,7 @@ export class KafkaJsInstrumentation extends InstrumentationBase<KafkaJsInstrumen
[ATTR_MESSAGING_DESTINATION_PARTITION_ID]: String(
payload.partition
),
...consumerGroupAttributes,
}
),
prepareCounter(instrumentation._consumedMessages, 1, {
Expand All @@ -445,6 +461,7 @@ export class KafkaJsInstrumentation extends InstrumentationBase<KafkaJsInstrumen
[ATTR_MESSAGING_DESTINATION_PARTITION_ID]: String(
payload.partition
),
...consumerGroupAttributes,
}),
];

Expand All @@ -463,7 +480,10 @@ export class KafkaJsInstrumentation extends InstrumentationBase<KafkaJsInstrumen
};
}

private _getConsumerEachBatchPatch(kafkaClient?: unknown) {
private _getConsumerEachBatchPatch(
kafkaClient: unknown,
consumerGroupAttributes: Attributes
) {
return (original: ConsumerRunConfig['eachBatch']) => {
const instrumentation = this;
return function eachBatch(
Expand All @@ -479,6 +499,7 @@ export class KafkaJsInstrumentation extends InstrumentationBase<KafkaJsInstrumen
ctx: ROOT_CONTEXT,
kafkaClient,
attributes: {
...consumerGroupAttributes,
[ATTR_MESSAGING_BATCH_MESSAGE_COUNT]: payload.batch.messages.length,
[ATTR_MESSAGING_DESTINATION_PARTITION_ID]: String(
payload.batch.partition
Expand All @@ -501,6 +522,7 @@ export class KafkaJsInstrumentation extends InstrumentationBase<KafkaJsInstrumen
[ATTR_MESSAGING_DESTINATION_PARTITION_ID]: String(
payload.batch.partition
),
...consumerGroupAttributes,
}
),
];
Expand All @@ -527,6 +549,7 @@ export class KafkaJsInstrumentation extends InstrumentationBase<KafkaJsInstrumen
link: origSpanLink,
kafkaClient,
attributes: {
...consumerGroupAttributes,
[ATTR_MESSAGING_DESTINATION_PARTITION_ID]: String(
payload.batch.partition
),
Expand All @@ -544,6 +567,7 @@ export class KafkaJsInstrumentation extends InstrumentationBase<KafkaJsInstrumen
[ATTR_MESSAGING_DESTINATION_PARTITION_ID]: String(
payload.batch.partition
),
...consumerGroupAttributes,
}
)
);
Expand Down
13 changes: 13 additions & 0 deletions packages/instrumentation-kafkajs/src/semconv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@
export const ATTR_MESSAGING_BATCH_MESSAGE_COUNT =
'messaging.batch.message_count' as const;

/**
* The name of the consumer group with which a consumer is associated.
*
* @example my-group
* @example indexer
*
* @note Semantic conventions for individual messaging systems **SHOULD** document whether `messaging.consumer.group.name` is applicable and what it means in the context of that system.
*
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
*/
export const ATTR_MESSAGING_CONSUMER_GROUP_NAME =
'messaging.consumer.group.name' as const;

/**
* The message destination name
*
Expand Down
19 changes: 19 additions & 0 deletions packages/instrumentation-kafkajs/test/kafkajs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
ATTR_MESSAGING_SYSTEM,
ATTR_MESSAGING_DESTINATION_NAME,
ATTR_MESSAGING_OPERATION_TYPE,
ATTR_MESSAGING_CONSUMER_GROUP_NAME,
ATTR_MESSAGING_DESTINATION_PARTITION_ID,
ATTR_MESSAGING_KAFKA_MESSAGE_KEY,
ATTR_MESSAGING_KAFKA_MESSAGE_TOMBSTONE,
Expand Down Expand Up @@ -1048,6 +1049,10 @@ describe('instrumentation-kafkajs', () => {
undefined
);
assert.strictEqual(span.attributes[ATTR_MESSAGING_KAFKA_OFFSET], '123');
assert.strictEqual(
span.attributes[ATTR_MESSAGING_CONSUMER_GROUP_NAME],
'testing-group-id'
);
assertMetricCollection(await metricReader.collect(), {
[METRIC_MESSAGING_PROCESS_DURATION]: [
{
Expand All @@ -1057,6 +1062,7 @@ describe('instrumentation-kafkajs', () => {
[ATTR_MESSAGING_DESTINATION_NAME]: 'topic-name-1',
[ATTR_MESSAGING_DESTINATION_PARTITION_ID]: '1',
[ATTR_MESSAGING_OPERATION_NAME]: 'process',
[ATTR_MESSAGING_CONSUMER_GROUP_NAME]: 'testing-group-id',
},
},
],
Expand All @@ -1068,6 +1074,7 @@ describe('instrumentation-kafkajs', () => {
[ATTR_MESSAGING_DESTINATION_NAME]: 'topic-name-1',
[ATTR_MESSAGING_DESTINATION_PARTITION_ID]: '1',
[ATTR_MESSAGING_OPERATION_NAME]: 'process',
[ATTR_MESSAGING_CONSUMER_GROUP_NAME]: 'testing-group-id',
},
},
],
Expand Down Expand Up @@ -1243,6 +1250,7 @@ describe('instrumentation-kafkajs', () => {
[ATTR_MESSAGING_DESTINATION_NAME]: 'topic-name-1',
[ATTR_MESSAGING_DESTINATION_PARTITION_ID]: '1',
[ATTR_MESSAGING_OPERATION_NAME]: 'process',
[ATTR_MESSAGING_CONSUMER_GROUP_NAME]: 'testing-group-id',
[ATTR_ERROR_TYPE]: 'Error',
},
},
Expand All @@ -1255,6 +1263,7 @@ describe('instrumentation-kafkajs', () => {
[ATTR_MESSAGING_DESTINATION_NAME]: 'topic-name-1',
[ATTR_MESSAGING_DESTINATION_PARTITION_ID]: '1',
[ATTR_MESSAGING_OPERATION_NAME]: 'process',
[ATTR_MESSAGING_CONSUMER_GROUP_NAME]: 'testing-group-id',
[ATTR_ERROR_TYPE]: 'Error',
},
},
Expand Down Expand Up @@ -1295,6 +1304,7 @@ describe('instrumentation-kafkajs', () => {
[ATTR_MESSAGING_DESTINATION_NAME]: 'topic-name-1',
[ATTR_MESSAGING_DESTINATION_PARTITION_ID]: '1',
[ATTR_MESSAGING_OPERATION_NAME]: 'process',
[ATTR_MESSAGING_CONSUMER_GROUP_NAME]: 'testing-group-id',
[ATTR_ERROR_TYPE]: '_OTHER',
},
},
Expand All @@ -1307,6 +1317,7 @@ describe('instrumentation-kafkajs', () => {
[ATTR_MESSAGING_DESTINATION_NAME]: 'topic-name-1',
[ATTR_MESSAGING_DESTINATION_PARTITION_ID]: '1',
[ATTR_MESSAGING_OPERATION_NAME]: 'process',
[ATTR_MESSAGING_CONSUMER_GROUP_NAME]: 'testing-group-id',
[ATTR_ERROR_TYPE]: '_OTHER',
},
},
Expand Down Expand Up @@ -1344,6 +1355,7 @@ describe('instrumentation-kafkajs', () => {
[ATTR_MESSAGING_DESTINATION_NAME]: 'topic-name-1',
[ATTR_MESSAGING_DESTINATION_PARTITION_ID]: '1',
[ATTR_MESSAGING_OPERATION_NAME]: 'process',
[ATTR_MESSAGING_CONSUMER_GROUP_NAME]: 'testing-group-id',
[ATTR_ERROR_TYPE]: '_OTHER',
},
},
Expand All @@ -1356,6 +1368,7 @@ describe('instrumentation-kafkajs', () => {
[ATTR_MESSAGING_DESTINATION_NAME]: 'topic-name-1',
[ATTR_MESSAGING_DESTINATION_PARTITION_ID]: '1',
[ATTR_MESSAGING_OPERATION_NAME]: 'process',
[ATTR_MESSAGING_CONSUMER_GROUP_NAME]: 'testing-group-id',
[ATTR_ERROR_TYPE]: '_OTHER',
},
},
Expand Down Expand Up @@ -1389,6 +1402,10 @@ describe('instrumentation-kafkajs', () => {
span.attributes[ATTR_MESSAGING_DESTINATION_NAME],
'topic-name-1'
);
assert.strictEqual(
span.attributes[ATTR_MESSAGING_CONSUMER_GROUP_NAME],
'testing-group-id'
);
});

const [recvSpan, msg1Span, msg2Span] = spans;
Expand Down Expand Up @@ -1441,6 +1458,7 @@ describe('instrumentation-kafkajs', () => {
[ATTR_MESSAGING_DESTINATION_NAME]: 'topic-name-1',
[ATTR_MESSAGING_DESTINATION_PARTITION_ID]: '1234',
[ATTR_MESSAGING_OPERATION_NAME]: 'process',
[ATTR_MESSAGING_CONSUMER_GROUP_NAME]: 'testing-group-id',
},
},
],
Expand All @@ -1452,6 +1470,7 @@ describe('instrumentation-kafkajs', () => {
[ATTR_MESSAGING_DESTINATION_NAME]: 'topic-name-1',
[ATTR_MESSAGING_DESTINATION_PARTITION_ID]: '1234',
[ATTR_MESSAGING_OPERATION_NAME]: 'process',
[ATTR_MESSAGING_CONSUMER_GROUP_NAME]: 'testing-group-id',
},
},
],
Expand Down