Files
Edu/services/parent-bff/src/shared/observability/tracer.ts
SpecialX 2229309a1e feat: initialize parent-bff service with full core features
add complete parent-bff implementation including:
- GraphQL endpoint with depth/cost validation
- ChildGuard越权校验 with redis cache and singleflight
- parallel orchestration with partial failure fallback
- three-level cache fallback strategy (Redis + LRU + downstream)
- Kafka consumer for cache invalidation and notification push
- opossum circuit breaker for downstream services
- Prometheus metrics and SLO alerts
- Helm chart for k8s deployment with multi-environment configs
- Grafana dashboard for observability
- complete unit and integration tests
2026-07-10 18:49:06 +08:00

42 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { getNodeAutoInstrumentations } from "@opentelemetry/auto-instrumentations-node";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-http";
import { NodeSDK } from "@opentelemetry/sdk-node";
import { env } from "../../config/env.js";
/**
* parent-bff OpenTelemetry 链路追踪初始化。
*
* 对齐 02-architecture-design.md §6.5
* - serviceName: parent-bff
* - exporter: OTLP HTTP → collector
* - auto-instrumentations: http, nestjs-core, express, ioredis, @grpc/grpc-js, kafka-node
* - 采样率:生产 10%,开发 100%(通过 OTEL_TRACES_SAMPLER_ARG 控制)
*
* span 属性parentId, childId, operation, downstream.service, cache.hit, childGuard.blocked
*/
let sdk: NodeSDK | null = null;
export function initTracer(): void {
if (!env.OTEL_EXPORTER_OTLP_ENDPOINT) return;
sdk = new NodeSDK({
serviceName: env.OTEL_SERVICE_NAME,
traceExporter: new OTLPTraceExporter({
url: `${env.OTEL_EXPORTER_OTLP_ENDPOINT}/v1/traces`,
}),
instrumentations: [getNodeAutoInstrumentations()],
});
sdk.start();
console.log(
`[parent-bff] Tracer initialized with auto-instrumentations, endpoint=${env.OTEL_EXPORTER_OTLP_ENDPOINT}`,
);
}
export async function shutdownTracer(): Promise<void> {
if (sdk) {
await sdk.shutdown();
sdk = null;
}
}