MCPcopy
hub / github.com/redis/node-redis / OTelCommandMetrics

Class OTelCommandMetrics

packages/client/lib/opentelemetry/metrics.ts:54–189  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

52}
53
54class OTelCommandMetrics implements IOTelCommandMetrics {
55 readonly #instruments: MetricInstruments;
56 readonly #options: MetricOptions;
57 readonly #metricsState = new WeakMap<object, CommandMetricsState>();
58 readonly #unsubscribers: Array<() => void> = [];
59
60 constructor(options: MetricOptions, instruments: MetricInstruments) {
61 this.#options = options;
62 this.#instruments = instruments;
63
64 this.#subscribeToTracingChannel();
65 }
66
67 #subscribeToTracingChannel() {
68 const commandTC = getTracingChannel(CHANNELS.TRACE_COMMAND);
69 const batchTC = getTracingChannel(CHANNELS.TRACE_BATCH);
70 if (!commandTC || !batchTC) return;
71
72 const onStart = (ctx: any) => {
73 const commandName = ctx.command?.toString() || "UNKNOWN";
74
75 if (this.#isCommandExcluded(commandName)) return;
76
77 this.#metricsState.set(ctx, {
78 startTime: performance.now(),
79 clientAttributes: resolveClientAttributes(ctx.clientId),
80 commandName,
81 });
82 };
83
84 const onAsyncEnd = (ctx: any) => {
85 const state = this.#metricsState.get(ctx);
86 if (!state) return;
87 this.#metricsState.delete(ctx);
88
89 this.#instruments.dbClientOperationDuration.record(
90 (performance.now() - state.startTime) / 1000,
91 {
92 ...this.#options.attributes,
93 [OTEL_ATTRIBUTES.dbNamespace]: state.clientAttributes?.db?.toString(),
94 [OTEL_ATTRIBUTES.serverAddress]: state.clientAttributes?.host,
95 [OTEL_ATTRIBUTES.serverPort]: state.clientAttributes?.port?.toString(),
96 [OTEL_ATTRIBUTES.dbOperationName]: state.commandName,
97 },
98 );
99 };
100
101 const onError = (ctx: any) => {
102 const state = this.#metricsState.get(ctx);
103 if (!state) return;
104 this.#metricsState.delete(ctx);
105
106 const errorInfo = getErrorInfo(ctx.error);
107 this.#instruments.dbClientOperationDuration.record(
108 (performance.now() - state.startTime) / 1000,
109 {
110 ...this.#options.attributes,
111 [OTEL_ATTRIBUTES.dbNamespace]: state.clientAttributes?.db?.toString(),

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected