-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtelemetry.test.ts
More file actions
133 lines (116 loc) · 5.55 KB
/
telemetry.test.ts
File metadata and controls
133 lines (116 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* eslint-disable @typescript-eslint/no-explicit-any */
import { describe, expect, it } from "vitest";
import { getTraceContext } from "../src/telemetry.js";
import type { TraceContextProvider } from "../src/types.js";
describe("telemetry", () => {
describe("getTraceContext", () => {
it("returns empty object when no provider is given", async () => {
const ctx = await getTraceContext();
expect(ctx).toEqual({});
});
it("returns empty object when provider is undefined", async () => {
const ctx = await getTraceContext(undefined);
expect(ctx).toEqual({});
});
it("calls provider and returns trace context", async () => {
const provider: TraceContextProvider = () => ({
traceparent: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
tracestate: "congo=t61rcWkgMzE",
});
const ctx = await getTraceContext(provider);
expect(ctx).toEqual({
traceparent: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
tracestate: "congo=t61rcWkgMzE",
});
});
it("supports async providers", async () => {
const provider: TraceContextProvider = async () => ({
traceparent: "00-abcdef1234567890abcdef1234567890-1234567890abcdef-01",
});
const ctx = await getTraceContext(provider);
expect(ctx).toEqual({
traceparent: "00-abcdef1234567890abcdef1234567890-1234567890abcdef-01",
});
});
it("returns empty object when provider throws", async () => {
const provider: TraceContextProvider = () => {
throw new Error("boom");
};
const ctx = await getTraceContext(provider);
expect(ctx).toEqual({});
});
it("returns empty object when async provider rejects", async () => {
const provider: TraceContextProvider = async () => {
throw new Error("boom");
};
const ctx = await getTraceContext(provider);
expect(ctx).toEqual({});
});
it("returns empty object when provider returns null", async () => {
const provider = (() => null) as unknown as TraceContextProvider;
const ctx = await getTraceContext(provider);
expect(ctx).toEqual({});
});
});
describe("TelemetryConfig env var mapping", () => {
it("sets correct env vars for full telemetry config", async () => {
const telemetry = {
otlpEndpoint: "http://localhost:4318",
filePath: "/tmp/traces.jsonl",
exporterType: "otlp-http",
sourceName: "my-app",
captureContent: true,
};
const env: Record<string, string | undefined> = {};
if (telemetry) {
const t = telemetry;
env.COPILOT_OTEL_ENABLED = "true";
if (t.otlpEndpoint !== undefined) env.OTEL_EXPORTER_OTLP_ENDPOINT = t.otlpEndpoint;
if (t.filePath !== undefined) env.COPILOT_OTEL_FILE_EXPORTER_PATH = t.filePath;
if (t.exporterType !== undefined) env.COPILOT_OTEL_EXPORTER_TYPE = t.exporterType;
if (t.sourceName !== undefined) env.COPILOT_OTEL_SOURCE_NAME = t.sourceName;
if (t.captureContent !== undefined)
env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = String(
t.captureContent
);
}
expect(env).toEqual({
COPILOT_OTEL_ENABLED: "true",
OTEL_EXPORTER_OTLP_ENDPOINT: "http://localhost:4318",
COPILOT_OTEL_FILE_EXPORTER_PATH: "/tmp/traces.jsonl",
COPILOT_OTEL_EXPORTER_TYPE: "otlp-http",
COPILOT_OTEL_SOURCE_NAME: "my-app",
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT: "true",
});
});
it("only sets COPILOT_OTEL_ENABLED for empty telemetry config", async () => {
const telemetry = {};
const env: Record<string, string | undefined> = {};
if (telemetry) {
const t = telemetry as any;
env.COPILOT_OTEL_ENABLED = "true";
if (t.otlpEndpoint !== undefined) env.OTEL_EXPORTER_OTLP_ENDPOINT = t.otlpEndpoint;
if (t.filePath !== undefined) env.COPILOT_OTEL_FILE_EXPORTER_PATH = t.filePath;
if (t.exporterType !== undefined) env.COPILOT_OTEL_EXPORTER_TYPE = t.exporterType;
if (t.sourceName !== undefined) env.COPILOT_OTEL_SOURCE_NAME = t.sourceName;
if (t.captureContent !== undefined)
env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = String(
t.captureContent
);
}
expect(env).toEqual({
COPILOT_OTEL_ENABLED: "true",
});
});
it("converts captureContent false to string 'false'", async () => {
const telemetry = { captureContent: false };
const env: Record<string, string | undefined> = {};
env.COPILOT_OTEL_ENABLED = "true";
if (telemetry.captureContent !== undefined)
env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT = String(
telemetry.captureContent
);
expect(env.OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT).toBe("false");
});
});
});