Перенесена кодовая база и инфраструктура lab6: NestJS-приложение (API +
worker, RabbitMQ, outbox, S3, OpenTelemetry), Dockerfile, compose.yaml,
infra/{grafana,haproxy,loki,minio,otel/collector.yaml,prometheus,
rabbitmq,tempo}, docs/runbooks, стендовые скрипты, scripts/lab{4,5,6} и
contracts/extensions.openapi.json.
Задание lab7 оставлено своим: README, REPORT, COURSE, CONTRIBUTING,
MAINTAINERS, lab.json, contracts/{openapi,infrastructure,jobs.schema,
attachment-states,examples,observability,trust-matrix,README},
security/THREAT_MODEL.md, tests (включая security.py), compose.example.yaml,
infra/{openbao,otel}/README.md, .github/pull_request_template.md.
Не переносились старые evidence/lab4..lab6 (18 МБ трека lab6) и
неотслеживаемый docs/lab6.html.
Адаптации при переносе:
- docker-проект mtusi-lab6 -> mtusi-lab7, тег образа mtusi-messenger:lab7
и папка Grafana Messenger lab7 (compose.yaml, scripts/stop-one.sh,
.env.example, infra/README.md, infra/grafana/provisioning/dashboards),
чтобы тома и контейнеры не пересекались с lab6;
- Makefile: сохранены BASE_URL=https://localhost:8443 и цель test-security
из skeleton lab7, добавлены стендовые и проверочные цели lab6;
- .env.example: рабочие переменные приложения и стенда lab6 + блок
переменных lab7 (PUBLIC_BASE_URL, TLS_*, BAO_*, ARGON2_*) из skeleton.
60 lines
1.7 KiB
TypeScript
60 lines
1.7 KiB
TypeScript
import {
|
|
CallHandler,
|
|
ExecutionContext,
|
|
Injectable,
|
|
NestInterceptor,
|
|
} from '@nestjs/common';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { HttpAdapterHost } from '@nestjs/core';
|
|
import { randomUUID } from 'node:crypto';
|
|
import { finalize, Observable } from 'rxjs';
|
|
import type { AppConfig } from '../../config/app-config.schema.js';
|
|
import { resolveInstanceId } from '../../config/instance-id.js';
|
|
import { MetricsService } from '../../metrics/metrics.service.js';
|
|
|
|
type RequestWithId = { requestId?: string };
|
|
|
|
@Injectable()
|
|
export class RequestIdInterceptor implements NestInterceptor {
|
|
// Resolved once: the identity must be stable for the whole process lifetime,
|
|
// otherwise a client cannot tell whether it moved to another replica.
|
|
private readonly instanceId: string;
|
|
|
|
constructor(
|
|
private readonly adapterHost: HttpAdapterHost,
|
|
private readonly metrics: MetricsService,
|
|
config: ConfigService<AppConfig>,
|
|
) {
|
|
this.instanceId = resolveInstanceId(config);
|
|
}
|
|
|
|
intercept(
|
|
context: ExecutionContext,
|
|
next: CallHandler,
|
|
): Observable<unknown> {
|
|
const request = context.switchToHttp().getRequest<RequestWithId>();
|
|
const response = context.switchToHttp().getResponse<unknown>();
|
|
const requestId = randomUUID();
|
|
const startedAt = performance.now();
|
|
request.requestId = requestId;
|
|
this.adapterHost.httpAdapter.setHeader(
|
|
response,
|
|
'X-Request-Id',
|
|
requestId,
|
|
);
|
|
this.adapterHost.httpAdapter.setHeader(
|
|
response,
|
|
'X-Instance-Id',
|
|
this.instanceId,
|
|
);
|
|
this.metrics.increment('request_count');
|
|
return next
|
|
.handle()
|
|
.pipe(
|
|
finalize(() =>
|
|
this.metrics.observeLatency(performance.now() - startedAt),
|
|
),
|
|
);
|
|
}
|
|
}
|