Skip to content

Commit f14cdb2

Browse files
committed
test(telemetry): tighten OTLP writer test coverage
Addresses R2 review feedback on PR #961: - records: assert the Math.max(0, ...) negative-counter clamp directly (existing Infinity case only exercised the isFinite guard). - records: cover the result="error" + error object combination so a regression on the spread of error.message is caught. - envelope: also assert stream.destroyed on the stream.end failure path (matches the sibling suffix-write test). - metrics: swap %p (not a Vitest format specifier) for %s in it.each titles so boolean rows render.
1 parent d83e697 commit f14cdb2

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

test/unit/telemetry/export/metrics.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe("isMetricEvent", () => {
1212
["ssh.network.sampled", true],
1313
["log.something", false],
1414
["remote.setup.workspace_ready", false],
15-
])("returns %s for %p", (name, expected) => {
15+
])("returns %s for %s", (name, expected) => {
1616
expect(isMetricEvent(makeEvent({ eventName: name }))).toBe(expected);
1717
});
1818
});

test/unit/telemetry/export/writers/otlp/envelope.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,13 @@ describe("openEnvelopeFile", () => {
101101
expect(stream.destroyed).toBe(1);
102102
});
103103

104-
it("wraps stream.end failures with the file path", async () => {
104+
it("wraps stream.end failures with the file path and releases the fd", async () => {
105105
const env = await openEnvelopeFile("/foo.json", "[", "]");
106106
stream.endFailMsg = "stream gone";
107107
await expect(env.close()).rejects.toThrow(
108108
"Failed to close /foo.json: stream gone",
109109
);
110+
expect(stream.destroyed).toBe(1);
110111
});
111112

112113
it("rejects subsequent writes after the stream emits 'error'", async () => {

test/unit/telemetry/export/writers/otlp/records.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ describe("spanRecord", () => {
140140
it.each([
141141
[{ properties: { result: "success" } }, { code: 1 }],
142142
[{ properties: { result: "error" } }, { code: 2 }],
143+
[
144+
{ properties: { result: "error" }, error: { message: "boom" } },
145+
{ code: 2, message: "boom" },
146+
],
143147
[{ error: { message: "boom" } }, { code: 2, message: "boom" }],
144148
[{}, { code: 0 }],
145149
])("maps span status: %j -> %j", (overrides, expected) => {
@@ -336,6 +340,24 @@ describe("metricRecords", () => {
336340
]);
337341
});
338342

343+
it("clamps negative counter deltas so the cumulative total never decreases", () => {
344+
// Without Math.max(0, ...) a negative delta would shrink the total; backends
345+
// read a decreasing monotonic sum as a counter reset.
346+
const state = newCumulativeState();
347+
metricRecords(
348+
makeEvent({ eventName: "http.requests" }),
349+
{ windowSeconds: 60, measurements: [counter("count.2xx", 5)] },
350+
state,
351+
);
352+
const [record] = metricRecords(
353+
makeEvent({ eventName: "http.requests" }),
354+
{ windowSeconds: 60, measurements: [counter("count.2xx", -3)] },
355+
state,
356+
);
357+
358+
expect(record.sum!.dataPoints[0].asInt).toBe("5");
359+
});
360+
339361
it("treats windowSeconds=0 as a zero-width window", () => {
340362
const [record] = metricRecords(
341363
makeEvent({ eventName: "http.requests" }),

0 commit comments

Comments
 (0)