File size: 7,315 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 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {
diag,
SpanStatusCode,
trace,
type AttributeValue,
type SpanOptions,
} from '@opentelemetry/api';
import { debugLogger } from '../utils/debugLogger.js';
import { safeJsonStringify } from '../utils/safeJsonStringify.js';
import { truncateString } from '../utils/textUtils.js';
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,
SERVICE_DESCRIPTION,
SERVICE_NAME,
type GeminiCliOperation,
} from './constants.js';
const TRACER_NAME = 'gemini-cli';
const TRACER_VERSION = 'v1';
/**
* Registry used to ensure that spans are properly ended when their associated
* async objects are garbage collected.
*/
export const spanRegistry = new FinalizationRegistry((endSpan: () => void) => {
try {
endSpan();
} catch (e) {
debugLogger.warn(
'Error in FinalizationRegistry callback for span cleanup',
e,
);
}
});
/**
* Truncates a value for inclusion in telemetry attributes.
*
* @param value The value to truncate.
* @param maxLength The maximum length of the stringified value.
* @returns The truncated value, or undefined if the value type is not supported.
*/
export function truncateForTelemetry(
value: unknown,
maxLength = 10000,
): AttributeValue | undefined {
if (typeof value === 'string') {
return truncateString(
value,
maxLength,
`...[TRUNCATED: original length ${value.length}]`,
) as AttributeValue;
}
if (typeof value === 'object' && value !== null) {
const stringified = safeJsonStringify(value);
return truncateString(
stringified,
maxLength,
`...[TRUNCATED: original length ${stringified.length}]`,
) as AttributeValue;
}
if (typeof value === 'number' || typeof value === 'boolean') {
return value as AttributeValue;
}
return undefined;
}
function isAsyncIterable<T>(value: T): value is T & AsyncIterable<unknown> {
return (
typeof value === 'object' && value !== null && Symbol.asyncIterator in value
);
}
/**
* Metadata for a span.
*/
export interface SpanMetadata {
/** The name of the span. */
name: string;
/** The input to the span. */
input?: unknown;
/** The output of the span. */
output?: unknown;
error?: unknown;
/** Additional attributes for the span. */
attributes: Record<string, AttributeValue>;
}
/**
* Runs a function in a new OpenTelemetry span.
*
* The `meta` object will be automatically used to set the span's status and attributes upon completion.
*
* @example
* ```typescript
* await runInDevTraceSpan(
* { operation: GeminiCliOperation.LLMCall, sessionId: 'my-session' },
* async ({ metadata }) => {
* metadata.input = { foo: 'bar' };
* // ... do work ...
* metadata.output = { result: 'baz' };
* metadata.attributes['my.custom.attribute'] = 'some-value';
* }
* );
* ```
*
* @param opts The options for the span.
* @param fn The function to run in the span.
* @returns The result of the function.
*/
export async function runInDevTraceSpan<R>(
opts: SpanOptions & {
operation: GeminiCliOperation;
logPrompts?: boolean;
sessionId: string;
tracesEnabled?: boolean;
},
fn: ({ metadata }: { metadata: SpanMetadata }) => Promise<R>,
): Promise<R> {
const { operation, logPrompts, sessionId, tracesEnabled, ...restOfSpanOpts } =
opts;
restOfSpanOpts.attributes = {
...restOfSpanOpts.attributes,
[GEN_AI_CONVERSATION_ID]: sessionId,
};
const tracer = trace.getTracer(TRACER_NAME, TRACER_VERSION);
return tracer.startActiveSpan(operation, restOfSpanOpts, async (span) => {
const meta: SpanMetadata = {
name: operation,
attributes: {
[GEN_AI_OPERATION_NAME]: operation,
[GEN_AI_AGENT_NAME]: SERVICE_NAME,
[GEN_AI_AGENT_DESCRIPTION]: SERVICE_DESCRIPTION,
[GEN_AI_CONVERSATION_ID]: sessionId,
},
};
let spanEnded = false;
const endSpan = () => {
if (spanEnded) {
return;
}
spanEnded = true;
try {
if (tracesEnabled) {
if (logPrompts !== false) {
if (meta.input !== undefined) {
const truncated = truncateForTelemetry(meta.input);
if (truncated !== undefined) {
span.setAttribute(GEN_AI_INPUT_MESSAGES, truncated);
}
}
if (meta.output !== undefined) {
const truncated = truncateForTelemetry(meta.output);
if (truncated !== undefined) {
span.setAttribute(GEN_AI_OUTPUT_MESSAGES, truncated);
}
}
}
for (const [key, value] of Object.entries(meta.attributes)) {
const truncated = truncateForTelemetry(value);
if (truncated !== undefined) {
span.setAttribute(key, truncated);
}
}
} else {
// Add basic attributes even when traces are disabled
for (const [key, value] of Object.entries(meta.attributes)) {
if (
key === GEN_AI_OPERATION_NAME ||
key === GEN_AI_AGENT_NAME ||
key === GEN_AI_AGENT_DESCRIPTION ||
key === GEN_AI_CONVERSATION_ID
) {
const truncated = truncateForTelemetry(value);
if (truncated !== undefined) {
span.setAttribute(key, truncated);
}
}
}
}
if (meta.error) {
span.setStatus({
code: SpanStatusCode.ERROR,
message: getErrorMessage(meta.error),
});
if (meta.error instanceof Error) {
span.recordException(meta.error);
}
} else {
span.setStatus({ code: SpanStatusCode.OK });
}
} catch (e) {
// Log the error but don't rethrow, to ensure span.end() is called.
diag.error('Error setting span attributes in endSpan', e);
span.setStatus({
code: SpanStatusCode.ERROR,
message: `Error in endSpan: ${getErrorMessage(e)}`,
});
} finally {
span.end();
}
};
let isStream = false;
try {
const result = await fn({ metadata: meta });
if (isAsyncIterable(result)) {
isStream = true;
const streamWrapper = (async function* () {
try {
yield* result;
} catch (e: unknown) {
meta.error = e;
throw e;
} finally {
endSpan();
}
})();
const finalResult = Object.assign(streamWrapper, result);
spanRegistry.register(finalResult, endSpan);
return finalResult;
}
return result;
} catch (e: unknown) {
meta.error = e;
throw e;
} finally {
if (!isStream) {
endSpan();
}
}
});
}
/**
* Gets the error message from an error object.
*
* @param e The error object.
* @returns The error message.
*/
function getErrorMessage(e: unknown): string {
if (e instanceof Error) {
return e.message;
}
if (typeof e === 'string') {
return e;
}
return safeJsonStringify(e);
}
|