Skip to content

Commit

Permalink
[Instrumentation.MySqlData] Fix analysis warnings
Browse files Browse the repository at this point in the history
Fix/suppress code analysis warnings in OpenTelemetry.Instrumentation.MySqlData.
Contributes to #950.
  • Loading branch information
martincostello committed Feb 21, 2023
1 parent 9277930 commit 50b67f2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 37 deletions.
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Globalization;
using System.Linq.Expressions;
using System.Reflection;
using MySql.Data.MySqlClient;
Expand Down Expand Up @@ -113,7 +114,7 @@ public MySqlDataInstrumentation(MySqlDataInstrumentationOptions options = null)
break;
case MySqlTraceEventType.QueryClosed:
// args: [driverId]
this.AfterExecuteCommand();
AfterExecuteCommand();
break;
case MySqlTraceEventType.StatementPrepared:
break;
Expand All @@ -129,21 +130,54 @@ public MySqlDataInstrumentation(MySqlDataInstrumentationOptions options = null)
break;
case MySqlTraceEventType.Error:
// args: [driverId, exNumber, exMessage]
this.ErrorExecuteCommand(this.GetMySqlErrorException(args[2]));
this.ErrorExecuteCommand(GetMySqlErrorException(args[2]));
break;
case MySqlTraceEventType.QueryNormalized:
// Should use QueryNormalized event when it exists. Because cmdText in QueryOpened event is incomplete when cmdText.length>300
// args: [driverId, threadId, normalized_query]
this.OverwriteDbStatement(this.GetCommand(args[0], args[2]));
break;
default:
MySqlDataInstrumentationEventSource.Log.UnknownMySqlTraceEventType(id, string.Format(format, args));
MySqlDataInstrumentationEventSource.Log.UnknownMySqlTraceEventType(id, string.Format(CultureInfo.InvariantCulture, format, args));
break;
}
}
catch (Exception e)
{
MySqlDataInstrumentationEventSource.Log.ErrorTraceEvent(id, string.Format(format, args), e.ToString());
MySqlDataInstrumentationEventSource.Log.ErrorTraceEvent(id, string.Format(CultureInfo.InvariantCulture, format, args), e.ToString());
}
}

private static Exception GetMySqlErrorException(object errorMsg)
{
#pragma warning disable CA2201 // Do not raise reserved exception types
return new Exception(errorMsg?.ToString());
#pragma warning restore CA2201 // Do not raise reserved exception types
}

private static void AfterExecuteCommand()
{
var activity = Activity.Current;
if (activity == null)
{
return;
}

if (activity.Source != MySqlActivitySourceHelper.ActivitySource)
{
return;
}

try
{
if (activity.IsAllDataRequested)
{
activity.SetStatus(Status.Unset);
}
}
finally
{
activity.Stop();
}
}

Expand Down Expand Up @@ -198,32 +232,6 @@ private void OverwriteDbStatement(MySqlDataTraceCommand command)
}
}

private void AfterExecuteCommand()
{
var activity = Activity.Current;
if (activity == null)
{
return;
}

if (activity.Source != MySqlActivitySourceHelper.ActivitySource)
{
return;
}

try
{
if (activity.IsAllDataRequested)
{
activity.SetStatus(Status.Unset);
}
}
finally
{
activity.Stop();
}
}

private void ErrorExecuteCommand(Exception exception)
{
var activity = Activity.Current;
Expand Down Expand Up @@ -266,11 +274,6 @@ private MySqlDataTraceCommand GetCommand(object driverIdObj, object cmd)
return command;
}

private Exception GetMySqlErrorException(object errorMsg)
{
return new Exception($"{errorMsg}");
}

private void AddConnectionLevelDetailsToActivity(MySqlConnectionStringBuilder dataSource, Activity sqlActivity)
{
if (!this.options.EnableConnectionLevelAttributes)
Expand Down
Expand Up @@ -55,7 +55,7 @@ public class MySqlDataTests

var traceListener = (TraceListener)Assert.Single(MySqlTrace.Listeners);

this.ExecuteSuccessQuery(traceListener, commandText, isFailure);
ExecuteSuccessQuery(traceListener, commandText, isFailure);

Assert.Equal(3, activityProcessor.Invocations.Count);

Expand Down Expand Up @@ -170,7 +170,7 @@ public void UnknownMySqlTraceEventType(MySqlTraceEventType eventType)
}
}

private void ExecuteSuccessQuery(TraceListener listener, string query, bool isFailure)
private static void ExecuteSuccessQuery(TraceListener listener, string query, bool isFailure)
{
// Connection opened
listener.TraceEvent(
Expand Down

0 comments on commit 50b67f2

Please sign in to comment.