Skip to content

Commit 0afa1fb

Browse files
committed
Add shouldEnableTruncation helper
1 parent f1acf51 commit 0afa1fb

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

packages/core/src/tracing/ai/utils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
import { captureException } from '../../exports';
55
import { getClient } from '../../currentScopes';
6+
import { hasSpanStreamingEnabled } from '../spans/hasSpanStreamingEnabled';
67
import type { Span } from '../../types-hoist/span';
78
import { isThenable } from '../../utils/is';
89
import {
@@ -56,6 +57,16 @@ export function resolveAIRecordingOptions<T extends AIRecordingOptions>(options?
5657
} as T & Required<AIRecordingOptions>;
5758
}
5859

60+
/**
61+
* Resolves whether truncation should be enabled.
62+
* If the user explicitly set `enableTruncation`, that value is used.
63+
* Otherwise, truncation is disabled when span streaming is active.
64+
*/
65+
export function shouldEnableTruncation(enableTruncation: boolean | undefined): boolean {
66+
const client = getClient();
67+
return enableTruncation ?? !(client && hasSpanStreamingEnabled(client));
68+
}
69+
5970
/**
6071
* Build method path from current traversal
6172
*/

packages/core/src/tracing/openai/index.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { getClient } from '../../currentScopes';
21
import { DEBUG_BUILD } from '../../debug-build';
32
import { captureException } from '../../exports';
43
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '../../semanticAttributes';
@@ -23,9 +22,9 @@ import {
2322
getJsonString,
2423
getTruncatedJsonString,
2524
resolveAIRecordingOptions,
25+
shouldEnableTruncation,
2626
wrapPromiseWithMethods,
2727
} from '../ai/utils';
28-
import { hasSpanStreamingEnabled } from '../spans/hasSpanStreamingEnabled';
2928
import { OPENAI_METHOD_REGISTRY } from './constants';
3029
import { instrumentStream } from './streaming';
3130
import type { ChatCompletionChunk, OpenAiOptions, OpenAIStream, ResponseStreamingEvent } from './types';
@@ -172,9 +171,7 @@ function instrumentMethod<T extends unknown[], R>(
172171
originalResult = originalMethod.apply(context, args);
173172

174173
if (options.recordInputs && params) {
175-
const client = getClient();
176-
const enableTruncation = options.enableTruncation ?? !(client && hasSpanStreamingEnabled(client));
177-
addRequestAttributes(span, params, operationName, enableTruncation);
174+
addRequestAttributes(span, params, operationName, shouldEnableTruncation(options.enableTruncation));
178175
}
179176

180177
// Return async processing
@@ -212,9 +209,7 @@ function instrumentMethod<T extends unknown[], R>(
212209
originalResult = originalMethod.apply(context, args);
213210

214211
if (options.recordInputs && params) {
215-
const client = getClient();
216-
const enableTruncation = options.enableTruncation ?? !(client && hasSpanStreamingEnabled(client));
217-
addRequestAttributes(span, params, operationName, enableTruncation);
212+
addRequestAttributes(span, params, operationName, shouldEnableTruncation(options.enableTruncation));
218213
}
219214

220215
return originalResult.then(

0 commit comments

Comments
 (0)