File size: 9,343 Bytes
7a1ad33 | 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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | /**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { diag, SpanStatusCode, trace } from '@opentelemetry/api';
import type { Tracer } from '@opentelemetry/api';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
GEN_AI_AGENT_DESCRIPTION,
GEN_AI_AGENT_NAME,
GEN_AI_CONVERSATION_ID,
GEN_AI_INPUT_MESSAGES,
GEN_AI_OPERATION_NAME,
GEN_AI_OUTPUT_MESSAGES,
GeminiCliOperation,
SERVICE_DESCRIPTION,
SERVICE_NAME,
} from './constants.js';
import {
runInDevTraceSpan,
spanRegistry,
truncateForTelemetry,
} from './trace.js';
vi.mock('@opentelemetry/api', async (importOriginal) => {
const original = await importOriginal();
return Object.assign({}, original, {
trace: {
getTracer: vi.fn(),
},
diag: {
error: vi.fn(),
},
});
});
vi.mock('../utils/session.js', () => ({
sessionId: 'test-session-id',
}));
describe('truncateForTelemetry', () => {
it('should return string unchanged if within maxLength', () => {
expect(truncateForTelemetry('hello', 10)).toBe('hello');
});
it('should truncate string if exceeding maxLength', () => {
const result = truncateForTelemetry('hello world', 5);
expect(result).toBe('hello...[TRUNCATED: original length 11]');
});
it('should correctly truncate strings with multi-byte unicode characters (emojis)', () => {
// 5 emojis, each is multiple bytes in UTF-16
const emojis = 'ππππ₯π';
// Truncating to length 5 (which is 2.5 emojis in UTF-16 length terms)
// truncateString will stop after the full grapheme clusters that fit within 5
const result = truncateForTelemetry(emojis, 5);
expect(result).toBe('ππ...[TRUNCATED: original length 10]');
});
it('should stringify and truncate objects if exceeding maxLength', () => {
const obj = { message: 'hello world', nested: { a: 1 } };
const stringified = JSON.stringify(obj);
const result = truncateForTelemetry(obj, 10);
expect(result).toBe(
stringified.substring(0, 10) +
`...[TRUNCATED: original length ${stringified.length}]`,
);
});
it('should stringify objects unchanged if within maxLength', () => {
const obj = { a: 1 };
expect(truncateForTelemetry(obj, 100)).toBe(JSON.stringify(obj));
});
it('should return booleans and numbers unchanged', () => {
expect(truncateForTelemetry(100)).toBe(100);
expect(truncateForTelemetry(true)).toBe(true);
expect(truncateForTelemetry(false)).toBe(false);
});
it('should return undefined for unsupported types', () => {
expect(truncateForTelemetry(undefined)).toBeUndefined();
expect(truncateForTelemetry(() => {})).toBeUndefined();
expect(truncateForTelemetry(Symbol('test'))).toBeUndefined();
});
});
describe('runInDevTraceSpan', () => {
const mockSpan = {
setAttribute: vi.fn(),
setStatus: vi.fn(),
recordException: vi.fn(),
end: vi.fn(),
};
const mockTracer = {
startActiveSpan: vi.fn((name, options, callback) => callback(mockSpan)),
} as unknown as Tracer;
beforeEach(() => {
vi.clearAllMocks();
vi.mocked(trace.getTracer).mockReturnValue(mockTracer);
});
afterEach(() => {
vi.unstubAllEnvs();
});
it('should start an active span', async () => {
const fn = vi.fn(async () => 'result');
const result = await runInDevTraceSpan(
{
operation: GeminiCliOperation.LLMCall,
sessionId: 'test-session-id',
tracesEnabled: true,
},
fn,
);
expect(result).toBe('result');
expect(trace.getTracer).toHaveBeenCalled();
expect(mockTracer.startActiveSpan).toHaveBeenCalledWith(
GeminiCliOperation.LLMCall,
{
attributes: {
[GEN_AI_CONVERSATION_ID]: 'test-session-id',
},
},
expect.any(Function),
);
});
it('should set default attributes on the span metadata', async () => {
await runInDevTraceSpan(
{
operation: GeminiCliOperation.LLMCall,
sessionId: 'test-session-id',
tracesEnabled: true,
},
async ({ metadata }) => {
expect(metadata.attributes[GEN_AI_OPERATION_NAME]).toBe(
GeminiCliOperation.LLMCall,
);
expect(metadata.attributes[GEN_AI_AGENT_NAME]).toBe(SERVICE_NAME);
expect(metadata.attributes[GEN_AI_AGENT_DESCRIPTION]).toBe(
SERVICE_DESCRIPTION,
);
expect(metadata.attributes[GEN_AI_CONVERSATION_ID]).toBe(
'test-session-id',
);
},
);
});
it('should set span attributes from metadata on completion', async () => {
await runInDevTraceSpan(
{
operation: GeminiCliOperation.LLMCall,
sessionId: 'test-session-id',
tracesEnabled: true,
},
async ({ metadata }) => {
metadata.input = { query: 'hello' };
metadata.output = { response: 'world' };
metadata.attributes['custom.attr'] = 'value';
},
);
expect(mockSpan.setAttribute).toHaveBeenCalledWith(
GEN_AI_INPUT_MESSAGES,
JSON.stringify({ query: 'hello' }),
);
expect(mockSpan.setAttribute).toHaveBeenCalledWith(
GEN_AI_OUTPUT_MESSAGES,
JSON.stringify({ response: 'world' }),
);
expect(mockSpan.setAttribute).toHaveBeenCalledWith('custom.attr', 'value');
expect(mockSpan.setStatus).toHaveBeenCalledWith({
code: SpanStatusCode.OK,
});
expect(mockSpan.end).toHaveBeenCalled();
});
it('should handle errors in the wrapped function', async () => {
const error = new Error('test error');
await expect(
runInDevTraceSpan(
{
operation: GeminiCliOperation.LLMCall,
sessionId: 'test-session-id',
tracesEnabled: true,
},
async () => {
throw error;
},
),
).rejects.toThrow(error);
expect(mockSpan.setStatus).toHaveBeenCalledWith({
code: SpanStatusCode.ERROR,
message: 'test error',
});
expect(mockSpan.recordException).toHaveBeenCalledWith(error);
expect(mockSpan.end).toHaveBeenCalled();
});
it('should auto-wrap async iterators and end span when iterator completes', async () => {
async function* testStream() {
yield 1;
yield 2;
}
const resultStream = await runInDevTraceSpan(
{
operation: GeminiCliOperation.LLMCall,
sessionId: 'test-session-id',
tracesEnabled: true,
},
async () => testStream(),
);
expect(mockSpan.end).not.toHaveBeenCalled();
const results = [];
for await (const val of resultStream) {
results.push(val);
}
expect(results).toEqual([1, 2]);
expect(mockSpan.end).toHaveBeenCalled();
});
it('should register async generators with spanRegistry', async () => {
const spy = vi.spyOn(spanRegistry, 'register');
async function* testStream() {
yield 1;
}
const resultStream = await runInDevTraceSpan(
{
operation: GeminiCliOperation.LLMCall,
sessionId: 'test-session-id',
tracesEnabled: true,
},
async () => testStream(),
);
expect(spy).toHaveBeenCalledWith(resultStream, expect.any(Function));
});
it('should be idempotent and call span.end only once', async () => {
vi.spyOn(spanRegistry, 'register');
async function* testStream() {
yield 1;
}
const resultStream = await runInDevTraceSpan(
{
operation: GeminiCliOperation.LLMCall,
sessionId: 'test-session-id',
tracesEnabled: true,
},
async () => testStream(),
);
// Simulate completion
for await (const _ of resultStream) {
// iterate
}
expect(mockSpan.end).toHaveBeenCalledTimes(1);
// Try to end again (simulating registry or double call)
const endSpanFn = vi.mocked(spanRegistry.register).mock
.calls[0][1] as () => void;
endSpanFn();
expect(mockSpan.end).toHaveBeenCalledTimes(1);
});
it('should end span automatically on error in async iterators', async () => {
const error = new Error('streaming error');
async function* errorStream() {
yield 1;
throw error;
}
const resultStream = await runInDevTraceSpan(
{
operation: GeminiCliOperation.LLMCall,
sessionId: 'test-session-id',
tracesEnabled: true,
},
async () => errorStream(),
);
await expect(async () => {
for await (const _ of resultStream) {
// iterate
}
}).rejects.toThrow(error);
expect(mockSpan.end).toHaveBeenCalled();
});
it('should handle exceptions in endSpan gracefully', async () => {
mockSpan.setAttribute.mockImplementation(() => {
throw new Error('attribute error');
});
await runInDevTraceSpan(
{
operation: GeminiCliOperation.LLMCall,
sessionId: 'test-session-id',
tracesEnabled: true,
},
async ({ metadata }) => {
metadata.input = 'trigger error';
},
);
expect(diag.error).toHaveBeenCalled();
expect(mockSpan.setStatus).toHaveBeenCalledWith(
expect.objectContaining({
code: SpanStatusCode.ERROR,
message: expect.stringContaining('attribute error'),
}),
);
expect(mockSpan.end).toHaveBeenCalled();
});
});
|