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 15 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
84 changes: 83 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,
SpanKind,
Propagation,
} 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 } 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,13 @@ 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;
}
}

/**
Expand Down Expand Up @@ -175,4 +202,59 @@ 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 spanType - type of the span: Producer, Consumer, Internal
* @param getSpanName - name of the span
* @param callback - code to wrap with telemetry
* @returns
*/
protected trace<T>(
getSpanType: () => SpanKind,
Copy link
Contributor

Choose a reason for hiding this comment

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

We do not need to have a function for getting the span type here, the reason for having it for the span name is because as the span name usually requires some computation (like concatenating strings, etc), by having it as a callback it does not perform those computations if telemetry is not enabled. For the spanType we are just passing a const, so this will not impact performance in any way.

getSpanName: () => string,
callback: (
span?: Span,
telemetryHeaders?: Record<string, string>,
) => Promise<T> | T,
activeTelemetryHeaders?: Record<string, string>,
) {
if (!this.tracer) {
return callback();
}

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

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

try {
if (activeTelemetryHeaders) {
const activeContext = this.propagation.extract(
this.contextManager.active(),
activeTelemetryHeaders,
);

return this.contextManager.with(activeContext, () => callback(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 assume contextManager.with will (for the Otel case), create a parent-child relationship for the spans?

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, in this case we need to call it like awaiting like this:

        return await this.contextManager.with(activeContext, () => callback(span));

The reason being that we want to catch potential exceptions thrown by the callback, without this await the exception will just bubble up and we loose the ability to record the exception in the tracer.

}

const telemetryHeaders: Record<string, string> = {};

this.propagation.inject(this.contextManager.active(), telemetryHeaders);
Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, so we are going to always inject the headers, hmm, we may need to give it a bit more of thought, not sure this is 100% correct.


return this.contextManager.with(
Copy link
Contributor

Choose a reason for hiding this comment

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

same issue here we need to await before returning.

this.setSpan(this.contextManager.active(), span),
() => callback(span, telemetryHeaders),
);
} catch (err) {
span.recordException(err as Error);
throw err;
} finally {
span.end();
}
}
}
Loading
Loading