Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak "producer as parent" config option #12

Merged
merged 1 commit into from
Oct 8, 2024
Merged
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
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 in the same trace, as a child span 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 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

Expand Down
50 changes: 30 additions & 20 deletions src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<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 +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,
Expand Down Expand Up @@ -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);
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