|
7 | 7 | using System.Runtime.CompilerServices; |
8 | 8 | using System.Threading; |
9 | 9 | using System.Threading.Tasks; |
| 10 | +using Microsoft.Extensions.Logging; |
| 11 | +using Microsoft.Extensions.Logging.Testing; |
10 | 12 | using OpenTelemetry.Trace; |
11 | 13 | using Xunit; |
12 | 14 |
|
@@ -127,4 +129,64 @@ static async IAsyncEnumerable<TextToSpeechResponseUpdate> TestClientStreamAsync( |
127 | 129 |
|
128 | 130 | Assert.True(activity.Duration.TotalMilliseconds > 0); |
129 | 131 | } |
| 132 | + |
| 133 | + [Theory] |
| 134 | + [InlineData(false)] |
| 135 | + [InlineData(true)] |
| 136 | + public async Task ExceptionLogged_Async(bool streaming) |
| 137 | + { |
| 138 | + var sourceName = Guid.NewGuid().ToString(); |
| 139 | + var activities = new List<Activity>(); |
| 140 | + using var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder() |
| 141 | + .AddSource(sourceName) |
| 142 | + .AddInMemoryExporter(activities) |
| 143 | + .Build(); |
| 144 | + |
| 145 | + var collector = new FakeLogCollector(); |
| 146 | + using var loggerFactory = LoggerFactory.Create(b => b.AddProvider(new FakeLoggerProvider(collector))); |
| 147 | + |
| 148 | + var expectedException = new InvalidOperationException("test exception message"); |
| 149 | + |
| 150 | + using var innerClient = new TestTextToSpeechClient |
| 151 | + { |
| 152 | + GetAudioAsyncCallback = (text, options, cancellationToken) => throw expectedException, |
| 153 | + GetStreamingAudioAsyncCallback = (text, options, cancellationToken) => throw expectedException, |
| 154 | + GetServiceCallback = (serviceType, serviceKey) => |
| 155 | + serviceType == typeof(TextToSpeechClientMetadata) ? new TextToSpeechClientMetadata("testservice", new Uri("http://localhost:12345"), "testmodel") : |
| 156 | + null, |
| 157 | + }; |
| 158 | + |
| 159 | + using var client = innerClient |
| 160 | + .AsBuilder() |
| 161 | + .UseOpenTelemetry(loggerFactory, sourceName) |
| 162 | + .Build(); |
| 163 | + |
| 164 | + if (streaming) |
| 165 | + { |
| 166 | + await Assert.ThrowsAsync<InvalidOperationException>(async () => |
| 167 | + { |
| 168 | + await foreach (var update in client.GetStreamingAudioAsync("Hello")) |
| 169 | + { |
| 170 | + _ = update; |
| 171 | + } |
| 172 | + }); |
| 173 | + } |
| 174 | + else |
| 175 | + { |
| 176 | + await Assert.ThrowsAsync<InvalidOperationException>(() => |
| 177 | + client.GetAudioAsync("Hello")); |
| 178 | + } |
| 179 | + |
| 180 | + var activity = Assert.Single(activities); |
| 181 | + |
| 182 | + // Existing error behavior is preserved |
| 183 | + Assert.Equal(expectedException.GetType().FullName, activity.GetTagItem("error.type")); |
| 184 | + Assert.Equal(ActivityStatusCode.Error, activity.Status); |
| 185 | + |
| 186 | + // Exception is logged via ILogger |
| 187 | + var logEntry = Assert.Single(collector.GetSnapshot()); |
| 188 | + Assert.Equal("gen_ai.client.operation.exception", logEntry.Id.Name); |
| 189 | + Assert.Equal(LogLevel.Warning, logEntry.Level); |
| 190 | + Assert.Same(expectedException, logEntry.Exception); |
| 191 | + } |
130 | 192 | } |
0 commit comments