-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(core): stop custom metric exporters breaking the metrics export #4613
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+544
−8
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@trigger.dev/core": patch | ||
| --- | ||
|
|
||
| Task metrics no longer go missing for projects that configure their own `metricExporters` or `metricReaders`, and the flush error that came with it is gone. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import type { StartedTestContainer } from "testcontainers"; | ||
| import { AbstractStartedContainer, GenericContainer, Wait } from "testcontainers"; | ||
|
|
||
| const OTLP_HTTP_PORT = 4318; | ||
| const CONFIG_PATH = "/etc/otelcol-config.yaml"; | ||
|
|
||
| const CONFIG = `receivers: | ||
| otlp: | ||
| protocols: | ||
| http: | ||
| endpoint: 0.0.0.0:${OTLP_HTTP_PORT} | ||
| exporters: | ||
| debug: {} | ||
| service: | ||
| telemetry: | ||
| logs: | ||
| level: WARN | ||
| pipelines: | ||
| traces: | ||
| receivers: [otlp] | ||
| exporters: [debug] | ||
| metrics: | ||
| receivers: [otlp] | ||
| exporters: [debug] | ||
| logs: | ||
| receivers: [otlp] | ||
| exporters: [debug] | ||
| `; | ||
|
|
||
| export class OtelCollectorContainer extends GenericContainer { | ||
| constructor( | ||
| image = "otel/opentelemetry-collector-k8s:0.158.0@sha256:c09130a633196a5becee164411473a0932ecf223f94fda6dab5f22798ff9f376" | ||
| ) { | ||
| super(image); | ||
| this.withExposedPorts(OTLP_HTTP_PORT); | ||
| this.withCopyContentToContainer([{ content: CONFIG, target: CONFIG_PATH }]); | ||
| this.withCommand([`--config=${CONFIG_PATH}`]); | ||
| this.withWaitStrategy(Wait.forHttp("/v1/metrics", OTLP_HTTP_PORT).forStatusCode(405)); | ||
| this.withStartupTimeout(120_000); | ||
| } | ||
|
|
||
| public override async start(): Promise<StartedOtelCollectorContainer> { | ||
| return new StartedOtelCollectorContainer(await super.start()); | ||
| } | ||
| } | ||
|
|
||
| export class StartedOtelCollectorContainer extends AbstractStartedContainer { | ||
| constructor(startedTestContainer: StartedTestContainer) { | ||
| super(startedTestContainer); | ||
| } | ||
|
|
||
| public getPort(): number { | ||
| return super.getMappedPort(OTLP_HTTP_PORT); | ||
| } | ||
|
|
||
| /** | ||
| * Base URL for OTLP/HTTP, without a signal path. | ||
| * Example: `http://localhost:32768` | ||
| */ | ||
| public getOtlpHttpUrl(): string { | ||
| return `http://${this.getHost()}:${this.getPort()}`; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,234 @@ | ||
| import { | ||
| OtelCollectorContainer, | ||
| type StartedOtelCollectorContainer, | ||
| } from "@internal/testcontainers"; | ||
|
|
||
| import { metrics } from "@opentelemetry/api"; | ||
| import { ExportResultCode } from "@opentelemetry/core"; | ||
| import { | ||
| MetricReader, | ||
| type PushMetricExporter, | ||
| type ResourceMetrics, | ||
| } from "@opentelemetry/sdk-metrics"; | ||
| import { afterAll, beforeAll, describe, expect, it } from "vitest"; | ||
| import { TracingSDK } from "./tracingSDK.js"; | ||
|
|
||
| class NoopMetricExporter implements PushMetricExporter { | ||
| forceFlushCount = 0; | ||
|
|
||
| export(_metrics: ResourceMetrics, resultCallback: (result: { code: number }) => void): void { | ||
| resultCallback({ code: ExportResultCode.SUCCESS }); | ||
| } | ||
|
|
||
| async forceFlush(): Promise<void> { | ||
| this.forceFlushCount++; | ||
| } | ||
|
|
||
| async shutdown(): Promise<void> {} | ||
| } | ||
|
|
||
| describe("TracingSDK with an external metric exporter", () => { | ||
| let collector: StartedOtelCollectorContainer; | ||
| let tracingSDK: TracingSDK; | ||
|
|
||
| beforeAll(async () => { | ||
| collector = await new OtelCollectorContainer().start(); | ||
|
|
||
| process.env.TRIGGER_OTEL_METRICS_COLLECTION_INTERVAL_MILLIS = "600000"; | ||
|
|
||
| tracingSDK = new TracingSDK({ | ||
| url: collector.getOtlpHttpUrl(), | ||
| forceFlushTimeoutMillis: 30_000, | ||
| diagLogLevel: "none", | ||
| metricExporters: [new NoopMetricExporter()], | ||
| hostMetrics: true, | ||
| hostMetricGroups: ["process.cpu", "process.memory"], | ||
| nodejsRuntimeMetrics: true, | ||
| }); | ||
| }, 180_000); | ||
|
|
||
| afterAll(async () => { | ||
| await tracingSDK?.shutdown(); | ||
| await collector?.stop(); | ||
| delete process.env.TRIGGER_OTEL_METRICS_COLLECTION_INTERVAL_MILLIS; | ||
| }); | ||
|
|
||
| it("flushes without the collector rejecting a batch containing a NaN reading", async () => { | ||
| const gauge = metrics.getMeter("test").createObservableGauge("test.utilization"); | ||
| gauge.addCallback((result) => result.observe(NaN)); | ||
|
|
||
| await expect(tracingSDK.flush()).resolves.toBeUndefined(); | ||
| }); | ||
|
|
||
| it("collects from each metric reader one at a time", async () => { | ||
| let inFlight = 0; | ||
| let maxInFlight = 0; | ||
|
|
||
| const gauge = metrics.getMeter("test").createObservableGauge("test.concurrency"); | ||
| gauge.addCallback(async (result) => { | ||
| inFlight++; | ||
| maxInFlight = Math.max(maxInFlight, inFlight); | ||
| await new Promise((resolve) => setTimeout(resolve, 5)); | ||
| result.observe(1); | ||
| inFlight--; | ||
| }); | ||
|
|
||
| await tracingSDK.flush(); | ||
|
|
||
| expect(maxInFlight).toBe(1); | ||
| }); | ||
| }); | ||
|
|
||
| class FailingMetricReader extends MetricReader { | ||
| protected async onForceFlush(): Promise<void> { | ||
| throw new Error("reader flush failed"); | ||
| } | ||
|
|
||
| protected async onShutdown(): Promise<void> {} | ||
| } | ||
|
|
||
| class FailingShutdownMetricReader extends MetricReader { | ||
| shutdownAttempts = 0; | ||
|
|
||
| protected async onForceFlush(): Promise<void> {} | ||
|
|
||
| protected async onShutdown(): Promise<void> { | ||
| this.shutdownAttempts++; | ||
| throw new Error(`reader shutdown failed (attempt ${this.shutdownAttempts})`); | ||
| } | ||
| } | ||
|
|
||
| class RecordingMetricReader extends MetricReader { | ||
| forceFlushCount = 0; | ||
| shutdownCount = 0; | ||
|
|
||
| protected async onForceFlush(): Promise<void> { | ||
| this.forceFlushCount++; | ||
| } | ||
|
|
||
| protected async onShutdown(): Promise<void> { | ||
| this.shutdownCount++; | ||
| } | ||
| } | ||
|
|
||
| function captureConsoleErrors(): { lines: string[]; restore: () => void } { | ||
| const lines: string[] = []; | ||
| const original = console.error; | ||
|
|
||
| console.error = (...args: unknown[]) => { | ||
| lines.push(args.map(String).join(" ")); | ||
| }; | ||
|
|
||
| return { lines, restore: () => (console.error = original) }; | ||
| } | ||
|
|
||
| describe("TracingSDK when one metric reader fails to flush", () => { | ||
| let recordingReader: RecordingMetricReader; | ||
| let tracingSDK: TracingSDK; | ||
|
|
||
| beforeAll(() => { | ||
| recordingReader = new RecordingMetricReader(); | ||
|
|
||
| tracingSDK = new TracingSDK({ | ||
| url: "http://localhost:1", | ||
| forceFlushTimeoutMillis: 5_000, | ||
| diagLogLevel: "none", | ||
| metricReaders: [new FailingMetricReader(), recordingReader], | ||
| }); | ||
| }); | ||
|
|
||
| it("still flushes the readers after it", async () => { | ||
| await tracingSDK.flush().catch(() => {}); | ||
|
|
||
| expect(recordingReader.forceFlushCount).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| it("still reports the failure to the caller", async () => { | ||
| await expect(tracingSDK.flush()).rejects.toThrow("reader flush failed"); | ||
| }); | ||
|
nicktrn marked this conversation as resolved.
|
||
|
|
||
| it("logs the failure as it happens", async () => { | ||
| const console = captureConsoleErrors(); | ||
|
|
||
| await tracingSDK.flush().catch(() => {}); | ||
| console.restore(); | ||
|
|
||
| expect(console.lines.join("\n")).toContain("reader flush failed"); | ||
| }); | ||
| }); | ||
|
|
||
| class OverlapRecordingMetricReader extends MetricReader { | ||
| static inFlight = 0; | ||
| static maxInFlight = 0; | ||
|
|
||
| protected async onForceFlush(): Promise<void> {} | ||
|
|
||
| protected async onShutdown(): Promise<void> { | ||
| OverlapRecordingMetricReader.inFlight++; | ||
| OverlapRecordingMetricReader.maxInFlight = Math.max( | ||
| OverlapRecordingMetricReader.maxInFlight, | ||
| OverlapRecordingMetricReader.inFlight | ||
| ); | ||
| await new Promise((resolve) => setTimeout(resolve, 5)); | ||
| OverlapRecordingMetricReader.inFlight--; | ||
| } | ||
| } | ||
|
|
||
| describe("TracingSDK shutdown", () => { | ||
| it("shuts down each metric reader one at a time", async () => { | ||
| OverlapRecordingMetricReader.inFlight = 0; | ||
| OverlapRecordingMetricReader.maxInFlight = 0; | ||
|
|
||
| const tracingSDK = new TracingSDK({ | ||
| url: "http://localhost:1", | ||
| forceFlushTimeoutMillis: 5_000, | ||
| diagLogLevel: "none", | ||
| metricReaders: [new OverlapRecordingMetricReader(), new OverlapRecordingMetricReader()], | ||
| }); | ||
|
|
||
| await tracingSDK.shutdown().catch(() => {}); | ||
|
|
||
| expect(OverlapRecordingMetricReader.maxInFlight).toBe(1); | ||
| }); | ||
|
|
||
| it("still shuts down the readers after one that fails", async () => { | ||
| const recordingReader = new RecordingMetricReader(); | ||
|
|
||
| const tracingSDK = new TracingSDK({ | ||
| url: "http://localhost:1", | ||
| forceFlushTimeoutMillis: 5_000, | ||
| diagLogLevel: "none", | ||
| metricReaders: [new FailingShutdownMetricReader(), recordingReader], | ||
| }); | ||
|
|
||
| await tracingSDK.shutdown().catch(() => {}); | ||
|
|
||
| expect(recordingReader.shutdownCount).toBeGreaterThan(0); | ||
| }); | ||
|
|
||
| it("does not retry a metric reader that failed to shut down", async () => { | ||
| const failingReader = new FailingShutdownMetricReader(); | ||
|
|
||
| const tracingSDK = new TracingSDK({ | ||
| url: "http://localhost:1", | ||
| forceFlushTimeoutMillis: 5_000, | ||
| diagLogLevel: "none", | ||
| metricReaders: [failingReader], | ||
| }); | ||
|
|
||
| await tracingSDK.shutdown().catch(() => {}); | ||
|
|
||
| expect(failingReader.shutdownAttempts).toBe(1); | ||
| }); | ||
|
|
||
| it("reports the original shutdown failure, not a later one", async () => { | ||
| const tracingSDK = new TracingSDK({ | ||
| url: "http://localhost:1", | ||
| forceFlushTimeoutMillis: 5_000, | ||
| diagLogLevel: "none", | ||
| metricReaders: [new FailingShutdownMetricReader()], | ||
| }); | ||
|
|
||
| await expect(tracingSDK.shutdown()).rejects.toThrow("attempt 1"); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.