Перенесено дерево lab4 целиком: NestJS-приложение с вложениями и S3,
Dockerfile, compose.yaml, infra/{haproxy,minio}, стендовые скрипты,
scripts/lab4, evidence/lab4 и contracts/extensions.openapi.json.
Задание lab5 оставлено своим: README, REPORT, COURSE, lab.json,
contracts/{openapi,infrastructure,jobs.schema,attachment-states,examples},
tests, compose.example.yaml, .github/pull_request_template.md.
Адаптации при переносе:
- docker-проект mtusi-lab4 -> mtusi-lab5 и тег образа mtusi-messenger:lab5,
чтобы тома и контейнеры не пересекались с lab4 (scripts/stop-one.sh:
LAB_PROJECT вместо LAB4_PROJECT);
- Makefile: цели lab5 сохранены, добавлены стендовые (stand-up/down,
wait-ready, init-env, test-e2e, test-dependencies, s3-security,
consistency, bench);
- .env.example: реальные переменные приложения и стенда lab4 (без них
compose.yaml не поднимается) + блок lab5 (BROKER_URL_FILE, WORKER_*).
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),
|
|
),
|
|
);
|
|
}
|
|
}
|