Skip to content

Commit c9dfdd7

Browse files
authored
Merge pull request #1171 from qq362220083/master
Update activity status on failure MySqlDataReader.
2 parents 88a0fcf + c007280 commit c9dfdd7

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/MySqlConnector/MySqlDataReader.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,8 +474,13 @@ internal static async Task<MySqlDataReader> CreateAsync(CommandListPosition comm
474474
await dataReader.NextResultAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
475475
}
476476
}
477-
catch (Exception)
477+
catch (Exception ex)
478478
{
479+
if (activity is { IsAllDataRequested: true })
480+
{
481+
activity.SetException(ex);
482+
activity.Stop();
483+
}
479484
dataReader.Dispose();
480485
throw;
481486
}

src/MySqlConnector/Utilities/ActivitySourceHelper.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ internal static class ActivitySourceHelper
1717
public const string NetPeerNameTagName = "net.peer.name";
1818
public const string NetPeerPortTagName = "net.peer.port";
1919
public const string NetTransportTagName = "net.transport";
20+
#if !NET6_0_OR_GREATER
2021
public const string StatusCodeTagName = "otel.status_code";
22+
#endif
2123
public const string ThreadIdTagName = "thread.id";
2224

2325
public const string DatabaseSystemValue = "mysql";
@@ -36,12 +38,30 @@ internal static class ActivitySourceHelper
3638
return activity;
3739
}
3840

39-
public static void SetSuccess(this Activity activity) => activity.SetTag(StatusCodeTagName, "OK");
41+
public static void SetSuccess(this Activity activity)
42+
{
43+
#if NET6_0_OR_GREATER
44+
if (activity.Status == ActivityStatusCode.Unset)
45+
{
46+
activity.SetStatus(ActivityStatusCode.Ok);
47+
}
48+
#else
49+
if (activity.Duration == TimeSpan.Zero)
50+
{
51+
activity.SetTag(StatusCodeTagName, "OK");
52+
}
53+
#endif
54+
}
4055

4156
public static void SetException(this Activity activity, Exception exception)
4257
{
58+
var description = exception is MySqlException mySqlException ? mySqlException.ErrorCode.ToString() : exception.Message;
59+
#if NET6_0_OR_GREATER
60+
activity.SetStatus(ActivityStatusCode.Error, description);
61+
#else
4362
activity.SetTag(StatusCodeTagName, "ERROR");
44-
activity.SetTag("otel.status_description", exception is MySqlException mySqlException ? mySqlException.ErrorCode.ToString() : exception.Message);
63+
activity.SetTag("otel.status_description", description);
64+
#endif
4565
activity.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection
4666
{
4767
{ "exception.type", exception.GetType().FullName },

0 commit comments

Comments
 (0)