Skip to content

Commit

Permalink
[Instrumentation.MySql] fix sql text incomplete (#424)
Browse files Browse the repository at this point in the history
* fix sql text incomplete
  • Loading branch information
chad122 authored Jun 28, 2022
1 parent 73d733f commit 00ac360
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Instrumentation.MySqlData/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Fix incomplete db.statement when the length>300
[(#424)](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/424)

## 1.0.0-beta.2

* Going forward the NuGet package will be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public override void TraceEvent(TraceEventCache eventCache, string source, Trace
this.ErrorExecuteCommand(this.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));
Expand Down Expand Up @@ -125,6 +128,28 @@ private void BeforeExecuteCommand(MySqlDataTraceCommand command)
}
}

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

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

if (activity.IsAllDataRequested)
{
if (this.options.SetDbStatement)
{
activity.SetTag(SemanticConventions.AttributeDbStatement, command.SqlText);
}
}
}

private void AfterExecuteCommand()
{
var activity = Activity.Current;
Expand Down

0 comments on commit 00ac360

Please sign in to comment.