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

Reintroduce System.Data.SqlClient trace instrumentation #1650

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ jobs:
dotnet-version: |
6.0.x
7.0.x
- name: Install SQL Server (localdb)
uses: potatoqualitee/[email protected]
if: ${{ runner.os == 'Windows' }}
with:
install: localdb
- run: ./build.cmd Workflow --containers ${{ matrix.containers }}
- name: Upload logs
uses: actions/[email protected]
Expand Down
4 changes: 1 addition & 3 deletions OpenTelemetry.AutoInstrumentation.proj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
<TestApplicationProject Include="test\test-applications\integrations\*\*.csproj" />
<TestApplicationProject Include="test\test-applications\integrations\dependency-libs\*\*.csproj" />
<TestApplicationProject Remove="test\test-applications\integrations\TestApplication.AspNet\TestApplication.AspNet.csproj" />
<TestApplicationProject Remove="test\test-applications\integrations\TestApplication.Wcf.Server.NetFramework\TestApplication.Wcf.Server.NetFramework.csproj" Condition="'$(OS)' != 'Windows_NT'" />
<TestApplicationProject Remove="test\test-applications\integrations\TestApplication.Wcf.Client.NetFramework\TestApplication.Wcf.Client.NetFramework.csproj" Condition="'$(OS)' != 'Windows_NT'" />
<TestApplicationProjectFX Include="test\test-applications\integrations\TestApplication.AspNet\TestApplication.AspNet.csproj" Condition="'$(OS)' == 'Windows_NT'" />
<TestApplicationProject Remove="test\test-applications\integrations\*.NetFramework\*.NetFramework.csproj" Condition="'$(OS)' != 'Windows_NT'" />
<MockProject Include="test\test-applications\mocks\*\*.csproj" />
<IntegrationProject Include="test\IntegrationTests\IntegrationTests.csproj" />
</ItemGroup>
Expand Down
15 changes: 15 additions & 0 deletions OpenTelemetry.AutoInstrumentation.sln
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestApplication.Modules", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestApplication.Http.NetFramework", "test\test-applications\integrations\TestApplication.Http.NetFramework\TestApplication.Http.NetFramework.csproj", "{5D0FECF8-1954-449D-8C42-5373D91154FA}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestApplication.SqlClient.NetFramework", "test\test-applications\integrations\TestApplication.SqlClient.NetFramework\TestApplication.SqlClient.NetFramework.csproj", "{C5EB9F63-DE06-4444-A0DB-08F1CAC07B42}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -751,6 +753,18 @@ Global
{5D0FECF8-1954-449D-8C42-5373D91154FA}.Release|x64.Build.0 = Release|x64
{5D0FECF8-1954-449D-8C42-5373D91154FA}.Release|x86.ActiveCfg = Release|x86
{5D0FECF8-1954-449D-8C42-5373D91154FA}.Release|x86.Build.0 = Release|x86
{C5EB9F63-DE06-4444-A0DB-08F1CAC07B42}.Debug|Any CPU.ActiveCfg = Debug|x64
{C5EB9F63-DE06-4444-A0DB-08F1CAC07B42}.Debug|Any CPU.Build.0 = Debug|x64
{C5EB9F63-DE06-4444-A0DB-08F1CAC07B42}.Debug|x64.ActiveCfg = Debug|x64
{C5EB9F63-DE06-4444-A0DB-08F1CAC07B42}.Debug|x64.Build.0 = Debug|x64
{C5EB9F63-DE06-4444-A0DB-08F1CAC07B42}.Debug|x86.ActiveCfg = Debug|x86
{C5EB9F63-DE06-4444-A0DB-08F1CAC07B42}.Debug|x86.Build.0 = Debug|x86
{C5EB9F63-DE06-4444-A0DB-08F1CAC07B42}.Release|Any CPU.ActiveCfg = Release|x64
{C5EB9F63-DE06-4444-A0DB-08F1CAC07B42}.Release|Any CPU.Build.0 = Release|x64
{C5EB9F63-DE06-4444-A0DB-08F1CAC07B42}.Release|x64.ActiveCfg = Release|x64
{C5EB9F63-DE06-4444-A0DB-08F1CAC07B42}.Release|x64.Build.0 = Release|x64
{C5EB9F63-DE06-4444-A0DB-08F1CAC07B42}.Release|x86.ActiveCfg = Release|x86
{C5EB9F63-DE06-4444-A0DB-08F1CAC07B42}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -809,6 +823,7 @@ Global
{0A950D85-E813-4CA3-8927-334BB7787372} = {00F4C92D-6652-4BD8-A334-B35D3E711BE6}
{F8C3A1FF-2333-45C8-9174-75F12526DFDD} = {E409ADD3-9574-465C-AB09-4324D205CC7C}
{5D0FECF8-1954-449D-8C42-5373D91154FA} = {E409ADD3-9574-465C-AB09-4324D205CC7C}
{C5EB9F63-DE06-4444-A0DB-08F1CAC07B42} = {E409ADD3-9574-465C-AB09-4324D205CC7C}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {160A1D00-1F5B-40F8-A155-621B4459D78F}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static TracerProviderBuilder AddMySqlClientInstrumentation(TracerProvider
[MethodImpl(MethodImplOptions.NoInlining)]
public static TracerProviderBuilder AddSqlClientInstrumentation(TracerProviderBuilder builder, PluginManager pluginManager, LazyInstrumentationLoader lazyInstrumentationLoader)
{
lazyInstrumentationLoader.Add(new SqlClientInitializer(pluginManager));
new SqlClientInitializer(lazyInstrumentationLoader, pluginManager);

return builder.AddSource("OpenTelemetry.SqlClient");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,35 @@
// </copyright>

using System;
using System.Threading;
using OpenTelemetry.AutoInstrumentation.Plugins;

namespace OpenTelemetry.AutoInstrumentation.Loading.Initializers;

internal class SqlClientInitializer : InstrumentationInitializer
internal class SqlClientInitializer
{
private readonly PluginManager _pluginManager;

public SqlClientInitializer(PluginManager pluginManager)
: base("Microsoft.Data.SqlClient")
private int _initialized;

public SqlClientInitializer(LazyInstrumentationLoader lazyInstrumentationLoader, PluginManager pluginManager)
{
_pluginManager = pluginManager;
lazyInstrumentationLoader.Add(new GenericInitializer("Microsoft.Data.SqlClient", InitializeOnFirstCall));

#if NETFRAMEWORK
lazyInstrumentationLoader.Add(new GenericInitializer("System.Data", InitializeOnFirstCall));
#endif
}

public override void Initialize(ILifespanManager lifespanManager)
private void InitializeOnFirstCall(ILifespanManager lifespanManager)
{
if (Interlocked.Exchange(ref _initialized, value: 1) != default)
{
// InitializeOnFirstCall() was already called before
return;
}

var instrumentationType = Type.GetType("OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentation, OpenTelemetry.Instrumentation.SqlClient");

var options = new OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions();
Expand Down
74 changes: 74 additions & 0 deletions test/IntegrationTests/Helpers/RuntimeHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// <copyright file="RuntimeHelper.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// 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.
// </copyright>

#if NETFRAMEWORK

using Microsoft.Win32;

namespace IntegrationTests.Helpers;

internal static class RuntimeHelper
{
// https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed
public static string GetRuntimeVersion()
{
const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";

using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
{
if (ndpKey != null && ndpKey.GetValue("Release") != null)
{
return CheckFor45PlusVersion((int)ndpKey.GetValue("Release"));
}

return null;
}

// Checking the version using >= enables forward compatibility.
static string CheckFor45PlusVersion(int releaseKey)
{
#pragma warning disable SA1503 // Braces should not be omitted
if (releaseKey >= 533320)
return "4.8.1+";
if (releaseKey >= 528040)
return "4.8";
if (releaseKey >= 461808)
return "4.7.2";
if (releaseKey >= 461308)
return "4.7.1";
if (releaseKey >= 460798)
return "4.7";
if (releaseKey >= 394802)
return "4.6.2";
if (releaseKey >= 394254)
return "4.6.1";
if (releaseKey >= 393295)
return "4.6";
if (releaseKey >= 379893)
return "4.5.2";
if (releaseKey >= 378675)
return "4.5.1";
if (releaseKey >= 378389)
return "4.5";
// This code should never execute. A non-null release key should mean
// that 4.5 or later is installed.
return "No 4.5+";
#pragma warning restore SA1503 // Braces should not be omitted
}
}
}

#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="SqlClientTests.cs" company="OpenTelemetry Authors">
// <copyright file="SqlClientMicrosoftTests.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -21,11 +21,11 @@
namespace IntegrationTests;

[Collection(SqlServerCollection.Name)]
public class SqlClientTests : TestHelper
public class SqlClientMicrosoftTests : TestHelper
{
private readonly SqlServerFixture _sqlServerFixture;

public SqlClientTests(ITestOutputHelper output, SqlServerFixture sqlServerFixture)
public SqlClientMicrosoftTests(ITestOutputHelper output, SqlServerFixture sqlServerFixture)
: base("SqlClient", output)
{
_sqlServerFixture = sqlServerFixture;
Expand Down
59 changes: 59 additions & 0 deletions test/IntegrationTests/SqlClientSystemDataTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// <copyright file="SqlClientSystemDataTests.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// 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.
// </copyright>

#if NETFRAMEWORK

using IntegrationTests.Helpers;
using Xunit;
using Xunit.Abstractions;

namespace IntegrationTests;

public class SqlClientSystemDataTests : TestHelper
{
public SqlClientSystemDataTests(ITestOutputHelper output)
: base("SqlClient.NetFramework", output)
{
}

[IgnoreRunningOnNet481Fact]
[Trait("Category", "EndToEnd")]
public void SubmitTraces()
{
using var collector = new MockSpansCollector(Output);
SetExporter(collector);
collector.Expect("OpenTelemetry.SqlClient");

RunTestApplication();

collector.AssertExpectations();
}
}

public sealed class IgnoreRunningOnNet481Fact : FactAttribute
{
public IgnoreRunningOnNet481Fact()
{
var netVersion = RuntimeHelper.GetRuntimeVersion();
if (netVersion == "4.8.1+")
{
// https://github.com/open-telemetry/opentelemetry-dotnet/issues/3901
Skip = "NET Framework 4.8.1 is skipped due bug.";
}
}
}

#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"profiles": {
"TestApplication.Http.NetFramework": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:63944"
}
}
}
Loading