@opentelemetry/instrumentation-kafkajs 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { KafkaJsInstrumentationConfig } from './types';
|
|
2
2
|
import { InstrumentationBase, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation';
|
|
3
|
-
export declare class KafkaJsInstrumentation extends InstrumentationBase {
|
|
4
|
-
protected _config: KafkaJsInstrumentationConfig;
|
|
3
|
+
export declare class KafkaJsInstrumentation extends InstrumentationBase<KafkaJsInstrumentationConfig> {
|
|
5
4
|
constructor(config?: KafkaJsInstrumentationConfig);
|
|
6
5
|
protected init(): InstrumentationNodeModuleDefinition;
|
|
7
6
|
private _getConsumerPatch;
|
|
@@ -35,7 +35,7 @@ class KafkaJsInstrumentation extends instrumentation_1.InstrumentationBase {
|
|
|
35
35
|
this._unwrap(moduleExports.Kafka.prototype, 'consumer');
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
|
-
const module = new instrumentation_1.InstrumentationNodeModuleDefinition('kafkajs', ['<
|
|
38
|
+
const module = new instrumentation_1.InstrumentationNodeModuleDefinition('kafkajs', ['>=0.1.0 <3'], (moduleExports) => {
|
|
39
39
|
var _a, _b;
|
|
40
40
|
unpatch(moduleExports);
|
|
41
41
|
this._wrap((_a = moduleExports === null || moduleExports === void 0 ? void 0 : moduleExports.Kafka) === null || _a === void 0 ? void 0 : _a.prototype, 'producer', this._getProducerPatch());
|
|
@@ -184,7 +184,6 @@ class KafkaJsInstrumentation extends instrumentation_1.InstrumentationBase {
|
|
|
184
184
|
});
|
|
185
185
|
}
|
|
186
186
|
_startConsumerSpan(topic, message, operation, context, link) {
|
|
187
|
-
var _a;
|
|
188
187
|
const span = this.tracer.startSpan(topic, {
|
|
189
188
|
kind: api_1.SpanKind.CONSUMER,
|
|
190
189
|
attributes: {
|
|
@@ -194,8 +193,9 @@ class KafkaJsInstrumentation extends instrumentation_1.InstrumentationBase {
|
|
|
194
193
|
},
|
|
195
194
|
links: link ? [link] : [],
|
|
196
195
|
}, context);
|
|
197
|
-
|
|
198
|
-
|
|
196
|
+
const { consumerHook } = this.getConfig();
|
|
197
|
+
if (consumerHook && message) {
|
|
198
|
+
(0, instrumentation_1.safeExecuteInTheMiddle)(() => consumerHook(span, { topic, message }), e => {
|
|
199
199
|
if (e)
|
|
200
200
|
this._diag.error('consumerHook error', e);
|
|
201
201
|
}, true);
|
|
@@ -203,7 +203,7 @@ class KafkaJsInstrumentation extends instrumentation_1.InstrumentationBase {
|
|
|
203
203
|
return span;
|
|
204
204
|
}
|
|
205
205
|
_startProducerSpan(topic, message) {
|
|
206
|
-
var _a
|
|
206
|
+
var _a;
|
|
207
207
|
const span = this.tracer.startSpan(topic, {
|
|
208
208
|
kind: api_1.SpanKind.PRODUCER,
|
|
209
209
|
attributes: {
|
|
@@ -213,8 +213,9 @@ class KafkaJsInstrumentation extends instrumentation_1.InstrumentationBase {
|
|
|
213
213
|
});
|
|
214
214
|
message.headers = (_a = message.headers) !== null && _a !== void 0 ? _a : {};
|
|
215
215
|
api_1.propagation.inject(api_1.trace.setSpan(api_1.context.active(), span), message.headers);
|
|
216
|
-
|
|
217
|
-
|
|
216
|
+
const { producerHook } = this.getConfig();
|
|
217
|
+
if (producerHook) {
|
|
218
|
+
(0, instrumentation_1.safeExecuteInTheMiddle)(() => producerHook(span, { topic, message }), e => {
|
|
218
219
|
if (e)
|
|
219
220
|
this._diag.error('producerHook error', e);
|
|
220
221
|
}, true);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"instrumentation.js","sourceRoot":"","sources":["../../src/instrumentation.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,4CAU4B;AAC5B,8EAM6C;AAa7C,uCAA0D;AAC1D,6CAAmD;AACnD,oEAKwC;AAExC,MAAa,sBAAuB,SAAQ,qCAA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,mCAAI,EAAE,CAAC;QACxC,iBAAW,CAAC,MAAM,CAAC,WAAK,CAAC,OAAO,CAAC,aAAO,CAAC,MAAM,EAAE,EAAE,IAAI,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;QAE3E,IAAI,MAAA,IAAI,CAAC,OAAO,0CAAE,YAAY,EAAE;YAC9B,IAAA,wCAAsB,EACpB,GAAG,EAAE,CAAC,IAAI,CAAC,OAAO,CAAC,YAAa,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,EAC1D,CAAC,CAAC,EAAE;gBACF,IAAI,CAAC;oBAAE,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,oBAAoB,EAAE,CAAC,CAAC,CAAC;YACnD,CAAC,EACD,IAAI,CACL,CAAC;SACH;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AA/VD,wDA+VC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors, Aspecto\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n SpanKind,\n Span,\n SpanStatusCode,\n Context,\n propagation,\n Link,\n trace,\n context,\n ROOT_CONTEXT,\n} from '@opentelemetry/api';\nimport {\n MESSAGINGOPERATIONVALUES_PROCESS,\n MESSAGINGOPERATIONVALUES_RECEIVE,\n SEMATTRS_MESSAGING_SYSTEM,\n SEMATTRS_MESSAGING_DESTINATION,\n SEMATTRS_MESSAGING_OPERATION,\n} from '@opentelemetry/semantic-conventions';\nimport type * as kafkaJs from 'kafkajs';\nimport type {\n EachBatchHandler,\n EachMessageHandler,\n Producer,\n RecordMetadata,\n Message,\n ConsumerRunConfig,\n KafkaMessage,\n Consumer,\n} from 'kafkajs';\nimport { KafkaJsInstrumentationConfig } from './types';\nimport { PACKAGE_NAME, PACKAGE_VERSION } from './version';\nimport { bufferTextMapGetter } from './propagator';\nimport {\n InstrumentationBase,\n InstrumentationNodeModuleDefinition,\n safeExecuteInTheMiddle,\n isWrapped,\n} from '@opentelemetry/instrumentation';\n\nexport class KafkaJsInstrumentation extends InstrumentationBase {\n protected override _config!: KafkaJsInstrumentationConfig;\n\n constructor(config: KafkaJsInstrumentationConfig = {}) {\n super(PACKAGE_NAME, PACKAGE_VERSION, config);\n }\n\n protected init() {\n const unpatch = (moduleExports: typeof kafkaJs) => {\n if (isWrapped(moduleExports?.Kafka?.prototype.producer)) {\n this._unwrap(moduleExports.Kafka.prototype, 'producer');\n }\n if (isWrapped(moduleExports?.Kafka?.prototype.consumer)) {\n this._unwrap(moduleExports.Kafka.prototype, 'consumer');\n }\n };\n\n const module = new InstrumentationNodeModuleDefinition(\n 'kafkajs',\n ['< 3'],\n (moduleExports: typeof kafkaJs) => {\n unpatch(moduleExports);\n this._wrap(\n moduleExports?.Kafka?.prototype,\n 'producer',\n this._getProducerPatch()\n );\n this._wrap(\n moduleExports?.Kafka?.prototype,\n 'consumer',\n this._getConsumerPatch()\n );\n\n return moduleExports;\n },\n unpatch\n );\n return module;\n }\n\n private _getConsumerPatch() {\n const instrumentation = this;\n return (original: kafkaJs.Kafka['consumer']) => {\n return function consumer(\n this: kafkaJs.Kafka,\n ...args: Parameters<kafkaJs.Kafka['consumer']>\n ) {\n const newConsumer: Consumer = original.apply(this, args);\n\n if (isWrapped(newConsumer.run)) {\n instrumentation._unwrap(newConsumer, 'run');\n }\n\n instrumentation._wrap(\n newConsumer,\n 'run',\n instrumentation._getConsumerRunPatch()\n );\n\n return newConsumer;\n };\n };\n }\n\n private _getProducerPatch() {\n const instrumentation = this;\n return (original: kafkaJs.Kafka['producer']) => {\n return function consumer(\n this: kafkaJs.Kafka,\n ...args: Parameters<kafkaJs.Kafka['producer']>\n ) {\n const newProducer: Producer = original.apply(this, args);\n\n if (isWrapped(newProducer.sendBatch)) {\n instrumentation._unwrap(newProducer, 'sendBatch');\n }\n instrumentation._wrap(\n newProducer,\n 'sendBatch',\n instrumentation._getProducerSendBatchPatch()\n );\n\n if (isWrapped(newProducer.send)) {\n instrumentation._unwrap(newProducer, 'send');\n }\n instrumentation._wrap(\n newProducer,\n 'send',\n instrumentation._getProducerSendPatch()\n );\n\n return newProducer;\n };\n };\n }\n\n private _getConsumerRunPatch() {\n const instrumentation = this;\n return (original: Consumer['run']) => {\n return function run(\n this: Consumer,\n ...args: Parameters<Consumer['run']>\n ): ReturnType<Consumer['run']> {\n const config = args[0];\n if (config?.eachMessage) {\n if (isWrapped(config.eachMessage)) {\n instrumentation._unwrap(config, 'eachMessage');\n }\n instrumentation._wrap(\n config,\n 'eachMessage',\n instrumentation._getConsumerEachMessagePatch()\n );\n }\n if (config?.eachBatch) {\n if (isWrapped(config.eachBatch)) {\n instrumentation._unwrap(config, 'eachBatch');\n }\n instrumentation._wrap(\n config,\n 'eachBatch',\n instrumentation._getConsumerEachBatchPatch()\n );\n }\n return original.call(this, config);\n };\n };\n }\n\n private _getConsumerEachMessagePatch() {\n const instrumentation = this;\n return (original: ConsumerRunConfig['eachMessage']) => {\n return function eachMessage(\n this: unknown,\n ...args: Parameters<EachMessageHandler>\n ): Promise<void> {\n const payload = args[0];\n const propagatedContext: Context = propagation.extract(\n ROOT_CONTEXT,\n payload.message.headers,\n bufferTextMapGetter\n );\n const span = instrumentation._startConsumerSpan(\n payload.topic,\n payload.message,\n MESSAGINGOPERATIONVALUES_PROCESS,\n propagatedContext\n );\n\n const eachMessagePromise = context.with(\n trace.setSpan(propagatedContext, span),\n () => {\n return original!.apply(this, args);\n }\n );\n return instrumentation._endSpansOnPromise([span], eachMessagePromise);\n };\n };\n }\n\n private _getConsumerEachBatchPatch() {\n return (original: ConsumerRunConfig['eachBatch']) => {\n const instrumentation = this;\n return function eachBatch(\n this: unknown,\n ...args: Parameters<EachBatchHandler>\n ): Promise<void> {\n const payload = args[0];\n // https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/messaging.md#topic-with-multiple-consumers\n const receivingSpan = instrumentation._startConsumerSpan(\n payload.batch.topic,\n undefined,\n MESSAGINGOPERATIONVALUES_RECEIVE,\n ROOT_CONTEXT\n );\n return context.with(\n trace.setSpan(context.active(), receivingSpan),\n () => {\n const spans = payload.batch.messages.map(\n (message: KafkaMessage) => {\n const propagatedContext: Context = propagation.extract(\n ROOT_CONTEXT,\n message.headers,\n bufferTextMapGetter\n );\n const spanContext = trace\n .getSpan(propagatedContext)\n ?.spanContext();\n let origSpanLink: Link | undefined;\n if (spanContext) {\n origSpanLink = {\n context: spanContext,\n };\n }\n return instrumentation._startConsumerSpan(\n payload.batch.topic,\n message,\n MESSAGINGOPERATIONVALUES_PROCESS,\n undefined,\n origSpanLink\n );\n }\n );\n const batchMessagePromise: Promise<void> = original!.apply(\n this,\n args\n );\n spans.unshift(receivingSpan);\n return instrumentation._endSpansOnPromise(\n spans,\n batchMessagePromise\n );\n }\n );\n };\n };\n }\n\n private _getProducerSendBatchPatch() {\n const instrumentation = this;\n return (original: Producer['sendBatch']) => {\n return function sendBatch(\n this: Producer,\n ...args: Parameters<Producer['sendBatch']>\n ): ReturnType<Producer['sendBatch']> {\n const batch = args[0];\n const messages = batch.topicMessages || [];\n const spans: Span[] = messages\n .map(topicMessage =>\n topicMessage.messages.map(message =>\n instrumentation._startProducerSpan(topicMessage.topic, message)\n )\n )\n .reduce((acc, val) => acc.concat(val), []);\n\n const origSendResult: Promise<RecordMetadata[]> = original.apply(\n this,\n args\n );\n return instrumentation._endSpansOnPromise(spans, origSendResult);\n };\n };\n }\n\n private _getProducerSendPatch() {\n const instrumentation = this;\n return (original: Producer['send']) => {\n return function send(\n this: Producer,\n ...args: Parameters<Producer['send']>\n ): ReturnType<Producer['send']> {\n const record = args[0];\n const spans: Span[] = record.messages.map(message => {\n return instrumentation._startProducerSpan(record.topic, message);\n });\n\n const origSendResult: Promise<RecordMetadata[]> = original.apply(\n this,\n args\n );\n return instrumentation._endSpansOnPromise(spans, origSendResult);\n };\n };\n }\n\n private _endSpansOnPromise<T>(\n spans: Span[],\n sendPromise: Promise<T>\n ): Promise<T> {\n return Promise.resolve(sendPromise)\n .catch(reason => {\n let errorMessage: string;\n if (typeof reason === 'string') errorMessage = reason;\n else if (\n typeof reason === 'object' &&\n Object.prototype.hasOwnProperty.call(reason, 'message')\n )\n errorMessage = reason.message;\n\n spans.forEach(span =>\n span.setStatus({\n code: SpanStatusCode.ERROR,\n message: errorMessage,\n })\n );\n\n throw reason;\n })\n .finally(() => {\n spans.forEach(span => span.end());\n });\n }\n\n private _startConsumerSpan(\n topic: string,\n message: KafkaMessage | undefined,\n operation: string,\n context: Context | undefined,\n link?: Link\n ) {\n const span = this.tracer.startSpan(\n topic,\n {\n kind: SpanKind.CONSUMER,\n attributes: {\n [SEMATTRS_MESSAGING_SYSTEM]: 'kafka',\n [SEMATTRS_MESSAGING_DESTINATION]: topic,\n [SEMATTRS_MESSAGING_OPERATION]: operation,\n },\n links: link ? [link] : [],\n },\n context\n );\n\n if (this._config?.consumerHook && message) {\n safeExecuteInTheMiddle(\n () => this._config.consumerHook!(span, { topic, message }),\n e => {\n if (e) this._diag.error('consumerHook error', e);\n },\n true\n );\n }\n\n return span;\n }\n\n private _startProducerSpan(topic: string, message: Message) {\n const span = this.tracer.startSpan(topic, {\n kind: SpanKind.PRODUCER,\n attributes: {\n [SEMATTRS_MESSAGING_SYSTEM]: 'kafka',\n [SEMATTRS_MESSAGING_DESTINATION]: topic,\n },\n });\n\n message.headers = message.headers ?? {};\n propagation.inject(trace.setSpan(context.active(), span), message.headers);\n\n if (this._config?.producerHook) {\n safeExecuteInTheMiddle(\n () => this._config.producerHook!(span, { topic, message }),\n e => {\n if (e) this._diag.error('producerHook error', e);\n },\n true\n );\n }\n\n return span;\n }\n}\n"]}
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,KAAK,EAAE;YACxC,IAAI,EAAE,cAAQ,CAAC,QAAQ;YACvB,UAAU,EAAE;gBACV,CAAC,gDAAyB,CAAC,EAAE,OAAO;gBACpC,CAAC,qDAA8B,CAAC,EAAE,KAAK;aACxC;SACF,CAAC,CAAC;QAEH,OAAO,CAAC,OAAO,GAAG,MAAA,OAAO,CAAC,OAAO,mCAAI,EAAE,CAAC;QACxC,iBAAW,CAAC,MAAM,CAAC,WAAK,CAAC,OAAO,CAAC,aAAO,CAAC,MAAM,EAAE,EAAE,IAAI,CAAC,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;QAE3E,MAAM,EAAE,YAAY,EAAE,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC;QAC1C,IAAI,YAAY,EAAE;YAChB,IAAA,wCAAsB,EACpB,GAAG,EAAE,CAAC,YAAY,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,EAC5C,CAAC,CAAC,EAAE;gBACF,IAAI,CAAC;oBAAE,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,oBAAoB,EAAE,CAAC,CAAC,CAAC;YACnD,CAAC,EACD,IAAI,CACL,CAAC;SACH;QAED,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AA/VD,wDA+VC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors, Aspecto\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n SpanKind,\n Span,\n SpanStatusCode,\n Context,\n propagation,\n Link,\n trace,\n context,\n ROOT_CONTEXT,\n} from '@opentelemetry/api';\nimport {\n MESSAGINGOPERATIONVALUES_PROCESS,\n MESSAGINGOPERATIONVALUES_RECEIVE,\n SEMATTRS_MESSAGING_SYSTEM,\n SEMATTRS_MESSAGING_DESTINATION,\n SEMATTRS_MESSAGING_OPERATION,\n} from '@opentelemetry/semantic-conventions';\nimport type * as kafkaJs from 'kafkajs';\nimport type {\n EachBatchHandler,\n EachMessageHandler,\n Producer,\n RecordMetadata,\n Message,\n ConsumerRunConfig,\n KafkaMessage,\n Consumer,\n} from 'kafkajs';\nimport { KafkaJsInstrumentationConfig } from './types';\nimport { PACKAGE_NAME, PACKAGE_VERSION } from './version';\nimport { bufferTextMapGetter } from './propagator';\nimport {\n InstrumentationBase,\n InstrumentationNodeModuleDefinition,\n safeExecuteInTheMiddle,\n isWrapped,\n} from '@opentelemetry/instrumentation';\n\nexport class KafkaJsInstrumentation extends InstrumentationBase<KafkaJsInstrumentationConfig> {\n constructor(config: KafkaJsInstrumentationConfig = {}) {\n super(PACKAGE_NAME, PACKAGE_VERSION, config);\n }\n\n protected init() {\n const unpatch = (moduleExports: typeof kafkaJs) => {\n if (isWrapped(moduleExports?.Kafka?.prototype.producer)) {\n this._unwrap(moduleExports.Kafka.prototype, 'producer');\n }\n if (isWrapped(moduleExports?.Kafka?.prototype.consumer)) {\n this._unwrap(moduleExports.Kafka.prototype, 'consumer');\n }\n };\n\n const module = new InstrumentationNodeModuleDefinition(\n 'kafkajs',\n ['>=0.1.0 <3'],\n (moduleExports: typeof kafkaJs) => {\n unpatch(moduleExports);\n this._wrap(\n moduleExports?.Kafka?.prototype,\n 'producer',\n this._getProducerPatch()\n );\n this._wrap(\n moduleExports?.Kafka?.prototype,\n 'consumer',\n this._getConsumerPatch()\n );\n\n return moduleExports;\n },\n unpatch\n );\n return module;\n }\n\n private _getConsumerPatch() {\n const instrumentation = this;\n return (original: kafkaJs.Kafka['consumer']) => {\n return function consumer(\n this: kafkaJs.Kafka,\n ...args: Parameters<kafkaJs.Kafka['consumer']>\n ) {\n const newConsumer: Consumer = original.apply(this, args);\n\n if (isWrapped(newConsumer.run)) {\n instrumentation._unwrap(newConsumer, 'run');\n }\n\n instrumentation._wrap(\n newConsumer,\n 'run',\n instrumentation._getConsumerRunPatch()\n );\n\n return newConsumer;\n };\n };\n }\n\n private _getProducerPatch() {\n const instrumentation = this;\n return (original: kafkaJs.Kafka['producer']) => {\n return function consumer(\n this: kafkaJs.Kafka,\n ...args: Parameters<kafkaJs.Kafka['producer']>\n ) {\n const newProducer: Producer = original.apply(this, args);\n\n if (isWrapped(newProducer.sendBatch)) {\n instrumentation._unwrap(newProducer, 'sendBatch');\n }\n instrumentation._wrap(\n newProducer,\n 'sendBatch',\n instrumentation._getProducerSendBatchPatch()\n );\n\n if (isWrapped(newProducer.send)) {\n instrumentation._unwrap(newProducer, 'send');\n }\n instrumentation._wrap(\n newProducer,\n 'send',\n instrumentation._getProducerSendPatch()\n );\n\n return newProducer;\n };\n };\n }\n\n private _getConsumerRunPatch() {\n const instrumentation = this;\n return (original: Consumer['run']) => {\n return function run(\n this: Consumer,\n ...args: Parameters<Consumer['run']>\n ): ReturnType<Consumer['run']> {\n const config = args[0];\n if (config?.eachMessage) {\n if (isWrapped(config.eachMessage)) {\n instrumentation._unwrap(config, 'eachMessage');\n }\n instrumentation._wrap(\n config,\n 'eachMessage',\n instrumentation._getConsumerEachMessagePatch()\n );\n }\n if (config?.eachBatch) {\n if (isWrapped(config.eachBatch)) {\n instrumentation._unwrap(config, 'eachBatch');\n }\n instrumentation._wrap(\n config,\n 'eachBatch',\n instrumentation._getConsumerEachBatchPatch()\n );\n }\n return original.call(this, config);\n };\n };\n }\n\n private _getConsumerEachMessagePatch() {\n const instrumentation = this;\n return (original: ConsumerRunConfig['eachMessage']) => {\n return function eachMessage(\n this: unknown,\n ...args: Parameters<EachMessageHandler>\n ): Promise<void> {\n const payload = args[0];\n const propagatedContext: Context = propagation.extract(\n ROOT_CONTEXT,\n payload.message.headers,\n bufferTextMapGetter\n );\n const span = instrumentation._startConsumerSpan(\n payload.topic,\n payload.message,\n MESSAGINGOPERATIONVALUES_PROCESS,\n propagatedContext\n );\n\n const eachMessagePromise = context.with(\n trace.setSpan(propagatedContext, span),\n () => {\n return original!.apply(this, args);\n }\n );\n return instrumentation._endSpansOnPromise([span], eachMessagePromise);\n };\n };\n }\n\n private _getConsumerEachBatchPatch() {\n return (original: ConsumerRunConfig['eachBatch']) => {\n const instrumentation = this;\n return function eachBatch(\n this: unknown,\n ...args: Parameters<EachBatchHandler>\n ): Promise<void> {\n const payload = args[0];\n // https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/messaging.md#topic-with-multiple-consumers\n const receivingSpan = instrumentation._startConsumerSpan(\n payload.batch.topic,\n undefined,\n MESSAGINGOPERATIONVALUES_RECEIVE,\n ROOT_CONTEXT\n );\n return context.with(\n trace.setSpan(context.active(), receivingSpan),\n () => {\n const spans = payload.batch.messages.map(\n (message: KafkaMessage) => {\n const propagatedContext: Context = propagation.extract(\n ROOT_CONTEXT,\n message.headers,\n bufferTextMapGetter\n );\n const spanContext = trace\n .getSpan(propagatedContext)\n ?.spanContext();\n let origSpanLink: Link | undefined;\n if (spanContext) {\n origSpanLink = {\n context: spanContext,\n };\n }\n return instrumentation._startConsumerSpan(\n payload.batch.topic,\n message,\n MESSAGINGOPERATIONVALUES_PROCESS,\n undefined,\n origSpanLink\n );\n }\n );\n const batchMessagePromise: Promise<void> = original!.apply(\n this,\n args\n );\n spans.unshift(receivingSpan);\n return instrumentation._endSpansOnPromise(\n spans,\n batchMessagePromise\n );\n }\n );\n };\n };\n }\n\n private _getProducerSendBatchPatch() {\n const instrumentation = this;\n return (original: Producer['sendBatch']) => {\n return function sendBatch(\n this: Producer,\n ...args: Parameters<Producer['sendBatch']>\n ): ReturnType<Producer['sendBatch']> {\n const batch = args[0];\n const messages = batch.topicMessages || [];\n const spans: Span[] = messages\n .map(topicMessage =>\n topicMessage.messages.map(message =>\n instrumentation._startProducerSpan(topicMessage.topic, message)\n )\n )\n .reduce((acc, val) => acc.concat(val), []);\n\n const origSendResult: Promise<RecordMetadata[]> = original.apply(\n this,\n args\n );\n return instrumentation._endSpansOnPromise(spans, origSendResult);\n };\n };\n }\n\n private _getProducerSendPatch() {\n const instrumentation = this;\n return (original: Producer['send']) => {\n return function send(\n this: Producer,\n ...args: Parameters<Producer['send']>\n ): ReturnType<Producer['send']> {\n const record = args[0];\n const spans: Span[] = record.messages.map(message => {\n return instrumentation._startProducerSpan(record.topic, message);\n });\n\n const origSendResult: Promise<RecordMetadata[]> = original.apply(\n this,\n args\n );\n return instrumentation._endSpansOnPromise(spans, origSendResult);\n };\n };\n }\n\n private _endSpansOnPromise<T>(\n spans: Span[],\n sendPromise: Promise<T>\n ): Promise<T> {\n return Promise.resolve(sendPromise)\n .catch(reason => {\n let errorMessage: string;\n if (typeof reason === 'string') errorMessage = reason;\n else if (\n typeof reason === 'object' &&\n Object.prototype.hasOwnProperty.call(reason, 'message')\n )\n errorMessage = reason.message;\n\n spans.forEach(span =>\n span.setStatus({\n code: SpanStatusCode.ERROR,\n message: errorMessage,\n })\n );\n\n throw reason;\n })\n .finally(() => {\n spans.forEach(span => span.end());\n });\n }\n\n private _startConsumerSpan(\n topic: string,\n message: KafkaMessage | undefined,\n operation: string,\n context: Context | undefined,\n link?: Link\n ) {\n const span = this.tracer.startSpan(\n topic,\n {\n kind: SpanKind.CONSUMER,\n attributes: {\n [SEMATTRS_MESSAGING_SYSTEM]: 'kafka',\n [SEMATTRS_MESSAGING_DESTINATION]: topic,\n [SEMATTRS_MESSAGING_OPERATION]: operation,\n },\n links: link ? [link] : [],\n },\n context\n );\n\n const { consumerHook } = this.getConfig();\n if (consumerHook && message) {\n safeExecuteInTheMiddle(\n () => consumerHook(span, { topic, message }),\n e => {\n if (e) this._diag.error('consumerHook error', e);\n },\n true\n );\n }\n\n return span;\n }\n\n private _startProducerSpan(topic: string, message: Message) {\n const span = this.tracer.startSpan(topic, {\n kind: SpanKind.PRODUCER,\n attributes: {\n [SEMATTRS_MESSAGING_SYSTEM]: 'kafka',\n [SEMATTRS_MESSAGING_DESTINATION]: topic,\n },\n });\n\n message.headers = message.headers ?? {};\n propagation.inject(trace.setSpan(context.active(), span), message.headers);\n\n const { producerHook } = this.getConfig();\n if (producerHook) {\n safeExecuteInTheMiddle(\n () => producerHook(span, { topic, message }),\n e => {\n if (e) this._diag.error('producerHook error', e);\n },\n true\n );\n }\n\n return span;\n }\n}\n"]}
|
package/build/src/version.d.ts
CHANGED
package/build/src/version.js
CHANGED
|
@@ -17,6 +17,6 @@
|
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
18
|
exports.PACKAGE_NAME = exports.PACKAGE_VERSION = void 0;
|
|
19
19
|
// this is autogenerated file, see scripts/version-update.js
|
|
20
|
-
exports.PACKAGE_VERSION = '0.
|
|
20
|
+
exports.PACKAGE_VERSION = '0.3.0';
|
|
21
21
|
exports.PACKAGE_NAME = '@opentelemetry/instrumentation-kafkajs';
|
|
22
22
|
//# sourceMappingURL=version.js.map
|
package/build/src/version.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,4DAA4D;AAC/C,QAAA,eAAe,GAAG,OAAO,CAAC;AAC1B,QAAA,YAAY,GAAG,wCAAwC,CAAC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n// this is autogenerated file, see scripts/version-update.js\nexport const PACKAGE_VERSION = '0.
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;GAcG;;;AAEH,4DAA4D;AAC/C,QAAA,eAAe,GAAG,OAAO,CAAC;AAC1B,QAAA,YAAY,GAAG,wCAAwC,CAAC","sourcesContent":["/*\n * Copyright The OpenTelemetry Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * https://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n// this is autogenerated file, see scripts/version-update.js\nexport const PACKAGE_VERSION = '0.3.0';\nexport const PACKAGE_NAME = '@opentelemetry/instrumentation-kafkajs';\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@opentelemetry/instrumentation-kafkajs",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "OpenTelemetry instrumentation for `kafkajs` messaging client for Apache Kafka",
|
|
5
5
|
"main": "build/src/index.js",
|
|
6
6
|
"types": "build/src/index.d.ts",
|
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
"clean": "rimraf build/*",
|
|
12
12
|
"lint": "eslint . --ext .ts",
|
|
13
13
|
"lint:fix": "eslint . --ext .ts --fix",
|
|
14
|
+
"lint:readme": "node ../../../scripts/lint-readme",
|
|
14
15
|
"precompile": "tsc --version && lerna run version:update --scope @opentelemetry/instrumentation-kafkajs --include-dependencies",
|
|
15
16
|
"prewatch": "npm run precompile",
|
|
16
17
|
"prepublishOnly": "npm run compile",
|
|
@@ -43,7 +44,7 @@
|
|
|
43
44
|
},
|
|
44
45
|
"devDependencies": {
|
|
45
46
|
"@opentelemetry/api": "^1.3.0",
|
|
46
|
-
"@opentelemetry/contrib-test-utils": "^0.
|
|
47
|
+
"@opentelemetry/contrib-test-utils": "^0.41.0",
|
|
47
48
|
"@opentelemetry/sdk-trace-base": "^1.24.0",
|
|
48
49
|
"@types/mocha": "7.0.2",
|
|
49
50
|
"@types/node": "18.6.5",
|
|
@@ -51,15 +52,15 @@
|
|
|
51
52
|
"kafkajs": "^2.2.4",
|
|
52
53
|
"mocha": "7.2.0",
|
|
53
54
|
"nyc": "15.1.0",
|
|
54
|
-
"rimraf": "5.0.
|
|
55
|
+
"rimraf": "5.0.10",
|
|
55
56
|
"sinon": "15.2.0",
|
|
56
57
|
"ts-mocha": "10.0.0",
|
|
57
58
|
"typescript": "4.4.4"
|
|
58
59
|
},
|
|
59
60
|
"dependencies": {
|
|
60
|
-
"@opentelemetry/instrumentation": "^0.
|
|
61
|
-
"@opentelemetry/semantic-conventions": "^1.
|
|
61
|
+
"@opentelemetry/instrumentation": "^0.53.0",
|
|
62
|
+
"@opentelemetry/semantic-conventions": "^1.27.0"
|
|
62
63
|
},
|
|
63
64
|
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-kafkajs#readme",
|
|
64
|
-
"gitHead": "
|
|
65
|
+
"gitHead": "9dc58afed8134f95908331bcff35c5d9ec46fe9a"
|
|
65
66
|
}
|