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

Remove dependency from OpenTelemetry.Instrumentation.SqlClient #3059

Merged
merged 11 commits into from
Mar 22, 2024
1 change: 1 addition & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<SharedDir>$(MSBuildThisFileDirectory)/src/Shared/</SharedDir>
<TestsSharedDir>$(MSBuildThisFileDirectory)/tests/Shared/</TestsSharedDir>
<VendoringDir>$(MSBuildThisFileDirectory)/src/Vendoring/</VendoringDir>
<!-- Capture PackageIconFullPath into DefaultDotnetIconFullPath before we overwrite PackageIconFullPath. -->
<!-- DefaultDotnetIconFullPath is only needed for the ServiceDisovery packages. The property can be removed when these libraries move. See https://github.com/dotnet/aspire/issues/170 -->
<DefaultDotnetIconFullPath>$(PackageIconFullPath)</DefaultDotnetIconFullPath>
Expand Down
3 changes: 1 addition & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.7.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.7.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.7.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.6.0-beta.3" />
<PackageVersion Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.0.0-rc9.13" />
<!-- build dependencies -->
<PackageVersion Include="MicroBuild.Plugins.SwixBuild.Dotnet" Version="1.1.87-gba258badda" />
Expand Down Expand Up @@ -143,4 +142,4 @@
<PackageVersion Include="Newtonsoft.Json" Version="13.0.3" />
<!-- Introduced by Microsoft.Azure.Cosmos, https://github.com/Azure/azure-cosmos-dotnet-v3/issues/4302 -->
</ItemGroup>
</Project>
</Project>
17 changes: 17 additions & 0 deletions THIRD-PARTY-NOTICES.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,20 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

License notice for OpenTelemetry .NET (https://github.com/open-telemetry/opentelemetry-dotnet)
sebastienros marked this conversation as resolved.
Show resolved Hide resolved
----------------------------------------------------------------------------------------------

Copyright 2023 OpenTelemetry Authors
sebastienros marked this conversation as resolved.
Show resolved Hide resolved

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,19 @@
<Compile Include="..\Common\ConnectionStringValidation.cs" Link="ConnectionStringValidation.cs" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(VendoringDir)OpenTelemetry.Instrumentation.SqlClient\**\*.cs" LinkBase="OpenTelemetry.Instrumentation.SqlClient" />
<Compile Include="$(VendoringDir)OpenTelemetry.Shared\**\*.cs" LinkBase="OpenTelemetry.Instrumentation.SqlClient.Shared" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="AspNetCore.HealthChecks.SqlServer" />
<PackageReference Include="Microsoft.Data.SqlClient" />
sebastienros marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think when I imported some source this was required for a specific type. Now not anymore so I assume that I removed a file that was the culprit. Removed.

<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
<PackageReference Include="OpenTelemetry.Instrumentation.EventCounters" />
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@
<Compile Include="..\Common\EntityFrameworkUtils.cs" Link="EntityFrameworkUtils.cs" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(VendoringDir)OpenTelemetry.Instrumentation.SqlClient\**\*.cs" LinkBase="OpenTelemetry.Instrumentation.SqlClient" />
<Compile Include="$(VendoringDir)OpenTelemetry.Shared\**\*.cs" LinkBase="OpenTelemetry.Instrumentation.SqlClient.Shared" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" />
<PackageReference Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" />
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" />
<PackageReference Include="OpenTelemetry.Instrumentation.EventCounters" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.

using System.Diagnostics.CodeAnalysis;

[assembly: SuppressMessage("Style", "IDE0028:Simplify collection initialization", Justification = "Vendor code", Scope = "namespaceanddescendants", Target = "~N:OpenTelemetry.Instrumentation.SqlClient")]
sebastienros marked this conversation as resolved.
Show resolved Hide resolved
[assembly: SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Vendor code", Scope = "namespaceanddescendants", Target = "~N:OpenTelemetry.Instrumentation")]
[assembly: SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Vendor code", Scope = "namespaceanddescendants", Target = "~N:OpenTelemetry.Instrumentation.SqlClient")]
[assembly: SuppressMessage("Style", "IDE0060:Remove unused parameter", Justification = "Vendor code", Scope = "namespaceanddescendants", Target = "~N:OpenTelemetry.Instrumentation")]
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#nullable disable

using System.Diagnostics;
using System.Reflection;
using OpenTelemetry.Trace;

namespace OpenTelemetry.Instrumentation.SqlClient.Implementation;

/// <summary>
/// Helper class to hold common properties used by both SqlClientDiagnosticListener on .NET Core
/// and SqlEventSourceListener on .NET Framework.
/// </summary>
internal sealed class SqlActivitySourceHelper
{
public const string MicrosoftSqlServerDatabaseSystemName = "mssql";

public static readonly AssemblyName AssemblyName = typeof(SqlActivitySourceHelper).Assembly.GetName();
public static readonly string ActivitySourceName = AssemblyName.Name;
public static readonly Version Version = AssemblyName.Version;
public static readonly ActivitySource ActivitySource = new(ActivitySourceName, Version.ToString());
public static readonly string ActivityName = ActivitySourceName + ".Execute";

public static readonly IEnumerable<KeyValuePair<string, object>> CreationTags = new[]
{
new KeyValuePair<string, object>(SemanticConventions.AttributeDbSystem, MicrosoftSqlServerDatabaseSystemName),
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#nullable disable

#if !NETFRAMEWORK
using System.Data;
using System.Diagnostics;
using OpenTelemetry.Trace;
#if NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif

namespace OpenTelemetry.Instrumentation.SqlClient.Implementation;

#if NET6_0_OR_GREATER
[RequiresUnreferencedCode(SqlClientInstrumentation.SqlClientTrimmingUnsupportedMessage)]
#endif
internal sealed class SqlClientDiagnosticListener : ListenerHandler
{
public const string SqlDataBeforeExecuteCommand = "System.Data.SqlClient.WriteCommandBefore";
public const string SqlMicrosoftBeforeExecuteCommand = "Microsoft.Data.SqlClient.WriteCommandBefore";

public const string SqlDataAfterExecuteCommand = "System.Data.SqlClient.WriteCommandAfter";
public const string SqlMicrosoftAfterExecuteCommand = "Microsoft.Data.SqlClient.WriteCommandAfter";

public const string SqlDataWriteCommandError = "System.Data.SqlClient.WriteCommandError";
public const string SqlMicrosoftWriteCommandError = "Microsoft.Data.SqlClient.WriteCommandError";

private readonly PropertyFetcher<object> commandFetcher = new("Command");
private readonly PropertyFetcher<object> connectionFetcher = new("Connection");
private readonly PropertyFetcher<object> dataSourceFetcher = new("DataSource");
private readonly PropertyFetcher<object> databaseFetcher = new("Database");
private readonly PropertyFetcher<CommandType> commandTypeFetcher = new("CommandType");
private readonly PropertyFetcher<object> commandTextFetcher = new("CommandText");
private readonly PropertyFetcher<Exception> exceptionFetcher = new("Exception");
private readonly SqlClientTraceInstrumentationOptions options;

public SqlClientDiagnosticListener(string sourceName, SqlClientTraceInstrumentationOptions options)
: base(sourceName)
{
this.options = options ?? new SqlClientTraceInstrumentationOptions();
}

public override bool SupportsNullActivity => true;

public override void OnEventWritten(string name, object payload)
{
var activity = Activity.Current;
switch (name)
{
case SqlDataBeforeExecuteCommand:
case SqlMicrosoftBeforeExecuteCommand:
{
// SqlClient does not create an Activity. So the activity coming in here will be null or the root span.
activity = SqlActivitySourceHelper.ActivitySource.StartActivity(
SqlActivitySourceHelper.ActivityName,
ActivityKind.Client,
default(ActivityContext),
SqlActivitySourceHelper.CreationTags);

if (activity == null)
{
// There is no listener or it decided not to sample the current request.
return;
}

_ = this.commandFetcher.TryFetch(payload, out var command);
if (command == null)
{
SqlClientInstrumentationEventSource.Log.NullPayload(nameof(SqlClientDiagnosticListener), name);
activity.Stop();
return;
}

if (activity.IsAllDataRequested)
{
try
{
if (this.options.Filter?.Invoke(command) == false)
{
SqlClientInstrumentationEventSource.Log.CommandIsFilteredOut(activity.OperationName);
activity.IsAllDataRequested = false;
activity.ActivityTraceFlags &= ~ActivityTraceFlags.Recorded;
return;
}
}
catch (Exception ex)
{
SqlClientInstrumentationEventSource.Log.CommandFilterException(ex);
activity.IsAllDataRequested = false;
activity.ActivityTraceFlags &= ~ActivityTraceFlags.Recorded;
return;
}

_ = this.connectionFetcher.TryFetch(command, out var connection);
_ = this.databaseFetcher.TryFetch(connection, out var database);

activity.DisplayName = (string)database;

_ = this.dataSourceFetcher.TryFetch(connection, out var dataSource);
_ = this.commandTextFetcher.TryFetch(command, out var commandText);

activity.SetTag(SemanticConventions.AttributeDbName, (string)database);

this.options.AddConnectionLevelDetailsToActivity((string)dataSource, activity);

if (this.commandTypeFetcher.TryFetch(command, out CommandType commandType))
{
switch (commandType)
{
case CommandType.StoredProcedure:
if (this.options.SetDbStatementForStoredProcedure)
{
activity.SetTag(SemanticConventions.AttributeDbStatement, (string)commandText);
}

break;

case CommandType.Text:
if (this.options.SetDbStatementForText)
{
activity.SetTag(SemanticConventions.AttributeDbStatement, (string)commandText);
}

break;

case CommandType.TableDirect:
break;
}
}

try
{
this.options.Enrich?.Invoke(activity, "OnCustom", command);
}
catch (Exception ex)
{
SqlClientInstrumentationEventSource.Log.EnrichmentException(ex);
}
}
}

break;
case SqlDataAfterExecuteCommand:
case SqlMicrosoftAfterExecuteCommand:
{
if (activity == null)
{
SqlClientInstrumentationEventSource.Log.NullActivity(name);
return;
}

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

activity.Stop();
}

break;
case SqlDataWriteCommandError:
case SqlMicrosoftWriteCommandError:
{
if (activity == null)
{
SqlClientInstrumentationEventSource.Log.NullActivity(name);
return;
}

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

try
{
if (activity.IsAllDataRequested)
{
if (this.exceptionFetcher.TryFetch(payload, out Exception exception) && exception != null)
{
activity.SetStatus(ActivityStatusCode.Error, exception.Message);

if (this.options.RecordException)
{
activity.RecordException(exception);
}
}
else
{
SqlClientInstrumentationEventSource.Log.NullPayload(nameof(SqlClientDiagnosticListener), name);
}
}
}
finally
{
activity.Stop();
}
}

break;
}
}
}
#endif
Loading