Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Instrumentation.MySqlData] Fix analysis warnings #965

Merged
merged 2 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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 override void TraceEvent(
break;
case MySqlTraceEventType.QueryClosed:
// args: [driverId]
this.AfterExecuteCommand();
AfterExecuteCommand();
break;
case MySqlTraceEventType.StatementPrepared:
break;
Expand All @@ -129,21 +130,54 @@ public override void TraceEvent(
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void SuccessTraceEventTest(

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 @@ private static void VerifyActivityData(
}
}

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