Skip to content

Commit 959a506

Browse files
authored
ref(AI): Microsoft.Extensions.AI Agent Monitoring (#4754)
1 parent ab2d7c4 commit 959a506

16 files changed

+94
-69
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Features
6+
7+
- Added a new SDK `Sentry.Extensions.AI` which allows LLM usage instrumentation via `Microsoft.Extensions.AI` ([#4657](https://github.com/getsentry/sentry-dotnet/pull/4657))
8+
59
### Fixes
610

711
- Captured [Http Client Errors](https://docs.sentry.io/platforms/dotnet/guides/aspnet/configuration/http-client-errors/) on .NET 5+ now include a full stack trace in order to improve Issue grouping ([#4724](https://github.com/getsentry/sentry-dotnet/pull/4724))
@@ -47,7 +51,6 @@
4751
- The SDK now makes use of the new SessionEndStatus `Unhandled` when capturing an unhandled but non-terminal exception, i.e. through the UnobservedTaskExceptionIntegration ([#4633](https://github.com/getsentry/sentry-dotnet/pull/4633), [#4653](https://github.com/getsentry/sentry-dotnet/pull/4653))
4852
- Implemented instance isolation so that multiple instances of the Sentry SDK can be instantiated inside the same process when using the Caching Transport ([#4498](https://github.com/getsentry/sentry-dotnet/pull/4498))
4953
- Extended the App context by `app_memory` that can hold the amount of memory used by the application in bytes. ([#4707](https://github.com/getsentry/sentry-dotnet/pull/4707))
50-
- Added a new SDK `Sentry.Extensions.AI` which allows LLM usage instrumentation via `Microsoft.Extensions.AI` ([#4657](https://github.com/getsentry/sentry-dotnet/pull/4657))
5154

5255
### Fixes
5356

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#nullable enable
21
using Microsoft.Extensions.AI;
32

43
var builder = WebApplication.CreateBuilder(args);

samples/Sentry.Samples.ME.AI.AspNetCore/Sentry.Samples.ME.AI.AspNetCore.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net9.0</TargetFramework>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<Nullable>enable</Nullable>
56
</PropertyGroup>
67

78
<ItemGroup>

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,8 @@
9696
"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.",
9797
options);
9898

99-
logger.LogInformation("Response: {ResponseText}", response.Messages?.FirstOrDefault()?.Text ?? "No response");
99+
logger.LogInformation("Response: {ResponseText}", response.Messages.FirstOrDefault()?.Text ?? "No response");
100100

101101
logger.LogInformation("Microsoft.Extensions.AI sample completed! Check your Sentry dashboard for the trace data.");
102102

103103
transaction.Finish();
104-
105-
// Flush Sentry to ensure all transactions are sent before the app exits
106-
await SentrySdk.FlushAsync(TimeSpan.FromSeconds(2));

samples/Sentry.Samples.ME.AI.Console/Sentry.Samples.ME.AI.Console.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>net9.0</TargetFramework>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<Nullable>enable</Nullable>
67
</PropertyGroup>
78

89
<ItemGroup>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFrameworks>$(CurrentTfms);netstandard2.0</TargetFrameworks>
55
<PackageTags>$(PackageTags);Microsoft.Extensions.AI;AI;LLM</PackageTags>
6-
<Description>Microsoft.Extensions.AI integration for Sentry - captures AI Agent telemetry spans per Sentry AI Agents module.</Description>
6+
<Description>Official Microsoft.Extensions.AI integration for Sentry - Open-source error tracking that helps developers monitor and fix crashes in real time.</Description>
77
</PropertyGroup>
88

99
<ItemGroup>

src/Sentry.Extensions.AI/SentryChatClient.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,20 @@ namespace Sentry.Extensions.AI;
55
internal sealed class SentryChatClient : DelegatingChatClient
66
{
77
private readonly ActivitySource _activitySource;
8-
private readonly HubAdapter _hub = HubAdapter.Instance;
8+
private readonly IHub _hub;
99
private readonly SentryAIOptions _sentryAIOptions;
1010

11-
public SentryChatClient(IChatClient client, Action<SentryAIOptions>? configure = null) : this(null, client, configure)
11+
public SentryChatClient(IChatClient client, Action<SentryAIOptions>? configure = null) : this(null, null, client, configure)
1212
{
1313
}
1414

1515
/// <summary>
16-
/// Internal ovverride for testing
16+
/// Internal overload for testing
1717
/// </summary>
18-
internal SentryChatClient(ActivitySource? activitySource, IChatClient client, Action<SentryAIOptions>? configure = null) : base(client)
18+
internal SentryChatClient(ActivitySource? activitySource, IHub? hub, IChatClient client, Action<SentryAIOptions>? configure = null) : base(client)
1919
{
2020
_activitySource = activitySource ?? SentryAIActivitySource.Instance;
21+
_hub = hub ?? HubAdapter.Instance;
2122
_sentryAIOptions = new SentryAIOptions();
2223
configure?.Invoke(_sentryAIOptions);
2324
}

src/Sentry.Extensions.AI/SentryInstrumentedFunction.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ internal sealed class SentryInstrumentedFunction(AIFunction innerFunction, IHub?
66
: DelegatingAIFunction(innerFunction)
77
{
88
private readonly IHub _hub = hub ?? HubAdapter.Instance;
9+
910
protected override async ValueTask<object?> InvokeCoreAsync(
1011
AIFunctionArguments arguments,
1112
CancellationToken cancellationToken)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace Microsoft.Extensions.AI
2+
{
3+
public static class SentryAIExtensions
4+
{
5+
public static Microsoft.Extensions.AI.IChatClient AddSentry(this Microsoft.Extensions.AI.IChatClient client, System.Action<Sentry.Extensions.AI.SentryAIOptions>? configure = null) { }
6+
public static Microsoft.Extensions.AI.ChatOptions AddSentryToolInstrumentation(this Microsoft.Extensions.AI.ChatOptions options) { }
7+
}
8+
}
9+
namespace Sentry.Extensions.AI
10+
{
11+
public class SentryAIOptions
12+
{
13+
public SentryAIOptions() { }
14+
public Sentry.Extensions.AI.SentryAIOptions.SentryAIExperimentalOptions Experimental { get; set; }
15+
public sealed class SentryAIExperimentalOptions
16+
{
17+
public SentryAIExperimentalOptions() { }
18+
public string AgentName { get; set; }
19+
public bool RecordInputs { get; set; }
20+
public bool RecordOutputs { get; set; }
21+
}
22+
}
23+
}

test/Sentry.Extensions.AI.Tests/Sentry.Extensions.AI.Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>$(CurrentTfms)</TargetFrameworks>
5+
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('Windows'))">$(TargetFrameworks);net48</TargetFrameworks>
6+
</PropertyGroup>
7+
8+
<PropertyGroup>
9+
<Nullable>enable</Nullable>
510
</PropertyGroup>
611

712
<ItemGroup>

0 commit comments

Comments
 (0)