Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,26 @@ async function run() {
const messages = [{ role: 'user', content: 'Aborted by the caller' }];

const requests = [
[{ model: 'abort-before-stream', stream: true }, { signal: abortAfter(50) }],
[{ model: 'abort-non-streaming' }, { signal: abortAfter(50) }],
[{ model: 'timeout-before-stream', stream: true }, { timeout: 50 }],
[{ model: 'timeout-non-streaming' }, { timeout: 50 }],
[{ model: 'abort-before-stream', stream: true }, () => ({ signal: abortAfter(50) })],
[{ model: 'abort-non-streaming' }, () => ({ signal: abortAfter(50) })],
[{ model: 'timeout-before-stream', stream: true }, () => ({ timeout: 50 })],
[{ model: 'timeout-non-streaming' }, () => ({ timeout: 50 })],
];
for (const [params, requestOptions] of requests) {
try {
await client.messages.create({ ...params, max_tokens: 100, messages }, requestOptions);
await client.messages.create({ ...params, max_tokens: 100, messages }, requestOptions());
} catch {
// Error expected
}
}

const streamRequests = [
[{ model: 'stream-helper-abort' }, { signal: abortAfter(50) }],
[{ model: 'stream-helper-signal-timeout' }, { signal: AbortSignal.timeout(50) }],
[{ model: 'stream-helper-abort' }, () => ({ signal: abortAfter(50) })],
[{ model: 'stream-helper-signal-timeout' }, () => ({ signal: AbortSignal.timeout(50) })],
];
for (const [params, requestOptions] of streamRequests) {
try {
await client.messages.stream({ ...params, max_tokens: 100, messages }, requestOptions).finalMessage();
await client.messages.stream({ ...params, max_tokens: 100, messages }, requestOptions()).finalMessage();
} catch {
// Error expected
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Mistral } from '@mistralai/mistralai';
import * as Sentry from '@sentry/node';
import express from 'express';

function startMockServer() {
const app = express();
app.use(express.json());

// Never responds, so each request only ends when the client gives up on it.
app.post('/v1/chat/completions', () => {});

return new Promise(resolve => {
const server = app.listen(0, () => {
resolve(server);
});
});
}

function abortAfter(ms) {
const controller = new AbortController();
setTimeout(() => controller.abort(), ms);
return controller.signal;
}

async function run() {
const server = await startMockServer();

await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
const client = new Mistral({
apiKey: 'mock-api-key',
serverURL: `http://localhost:${server.address().port}`,
retryConfig: { strategy: 'none' },
});
const messages = [{ role: 'user', content: 'Aborted by the caller' }];

const requests = [
['stream', 'abort-before-stream', () => ({ signal: abortAfter(50) })],
['complete', 'abort-non-streaming', () => ({ fetchOptions: { signal: abortAfter(50) } })],
['stream', 'timeout-before-stream', () => ({ timeoutMs: 50 })],
['complete', 'timeout-non-streaming', () => ({ timeoutMs: 50 })],
];
for (const [method, model, requestOptions] of requests) {
try {
await client.chat[method]({ model, messages }, requestOptions());
} catch {
// Error expected
}
}
});

await Sentry.flush(2000);

server.closeAllConnections();
server.close();
}

run();
25 changes: 25 additions & 0 deletions dev-packages/node-integration-tests/suites/tracing/mistral/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,29 @@ describe('Mistral integration', () => {
.completed();
});
});

createEsmTests(__dirname, 'scenario-abort.mjs', 'instrument.mjs', (createRunner, test) => {
test('ends a call the caller aborts as cancelled, but an SDK timeout as an error', async () => {
await createRunner()
.unordered()
.expect({
span: container => {
const spans = container.items.filter(span => span.attributes[SENTRY_ORIGIN]?.value === ORIGIN);
expect(spans).toHaveLength(4);
const statusByModel = Object.fromEntries(
spans.map(span => [span.attributes[GEN_AI_REQUEST_MODEL]?.value, span.status]),
);
// Span streaming sends `cancelled` as `ok`.
expect(statusByModel).toEqual({
'abort-before-stream': 'ok',
'abort-non-streaming': 'ok',
'timeout-before-stream': 'error',
'timeout-non-streaming': 'error',
});
},
})
.start()
.completed();
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ async function run() {
const messages = [{ role: 'user', content: 'Aborted by the caller' }];

const requests = [
[{ model: 'abort-before-stream', stream: true }, { signal: abortAfter(50) }],
[{ model: 'abort-non-streaming' }, { signal: abortAfter(50) }],
[{ model: 'timeout-before-stream', stream: true }, { timeout: 50 }],
[{ model: 'timeout-non-streaming' }, { timeout: 50 }],
[{ model: 'abort-before-stream', stream: true }, () => ({ signal: abortAfter(50) })],
[{ model: 'abort-non-streaming' }, () => ({ signal: abortAfter(50) })],
[{ model: 'timeout-before-stream', stream: true }, () => ({ timeout: 50 })],
[{ model: 'timeout-non-streaming' }, () => ({ timeout: 50 })],
];
for (const [params, requestOptions] of requests) {
try {
await client.chat.completions.create({ ...params, messages }, requestOptions);
await client.chat.completions.create({ ...params, messages }, requestOptions());
} catch {
// Error expected
}
Expand Down
24 changes: 16 additions & 8 deletions packages/server-utils/src/ai/mistral/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SPAN_STATUS_ERROR, startSpan, startSpanManual, stringify } from '@sentry/core';
import { startSpan, startSpanManual, stringify } from '@sentry/core';
import type { Span, SpanAttributeValue } from '@sentry/core';
import {
GEN_AI_AGENT_NAME,
Expand All @@ -14,6 +14,7 @@ import type { InstrumentedMethodEntry } from '../core/utils';
import {
buildMethodPath,
extractSystemInstructions,
getErrorStatus,
getGenAiSpanOp,
resolveAIRecordingOptions,
wrapPromiseWithMethods,
Expand All @@ -22,7 +23,7 @@ import { GEN_AI_REQUEST_STREAM_ATTRIBUTE } from '../core/gen-ai-attributes';
import { MISTRAL_METHOD_REGISTRY, MISTRAL_ORIGIN, MISTRAL_PROVIDER_NAME } from './constants';
import { instrumentEventStream } from './streaming';
import type { MistralOptions } from './types';
import { addResponseAttributes, extractRequestParameters, getSpanName } from './utils';
import { addResponseAttributes, extractRequestParameters, getMistralRequestSignal, getSpanName } from './utils';

/**
* Serialize tool definitions from request parameters, if present.
Expand Down Expand Up @@ -113,6 +114,7 @@ function instrumentMethod<T extends unknown[], R>(
const requestAttributes = extractRequestAttributes(args, operationName, !!options.recordInputs, isStreamRequested);

const params = args[0] as Record<string, unknown> | undefined;
const abortSignal = getMistralRequestSignal(args[1]);

const spanConfig = {
name: getSpanName(operationName, requestAttributes),
Expand All @@ -134,13 +136,13 @@ function instrumentMethod<T extends unknown[], R>(
result => {
// Patched in place so the caller keeps the real `EventStream`; `instrumentEventStream`
// takes over ending the span once the stream is drained.
if (!instrumentEventStream(result, span, options.recordOutputs ?? false)) {
if (!instrumentEventStream(result, span, options.recordOutputs ?? false, abortSignal)) {
span.end();
}
return result;
},
error => {
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
span.setStatus(getErrorStatus(abortSignal));
span.end();
throw error;
},
Expand All @@ -159,10 +161,16 @@ function instrumentMethod<T extends unknown[], R>(
addRequestAttributes(span, params, operationName);
}

return originalResult.then(result => {
addResponseAttributes(span, result, options.recordOutputs);
return result;
});
return originalResult.then(
result => {
addResponseAttributes(span, result, options.recordOutputs);
return result;
},
error => {
span.setStatus(getErrorStatus(abortSignal));
throw error;
},
);
});

return wrapPromiseWithMethods(originalResult, instrumentedPromise);
Expand Down
12 changes: 8 additions & 4 deletions packages/server-utils/src/ai/mistral/streaming.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Span } from '@sentry/core';
import { SPAN_STATUS_ERROR } from '@sentry/core';
import { endStreamSpan, setOutputMessagesAttribute } from '../core/utils';
import { endStreamSpan, getErrorStatus, setOutputMessagesAttribute } from '../core/utils';
import type { MistralCompletionChunk, MistralToolCall } from './types';
import { contentToString } from './utils';

Expand Down Expand Up @@ -234,7 +233,12 @@ function instrumentedSource(readable: ReadableStreamLike): ReadableStream<unknow
*
* Returns `false` for a value that is not a stream, leaving it untouched.
*/
export function instrumentEventStream(stream: unknown, span: Span, recordOutputs: boolean): boolean {
export function instrumentEventStream(
stream: unknown,
span: Span,
recordOutputs: boolean,
abortSignal?: AbortSignal,
): boolean {
if (!isAsyncIterable(stream)) {
return false;
}
Expand Down Expand Up @@ -262,7 +266,7 @@ export function instrumentEventStream(stream: unknown, span: Span, recordOutputs
}
settled = true;
if (error !== undefined && !cancelled) {
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
span.setStatus(getErrorStatus(abortSignal));
}

const toolCalls = Object.values(state.toolCalls);
Expand Down
9 changes: 9 additions & 0 deletions packages/server-utils/src/ai/mistral/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ export function getSpanName(operationName: string, attributes: Record<string, un
return `${operationName} ${model}`;
}

/**
* Reads the caller's signal from Mistral's request options. The SDK spreads `options` over
* `options.fetchOptions`, so a top-level `signal` wins.
*/
export function getMistralRequestSignal(requestOptions: unknown): AbortSignal | undefined {
const options = requestOptions as { signal?: AbortSignal; fetchOptions?: { signal?: AbortSignal } } | undefined;
return options?.signal ?? options?.fetchOptions?.signal;
}

/**
* Extract request parameters. Mistral request fields are camelCase.
*/
Expand Down
27 changes: 17 additions & 10 deletions packages/server-utils/src/ai/workers-ai/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SENTRY_OP } from '@sentry/conventions/attributes';
import { getClient, hasSpanStreamingEnabled, SPAN_STATUS_ERROR, startSpan, startSpanManual } from '@sentry/core';
import { getClient, hasSpanStreamingEnabled, startSpan, startSpanManual } from '@sentry/core';
import type { Span } from '@sentry/core';
import { isReadableStream, resolveAIRecordingOptions } from '../core/utils';
import { getErrorStatus, getRequestSignal, isReadableStream, resolveAIRecordingOptions } from '../core/utils';
import { shouldSkipAiProviderSpan } from '../pi-ai/providers';
import { WORKERS_AI_INTEGRATION_NAME } from './constants';
import { instrumentWorkersAiStream } from './streaming';
Expand Down Expand Up @@ -32,6 +32,7 @@ function instrumentRun(
const operationName = getOperationName(model, inputs);
const requestAttributes = extractRequestAttributes(model, inputs, operationName);
const modelName = typeof model === 'string' && model ? model : 'unknown';
const abortSignal = getRequestSignal(runOptions);
const client = getClient();

const isStreamRequested =
Expand All @@ -58,7 +59,7 @@ function instrumentRun(
// `startSpanManual` does not auto-end the span, so we must end it on every exit path,
// including a synchronous throw from `run`.
const handleError = (error: unknown): never => {
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
span.setStatus(getErrorStatus(abortSignal));
span.end();
throw error;
};
Expand All @@ -77,7 +78,7 @@ function instrumentRun(

return originalResult.then(result => {
if (isReadableStream<Uint8Array>(result)) {
return instrumentWorkersAiStream(result, span, options.recordOutputs);
return instrumentWorkersAiStream(result, span, options.recordOutputs, abortSignal);
}

// The model did not actually return a stream — finalize the span eagerly.
Expand All @@ -95,12 +96,18 @@ function instrumentRun(
addRequestAttributes(span, inputs, operationName);
}

return originalResult.then(result => {
if (!returnsRawResponse) {
addResponseAttributes(span, result, options.recordOutputs, operationName);
}
return result;
});
return originalResult.then(
result => {
if (!returnsRawResponse) {
addResponseAttributes(span, result, options.recordOutputs, operationName);
}
return result;
},
error => {
span.setStatus(getErrorStatus(abortSignal));
throw error;
},
);
});
};
}
Expand Down
6 changes: 3 additions & 3 deletions packages/server-utils/src/ai/workers-ai/streaming.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { SPAN_STATUS_ERROR } from '@sentry/core';
import type { Span } from '@sentry/core';
import { getToolCallKey } from '../core/streaming-tool-calls';
import { endStreamSpan, type StreamResponseState } from '../core/utils';
import { endStreamSpan, getErrorStatus, type StreamResponseState } from '../core/utils';
import type { WorkersAiUsage } from './types';
import { setOutputMessagesAttribute } from './utils';

Expand Down Expand Up @@ -164,6 +163,7 @@ export function instrumentWorkersAiStream(
stream: ReadableStream<Uint8Array>,
span: Span,
recordOutputs: boolean,
abortSignal?: AbortSignal,
): ReadableStream<Uint8Array> {
const reader = stream.getReader();
const decoder = new TextDecoder();
Expand Down Expand Up @@ -236,7 +236,7 @@ export function instrumentWorkersAiStream(
flushBuffer(false);
controller.enqueue(value);
} catch (error) {
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
span.setStatus(getErrorStatus(abortSignal));
finish();
controller.error(error);
}
Expand Down
15 changes: 12 additions & 3 deletions packages/server-utils/src/integrations/mistral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { SENTRY_ORIGIN } from '@sentry/conventions/attributes';
import * as diagnosticsChannel from '../utils/diagnosticsChannel';
import type { IntegrationFn, Span, SpanAttributeValue } from '@sentry/core';
import { defineIntegration, startInactiveSpan } from '@sentry/core';
import { getGenAiSpanOp, resolveAIRecordingOptions } from '../ai/core/utils';
import { getGenAiSpanOp, markCancelledIfAborted, resolveAIRecordingOptions } from '../ai/core/utils';
import { addRequestAttributes, extractRequestAttributes } from '../ai/mistral';
import { MISTRAL_INTEGRATION_NAME, MISTRAL_ORIGIN } from '../ai/mistral/constants';
import { instrumentEventStream } from '../ai/mistral/streaming';
import type { MistralOptions } from '../ai/mistral/types';
import { addResponseAttributes, getSpanName } from '../ai/mistral/utils';
import { addResponseAttributes, getMistralRequestSignal, getSpanName } from '../ai/mistral/utils';
import { shouldSkipAiProviderSpan } from '../ai/pi-ai/providers';
import type { OrchestrionChannelContext } from '../orchestrion/types';
import { CHANNELS } from '../orchestrion/channels';
Expand Down Expand Up @@ -43,11 +43,20 @@ function instrumentMistral(options: MistralOptions): void {
data => createGenAiSpan(data, operation, streaming, options),
{
beforeSpanEnd: (span, data) => {
if ('error' in data) {
markCancelledIfAborted(span, getMistralRequestSignal(data.arguments?.[1]));
}
addResponseAttributes(span, data.result, resolveAIRecordingOptions(options).recordOutputs);
},
// Streaming: the result is an `EventStream` consumed later, so instrument it and let it end the span.
deferSpanEnd: ({ span, data }) =>
streaming && instrumentEventStream(data.result, span, resolveAIRecordingOptions(options).recordOutputs),
streaming &&
instrumentEventStream(
data.result,
span,
resolveAIRecordingOptions(options).recordOutputs,
getMistralRequestSignal(data.arguments?.[1]),
),
},
);
}
Expand Down
Loading
Loading