-
Notifications
You must be signed in to change notification settings - Fork 43
feat(telemetry): add OTLP export writer #961
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
Open
EhabY
wants to merge
9
commits into
main
Choose a base branch
from
feat/issue-903-export-telemetry-otlp-writer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3adc419
feat(telemetry): add OTLP export writer
EhabY 3d63cd1
refactor(telemetry): tighten OTLP export writer and tests
EhabY e100618
fix(telemetry): make OTLP export ingestable by Prom-family backends
EhabY 995126f
refactor(telemetry): split OTLP writer into per-layer modules
EhabY 850fc7d
refactor(telemetry): tighten OTLP writer for readability
EhabY d83e697
fix(telemetry): address OTLP export writer review feedback
EhabY f14cdb2
test(telemetry): tighten OTLP writer test coverage
EhabY addc6b9
fix(telemetry): address OTLP export writer review findings
EhabY aa47ba1
test(telemetry): add OTLP golden-file coverage, address review comments
EhabY 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,77 @@ | ||
| import type { TelemetryEvent } from "../event"; | ||
|
|
||
| export interface MetricMeasurement { | ||
| readonly name: string; | ||
| readonly value: number; | ||
| readonly kind: "gauge" | "counter"; | ||
| /** OTel/UCUM unit (e.g. "ms", "Mbit/s", "{request}", "1"). */ | ||
| readonly unit: string; | ||
| } | ||
|
|
||
| /** | ||
| * `windowSeconds` is set on windowed events (`http.requests`) and absent on | ||
| * point-in-time samples; exporters use it to stamp gauge start times and | ||
| * anchor cumulative counters. | ||
| */ | ||
| export interface MetricDescriptor { | ||
| readonly windowSeconds?: number; | ||
| readonly measurements: readonly MetricMeasurement[]; | ||
| } | ||
|
|
||
| const METRIC_EVENT_NAMES: ReadonlySet<string> = new Set([ | ||
| "http.requests", | ||
| "ssh.network.sampled", | ||
| ]); | ||
|
|
||
| const UNIT_SUFFIXES: ReadonlyArray<readonly [string, string]> = [ | ||
| ["_ms", "ms"], | ||
| ["Ms", "ms"], | ||
| ["Mbits", "Mbit/s"], | ||
| ]; | ||
|
|
||
| export function isMetricEvent(event: TelemetryEvent): boolean { | ||
| return METRIC_EVENT_NAMES.has(event.eventName); | ||
| } | ||
|
|
||
| export function describeMetricEvent( | ||
| event: TelemetryEvent, | ||
| ): MetricDescriptor | undefined { | ||
| if (!isMetricEvent(event)) { | ||
| return undefined; | ||
| } | ||
| if (event.eventName === "http.requests") { | ||
| return describeHttpRequests(event); | ||
| } | ||
| return { | ||
| measurements: Object.entries(event.measurements).map(([name, value]) => ({ | ||
| name, | ||
| value, | ||
| kind: "gauge", | ||
| unit: measurementUnit(name), | ||
| })), | ||
| }; | ||
| } | ||
|
|
||
| function describeHttpRequests(event: TelemetryEvent): MetricDescriptor { | ||
| let windowSeconds = 0; | ||
| const measurements: MetricMeasurement[] = []; | ||
| for (const [name, value] of Object.entries(event.measurements)) { | ||
| if (name === "window_seconds") { | ||
| windowSeconds = value; | ||
| } else if (name.startsWith("count.")) { | ||
| measurements.push({ name, value, kind: "counter", unit: "{request}" }); | ||
| } else { | ||
| measurements.push({ | ||
| name, | ||
| value, | ||
| kind: "gauge", | ||
| unit: measurementUnit(name), | ||
| }); | ||
| } | ||
| } | ||
| return { windowSeconds, measurements }; | ||
| } | ||
|
|
||
| function measurementUnit(name: string): string { | ||
| return UNIT_SUFFIXES.find(([suffix]) => name.endsWith(suffix))?.[1] ?? "1"; | ||
| } |
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,77 @@ | ||
| import { createWriteStream } from "node:fs"; | ||
|
|
||
| import { wrapError } from "../../../../error/errorUtils"; | ||
|
|
||
| /** `append` is not re-entrant. */ | ||
| export interface EnvelopeFile { | ||
| append(value: unknown): Promise<void>; | ||
| close(): Promise<void>; | ||
| } | ||
|
|
||
| /** Streams `<prefix>v1,v2,...<suffix>` JSON into `filePath`. */ | ||
| export async function openEnvelopeFile( | ||
| filePath: string, | ||
| prefix: string, | ||
| suffix: string, | ||
| ): Promise<EnvelopeFile> { | ||
| const stream = createWriteStream(filePath, { encoding: "utf8" }); | ||
| // Open failures (ENOENT/EACCES) surface as 'error' events, not write | ||
| // callbacks; capture them so pending operations reject instead of hanging. | ||
| const errRef: { current?: Error } = {}; | ||
| stream.once("error", (err) => { | ||
| errRef.current ??= err; | ||
| }); | ||
|
|
||
| const awaitOp = (op: (cb: (err?: Error | null) => void) => void) => | ||
| new Promise<void>((resolve, reject) => { | ||
| if (errRef.current) { | ||
| reject(errRef.current); | ||
| return; | ||
| } | ||
| op((err) => { | ||
| const failure = err ?? errRef.current; | ||
| if (failure) { | ||
| reject(failure); | ||
| } else { | ||
| resolve(); | ||
| } | ||
| }); | ||
| }); | ||
|
|
||
| const writeChunk = (chunk: string) => | ||
| awaitOp((cb) => stream.write(chunk, "utf8", cb)); | ||
|
|
||
| try { | ||
| await writeChunk(prefix); | ||
| } catch (err) { | ||
|
EhabY marked this conversation as resolved.
|
||
| stream.destroy(); | ||
| throw wrapError("write", filePath, err); | ||
| } | ||
| let written = 0; | ||
| let closed = false; | ||
| return { | ||
| async append(value) { | ||
| try { | ||
| await writeChunk((written === 0 ? "" : ",") + JSON.stringify(value)); | ||
| } catch (err) { | ||
| throw wrapError("write", filePath, err); | ||
| } | ||
| written += 1; | ||
| }, | ||
| async close() { | ||
| if (closed) { | ||
| return; | ||
|
EhabY marked this conversation as resolved.
|
||
| } | ||
| closed = true; | ||
| try { | ||
| await writeChunk(suffix); | ||
| await awaitOp((cb) => stream.end(cb)); | ||
| } catch (err) { | ||
| // destroy() never throws synchronously, so it can't mask the | ||
| // rethrown error; any teardown failure routes to the 'error' event. | ||
| stream.destroy(); | ||
|
EhabY marked this conversation as resolved.
|
||
| throw wrapError("close", filePath, err); | ||
| } | ||
| }, | ||
| }; | ||
| } | ||
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.