Skip to content

Commit 549d28a

Browse files
committed
Follow exporter recording options for classifier data
1 parent ce304ab commit 549d28a

5 files changed

Lines changed: 20 additions & 13 deletions

File tree

‎packages/server-utils/src/ai/mastra/classifier-evaluation.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import type { Span, SpanTimeInput } from '@sentry/core';
88
*/
99
export interface ClassifierEvaluationCall {
1010
span?: Span;
11+
/** The recording options of the exporter that opened the span, so the call's data follows them. */
12+
recordInputs?: boolean;
13+
recordOutputs?: boolean;
1114
/** Mastra's end time, set when Mastra ended its span before the call settled. */
1215
endTime?: SpanTimeInput;
1316
settled?: boolean;

‎packages/server-utils/src/ai/mastra/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ export class SentryMastraExporter implements MastraObservabilityExporter {
153153

154154
const evaluation = span.type === 'classifier_evaluation' ? takeStartingClassifierEvaluation() : undefined;
155155
if (evaluation) {
156-
evaluation.span = sentrySpan;
156+
const { recordInputs, recordOutputs } = resolveAIRecordingOptions(this._options);
157+
Object.assign(evaluation, { span: sentrySpan, recordInputs, recordOutputs });
157158
}
158159

159160
this._trackSpan(span.id, { span: sentrySpan, spanType: span.type, usage: {}, evaluation });

‎packages/server-utils/src/integrations/mastra-classifier.ts‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { GEN_AI_INPUT_MESSAGES, GEN_AI_OUTPUT_MESSAGES } from '@sentry/conventions/attributes';
22
import { isObjectLike } from '@sentry/core';
3-
import { resolveAIRecordingOptions } from '../ai/core/utils';
4-
import type { GenAiOptions } from '../ai/core/utils';
53
import type { ClassifierEvaluationCall } from '../ai/mastra/classifier-evaluation';
64
import { setStartingClassifierEvaluation } from '../ai/mastra/classifier-evaluation';
75
import { getEvaluationInputMessages, getEvaluationOutputMessages } from '../ai/typesafe';
@@ -20,7 +18,7 @@ interface ClassifierEvaluateChannelContext {
2018
* Add the evaluated state, questions and answers of a `Classifier.evaluate()` call to the exporter's
2119
* `classifier_evaluation` span, which Mastra leaves without input or output.
2220
*/
23-
export function recordClassifierEvaluations(options: GenAiOptions): void {
21+
export function recordClassifierEvaluations(): void {
2422
const channel = diagnosticsChannel.tracingChannel<ClassifierEvaluateChannelContext>(
2523
CHANNELS.MASTRA_CLASSIFIER_EVALUATE,
2624
);
@@ -41,24 +39,19 @@ export function recordClassifierEvaluations(options: GenAiOptions): void {
4139
safeChannelCallback(() => {
4240
const call = calls.get(message as object);
4341
if (call) {
44-
finishClassifierEvaluation(call, message as ClassifierEvaluateChannelContext, options);
42+
finishClassifierEvaluation(call, message as ClassifierEvaluateChannelContext);
4543
}
4644
});
4745
});
4846
}
4947

50-
function finishClassifierEvaluation(
51-
call: ClassifierEvaluationCall,
52-
message: ClassifierEvaluateChannelContext,
53-
options: GenAiOptions,
54-
): void {
48+
function finishClassifierEvaluation(call: ClassifierEvaluationCall, message: ClassifierEvaluateChannelContext): void {
5549
call.settled = true;
56-
const { span } = call;
50+
const { span, recordInputs, recordOutputs } = call;
5751
if (!span) {
5852
return;
5953
}
6054

61-
const { recordInputs, recordOutputs } = resolveAIRecordingOptions(options);
6255
if (recordInputs) {
6356
const params = isObjectLike(message.arguments[0]) ? message.arguments[0] : {};
6457
// Questions given to the constructor take precedence over the ones passed to `evaluate()`, as in Mastra.

‎packages/server-utils/src/integrations/mastra.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function instrumentExporter(options: MastraOptions): void {
9797
});
9898

9999
captureExecuteWithContextErrors();
100-
recordClassifierEvaluations(options);
100+
recordClassifierEvaluations();
101101
}
102102

103103
/**

‎packages/server-utils/test/ai/lib/tracing/mastra-exporter.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ describe('SentryMastraExporter', () => {
190190
expect(spanToStaticSpanJSON(call.span!).description).toBe('evaluate jev-1');
191191
});
192192

193+
it('passes its own recording options to the classifier evaluation call', async () => {
194+
exporter = new SentryMastraExporter({ recordInputs: false, recordOutputs: false });
195+
const call: ClassifierEvaluationCall = {};
196+
197+
setStartingClassifierEvaluation(call);
198+
await run(started(makeSpan({ id: 'eval-1', type: 'classifier_evaluation' })));
199+
200+
expect(call).toMatchObject({ recordInputs: false, recordOutputs: false });
201+
});
202+
193203
it('records the agent-level prompt and response as gen_ai messages', async () => {
194204
const span = makeSpan({
195205
entityName: 'agent',

0 commit comments

Comments
 (0)