|
| 1 | +import { |
| 2 | + OtelCollectorContainer, |
| 3 | + type StartedOtelCollectorContainer, |
| 4 | +} from "@internal/testcontainers"; |
| 5 | + |
| 6 | +import { metrics } from "@opentelemetry/api"; |
| 7 | +import { ExportResultCode } from "@opentelemetry/core"; |
| 8 | +import { |
| 9 | + MetricReader, |
| 10 | + type PushMetricExporter, |
| 11 | + type ResourceMetrics, |
| 12 | +} from "@opentelemetry/sdk-metrics"; |
| 13 | +import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 14 | +import { TracingSDK } from "./tracingSDK.js"; |
| 15 | + |
| 16 | +class NoopMetricExporter implements PushMetricExporter { |
| 17 | + forceFlushCount = 0; |
| 18 | + |
| 19 | + export(_metrics: ResourceMetrics, resultCallback: (result: { code: number }) => void): void { |
| 20 | + resultCallback({ code: ExportResultCode.SUCCESS }); |
| 21 | + } |
| 22 | + |
| 23 | + async forceFlush(): Promise<void> { |
| 24 | + this.forceFlushCount++; |
| 25 | + } |
| 26 | + |
| 27 | + async shutdown(): Promise<void> {} |
| 28 | +} |
| 29 | + |
| 30 | +describe("TracingSDK with an external metric exporter", () => { |
| 31 | + let collector: StartedOtelCollectorContainer; |
| 32 | + let tracingSDK: TracingSDK; |
| 33 | + |
| 34 | + beforeAll(async () => { |
| 35 | + collector = await new OtelCollectorContainer().start(); |
| 36 | + |
| 37 | + process.env.TRIGGER_OTEL_METRICS_COLLECTION_INTERVAL_MILLIS = "600000"; |
| 38 | + |
| 39 | + tracingSDK = new TracingSDK({ |
| 40 | + url: collector.getOtlpHttpUrl(), |
| 41 | + forceFlushTimeoutMillis: 30_000, |
| 42 | + diagLogLevel: "none", |
| 43 | + metricExporters: [new NoopMetricExporter()], |
| 44 | + hostMetrics: true, |
| 45 | + hostMetricGroups: ["process.cpu", "process.memory"], |
| 46 | + nodejsRuntimeMetrics: true, |
| 47 | + }); |
| 48 | + }, 180_000); |
| 49 | + |
| 50 | + afterAll(async () => { |
| 51 | + await tracingSDK?.shutdown(); |
| 52 | + await collector?.stop(); |
| 53 | + delete process.env.TRIGGER_OTEL_METRICS_COLLECTION_INTERVAL_MILLIS; |
| 54 | + }); |
| 55 | + |
| 56 | + it("flushes without the collector rejecting a batch containing a NaN reading", async () => { |
| 57 | + const gauge = metrics.getMeter("test").createObservableGauge("test.utilization"); |
| 58 | + gauge.addCallback((result) => result.observe(NaN)); |
| 59 | + |
| 60 | + await expect(tracingSDK.flush()).resolves.toBeUndefined(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("collects from each metric reader one at a time", async () => { |
| 64 | + let inFlight = 0; |
| 65 | + let maxInFlight = 0; |
| 66 | + |
| 67 | + const gauge = metrics.getMeter("test").createObservableGauge("test.concurrency"); |
| 68 | + gauge.addCallback(async (result) => { |
| 69 | + inFlight++; |
| 70 | + maxInFlight = Math.max(maxInFlight, inFlight); |
| 71 | + await new Promise((resolve) => setTimeout(resolve, 5)); |
| 72 | + result.observe(1); |
| 73 | + inFlight--; |
| 74 | + }); |
| 75 | + |
| 76 | + await tracingSDK.flush(); |
| 77 | + |
| 78 | + expect(maxInFlight).toBe(1); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +class FailingMetricReader extends MetricReader { |
| 83 | + protected async onForceFlush(): Promise<void> { |
| 84 | + throw new Error("reader flush failed"); |
| 85 | + } |
| 86 | + |
| 87 | + protected async onShutdown(): Promise<void> {} |
| 88 | +} |
| 89 | + |
| 90 | +class FailingShutdownMetricReader extends MetricReader { |
| 91 | + shutdownAttempts = 0; |
| 92 | + |
| 93 | + protected async onForceFlush(): Promise<void> {} |
| 94 | + |
| 95 | + protected async onShutdown(): Promise<void> { |
| 96 | + this.shutdownAttempts++; |
| 97 | + throw new Error(`reader shutdown failed (attempt ${this.shutdownAttempts})`); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +class RecordingMetricReader extends MetricReader { |
| 102 | + forceFlushCount = 0; |
| 103 | + shutdownCount = 0; |
| 104 | + |
| 105 | + protected async onForceFlush(): Promise<void> { |
| 106 | + this.forceFlushCount++; |
| 107 | + } |
| 108 | + |
| 109 | + protected async onShutdown(): Promise<void> { |
| 110 | + this.shutdownCount++; |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +function captureConsoleErrors(): { lines: string[]; restore: () => void } { |
| 115 | + const lines: string[] = []; |
| 116 | + const original = console.error; |
| 117 | + |
| 118 | + console.error = (...args: unknown[]) => { |
| 119 | + lines.push(args.map(String).join(" ")); |
| 120 | + }; |
| 121 | + |
| 122 | + return { lines, restore: () => (console.error = original) }; |
| 123 | +} |
| 124 | + |
| 125 | +describe("TracingSDK when one metric reader fails to flush", () => { |
| 126 | + let recordingReader: RecordingMetricReader; |
| 127 | + let tracingSDK: TracingSDK; |
| 128 | + |
| 129 | + beforeAll(() => { |
| 130 | + recordingReader = new RecordingMetricReader(); |
| 131 | + |
| 132 | + tracingSDK = new TracingSDK({ |
| 133 | + url: "http://localhost:1", |
| 134 | + forceFlushTimeoutMillis: 5_000, |
| 135 | + diagLogLevel: "none", |
| 136 | + metricReaders: [new FailingMetricReader(), recordingReader], |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + it("still flushes the readers after it", async () => { |
| 141 | + await tracingSDK.flush().catch(() => {}); |
| 142 | + |
| 143 | + expect(recordingReader.forceFlushCount).toBeGreaterThan(0); |
| 144 | + }); |
| 145 | + |
| 146 | + it("still reports the failure to the caller", async () => { |
| 147 | + await expect(tracingSDK.flush()).rejects.toThrow("reader flush failed"); |
| 148 | + }); |
| 149 | + |
| 150 | + it("logs the failure as it happens", async () => { |
| 151 | + const console = captureConsoleErrors(); |
| 152 | + |
| 153 | + await tracingSDK.flush().catch(() => {}); |
| 154 | + console.restore(); |
| 155 | + |
| 156 | + expect(console.lines.join("\n")).toContain("reader flush failed"); |
| 157 | + }); |
| 158 | +}); |
| 159 | + |
| 160 | +class OverlapRecordingMetricReader extends MetricReader { |
| 161 | + static inFlight = 0; |
| 162 | + static maxInFlight = 0; |
| 163 | + |
| 164 | + protected async onForceFlush(): Promise<void> {} |
| 165 | + |
| 166 | + protected async onShutdown(): Promise<void> { |
| 167 | + OverlapRecordingMetricReader.inFlight++; |
| 168 | + OverlapRecordingMetricReader.maxInFlight = Math.max( |
| 169 | + OverlapRecordingMetricReader.maxInFlight, |
| 170 | + OverlapRecordingMetricReader.inFlight |
| 171 | + ); |
| 172 | + await new Promise((resolve) => setTimeout(resolve, 5)); |
| 173 | + OverlapRecordingMetricReader.inFlight--; |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +describe("TracingSDK shutdown", () => { |
| 178 | + it("shuts down each metric reader one at a time", async () => { |
| 179 | + OverlapRecordingMetricReader.inFlight = 0; |
| 180 | + OverlapRecordingMetricReader.maxInFlight = 0; |
| 181 | + |
| 182 | + const tracingSDK = new TracingSDK({ |
| 183 | + url: "http://localhost:1", |
| 184 | + forceFlushTimeoutMillis: 5_000, |
| 185 | + diagLogLevel: "none", |
| 186 | + metricReaders: [new OverlapRecordingMetricReader(), new OverlapRecordingMetricReader()], |
| 187 | + }); |
| 188 | + |
| 189 | + await tracingSDK.shutdown().catch(() => {}); |
| 190 | + |
| 191 | + expect(OverlapRecordingMetricReader.maxInFlight).toBe(1); |
| 192 | + }); |
| 193 | + |
| 194 | + it("still shuts down the readers after one that fails", async () => { |
| 195 | + const recordingReader = new RecordingMetricReader(); |
| 196 | + |
| 197 | + const tracingSDK = new TracingSDK({ |
| 198 | + url: "http://localhost:1", |
| 199 | + forceFlushTimeoutMillis: 5_000, |
| 200 | + diagLogLevel: "none", |
| 201 | + metricReaders: [new FailingShutdownMetricReader(), recordingReader], |
| 202 | + }); |
| 203 | + |
| 204 | + await tracingSDK.shutdown().catch(() => {}); |
| 205 | + |
| 206 | + expect(recordingReader.shutdownCount).toBeGreaterThan(0); |
| 207 | + }); |
| 208 | + |
| 209 | + it("does not retry a metric reader that failed to shut down", async () => { |
| 210 | + const failingReader = new FailingShutdownMetricReader(); |
| 211 | + |
| 212 | + const tracingSDK = new TracingSDK({ |
| 213 | + url: "http://localhost:1", |
| 214 | + forceFlushTimeoutMillis: 5_000, |
| 215 | + diagLogLevel: "none", |
| 216 | + metricReaders: [failingReader], |
| 217 | + }); |
| 218 | + |
| 219 | + await tracingSDK.shutdown().catch(() => {}); |
| 220 | + |
| 221 | + expect(failingReader.shutdownAttempts).toBe(1); |
| 222 | + }); |
| 223 | + |
| 224 | + it("reports the original shutdown failure, not a later one", async () => { |
| 225 | + const tracingSDK = new TracingSDK({ |
| 226 | + url: "http://localhost:1", |
| 227 | + forceFlushTimeoutMillis: 5_000, |
| 228 | + diagLogLevel: "none", |
| 229 | + metricReaders: [new FailingShutdownMetricReader()], |
| 230 | + }); |
| 231 | + |
| 232 | + await expect(tracingSDK.shutdown()).rejects.toThrow("attempt 1"); |
| 233 | + }); |
| 234 | +}); |
0 commit comments