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

feat(queue): observability for queue #2721

Draft
wants to merge 22 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
722fb5d
feat(queue): observability for queue
fgozdz Aug 22, 2024
1b2fd0b
feat(worker): observability for worker
fgozdz Aug 23, 2024
e6dfb85
feat(queue, worker): fix telemetry setup
fgozdz Aug 26, 2024
a75a8dd
feat(queue, worker): describe features
fgozdz Aug 27, 2024
1b176c6
Merge branch 'master' of github.com:taskforcesh/bullmq into feat/open…
fgozdz Aug 28, 2024
66b85c5
feat(queue, worker): add exception handler
fgozdz Aug 29, 2024
54a2c18
feat(queue, worker): remove redundancy in code by creating helper met…
fgozdz Aug 29, 2024
2e3fe37
feat(queue, worker): telemetry attributes change, and fix promises
fgozdz Sep 4, 2024
c4cb517
feat(queue, worker): resolve conflict
fgozdz Sep 4, 2024
14b2f2e
Merge branch 'master' of github.com:taskforcesh/bullmq into feat/open…
fgozdz Sep 5, 2024
7f6c674
Revert "feat(queue, worker): resolve conflict"
fgozdz Sep 5, 2024
059d50b
fix(documentation): fix documentation formatting
fgozdz Sep 5, 2024
f70cc8a
feat(queue-base): add context manager for telemetry
fgozdz Sep 5, 2024
8f38087
feat(worker, queue): add spankind and distributed context propagation
fgozdz Sep 10, 2024
379abf5
fix(worker, queue): remove unused import
fgozdz Sep 10, 2024
3e8e0d8
Merge branch 'master' into feat/opentelemetry
roggervalf Sep 10, 2024
6169ea3
feat(queue, worker): documentation and changes to propagation, basic …
fgozdz Sep 16, 2024
908ec13
feat(test_telemetry_interface): remove unused imports
fgozdz Sep 16, 2024
2dab35f
feat(queue, worker): correct tests and spankind, use property mapping…
fgozdz Sep 22, 2024
846e3ef
feat(worker): minor changes
fgozdz Sep 23, 2024
fc733e9
feat(queue, worker): distributed tracing
fgozdz Oct 9, 2024
4c199f8
feat(queue, worker): resolve conflicts
fgozdz Oct 9, 2024
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
100 changes: 99 additions & 1 deletion src/classes/queue-base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { EventEmitter } from 'events';
import { QueueBaseOptions, RedisClient } from '../interfaces';
import {
QueueBaseOptions,
RedisClient,
Span,
Tracer,
SetSpan,
ContextManager,
Propagation,
Context,
} from '../interfaces';
import { MinimalQueue } from '../types';
import {
delay,
Expand All @@ -11,6 +20,7 @@ import { RedisConnection } from './redis-connection';
import { Job } from './job';
import { KeysMap, QueueKeys } from './queue-keys';
import { Scripts } from './scripts';
import { TelemetryAttributes, SpanKind } from '../enums';

/**
* @class QueueBase
Expand All @@ -30,6 +40,16 @@ export class QueueBase extends EventEmitter implements MinimalQueue {
protected connection: RedisConnection;
public readonly qualifiedName: string;

/**
* Instance of a telemetry client
* To use it create if statement in a method to observe with start and end of a span
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this comment is correct anymore as we are using the trace helper.

* It will check if tracer is provided and if not it will continue as is
*/
private tracer: Tracer | undefined;
private setSpan: SetSpan | undefined;
protected contextManager: ContextManager | undefined;
protected propagation: Propagation | undefined;

/**
*
* @param name - The name of the queue.
Expand Down Expand Up @@ -76,6 +96,27 @@ export class QueueBase extends EventEmitter implements MinimalQueue {
this.keys = queueKeys.getKeys(name);
this.toKey = (type: string) => queueKeys.toKey(name, type);
this.setScripts();

if (opts?.telemetry) {
this.tracer = opts.telemetry.trace.getTracer(opts.telemetry.tracerName);
manast marked this conversation as resolved.
Show resolved Hide resolved
this.setSpan = opts.telemetry.trace.setSpan;
this.contextManager = opts.telemetry.contextManager;
this.propagation = opts.telemetry.propagation;

this.contextManager.getMetadata = (context: Context) => {
const metadata = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getMetadata and fromMetadata should be implemented in the integration. For Otel for example they will use the propagation module, but we do not need to expose the propagation module in our generic telemetry interface.

this.propagation.inject(context, metadata);
return metadata;
};

this.contextManager.fromMetadata = (
activeContext: Context,
metadata: Record<string, string>,
) => {
const context = this.propagation.extract(activeContext, metadata);
return context;
};
}
}

/**
Expand Down Expand Up @@ -175,4 +216,61 @@ export class QueueBase extends EventEmitter implements MinimalQueue {
}
}
}

/**
* Wraps the code with telemetry and provides span for configuration.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"... provides a span for configuration."

*
* @param spanKind - kind of the span: Producer, Consumer, Internal
* @param getSpanName - name of the span
* @param callback - code to wrap with telemetry
* @param srcPropagationMedatada -
* @returns
*/
protected async trace<T>(
spanKind: SpanKind,
getSpanName: () => string,
callback: (
span?: Span,
dstPropagationMetadata?: Record<string, string>,
) => Promise<T> | T,
srcPropagationMetadata?: Record<string, string>,
) {
if (!this.tracer) {
return callback();
}

const span = this.tracer.startSpan(getSpanName(), {
kind: spanKind,
});

try {
span.setAttributes({
[TelemetryAttributes.QueueName]: this.name,
});

let activeContext = this.contextManager.active();
if (srcPropagationMetadata) {
activeContext = this.contextManager.fromMetadata(
activeContext,
srcPropagationMetadata,
);
}

let dstPropagationMetadata: undefined | Record<string, string>;
if (spanKind === SpanKind.PRODUCER) {
dstPropagationMetadata = this.contextManager.getMetadata(activeContext);
}

const messageContext = this.setSpan(activeContext, span);

return await this.contextManager.with(messageContext, () =>
callback(span, dstPropagationMetadata),
);
} catch (err) {
span.recordException(err as Error);
throw err;
} finally {
span.end();
}
}
}
Loading
Loading