feat: add queue, pool, saturation metrics, slo and alert rules

This commit is contained in:
2026-11-20 13:24:31 +03:00
parent 307b658bfb
commit 3171835fff
10 changed files with 511 additions and 55 deletions
+12 -2
View File
@@ -1,5 +1,5 @@
import { Logger } from '@nestjs/common';
import { SpanStatusCode, trace } from '@opentelemetry/api';
import { metrics, SpanStatusCode, trace } from '@opentelemetry/api';
import { Attachment, AttachmentJob, PrismaClient } from '@prisma/client';
import { createHash, randomUUID } from 'crypto';
import {
@@ -7,7 +7,7 @@ import {
ObjectTooLargeError,
S3Service,
} from '../storage/s3.service';
import { JobMessage, PIPELINE_VERSION } from './job-message';
import { JOB_TYPE, JobMessage, PIPELINE_VERSION } from './job-message';
import { markFailed, retryOrFail } from './job-store';
import {
InvalidImageError,
@@ -52,6 +52,13 @@ class PermanentError extends Error {
class TimeoutError extends Error {}
const tracer = trace.getTracer('messenger-worker');
const timeToReady = metrics.getMeter('messenger-worker').createHistogram('attachments.time_to_ready', {
description: 'From durable upload acceptance to the committed ready state',
unit: 's',
advice: {
explicitBucketBoundaries: [0.1, 0.25, 0.5, 1, 2, 3, 5, 10, 20, 30, 60, 120, 300, 600],
},
});
export class JobProcessor {
private readonly logger = new Logger('JobProcessor');
@@ -225,6 +232,9 @@ export class JobProcessor {
this.logger.warn(`job ${lease.id}: lease lost before commit, result left to owner`);
return 'skipped';
}
timeToReady.record((now.getTime() - attachment.createdAt.getTime()) / 1000, {
'job.type': JOB_TYPE,
});
await this.hooks.afterCommit?.();
await this.removeStaging(attachment);
return 'ready';