Skip to content

Commit

Permalink
Tweak "producer as parent" config option
Browse files Browse the repository at this point in the history
Some tweaks to the changes introduced in #11.

Rename the config option to `useProducerSpanAsConsumerParent` (that
is, `s/Context/Span`) to use language that is more familiar to casual
OpenTelemetry users.

Rewrite the config option description to describe what happens with
and without the config option.

Rearrange the instrumentation to move logic branches out of the span
creation function.
  • Loading branch information
unflxw committed Oct 8, 2024
1 parent f836455 commit 0408eed
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -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 the parent of the producer span.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 span (create or publish) as the parent for the consumer (process) span. When set to true, the consumer and producer will be part of the same trace. When set to false, the consumer span will contain a link to the producer span. |

## Emitted spans

Expand Down
33 changes: 17 additions & 16 deletions src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,18 @@ export interface BullMQInstrumentationConfig extends InstrumentationConfig {
* (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 span (create or publish) as the parent for the
* consumer (process) span. When set to true, the consumer and producer will be
* part of the same trace. When set to false, the consumer span will contain a
* link to the producer span. */
useProducerSpanAsConsumerParent?: boolean;
}

export const defaultConfig: Required<BullMQInstrumentationConfig> = {
emitCreateSpansForBulk: true,
emitCreateSpansForFlow: true,
requireParentSpanForPublish: false,
useProducerContextAsConsumerParent: false,
useProducerSpanAsConsumerParent: false,
// unused by `configFor` but required for the type
enabled: true,
};
Expand Down Expand Up @@ -446,10 +448,19 @@ 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,
Expand Down Expand Up @@ -483,19 +494,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);
Expand Down
5 changes: 3 additions & 2 deletions test/instrumentation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ function getWait(): [Promise<any>, 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) {
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 0408eed

Please sign in to comment.