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.SqlClient] Move package from main repository #1673

Merged
merged 11 commits into from
Apr 24, 2024
Prev Previous commit
Next Next commit
Fix build test project
  • Loading branch information
Kielek committed Apr 22, 2024

Verified

This commit was signed with the committer’s verified signature.
Kielek Piotr Kiełkowicz
commit 9eb8314df1da835aec50887a0c861c4950b22958
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma warning disable IDE0005 // Using directive is unnecessary.using System;
Copy link
Member

Choose a reason for hiding this comment

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

curious why do we need additional using? original file does not have it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Upstream - all usages have enable ImplicitUsings. In this repository, this file is also part of OpenTelemetry.Contrib.Tests.Shared. This feature is not enabled so I have to add it manually, but the SqlClient project enables Implicitusings - it requiers additional #pragma disable.

using System;
using System.Diagnostics;
using System.Text;
using Xunit;
#pragma warning restore IDE0005 // Using directive is unnecessary.

namespace OpenTelemetry.Tests;

/// <summary>
/// This <see cref="TheoryAttribute" /> skips tests if the required Docker engine is not available.
/// </summary>
internal class EnabledOnDockerPlatformTheoryAttribute : TheoryAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="EnabledOnDockerPlatformTheoryAttribute" /> class.
/// </summary>
public EnabledOnDockerPlatformTheoryAttribute(DockerPlatform dockerPlatform)
{
const string executable = "docker";

var stdout = new StringBuilder();
var stderr = new StringBuilder();

void AppendStdout(object sender, DataReceivedEventArgs e) => stdout.Append(e.Data);
void AppendStderr(object sender, DataReceivedEventArgs e) => stderr.Append(e.Data);

var processStartInfo = new ProcessStartInfo();
processStartInfo.FileName = executable;
processStartInfo.Arguments = string.Join(" ", "version", "--format '{{.Server.Os}}'");
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.UseShellExecute = false;

var process = new Process();
process.StartInfo = processStartInfo;
process.OutputDataReceived += AppendStdout;
process.ErrorDataReceived += AppendStderr;

try
{
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
finally
{
process.OutputDataReceived -= AppendStdout;
process.ErrorDataReceived -= AppendStderr;
}

if (0.Equals(process.ExitCode) && stdout.ToString().IndexOf(dockerPlatform.ToString(), StringComparison.OrdinalIgnoreCase) > 0)
{
return;
}

this.Skip = $"The Docker {dockerPlatform} engine is not available.";
}

public enum DockerPlatform
{
/// <summary>
/// Docker Linux engine.
/// </summary>
Linux,

/// <summary>
/// Docker Windows engine.
/// </summary>
Windows,
}
}
Original file line number Diff line number Diff line change
@@ -3,12 +3,14 @@

#nullable enable

#pragma warning disable IDE0005 // Using directive is unnecessary.
using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Globalization;
using System.Linq;
using System.Reflection;
#pragma warning restore IDE0005 // Using directive is unnecessary.

namespace OpenTelemetry.Tests;

Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;net462;net6.0;net8.0</TargetFrameworks>
<TargetFrameworks>net8.0;net6.0</TargetFrameworks>
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);net462</TargetFrameworks>
<IsTestProject>false</IsTestProject>
</PropertyGroup>

Original file line number Diff line number Diff line change
@@ -3,10 +3,12 @@

#nullable enable

#pragma warning disable IDE0005 // Using directive is unnecessary.
using System;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using System.Threading;
#pragma warning restore IDE0005 // Using directive is unnecessary.

namespace OpenTelemetry.Tests;

2 changes: 2 additions & 0 deletions test/OpenTelemetry.Contrib.Tests.Shared/TestSampler.cs
Original file line number Diff line number Diff line change
@@ -3,8 +3,10 @@

#nullable enable

#pragma warning disable IDE0005 // Using directive is unnecessary.
using System;
using OpenTelemetry.Trace;
#pragma warning restore IDE0005 // Using directive is unnecessary.

namespace OpenTelemetry.Tests;

Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>Unit test project for OpenTelemetry SqlClient instrumentations</Description>
<TargetFrameworks>$(TargetFrameworksForTests)</TargetFrameworks>
<TargetFrameworks>net8.0;net7.0;net6.0</TargetFrameworks>
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
<TargetFrameworks Condition="$(OS) == 'Windows_NT'">$(TargetFrameworks);net462</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<!-- this is temporary. will remove in future PR. -->
<Nullable>disable</Nullable>
</PropertyGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\EnabledOnDockerPlatformTheoryAttribute.cs" Link="Includes\EnabledOnDockerPlatformTheoryAttribute.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\SkipUnlessEnvVarFoundTheoryAttribute.cs" Link="Includes\SkipUnlessEnvVarFoundTheoryAttribute.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\EventSourceTestHelper.cs" Link="Includes\EventSourceTestHelper.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\TestEventListener.cs" Link="Includes\TestEventListener.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\TestSampler.cs" Link="Includes\TestSampler.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Contrib.Tests.Shared\ActivityHelperExtensions.cs" Link="Includes\ActivityHelperExtensions.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Contrib.Tests.Shared\EnabledOnDockerPlatformTheoryAttribute.cs" Link="Includes\EnabledOnDockerPlatformTheoryAttribute.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Contrib.Tests.Shared\SkipUnlessEnvVarFoundTheoryAttribute.cs" Link="Includes\SkipUnlessEnvVarFoundTheoryAttribute.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Contrib.Tests.Shared\EventSourceTestHelper.cs" Link="Includes\EventSourceTestHelper.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Contrib.Tests.Shared\TestEventListener.cs" Link="Includes\TestEventListener.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Contrib.Tests.Shared\TestSampler.cs" Link="Includes\TestSampler.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Testcontainers.MsSql" />
<PackageReference Include="Testcontainers.SqlEdge" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio" PrivateAssets="All">
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.3" />
<PackageReference Include="Testcontainers.MsSql" Version="3.6.0" />
<PackageReference Include="Testcontainers.SqlEdge" Version="3.6.0" />
<PackageReference Include="OpenTelemetry.Exporter.InMemory" Version="$(OpenTelemetryExporterInMemoryPkgVer)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Instrumentation.SqlClient\OpenTelemetry.Instrumentation.SqlClient.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry\OpenTelemetry.csproj" />
<ProjectReference Include="$(RepoRoot)\src\OpenTelemetry.Exporter.InMemory\OpenTelemetry.Exporter.InMemory.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -81,8 +81,9 @@ public void SuccessfulCommandTest(
string dataSource = sqlConnection.DataSource;

sqlConnection.ChangeDatabase("master");

#pragma warning disable CA2100
using SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection)
#pragma warning restore CA2100
{
CommandType = commandType,
};
Original file line number Diff line number Diff line change
@@ -6,17 +6,17 @@
using Microsoft.Data.SqlClient;
using Microsoft.Extensions.DependencyInjection;
using OpenTelemetry.Instrumentation.SqlClient.Implementation;
#if !NETFRAMEWORK
using OpenTelemetry.Tests;
#endif
using OpenTelemetry.Trace;
using Xunit;

namespace OpenTelemetry.Instrumentation.SqlClient.Tests;

public class SqlClientTests : IDisposable
{
#if !NETFRAMEWORK
private const string TestConnectionString = "Data Source=(localdb)\\MSSQLLocalDB;Database=master";
#endif

private readonly FakeSqlClientDiagnosticSource fakeSqlClientDiagnosticSource;

@@ -99,7 +99,9 @@ public void SqlClientCallsAreCollectedSuccessfully(
{
var operationId = Guid.NewGuid();
sqlCommand.CommandType = commandType;
#pragma warning disable CA2100
sqlCommand.CommandText = commandText;
#pragma warning restore CA2100

var beforeExecuteEventData = new
{
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics;
using OpenTelemetry.Tests;
using OpenTelemetry.Trace;
using Xunit;

Original file line number Diff line number Diff line change
@@ -51,7 +51,9 @@ public async Task SuccessfulCommandTest(CommandType commandType, string commandT

sqlConnection.ChangeDatabase("master");

#pragma warning disable CA2100
using SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection)
#pragma warning restore CA2100
{
CommandType = commandType,
};