Skip to content

Commit 8267cfb

Browse files
committed
Ensure span is recording before ending as there are multiple paths where it can
end
1 parent 23dae8b commit 8267cfb

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

packages/core/src/utils/anthropic-ai/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ function instrumentMethod<T extends unknown[], R>(
226226
const messageStream = target.apply(context, args);
227227
return instrumentStream(messageStream, span, options.recordOutputs ?? false);
228228
} catch (error) {
229-
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
230229
captureException(error, {
231230
mechanism: {
232231
handled: false,
@@ -236,7 +235,12 @@ function instrumentMethod<T extends unknown[], R>(
236235
},
237236
},
238237
});
239-
span.end();
238+
239+
if (span.isRecording()) {
240+
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'internal_error' });
241+
span.end();
242+
}
243+
240244
throw error;
241245
}
242246
},

packages/core/src/utils/anthropic-ai/streaming.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ function finalizeStreamSpan(state: StreamingState, span: Span, recordOutputs: bo
254254
});
255255
}
256256

257-
span.end();
257+
if (span.isRecording()) {
258+
span.end();
259+
}
258260
}
259261

260262
/**
@@ -289,14 +291,17 @@ export function instrumentStream<R extends { on: (...args: unknown[]) => void }>
289291
});
290292

291293
stream.on('error', (error: unknown) => {
292-
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'stream_error' });
293294
captureException(error, {
294295
mechanism: {
295296
handled: false,
296297
type: 'auto.ai.anthropic.stream_error',
297298
},
298299
});
299-
span.end();
300+
301+
if (span.isRecording()) {
302+
span.setStatus({ code: SPAN_STATUS_ERROR, message: 'stream_error' });
303+
span.end();
304+
}
300305
});
301306

302307
return stream;

0 commit comments

Comments
 (0)