Skip to content

Commit

Permalink
Merge pull request #1334 from Kielek/activities-handle-status
Browse files Browse the repository at this point in the history
Update handling of successful Activity status.
  • Loading branch information
bgrainger authored Jun 24, 2023
2 parents 675b43d + 9b57d39 commit d354b8e
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 30 deletions.
2 changes: 0 additions & 2 deletions src/MySqlConnector/MySqlConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,6 @@ internal async Task OpenAsync(IOBehavior? ioBehavior, CancellationToken cancella

if (m_connectionSettings.AutoEnlist && System.Transactions.Transaction.Current is not null)
EnlistTransaction(System.Transactions.Transaction.Current);

activity?.SetSuccess();
}
catch (Exception ex) when (activity is { IsAllDataRequested: true })
{
Expand Down
5 changes: 0 additions & 5 deletions src/MySqlConnector/MySqlDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,6 @@ internal async Task InitAsync(CommandListPosition commandListPosition, ICommandP
throw new InvalidOperationException("Expected ColumnDefinitions to be null");
m_closed = false;
m_hasWarnings = false;
m_initializationFailed = false;
RealRecordsAffected = null;

// initialize for new command
Expand Down Expand Up @@ -503,7 +502,6 @@ internal async Task InitAsync(CommandListPosition commandListPosition, ICommandP
activity.SetException(ex);
activity.Stop();
}
m_initializationFailed = true;
Dispose();
throw;
}
Expand Down Expand Up @@ -638,8 +636,6 @@ internal async Task DisposeAsync(IOBehavior ioBehavior, CancellationToken cancel
Command.CancellableCommand.SetTimeout(Constants.InfiniteTimeout);
connection.FinishQuerying(m_hasWarnings);

if (!m_initializationFailed)
Activity?.SetSuccess();
Activity?.Stop();
Activity = null;

Expand Down Expand Up @@ -709,6 +705,5 @@ private ResultSet GetResultSet()
private bool m_closed;
private bool m_hasWarnings;
private bool m_hasMoreResults;
private bool m_initializationFailed;
private DataTable? m_schemaTable;
}
2 changes: 0 additions & 2 deletions src/MySqlConnector/MySqlTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ private async Task CommitAsync(IOBehavior ioBehavior, CancellationToken cancella
await cmd.ExecuteNonQueryAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
Connection!.CurrentTransaction = null;
Connection = null;
activity?.SetSuccess();
}
catch (Exception ex) when (activity is { IsAllDataRequested: true })
{
Expand Down Expand Up @@ -260,7 +259,6 @@ private async Task DoRollback(IOBehavior ioBehavior, CancellationToken cancellat
{
using var cmd = new MySqlCommand("rollback", Connection, this) { NoActivity = true };
await cmd.ExecuteNonQueryAsync(ioBehavior, cancellationToken).ConfigureAwait(false);
activity?.SetSuccess();
}
catch (Exception ex) when (activity is { IsAllDataRequested: true })
{
Expand Down
9 changes: 0 additions & 9 deletions src/MySqlConnector/Utilities/ActivitySourceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ internal static class ActivitySourceHelper
public const string NetPeerNameTagName = "net.peer.name";
public const string NetPeerPortTagName = "net.peer.port";
public const string NetTransportTagName = "net.transport";
public const string StatusCodeTagName = "otel.status_code";
public const string ThreadIdTagName = "thread.id";

public const string DatabaseSystemValue = "mysql";
Expand All @@ -35,18 +34,10 @@ internal static class ActivitySourceHelper
return activity;
}

public static void SetSuccess(this Activity activity)
{
activity.SetStatus(ActivityStatusCode.Ok);
activity.SetTag(StatusCodeTagName, "OK");
}

public static void SetException(this Activity activity, Exception exception)
{
var description = exception is MySqlException mySqlException ? mySqlException.ErrorCode.ToString() : exception.Message;
activity.SetStatus(ActivityStatusCode.Error, description);
activity.SetTag(StatusCodeTagName, "ERROR");
activity.SetTag("otel.status_description", description);
activity.AddEvent(new ActivityEvent("exception", tags: new ActivityTagsCollection
{
{ "exception.type", exception.GetType().FullName },
Expand Down
14 changes: 2 additions & 12 deletions tests/IntegrationTests/ActivityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,9 @@ public void OpenTags()
Assert.NotNull(activity);
Assert.Equal(ActivityKind.Client, activity.Kind);
Assert.Equal("Open", activity.OperationName);
#if NET6_0_OR_GREATER
Assert.Equal(ActivityStatusCode.Ok, activity.Status);
#endif
Assert.Equal(ActivityStatusCode.Unset, activity.Status);

AssertTags(activity.Tags, csb);
AssertTag(activity.Tags, "otel.status_code", "OK");
}

[Fact]
Expand Down Expand Up @@ -74,7 +71,6 @@ public void OpenFromPoolTags()
connection.Open();

Assert.NotNull(activities[i]);
AssertTag(activities[i].Tags, "otel.status_code", "OK");
}

// activities should have the same connection ID
Expand Down Expand Up @@ -104,12 +100,9 @@ public void OpenFailedTags()
Assert.NotNull(activity);
Assert.Equal(ActivityKind.Client, activity.Kind);
Assert.Equal("Open", activity.OperationName);
#if NET6_0_OR_GREATER
Assert.Equal(ActivityStatusCode.Error, activity.Status);
#endif

AssertTags(activity.Tags, csb);
AssertTag(activity.Tags, "otel.status_code", "ERROR");
}

[Fact]
Expand Down Expand Up @@ -140,14 +133,11 @@ public void SelectTags()
Assert.NotNull(activity);
Assert.Equal(ActivityKind.Client, activity.Kind);
Assert.Equal("Execute", activity.OperationName);
#if NET6_0_OR_GREATER
Assert.Equal(ActivityStatusCode.Ok, activity.Status);
#endif
Assert.Equal(ActivityStatusCode.Unset, activity.Status);

AssertTags(activity.Tags, csb);
AssertTag(activity.Tags, "db.connection_id", connection.ServerThread.ToString(CultureInfo.InvariantCulture));
AssertTag(activity.Tags, "db.statement", "SELECT 1;");
AssertTag(activity.Tags, "otel.status_code", "OK");
}

private void AssertTags(IEnumerable<KeyValuePair<string, string>> tags, MySqlConnectionStringBuilder csb)
Expand Down

0 comments on commit d354b8e

Please sign in to comment.