forked from mysql/mysql-connector-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WL#15707 [Add OpenTelemetry tracing]
Change-Id: Id1fc09926e37c52c66df6c2d3a00d2fcb78c91ad
- Loading branch information
Reggie Burnett
committed
Jun 12, 2023
1 parent
98633c6
commit 2d11201
Showing
7 changed files
with
238 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
MySQL.Data.OpenTelemetry/src/MySQL.Data.OpenTelemetry.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Description>MySql.Data.OpenTelemetry</Description> | ||
<Copyright>Copyright (c) 2023, Oracle and/or its affiliates.</Copyright> | ||
<NeutralLanguage>en-US</NeutralLanguage> | ||
<Version>8.1.0</Version> | ||
<Authors>Oracle</Authors> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<AssemblyName>MySql.Data.OpenTelemetry</AssemblyName> | ||
<PackageId>MySql.Data.OpenTelemetry</PackageId> | ||
<PackageTags>MySql;.NET Connector;MySql Connector/NET;ado;ado.net;database;sql;opentelemetry;tracing;diagnostics;instrumentation</PackageTags> | ||
<PackageIconUrl>http://www.mysql.com/common/logos/logo-mysql-170x115.png</PackageIconUrl> | ||
<PackageProjectUrl>https://dev.mysql.com/downloads/</PackageProjectUrl> | ||
<PackageLicenseExpression>GPL-2.0-only</PackageLicenseExpression> | ||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance> | ||
<SignAssembly>True</SignAssembly> | ||
<DelaySign>True</DelaySign> | ||
<AssemblyOriginatorKeyFile>..\..\ConnectorNetPublicKey.snk</AssemblyOriginatorKeyFile> | ||
<GeneratePackageOnBuild>True</GeneratePackageOnBuild> | ||
<Title>MySql.Data.OpenTelemetry</Title> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="OpenTelemetry.API" Version="1.4.0" /> | ||
</ItemGroup> | ||
</Project> |
12 changes: 12 additions & 0 deletions
12
MySQL.Data.OpenTelemetry/src/TraceProviderBuilderExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using OpenTelemetry.Trace; | ||
|
||
/// <summary> | ||
/// Extension method for setting up Connector/Net OpenTelemetry tracing. | ||
/// </summary> | ||
public static class TracerProviderBuilderExtensions | ||
{ | ||
public static TracerProviderBuilder AddConnectorNet( | ||
this TracerProviderBuilder builder) | ||
=> builder.AddSource("connector-net"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System; | ||
using System.Data; | ||
using System.Diagnostics; | ||
using System.Reflection; | ||
using System.Threading; | ||
using MySql.Data.MySqlClient; | ||
using Mysqlx.Notice; | ||
|
||
namespace MySql.Data | ||
{ | ||
#if NET5_0_OR_GREATER | ||
static class MySQLActivitySource | ||
{ | ||
static readonly ActivitySource Source; | ||
|
||
static MySQLActivitySource() | ||
{ | ||
var assembly = typeof(MySQLActivitySource).Assembly; | ||
var version = assembly.GetCustomAttribute<AssemblyFileVersionAttribute>()?.Version ?? "0.0.0"; | ||
Source = new("connector-net", version); | ||
} | ||
|
||
private static bool Active => Source.HasListeners(); | ||
|
||
internal static Activity OpenConnection(MySqlConnectionStringBuilder settings) | ||
{ | ||
return InternalOpenConnection(settings, "Connection Open"); | ||
} | ||
|
||
internal static Activity OpenPooledConnection(MySqlConnectionStringBuilder settings) | ||
{ | ||
return InternalOpenConnection(settings, "Connection Open (pooled)"); | ||
} | ||
|
||
private static Activity InternalOpenConnection(MySqlConnectionStringBuilder settings, string name) | ||
{ | ||
if (!Active) return null; | ||
|
||
var activity = Source.StartActivity(name, ActivityKind.Client, Activity.Current.Context); | ||
activity?.SetTag("net.transport", settings.ConnectionProtocol); | ||
activity?.SetTag("db.connection_string", settings.GetConnectionString(false)); | ||
if (settings.ConnectionProtocol == MySqlConnectionProtocol.Tcp) | ||
activity?.SetTag("net.peer.port", settings.Port); | ||
|
||
return activity; | ||
} | ||
|
||
internal static void CloseConnection(Activity activity) | ||
{ | ||
activity?.SetTag("otel.status_code", "OK"); | ||
activity?.Dispose(); | ||
} | ||
|
||
internal static void SetException(Activity activity, Exception ex) | ||
{ | ||
var tags = new ActivityTagsCollection | ||
{ | ||
{ "exception.type", ex.GetType().FullName }, | ||
{ "exception.message", ex.Message }, | ||
{ "exception.stacktrace", ex.ToString() }, | ||
}; | ||
|
||
var activityEvent = new ActivityEvent("exception", tags: tags); | ||
activity?.AddEvent(activityEvent); | ||
activity?.SetTag("otel.status_code", "ERROR"); | ||
activity?.SetTag("otel.status_description", ex is MySqlException ? (ex as MySqlException).SqlState : ex.Message); | ||
activity?.Dispose(); | ||
} | ||
|
||
|
||
internal static Activity CommandStart(MySqlCommand command) | ||
{ | ||
if (!Active) return null; | ||
|
||
var settings = command.Connection.Settings; | ||
|
||
var activity = Source.StartActivity("SQL Statement", ActivityKind.Client, Activity.Current.Context); | ||
|
||
// passing through this attribute will propagate the context into the server | ||
string query_attr = $"00-{Activity.Current.Context.TraceId}-{Activity.Current.Context.SpanId}-00"; | ||
command.Attributes.SetAttribute("traceparent", query_attr); | ||
|
||
activity?.SetTag("db.system", "mysql"); | ||
activity?.SetTag("db.name", command.Connection.Database); | ||
activity?.SetTag("db.user", command.Connection.Settings.UserID); | ||
activity?.SetTag("thread.id", Thread.CurrentThread.ManagedThreadId); | ||
activity?.SetTag("thread.name", Thread.CurrentThread.Name); | ||
if (command.CommandType == CommandType.TableDirect) | ||
activity?.SetTag("db.sql.table", command.CommandText); | ||
return activity; | ||
} | ||
|
||
internal static void ReceivedFirstResponse(Activity activity) | ||
{ | ||
var activityEvent = new ActivityEvent("first-packet-received"); | ||
activity.AddEvent(activityEvent); | ||
} | ||
|
||
internal static void CommandStop(Activity activity) | ||
{ | ||
activity?.SetTag("otel.status_code", "OK"); | ||
activity?.Dispose(); | ||
} | ||
} | ||
#endif | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.