forked from mtusi2026/mtusi2026lab6
lab6: инструментирование кода — HTTP, PostgreSQL, RabbitMQ (W3C trace context), S3, обработка задач, структурные логи
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user