Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/platform/otel/common/test/agentOTelEnv.spec.ts
13406 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { describe, expect, it } from 'vitest';
7
import { deriveClaudeOTelEnv, deriveCopilotCliOTelEnv } from '../agentOTelEnv';
8
import type { OTelConfig } from '../otelConfig';
9
10
function makeConfig(overrides: Partial<OTelConfig> = {}): OTelConfig {
11
return {
12
enabled: true,
13
enabledExplicitly: true,
14
enabledVia: 'setting',
15
exporterType: 'otlp-http',
16
otlpEndpoint: 'http://localhost:4318',
17
otlpProtocol: 'http',
18
captureContent: false,
19
dbSpanExporter: false,
20
logLevel: 'info',
21
httpInstrumentation: false,
22
serviceName: 'copilot-chat',
23
serviceVersion: '1.0.0',
24
sessionId: 'test-session',
25
resourceAttributes: {},
26
...overrides,
27
};
28
}
29
30
const emptyEnv: Record<string, string | undefined> = {};
31
32
describe('deriveCopilotCliOTelEnv', () => {
33
it('returns empty when disabled', () => {
34
const result = deriveCopilotCliOTelEnv(makeConfig({ enabled: false }), emptyEnv);
35
expect(result).toEqual({});
36
});
37
38
it('returns correct env vars when enabled', () => {
39
const result = deriveCopilotCliOTelEnv(makeConfig(), emptyEnv);
40
expect(result).toEqual({
41
COPILOT_OTEL_ENABLED: 'true',
42
OTEL_EXPORTER_OTLP_ENDPOINT: 'http://localhost:4318',
43
});
44
});
45
46
it('includes capture content var when captureContent is true', () => {
47
const result = deriveCopilotCliOTelEnv(makeConfig({ captureContent: true }), emptyEnv);
48
expect(result['OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT']).toBe('true');
49
});
50
51
it('includes file exporter path when set', () => {
52
const result = deriveCopilotCliOTelEnv(makeConfig({ fileExporterPath: '/tmp/otel.jsonl', exporterType: 'file' }), emptyEnv);
53
expect(result['COPILOT_OTEL_FILE_EXPORTER_PATH']).toBe('/tmp/otel.jsonl');
54
expect(result['COPILOT_OTEL_EXPORTER_TYPE']).toBe('file');
55
});
56
57
it('does not set exporter type for non-file exporters', () => {
58
const result = deriveCopilotCliOTelEnv(makeConfig({ exporterType: 'otlp-http' }), emptyEnv);
59
expect(result['COPILOT_OTEL_EXPORTER_TYPE']).toBeUndefined();
60
});
61
62
it('does not overwrite existing env vars', () => {
63
const existingEnv: Record<string, string | undefined> = {
64
COPILOT_OTEL_ENABLED: 'false',
65
OTEL_EXPORTER_OTLP_ENDPOINT: 'http://custom:9999',
66
};
67
const result = deriveCopilotCliOTelEnv(makeConfig(), existingEnv);
68
expect(result['COPILOT_OTEL_ENABLED']).toBeUndefined();
69
expect(result['OTEL_EXPORTER_OTLP_ENDPOINT']).toBeUndefined();
70
});
71
72
it('does not include capture content when captureContent is false', () => {
73
const result = deriveCopilotCliOTelEnv(makeConfig({ captureContent: false }), emptyEnv);
74
expect(result['OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT']).toBeUndefined();
75
});
76
});
77
78
describe('deriveClaudeOTelEnv', () => {
79
it('returns empty when disabled', () => {
80
const result = deriveClaudeOTelEnv(makeConfig({ enabled: false }), emptyEnv);
81
expect(result).toEqual({});
82
});
83
84
it('returns correct env vars when enabled with HTTP', () => {
85
const result = deriveClaudeOTelEnv(makeConfig(), emptyEnv);
86
expect(result).toEqual({
87
CLAUDE_CODE_ENABLE_TELEMETRY: '1',
88
OTEL_METRICS_EXPORTER: 'otlp',
89
OTEL_LOGS_EXPORTER: 'otlp',
90
OTEL_EXPORTER_OTLP_ENDPOINT: 'http://localhost:4318',
91
OTEL_EXPORTER_OTLP_PROTOCOL: 'http/json',
92
});
93
});
94
95
it('uses gRPC protocol when configured', () => {
96
const result = deriveClaudeOTelEnv(makeConfig({ otlpProtocol: 'grpc' }), emptyEnv);
97
expect(result['OTEL_EXPORTER_OTLP_PROTOCOL']).toBe('grpc');
98
});
99
100
it('includes content capture vars when captureContent is true', () => {
101
const result = deriveClaudeOTelEnv(makeConfig({ captureContent: true }), emptyEnv);
102
expect(result['OTEL_LOG_USER_PROMPTS']).toBe('1');
103
expect(result['OTEL_LOG_TOOL_DETAILS']).toBe('1');
104
});
105
106
it('does not include content capture vars when captureContent is false', () => {
107
const result = deriveClaudeOTelEnv(makeConfig({ captureContent: false }), emptyEnv);
108
expect(result['OTEL_LOG_USER_PROMPTS']).toBeUndefined();
109
expect(result['OTEL_LOG_TOOL_DETAILS']).toBeUndefined();
110
});
111
112
it('does not overwrite existing env vars', () => {
113
const existingEnv: Record<string, string | undefined> = {
114
CLAUDE_CODE_ENABLE_TELEMETRY: '0',
115
OTEL_EXPORTER_OTLP_ENDPOINT: 'http://custom:9999',
116
OTEL_EXPORTER_OTLP_PROTOCOL: 'grpc',
117
};
118
const result = deriveClaudeOTelEnv(makeConfig(), existingEnv);
119
expect(result['CLAUDE_CODE_ENABLE_TELEMETRY']).toBeUndefined();
120
expect(result['OTEL_EXPORTER_OTLP_ENDPOINT']).toBeUndefined();
121
expect(result['OTEL_EXPORTER_OTLP_PROTOCOL']).toBeUndefined();
122
});
123
124
it('does not include file exporter path (not supported by Claude SDK)', () => {
125
const result = deriveClaudeOTelEnv(makeConfig({ fileExporterPath: '/tmp/otel.jsonl' }), emptyEnv);
126
expect(result['COPILOT_OTEL_FILE_EXPORTER_PATH']).toBeUndefined();
127
});
128
});
129
130