|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { OtelLogWriter } from "./otel-log-writer.js"; |
| 3 | +import type { StoredNotification } from "./types.js"; |
| 4 | + |
| 5 | +// Mock the OTEL exporter |
| 6 | +const mockExport = vi.fn((_logs, callback) => { |
| 7 | + callback({ code: 0 }); // Success |
| 8 | +}); |
| 9 | + |
| 10 | +vi.mock("@opentelemetry/exporter-logs-otlp-http", () => ({ |
| 11 | + OTLPLogExporter: vi.fn().mockImplementation(() => ({ |
| 12 | + export: mockExport, |
| 13 | + shutdown: vi.fn().mockResolvedValue(undefined), |
| 14 | + })), |
| 15 | +})); |
| 16 | + |
| 17 | +describe("OtelLogWriter", () => { |
| 18 | + let writer: OtelLogWriter; |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + mockExport.mockClear(); |
| 22 | + // Session context (taskId, runId) is now passed in constructor as resource attributes |
| 23 | + writer = new OtelLogWriter( |
| 24 | + { |
| 25 | + posthogHost: "https://us.i.posthog.com", |
| 26 | + apiKey: "phc_test_key", |
| 27 | + flushIntervalMs: 100, |
| 28 | + }, |
| 29 | + { |
| 30 | + taskId: "task-123", |
| 31 | + runId: "run-456", |
| 32 | + }, |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + afterEach(async () => { |
| 37 | + await writer.shutdown(); |
| 38 | + }); |
| 39 | + |
| 40 | + it("should emit a log entry with event_type as regular attribute", async () => { |
| 41 | + const notification: StoredNotification = { |
| 42 | + type: "notification", |
| 43 | + timestamp: new Date().toISOString(), |
| 44 | + notification: { |
| 45 | + jsonrpc: "2.0", |
| 46 | + method: "_posthog/test_event", |
| 47 | + params: { foo: "bar" }, |
| 48 | + }, |
| 49 | + }; |
| 50 | + |
| 51 | + // taskId and runId are now resource attributes set in constructor, |
| 52 | + // only notification is passed per-emit |
| 53 | + writer.emit({ notification }); |
| 54 | + |
| 55 | + // Force flush to trigger export |
| 56 | + await writer.flush(); |
| 57 | + |
| 58 | + // Verify export was called |
| 59 | + expect(mockExport).toHaveBeenCalled(); |
| 60 | + |
| 61 | + // Get the logs that were exported |
| 62 | + const exportedLogs = mockExport.mock.calls[0][0]; |
| 63 | + expect(exportedLogs.length).toBe(1); |
| 64 | + |
| 65 | + const log = exportedLogs[0]; |
| 66 | + // task_id and run_id are now resource attributes, not regular attributes |
| 67 | + expect(log.attributes.task_id).toBeUndefined(); |
| 68 | + expect(log.attributes.run_id).toBeUndefined(); |
| 69 | + // event_type is still a regular attribute (varies per log entry) |
| 70 | + expect(log.attributes.event_type).toBe("_posthog/test_event"); |
| 71 | + expect(log.body).toBe(JSON.stringify(notification)); |
| 72 | + |
| 73 | + // Verify resource attributes contain task_id and run_id |
| 74 | + expect(log.resource.attributes.task_id).toBe("task-123"); |
| 75 | + expect(log.resource.attributes.run_id).toBe("run-456"); |
| 76 | + expect(log.resource.attributes["service.name"]).toBe("twig-agent"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should batch multiple log entries", async () => { |
| 80 | + const makeNotification = (method: string): StoredNotification => ({ |
| 81 | + type: "notification", |
| 82 | + timestamp: new Date().toISOString(), |
| 83 | + notification: { |
| 84 | + jsonrpc: "2.0", |
| 85 | + method, |
| 86 | + }, |
| 87 | + }); |
| 88 | + |
| 89 | + writer.emit({ notification: makeNotification("event_1") }); |
| 90 | + writer.emit({ notification: makeNotification("event_2") }); |
| 91 | + writer.emit({ notification: makeNotification("event_3") }); |
| 92 | + |
| 93 | + await writer.flush(); |
| 94 | + |
| 95 | + expect(mockExport).toHaveBeenCalled(); |
| 96 | + const exportedLogs = mockExport.mock.calls[0][0]; |
| 97 | + expect(exportedLogs.length).toBe(3); |
| 98 | + |
| 99 | + // All logs should share the same resource attributes |
| 100 | + for (const log of exportedLogs) { |
| 101 | + expect(log.resource.attributes.task_id).toBe("task-123"); |
| 102 | + expect(log.resource.attributes.run_id).toBe("run-456"); |
| 103 | + } |
| 104 | + }); |
| 105 | +}); |
0 commit comments