|
| 1 | +import { test, describe, beforeEach, afterEach, expect } from "bun:test"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { mkdtemp, rm } from "node:fs/promises"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import { DefaultTelemetryClient } from "./client"; |
| 6 | +import { TelemetryAttributesRecorder } from "./recorder"; |
| 7 | +import { createFileLogger, type Logger } from "../logging"; |
| 8 | +import { LOG_LEVEL } from "../logging"; |
| 9 | +import { assertLogsMatch, TestGlobalConfigAccessor } from "../testing"; |
| 10 | +import type { MetricSink } from "./types"; |
| 11 | +import { LoggingSink } from "./loggingSink"; |
| 12 | + |
| 13 | +describe("DefaultTelemetryClient", () => { |
| 14 | + let tempDir: string; |
| 15 | + let logger: Logger; |
| 16 | + |
| 17 | + beforeEach(async () => { |
| 18 | + tempDir = await mkdtemp(join(tmpdir(), "telemetry-client-test-")); |
| 19 | + logger = createFileLogger({ |
| 20 | + filePath: join(tempDir, "output"), |
| 21 | + logLevel: LOG_LEVEL.DEBUG, |
| 22 | + }); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(async () => { |
| 26 | + await rm(tempDir, { recursive: true, force: true }); |
| 27 | + }); |
| 28 | + |
| 29 | + test("emits metrics with resource and command attributes to configured sinks", async () => { |
| 30 | + const sink = new LoggingSink({ logger: logger.child({ module: "loggingSink" }) }); |
| 31 | + const sessionId = "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"; |
| 32 | + const globalConfigAccessor = new TestGlobalConfigAccessor(); |
| 33 | + const client = new DefaultTelemetryClient({ |
| 34 | + logger, |
| 35 | + sessionId, |
| 36 | + globalConfigAccessor, |
| 37 | + metricSinks: [sink], |
| 38 | + }); |
| 39 | + |
| 40 | + const recorder = new TelemetryAttributesRecorder("cli.command_run", { exit_reason: "success" }); |
| 41 | + |
| 42 | + recorder.set({ exit_reason: "failure" }); |
| 43 | + |
| 44 | + await client.emit("cli.command_run", 123, recorder.get()); |
| 45 | + |
| 46 | + expect(sink.name).toBe("LoggingSink"); |
| 47 | + await sink.shutdown(); |
| 48 | + await client.shutdown(); |
| 49 | + |
| 50 | + const { installationId } = await globalConfigAccessor.get(); |
| 51 | + await assertLogsMatch(tempDir, [ |
| 52 | + { |
| 53 | + filter: (log: any) => |
| 54 | + log.metricName === "cli.command_run" && |
| 55 | + log.metricValue === 123 && |
| 56 | + log.metricAttributes?.["exit_reason"] === "failure" && |
| 57 | + log.metricAttributes?.["service.name"] === "agentcore-cli" && |
| 58 | + log.metricAttributes?.["agentcore-cli.session_id"] === sessionId && |
| 59 | + log.metricAttributes?.["agentcore-cli.installation_id"] === installationId, |
| 60 | + expectedCount: 1, |
| 61 | + }, |
| 62 | + ]); |
| 63 | + }); |
| 64 | + |
| 65 | + test("rejects malformed payloads before the sinks sees data", async () => { |
| 66 | + const recordedCalls: Array<{ metricName: string; value: number; attributes: any }> = []; |
| 67 | + const spySink: MetricSink = { |
| 68 | + name: "SpySink", |
| 69 | + record: (metricName, value, attributes) => { |
| 70 | + recordedCalls.push({ metricName, value, attributes }); |
| 71 | + }, |
| 72 | + shutdown: async () => {}, |
| 73 | + }; |
| 74 | + |
| 75 | + const client = new DefaultTelemetryClient({ |
| 76 | + logger, |
| 77 | + sessionId: "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", |
| 78 | + globalConfigAccessor: new TestGlobalConfigAccessor(), |
| 79 | + metricSinks: [spySink], |
| 80 | + }); |
| 81 | + |
| 82 | + // Negative metric value violates z.number().min(0) |
| 83 | + await client.emit("cli.command_run", -1, { exit_reason: "success" }); |
| 84 | + |
| 85 | + // The sink should never have been called — validation rejects the payload |
| 86 | + expect(recordedCalls).toHaveLength(0); |
| 87 | + |
| 88 | + await client.shutdown(); |
| 89 | + |
| 90 | + await assertLogsMatch(tempDir, [ |
| 91 | + { |
| 92 | + filter: (log: any) => |
| 93 | + log.msg === "failed to emit telemetry" && log.errorName === "ZodError", |
| 94 | + expectedCount: 1, |
| 95 | + }, |
| 96 | + ]); |
| 97 | + }); |
| 98 | + |
| 99 | + test("handles sink errors gracefully without throwing", async () => { |
| 100 | + const recordedMetrics: string[] = []; |
| 101 | + const goodSink: MetricSink = { |
| 102 | + name: "GoodSink", |
| 103 | + record: (metricName) => { |
| 104 | + recordedMetrics.push(metricName); |
| 105 | + }, |
| 106 | + shutdown: async () => {}, |
| 107 | + }; |
| 108 | + |
| 109 | + const badSink: MetricSink = { |
| 110 | + name: "BadSink", |
| 111 | + record: () => { |
| 112 | + throw new Error("record exploded"); |
| 113 | + }, |
| 114 | + shutdown: async () => { |
| 115 | + throw new Error("shutdown exploded"); |
| 116 | + }, |
| 117 | + }; |
| 118 | + |
| 119 | + const client = new DefaultTelemetryClient({ |
| 120 | + logger, |
| 121 | + sessionId: "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee", |
| 122 | + globalConfigAccessor: new TestGlobalConfigAccessor(), |
| 123 | + metricSinks: [badSink, goodSink], |
| 124 | + }); |
| 125 | + |
| 126 | + // emit should not throw even though the sink's record() throws |
| 127 | + await client.emit("cli.command_run", 100, { exit_reason: "success" }); |
| 128 | + // shutdown should not throw even though the sink's shutdown() rejects |
| 129 | + await client.shutdown(); |
| 130 | + |
| 131 | + // GoodSink still receives data despite BadSink throwing |
| 132 | + expect(recordedMetrics).toEqual(["cli.command_run"]); |
| 133 | + |
| 134 | + await assertLogsMatch(tempDir, [ |
| 135 | + { |
| 136 | + filter: (log: any) => |
| 137 | + log.msg === "failed to record to sink 'BadSink'" && |
| 138 | + log.errorName === "Error" && |
| 139 | + log.errorMessage === "record exploded", |
| 140 | + expectedCount: 1, |
| 141 | + }, |
| 142 | + { |
| 143 | + filter: (log: any) => |
| 144 | + log.msg === "failed to shutdown metric sink with name 'BadSink'" && |
| 145 | + log.errorName === "Error" && |
| 146 | + log.errorMessage === "shutdown exploded", |
| 147 | + expectedCount: 1, |
| 148 | + }, |
| 149 | + ]); |
| 150 | + }); |
| 151 | +}); |
0 commit comments