forked from mtusi2026/mtusi2026lab6
feat: instrument api, relay and worker with opentelemetry
This commit is contained in:
+31
-2
@@ -1,9 +1,18 @@
|
||||
import { context, propagation } from '@opentelemetry/api';
|
||||
import { validate as isUuid } from '../common/uuid';
|
||||
|
||||
export const PIPELINE_VERSION = 1;
|
||||
export const JOB_TYPE = 'attachment.process.v1';
|
||||
|
||||
export interface JobMessage {
|
||||
const TRACEPARENT = /^00-[0-9a-f]{32}-[0-9a-f]{16}-[0-9a-f]{2}$/;
|
||||
const TRACESTATE_MAX = 512;
|
||||
|
||||
export interface TraceCarrier {
|
||||
traceparent?: string;
|
||||
tracestate?: string;
|
||||
}
|
||||
|
||||
export interface JobMessage extends TraceCarrier {
|
||||
schema_version: 1;
|
||||
job_id: string;
|
||||
type: typeof JOB_TYPE;
|
||||
@@ -21,6 +30,8 @@ const FIELDS = [
|
||||
'pipeline_version',
|
||||
'attempt',
|
||||
'created_at',
|
||||
'traceparent',
|
||||
'tracestate',
|
||||
];
|
||||
|
||||
export function jobMessage(
|
||||
@@ -35,9 +46,23 @@ export function jobMessage(
|
||||
pipeline_version: job.pipelineVersion,
|
||||
attempt: job.attempt,
|
||||
created_at: createdAt.toISOString(),
|
||||
...currentTraceCarrier(),
|
||||
};
|
||||
}
|
||||
|
||||
export function currentTraceCarrier(): TraceCarrier {
|
||||
const carrier: Record<string, string> = {};
|
||||
propagation.inject(context.active(), carrier);
|
||||
const result: TraceCarrier = {};
|
||||
if (TRACEPARENT.test(carrier.traceparent ?? '')) {
|
||||
result.traceparent = carrier.traceparent;
|
||||
if (carrier.tracestate && carrier.tracestate.length <= TRACESTATE_MAX) {
|
||||
result.tracestate = carrier.tracestate;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
export function parseJobMessage(raw: Buffer): JobMessage | null {
|
||||
let value: any;
|
||||
try {
|
||||
@@ -61,7 +86,11 @@ export function parseJobMessage(raw: Buffer): JobMessage | null {
|
||||
!Number.isInteger(value.attempt) ||
|
||||
value.attempt < 1 ||
|
||||
typeof value.created_at !== 'string' ||
|
||||
Number.isNaN(Date.parse(value.created_at))
|
||||
Number.isNaN(Date.parse(value.created_at)) ||
|
||||
(value.traceparent !== undefined &&
|
||||
(typeof value.traceparent !== 'string' || !TRACEPARENT.test(value.traceparent))) ||
|
||||
(value.tracestate !== undefined &&
|
||||
(typeof value.tracestate !== 'string' || value.tracestate.length > TRACESTATE_MAX))
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
+18
-5
@@ -1,4 +1,5 @@
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { SpanStatusCode, trace } from '@opentelemetry/api';
|
||||
import { Attachment, AttachmentJob, PrismaClient } from '@prisma/client';
|
||||
import { createHash, randomUUID } from 'crypto';
|
||||
import {
|
||||
@@ -50,6 +51,8 @@ class PermanentError extends Error {
|
||||
|
||||
class TimeoutError extends Error {}
|
||||
|
||||
const tracer = trace.getTracer('messenger-worker');
|
||||
|
||||
export class JobProcessor {
|
||||
private readonly logger = new Logger('JobProcessor');
|
||||
|
||||
@@ -141,11 +144,21 @@ export class JobProcessor {
|
||||
}
|
||||
let variants: RenderedVariant[];
|
||||
try {
|
||||
variants = await renderVariants(
|
||||
original,
|
||||
this.options.maxPixels,
|
||||
Math.ceil(this.options.timeoutMs / 1000),
|
||||
);
|
||||
variants = await tracer.startActiveSpan('attachment.render_variants', async (span) => {
|
||||
span.setAttribute('messenger.image.bytes', original.length);
|
||||
try {
|
||||
return await renderVariants(
|
||||
original,
|
||||
this.options.maxPixels,
|
||||
Math.ceil(this.options.timeoutMs / 1000),
|
||||
);
|
||||
} catch (error) {
|
||||
span.setStatus({ code: SpanStatusCode.ERROR, message: error instanceof Error ? error.name : 'error' });
|
||||
throw error;
|
||||
} finally {
|
||||
span.end();
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof InvalidImageError) {
|
||||
throw new PermanentError('invalid_image', 'image cannot be decoded');
|
||||
|
||||
Reference in New Issue
Block a user