diff --git a/.changesets/allow-producer-to-be-used-as-parent-of-consumer.md b/.changesets/allow-producer-to-be-used-as-parent-of-consumer.md new file mode 100644 index 0000000..2b596bb --- /dev/null +++ b/.changesets/allow-producer-to-be-used-as-parent-of-consumer.md @@ -0,0 +1,6 @@ +--- +bump: patch +type: add +--- + +Add a `useProducerSpanAsConsumerParent` configuration option that defaults to `false`. When set to `true`, instead of establishing a span link from the consumer span to the producer span, the consumer span will be in the same trace, as a child span of the producer span. diff --git a/README.md b/README.md index 05bf36c..37b520e 100644 --- a/README.md +++ b/README.md @@ -46,12 +46,12 @@ registerInstrumentations({ ## Configuration options -| Name | Type | Default value | Description | -| ------------------------------------ | --------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | -| `emitCreateSpansForBulk` | `boolean` | `true` | Whether to emit a create span for each individual job enqueued by `Queue.addBulk` or `FlowProducer.addBulk`. The span representing the overall bulk operation is emitted regardless. | -| `emitCreateSpansForFlow` | `boolean` | `true` | Whether to emit a create span for each individual job enqueued by `FlowProducer.add` or `FlowProducer.addBulk`. The span representing the overall flow operation is emitted regardless. | -| `requireParentSpanForPublish` | `boolean` | `false` | Whether to omit emitting a publish span (and the create child spans for it, for bulk and flow operations) when there is no parent span, meaning that the span created would be the root span of a new trace. | -| `useProducerContextAsConsumerParent` | `boolean` | `false` | Whether to use the producer context as the parent for the consumer span. Consumer and Producer will share the same TraceId in this case. | +| Name | Type | Default value | Description | +| --------------------------------- | --------- | ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `emitCreateSpansForBulk` | `boolean` | `true` | Whether to emit a create span for each individual job enqueued by `Queue.addBulk` or `FlowProducer.addBulk`. The span representing the overall bulk operation is emitted regardless. | +| `emitCreateSpansForFlow` | `boolean` | `true` | Whether to emit a create span for each individual job enqueued by `FlowProducer.add` or `FlowProducer.addBulk`. The span representing the overall flow operation is emitted regardless. | +| `requireParentSpanForPublish` | `boolean` | `false` | Whether to omit emitting a publish span (and the create child spans for it, for bulk and flow operations) when there is no parent span, meaning that the span created would be the root span of a new trace. | +| `useProducerSpanAsConsumerParent` | `boolean` | `false` | Whether to use the producer kind (create or publish) span as the parent for the consumer kind (process) span. When set to true, the consumer and producer spans will be part of the same trace. When set to false, the consumer span will be in a separate trace from the producer span, and it will contain a link to the producer span. | ## Emitted spans diff --git a/src/instrumentation.ts b/src/instrumentation.ts index 00ff69a..6c81170 100644 --- a/src/instrumentation.ts +++ b/src/instrumentation.ts @@ -35,32 +35,39 @@ const FLOW_CONTEXT = Symbol("BULLMQ_FLOW_CONTEXT"); export interface BullMQInstrumentationConfig extends InstrumentationConfig { /** * Emit spans for each individual job enqueueing in calls to `Queue.addBulk` - * or `FlowProducer.addBulk`. Defaults to true. Setting it to false disables + * or `FlowProducer.addBulk`. Defaults to `true`. Setting it to `false` disables * individual job spans for bulk operations. */ emitCreateSpansForBulk?: boolean; /** * Emit spans for each individual job enqueueing in calls to `FlowProducer.add` - * or `FlowProducer.addBulk`. Defaults to true. Setting it to false disables + * or `FlowProducer.addBulk`. Defaults to `true`. Setting it to `false` disables * individual job spans for bulk operations. */ emitCreateSpansForFlow?: boolean; - /** Require a parent span in order to create a producer span - * (a span for the enqueueing of one or more jobs) -- defaults to `false` */ + /** + * Require a parent span in order to create a producer span, that is, a span + * for the enqueueing of one or more jobs. Defaults to `false`. + */ requireParentSpanForPublish?: boolean; - /** Whether to use the producer context as the parent for the consumer span. - * Consumer and Producer will share the same TraceId in this case. Defaults to `false` */ - useProducerContextAsConsumerParent?: boolean; + /** + * Whether to use the producer kind (create or publish) span as the parent + * for the consumer kind (process) span. When set to `true`, the consumer and + * producer spans will be part of the same trace. When set to `false`, the + * consumer span will be in a separate trace from the producer span, and it + * will contain a link to the producer span. Defaults to `true`. + */ + useProducerSpanAsConsumerParent?: boolean; } export const defaultConfig: Required = { emitCreateSpansForBulk: true, emitCreateSpansForFlow: true, requireParentSpanForPublish: false, - useProducerContextAsConsumerParent: false, + useProducerSpanAsConsumerParent: false, // unused by `configFor` but required for the type enabled: true, }; @@ -446,10 +453,23 @@ export class BullMQInstrumentation extends InstrumentationBase { job: any, ...rest: any[] ) { + const producerParent = instrumentation.configFor( + "useProducerSpanAsConsumerParent", + ); + const workerName = this.name ?? "anonymous"; const currentContext = context.active(); const producerContext = propagation.extract(currentContext, job.opts); + const parentContext = producerParent ? producerContext : currentContext; + const links = BullMQInstrumentation.dropInvalidLinks([ + { + context: producerParent + ? undefined + : trace.getSpanContext(producerContext), + }, + ]); + const spanName = `${job.queueName} ${operationType}`; const span = tracer.startSpan( spanName, @@ -483,19 +503,9 @@ export class BullMQInstrumentation extends InstrumentationBase { )?.groupKey, }), kind: SpanKind.CONSUMER, - links: BullMQInstrumentation.dropInvalidLinks( - instrumentation.configFor("useProducerContextAsConsumerParent") - ? [] - : [ - { - context: trace.getSpanContext(producerContext), - }, - ], - ), + links, }, - instrumentation.configFor("useProducerContextAsConsumerParent") - ? producerContext - : currentContext, + parentContext, ); const consumerContext = trace.setSpan(currentContext, span); diff --git a/test/instrumentation.test.ts b/test/instrumentation.test.ts index 1df7ffc..5d7f89f 100644 --- a/test/instrumentation.test.ts +++ b/test/instrumentation.test.ts @@ -70,6 +70,7 @@ function getWait(): [Promise, Function, Function] { function assertSpanParent(span: ReadableSpan, parent: ReadableSpan) { assert.strictEqual(span.parentSpanId, parent.spanContext().spanId); + assert.strictEqual(span.spanContext().traceId, parent.spanContext().traceId); } function assertDifferentTrace(span: ReadableSpan, parent: ReadableSpan) { @@ -870,8 +871,8 @@ describe("bullmq", () => { assertSpanParent(insideJobSpan!, workerJobSpan!); }); - it("should set the producer context as active when useProducerContextAsConsumerParent true", async () => { - instrumentation.setConfig({ useProducerContextAsConsumerParent: true }); + it("should use the producer span as parent when useProducerSpanAsConsumerParent is true", async () => { + instrumentation.setConfig({ useProducerSpanAsConsumerParent: true }); const [processor, processorDone] = getWait();