lab6: инструментирование кода — HTTP, PostgreSQL, RabbitMQ (W3C trace context), S3, обработка задач, структурные логи

This commit is contained in:
Ilya
2026-11-09 20:38:40 +03:00
parent cf87f4dd43
commit 63dba7627c
11 changed files with 613 additions and 64 deletions
+16 -3
View File
@@ -9,11 +9,20 @@ import {
import { HttpAdapterHost } from '@nestjs/core';
import { ConfigService } from '@nestjs/config';
import { randomUUID } from 'node:crypto';
import { trace } from '@opentelemetry/api';
import type { AppConfig } from '../../config/app-config.schema.js';
import { resolveInstanceId } from '../../config/instance-id.js';
import { routeTemplate } from '../../telemetry/http.js';
import { errorType } from '../../telemetry/tracing.js';
import type { ApiErrorBody } from './api-error.js';
type RequestWithId = { requestId?: string; method?: string; url?: string };
type RequestWithId = {
requestId?: string;
method?: string;
url?: string;
// Fastify route template: logs must not carry raw URLs (they hold ids).
routeOptions?: { url?: string };
};
type ExceptionBody = {
message?: unknown;
challenge?: unknown;
@@ -146,11 +155,11 @@ export class ApiExceptionFilter implements ExceptionFilter {
status = HttpStatus.SERVICE_UNAVAILABLE;
message = 'Service unavailable';
this.logger.warn(
`Dependency outage on ${request.method ?? ''} ${request.url ?? ''}: ${exception instanceof Error ? exception.message : 'unknown error'}`,
`Dependency outage on ${request.method ?? ''} ${routeTemplate(request.routeOptions?.url)}: ${exception instanceof Error ? exception.message : 'unknown error'}`,
);
} else {
this.logger.error(
`Unhandled exception on ${request.method ?? ''} ${request.url ?? ''}`,
`Unhandled exception on ${request.method ?? ''} ${routeTemplate(request.routeOptions?.url)}`,
exception instanceof Error
? exception.stack
: String(exception),
@@ -176,6 +185,10 @@ export class ApiExceptionFilter implements ExceptionFilter {
request_id: requestId,
},
};
if (status >= HttpStatus.INTERNAL_SERVER_ERROR)
trace
.getActiveSpan()
?.setAttribute('error.type', errorType(exception));
adapter.reply(response, payload, status);
}
}