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

chore: improvements to OTEL system #752

Merged
merged 1 commit into from
Aug 28, 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
4 changes: 1 addition & 3 deletions apps/mail-bridge/queue/mail-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,7 @@ export const worker = createWorker<MailProcessorJobData>(
tracer.startActiveSpan('Mail Processor', async (span) => {
try {
span?.setAttributes({
'job.id': job.id,
'job.data': JSON.stringify(job.data)
'job.id': job.id
});

const { rawMessage, params } = job.data;
Expand Down Expand Up @@ -1034,7 +1033,6 @@ export const worker = createWorker<MailProcessorJobData>(
? e.message
: 'Unknown Error in Mail Processor, Check Logs'
);
span?.recordException(e as Error);
// Throw the error to be caught by the worker, and moving to failed jobs
throw e;
}
Expand Down
24 changes: 11 additions & 13 deletions packages/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,25 @@ if (opentelemetryEnabled) {
const originalExecute = Connection.prototype.execute;

Connection.prototype.execute = async function (query, args, options) {
return databaseTracer.startActiveSpan(`Database Query`, async (span) => {
return databaseTracer.startActiveSpan(`Database Call`, async (span) => {
if (span) {
span.addEvent('database.query.start');
span.setAttribute('database.statement', query);
span.addEvent('db.call.start');
span.setAttribute('db.statement', query);
if (Array.isArray(args)) {
span.setAttribute(
'database.values',
args.map(
(v: string | null | number) => v?.toString?.() ?? 'Unknown'
)
'db.values',
args.map((v: string | null | number) => {
if (v === null) return 'null';
if (typeof v === 'undefined') return 'undefined';
return v?.toString?.() ?? 'Unknown';
})
);
}
}
const result = await originalExecute
// @ts-expect-error, don't care about types here
.call(this, query, args, options)
.catch((err: Error) => {
span?.recordException(err);
throw err;
});
span?.addEvent('database.query.end');
.call(this, query, args, options);
span?.addEvent('db.call.end');
return result;
});
};
Expand Down
22 changes: 7 additions & 15 deletions packages/otel/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,18 @@ const { trace } = opentelemetryEnabled
? await import('@opentelemetry/api')
: { trace: undefined };

const { wrapTracer } = opentelemetryEnabled
? await import('@opentelemetry/api/experimental')
: { wrapTracer: undefined };

export function getTracer(name: string) {
if (!trace)
if (!trace || !wrapTracer)
return {
startActiveSpan: <Fn>(name: string, fn: (span?: Span) => Fn) => fn()
};

const tracer = trace.getTracer(name);
const tracer = wrapTracer(trace.getTracer(name));
return {
startActiveSpan<Fn>(name: string, fn: (span?: Span) => Fn) {
return tracer.startActiveSpan(name, (span) => {
const result = fn(span);
if (result instanceof Promise) {
void result.finally(() => span.end());
return result;
} else {
span.end();
return result;
}
});
}
startActiveSpan: tracer.withActiveSpan.bind(tracer)
};
}

Expand Down
3 changes: 2 additions & 1 deletion packages/otel/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const setupOpentelemetry = ({
name: string;
version: string;
}) => {
console.info(`Setting up OpenTelemetry for ${name}@${version}`);
const resource = new Resource({
'service.name': name,
'service.version': version
Expand Down Expand Up @@ -52,8 +53,8 @@ export const setupOpentelemetry = ({

registerInstrumentations({
instrumentations: [
new UndiciInstrumentation(),
new HttpInstrumentation(),
new UndiciInstrumentation(),
new WinstonInstrumentation()
]
});
Expand Down
Loading