Skip to content

Commit 81adbdf

Browse files
committed
fix broken tool spans + cleanup unused methods
1 parent 909c177 commit 81adbdf

File tree

6 files changed

+29
-41
lines changed

6 files changed

+29
-41
lines changed

samples/Sentry.Samples.ME.AI.AspNetCore/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
}, "CalculateAverageAge",
132132
"Calculates the average from a list of ages. You should first get individual ages using GetPersonAge, then use this tool to calculate the average. Takes about 200ms to complete.")
133133
]
134-
};
134+
}.WithSentryToolInstrumentation();
135135

136136
try
137137
{

samples/Sentry.Samples.ME.AI.Console/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
return $"Complex calculation result for {number}: {result}";
9090
}, "ComplexCalculation", "Performs a complex mathematical calculation. Takes about 1 second to complete.")
9191
]
92-
};
92+
}.WithSentryToolInstrumentation();
9393

9494
var response = await client.GetResponseAsync(
9595
"Please help me with the following tasks: 1) Find Alice's age, 2) Get weather in New York, and 3) Calculate a complex result for number 15. Please use the appropriate tools for each task.",

src/Sentry.Extensions.AI/Extensions/SentryAIExtensions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@ namespace Sentry.Extensions.AI;
1010
[EditorBrowsable(EditorBrowsableState.Never)]
1111
public static class SentryAIExtensions
1212
{
13+
/// <summary>
14+
/// Wrap tool calls specified in <see cref="ChatOptions"/> with Sentry agent instrumentation
15+
/// </summary>
16+
/// <param name="options">The <see cref="ChatOptions"/> that contains the <see cref="AIFunction"/> to instrument</param>
17+
public static ChatOptions WithSentryToolInstrumentation(this ChatOptions options)
18+
{
19+
if (options.Tools is null || options.Tools.Count == 0)
20+
{
21+
return options;
22+
}
23+
24+
for (var i = 0; i < options.Tools.Count; i++)
25+
{
26+
if (options.Tools[i] is AIFunction fn and not SentryInstrumentedFunction)
27+
{
28+
options.Tools[i] = new SentryInstrumentedFunction(fn, options);
29+
}
30+
}
31+
32+
return options;
33+
}
34+
1335
/// <summary>
1436
/// Wraps an IChatClient with Sentry agent instrumentation.
1537
/// </summary>

src/Sentry.Extensions.AI/Sentry.Extensions.AI.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
<Description>Microsoft.Extensions.AI integration for Sentry - captures AI Agent telemetry spans per Sentry AI Agents module.</Description>
77
</PropertyGroup>
88

9+
<ItemGroup>
10+
<Using Include="Sentry.Extensibility" />
11+
<Using Include="Sentry.Integrations" />
12+
</ItemGroup>
13+
914
<ItemGroup>
1015
<ProjectReference Include="..\Sentry\Sentry.csproj" />
1116
</ItemGroup>

src/Sentry.Extensions.AI/SentryAIConstants.cs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,6 @@ namespace Sentry.Extensions.AI;
44

55
internal static class SentryAIConstants
66
{
7-
/// <summary>
8-
/// <para>
9-
/// Sentry will add a <see cref="ISpan"/> to AdditionalAttribute in <see cref="ChatOptions"/>.
10-
/// </para>
11-
/// <para>
12-
/// This constant represents the string key to get the span which represents the agent span.
13-
/// </para>
14-
/// </summary>
15-
internal const string OptionsAdditionalAttributeAgentSpanName = "SentryChatMessageAgentSpan";
16-
17-
/// <summary>
18-
/// <para>
19-
/// When an LLM uses a tool, Sentry will add an argument to <see cref="AIFunctionArguments"/>.
20-
/// The additional argument will contain the request <see cref="ChatMessage"/> which initialized the tool call.
21-
/// </para>
22-
/// <para>
23-
/// This constant represents the string key to get the message.
24-
/// </para>
25-
/// </summary>
26-
internal const string KeyMessageFunctionArgumentDictKey = "SentrySpanToMessageDictKey";
27-
287
/// <summary>
298
/// The list of strings which FunctionInvokingChatClient(FICC) uses to start the tool call <see cref="Activity"/>.
309
/// </summary>

src/Sentry.Extensions.AI/SentryChatClient.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public override async Task<ChatResponse> GetResponseAsync(IEnumerable<ChatMessag
2929
var chatMessages = messages as ChatMessage[] ?? messages.ToArray();
3030
var outerSpan = TryGetRootSpan(options);
3131
var innerSpan = CreateChatSpan(outerSpan, options);
32-
WrapTools(options);
3332

3433
try
3534
{
@@ -69,7 +68,6 @@ public override async IAsyncEnumerable<ChatResponseUpdate> GetStreamingResponseA
6968
var chatMessages = messages as ChatMessage[] ?? messages.ToArray();
7069
var outerSpan = TryGetRootSpan(options);
7170
var innerSpan = CreateChatSpan(outerSpan, options);
72-
WrapTools(options);
7371

7472
var hasNext = true;
7573
var responses = new List<ChatResponseUpdate>();
@@ -146,20 +144,4 @@ private ISpan CreateChatSpan(ISpan? outerSpan, ChatOptions? options)
146144
? outerSpan.StartChild(SentryAIConstants.SpanAttributes.ChatOperation, chatSpanName)
147145
: _hub.StartSpan(SentryAIConstants.SpanAttributes.ChatOperation, chatSpanName);
148146
}
149-
150-
private static void WrapTools(ChatOptions? options)
151-
{
152-
if (options?.Tools is null || options.Tools.Count == 0)
153-
{
154-
return;
155-
}
156-
// We wrap tools here so we don't have to wrap them each time we grab the response
157-
for (var i = 0; i < options.Tools.Count; i++)
158-
{
159-
if (options.Tools[i] is AIFunction fn and not SentryInstrumentedFunction)
160-
{
161-
options.Tools[i] = new SentryInstrumentedFunction(fn, options);
162-
}
163-
}
164-
}
165147
}

0 commit comments

Comments
 (0)