feat(core-shared): wire IMetrics into DI bindings

This commit is contained in:
2026-05-11 12:01:01 +02:00
parent f2627890be
commit 0850711986
5 changed files with 31 additions and 8 deletions

View File

@@ -2,22 +2,29 @@
import type { Container } from "inversify"; import type { Container } from "inversify";
import { NoopTracer } from "../noop-tracer"; import { NoopTracer } from "../noop-tracer";
import { NoopLogger } from "../noop-logger"; import { NoopLogger } from "../noop-logger";
import { NoopMetrics } from "../noop-metrics";
import { INSTRUMENTATION_SYMBOLS } from "../symbols"; import { INSTRUMENTATION_SYMBOLS } from "../symbols";
import type { ITracer, ILogger } from "../index"; import type { ITracer, ILogger, IMetrics } from "../index";
export function bindNoopInstrumentation(container: Container): { export function bindNoopInstrumentation(container: Container): {
tracer: ITracer; tracer: ITracer;
logger: ILogger; logger: ILogger;
metrics: IMetrics;
} { } {
const tracer = new NoopTracer(); const tracer = new NoopTracer();
const logger = new NoopLogger(); const logger = new NoopLogger();
const metrics = new NoopMetrics();
if (container.isBound(INSTRUMENTATION_SYMBOLS.TRACER)) { if (container.isBound(INSTRUMENTATION_SYMBOLS.TRACER)) {
container.unbind(INSTRUMENTATION_SYMBOLS.TRACER); container.unbind(INSTRUMENTATION_SYMBOLS.TRACER);
} }
if (container.isBound(INSTRUMENTATION_SYMBOLS.LOGGER)) { if (container.isBound(INSTRUMENTATION_SYMBOLS.LOGGER)) {
container.unbind(INSTRUMENTATION_SYMBOLS.LOGGER); container.unbind(INSTRUMENTATION_SYMBOLS.LOGGER);
} }
if (container.isBound(INSTRUMENTATION_SYMBOLS.METRICS)) {
container.unbind(INSTRUMENTATION_SYMBOLS.METRICS);
}
container.bind<ITracer>(INSTRUMENTATION_SYMBOLS.TRACER).toConstantValue(tracer); container.bind<ITracer>(INSTRUMENTATION_SYMBOLS.TRACER).toConstantValue(tracer);
container.bind<ILogger>(INSTRUMENTATION_SYMBOLS.LOGGER).toConstantValue(logger); container.bind<ILogger>(INSTRUMENTATION_SYMBOLS.LOGGER).toConstantValue(logger);
return { tracer, logger }; container.bind<IMetrics>(INSTRUMENTATION_SYMBOLS.METRICS).toConstantValue(metrics);
return { tracer, logger, metrics };
} }

View File

@@ -2,9 +2,10 @@
import type { Container } from "inversify"; import type { Container } from "inversify";
import { OtelTracer } from "../otel/otel-tracer"; import { OtelTracer } from "../otel/otel-tracer";
import { OtelLogger } from "../otel/otel-logger"; import { OtelLogger } from "../otel/otel-logger";
import { OtelMetrics } from "../otel/otel-metrics";
import { initOtelServerNode } from "../otel/init-server-node"; import { initOtelServerNode } from "../otel/init-server-node";
import { INSTRUMENTATION_SYMBOLS } from "../symbols"; import { INSTRUMENTATION_SYMBOLS } from "../symbols";
import type { ITracer, ILogger } from "../index"; import type { ITracer, ILogger, IMetrics } from "../index";
export type BindOtelOpts = { export type BindOtelOpts = {
dsn: string; dsn: string;
@@ -15,7 +16,7 @@ export type BindOtelOpts = {
export function bindOtelInstrumentation( export function bindOtelInstrumentation(
container: Container, container: Container,
opts: BindOtelOpts, opts: BindOtelOpts,
): { tracer: ITracer; logger: ILogger } { ): { tracer: ITracer; logger: ILogger; metrics: IMetrics } {
const environment = const environment =
process.env["SENTRY_ENVIRONMENT"] ?? process.env["SENTRY_ENVIRONMENT"] ??
process.env["VERCEL_ENV"] ?? process.env["VERCEL_ENV"] ??
@@ -32,6 +33,7 @@ export function bindOtelInstrumentation(
const tracer = new OtelTracer(); const tracer = new OtelTracer();
const logger = new OtelLogger(); const logger = new OtelLogger();
const metrics = new OtelMetrics();
if (container.isBound(INSTRUMENTATION_SYMBOLS.TRACER)) { if (container.isBound(INSTRUMENTATION_SYMBOLS.TRACER)) {
container.unbind(INSTRUMENTATION_SYMBOLS.TRACER); container.unbind(INSTRUMENTATION_SYMBOLS.TRACER);
@@ -39,7 +41,11 @@ export function bindOtelInstrumentation(
if (container.isBound(INSTRUMENTATION_SYMBOLS.LOGGER)) { if (container.isBound(INSTRUMENTATION_SYMBOLS.LOGGER)) {
container.unbind(INSTRUMENTATION_SYMBOLS.LOGGER); container.unbind(INSTRUMENTATION_SYMBOLS.LOGGER);
} }
if (container.isBound(INSTRUMENTATION_SYMBOLS.METRICS)) {
container.unbind(INSTRUMENTATION_SYMBOLS.METRICS);
}
container.bind<ITracer>(INSTRUMENTATION_SYMBOLS.TRACER).toConstantValue(tracer); container.bind<ITracer>(INSTRUMENTATION_SYMBOLS.TRACER).toConstantValue(tracer);
container.bind<ILogger>(INSTRUMENTATION_SYMBOLS.LOGGER).toConstantValue(logger); container.bind<ILogger>(INSTRUMENTATION_SYMBOLS.LOGGER).toConstantValue(logger);
return { tracer, logger }; container.bind<IMetrics>(INSTRUMENTATION_SYMBOLS.METRICS).toConstantValue(metrics);
return { tracer, logger, metrics };
} }

View File

@@ -9,8 +9,10 @@ export type {
Breadcrumb, Breadcrumb,
CaptureContext, CaptureContext,
} from "./logger.interface"; } from "./logger.interface";
export type { IMetrics, MetricAttributeValue } from "./metrics.interface";
export { NoopTracer } from "./noop-tracer"; export { NoopTracer } from "./noop-tracer";
export { NoopLogger } from "./noop-logger"; export { NoopLogger } from "./noop-logger";
export { NoopMetrics } from "./noop-metrics";
export { withSpan } from "./with-span"; export { withSpan } from "./with-span";
export { withCapture } from "./with-capture"; export { withCapture } from "./with-capture";
export { isReported, markReported } from "./reported-flag"; export { isReported, markReported } from "./reported-flag";

View File

@@ -18,8 +18,9 @@ export type InitOtelServerNodeOpts = {
* Initializes the OpenTelemetry NodeSDK for a server-side app. * Initializes the OpenTelemetry NodeSDK for a server-side app.
* - Configures Resource attributes per OTel semantic conventions. * - Configures Resource attributes per OTel semantic conventions.
* - Registers Sentry span processor (via createSentryOtelBridge) when DSN is set. * - Registers Sentry span processor (via createSentryOtelBridge) when DSN is set.
* - Registers Sentry log record processor (Phase 3) when DSN is set. * - Registers Sentry log record processor when DSN is set.
* - metricReader filled in Phase 4. * - Registers an in-process MeterProvider (no exporter — Sentry metrics not yet
* wired; a future phase or vendor-specific exporter can add a MetricReader).
* - PII scrub processors land in Phase 5. * - PII scrub processors land in Phase 5.
* *
* Caller is responsible for `sdk.shutdown()` on process exit. * Caller is responsible for `sdk.shutdown()` on process exit.
@@ -42,11 +43,17 @@ export function initOtelServerNode(opts: InitOtelServerNodeOpts): NodeSDK {
// to avoid blocking the hot path). // to avoid blocking the hot path).
const logRecordProcessors = bridge.logRecordProcessor ? [bridge.logRecordProcessor] : []; const logRecordProcessors = bridge.logRecordProcessor ? [bridge.logRecordProcessor] : [];
// In-process MeterProvider with no reader/exporter. Sentry metrics ingestion
// is experimental in @sentry/opentelemetry 10.x and not wired here; metrics
// emit through the API but are not exported anywhere. A future phase can add
// a PeriodicExportingMetricReader when a vendor exporter is available.
const metricReader = undefined;
const sdk = new NodeSDK({ const sdk = new NodeSDK({
resource, resource,
spanProcessors, spanProcessors,
logRecordProcessors, logRecordProcessors,
// metricReader filled in Phase 4 metricReader,
}); });
sdk.start(); sdk.start();

View File

@@ -1,4 +1,5 @@
export const INSTRUMENTATION_SYMBOLS = { export const INSTRUMENTATION_SYMBOLS = {
TRACER: Symbol.for("core-shared.TRACER"), TRACER: Symbol.for("core-shared.TRACER"),
LOGGER: Symbol.for("core-shared.LOGGER"), LOGGER: Symbol.for("core-shared.LOGGER"),
METRICS: Symbol.for("core-shared.METRICS"),
} as const; } as const;