Skip to content

Commit

Permalink
Merge branch 'main' into vibankwa/fix-4333
Browse files Browse the repository at this point in the history
  • Loading branch information
vishweshbankwar authored Mar 30, 2023
2 parents f11e508 + a274f06 commit 3484019
Show file tree
Hide file tree
Showing 14 changed files with 274 additions and 157 deletions.
12 changes: 0 additions & 12 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@ on:
- '**.md'

jobs:
sql-test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
version: [net6.0,net7.0]
steps:
- uses: actions/checkout@v3

- name: Run sql docker-compose.integration
run: docker-compose --file=test/OpenTelemetry.Instrumentation.SqlClient.Tests/docker-compose.yml --file=build/docker-compose.${{ matrix.version }}.yml --project-directory=. up --exit-code-from=tests --build

w3c-trace-context-test:
runs-on: ubuntu-latest
strategy:
Expand Down
1 change: 1 addition & 0 deletions build/Common.nonprod.props
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
<MicrosoftExtensionsLoggingAbstractionsPkgVer>[6.0.0,)</MicrosoftExtensionsLoggingAbstractionsPkgVer>
<MicrosoftNETTestSdkPkgVer>[17.4.1]</MicrosoftNETTestSdkPkgVer>
<MoqPkgVer>[4.18.3,5.0)</MoqPkgVer>
<TestcontainersPkgVer>3.0.0</TestcontainersPkgVer>
<RabbitMQClientPkgVer>[6.4.0,7.0)</RabbitMQClientPkgVer>
<RuntimeInstrumentationPkgVer>[1.0.0,2.0)</RuntimeInstrumentationPkgVer>
<SwashbuckleAspNetCorePkgVer>[6.4.0]</SwashbuckleAspNetCorePkgVer>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public override void WriteDouble(double d)
WriteI64(BitConverter.DoubleToInt64Bits(d));
}

#if NETSTANDARD2_1_OR_GREATER
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
public override void WriteBinary(ReadOnlySpan<byte> bytes)
{
WriteI32(bytes.Length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public override void WriteDouble(double d)
Transport.Write(PreAllocatedBuffer, 0, 8);
}

#if NETSTANDARD2_1_OR_GREATER
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
public override void WriteBinary(ReadOnlySpan<byte> bytes)
{
Int32ToVarInt((uint)bytes.Length, ref PreAllocatedVarInt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ protected virtual void Dispose(bool disposing)

public virtual void WriteString(string s)
{
#if NETSTANDARD2_1_OR_GREATER
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
if (s.Length <= 128)
{
Span<byte> buffer = stackalloc byte[256];
Expand All @@ -165,7 +165,7 @@ public virtual void WriteString(string s)
}
}

#if NETSTANDARD2_1_OR_GREATER
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
public abstract void WriteBinary(ReadOnlySpan<byte> bytes);
#endif

Expand Down
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Exporter.Jaeger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

* Enabled performance optimizations for .NET 6.0+ runtimes.
([#4349](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4349))

## 1.5.0-alpha.1

Released 2023-Mar-07
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)

var exponentialHistogramData = metricPoint.GetExponentialHistogramData();
dataPoint.Scale = exponentialHistogramData.Scale;
dataPoint.ZeroCount = (ulong)exponentialHistogramData.ZeroCount;

dataPoint.Positive = new OtlpMetrics.ExponentialHistogramDataPoint.Types.Buckets();
dataPoint.Positive.Offset = exponentialHistogramData.PositiveBuckets.Offset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ public void TestCounterToOtlpMetric(string name, string description, string unit
[Theory]
[InlineData("test_counter", null, null, 123L, null, MetricReaderTemporalityPreference.Cumulative)]
[InlineData("test_counter", null, null, null, 123.45, MetricReaderTemporalityPreference.Cumulative)]
[InlineData("test_counter", null, null, -123L, null, MetricReaderTemporalityPreference.Cumulative)]
[InlineData("test_counter", null, null, null, -123.45, MetricReaderTemporalityPreference.Cumulative)]
[InlineData("test_counter", null, null, 123L, null, MetricReaderTemporalityPreference.Delta)]
[InlineData("test_counter", "description", "unit", 123L, null, MetricReaderTemporalityPreference.Cumulative)]
[InlineData("test_counter", null, null, 123L, null, MetricReaderTemporalityPreference.Delta, "key1", "value1", "key2", 123)]
Expand Down Expand Up @@ -472,6 +474,8 @@ public void TestUpDownCounterToOtlpMetric(string name, string description, strin
[Theory]
[InlineData("test_histogram", null, null, 123L, null, MetricReaderTemporalityPreference.Cumulative)]
[InlineData("test_histogram", null, null, null, 123.45, MetricReaderTemporalityPreference.Cumulative)]
[InlineData("test_histogram", null, null, -123L, null, MetricReaderTemporalityPreference.Cumulative)]
[InlineData("test_histogram", null, null, null, -123.45, MetricReaderTemporalityPreference.Cumulative)]
[InlineData("test_histogram", null, null, 123L, null, MetricReaderTemporalityPreference.Delta)]
[InlineData("test_histogram", "description", "unit", 123L, null, MetricReaderTemporalityPreference.Cumulative)]
[InlineData("test_histogram", null, null, 123L, null, MetricReaderTemporalityPreference.Delta, "key1", "value1", "key2", 123)]
Expand All @@ -497,11 +501,13 @@ public void TestExponentialHistogramToOtlpMetric(string name, string description
{
var histogram = meter.CreateHistogram<long>(name, unit, description);
histogram.Record(longValue.Value, attributes);
histogram.Record(0, attributes);
}
else
{
var histogram = meter.CreateHistogram<double>(name, unit, description);
histogram.Record(doubleValue.Value, attributes);
histogram.Record(0, attributes);
}

provider.ForceFlush();
Expand Down Expand Up @@ -537,23 +543,49 @@ public void TestExponentialHistogramToOtlpMetric(string name, string description
Assert.True(dataPoint.StartTimeUnixNano > 0);
Assert.True(dataPoint.TimeUnixNano > 0);

Assert.Equal(1UL, dataPoint.Count);
Assert.Equal(20, dataPoint.Scale);
Assert.Equal(2UL, dataPoint.Count);
Assert.Equal(1UL, dataPoint.ZeroCount);

if (longValue.HasValue)
{
// Known issue: Negative measurements affect the Sum. Per the spec, they should not.
Assert.Equal((double)longValue, dataPoint.Sum);
if (longValue > 0)
{
Assert.True(dataPoint.Positive.Offset > 0);
Assert.Equal(1UL, dataPoint.Positive.BucketCounts[0]);
Assert.True(dataPoint.Negative.Offset == 0);
Assert.Empty(dataPoint.Negative.BucketCounts);
}
else
{
Assert.True(dataPoint.Negative.Offset > 0);
Assert.Equal(1UL, dataPoint.Negative.BucketCounts[0]);
Assert.True(dataPoint.Positive.Offset == 0);
Assert.Empty(dataPoint.Positive.BucketCounts);
}
}
else
{
// Known issue: Negative measurements affect the Sum. Per the spec, they should not.
Assert.Equal(doubleValue, dataPoint.Sum);
if (doubleValue > 0)
{
Assert.True(dataPoint.Positive.Offset > 0);
Assert.Equal(1UL, dataPoint.Positive.BucketCounts[0]);
Assert.True(dataPoint.Negative.Offset == 0);
Assert.Empty(dataPoint.Negative.BucketCounts);
}
else
{
Assert.True(dataPoint.Negative.Offset > 0);
Assert.Equal(1UL, dataPoint.Negative.BucketCounts[0]);
Assert.True(dataPoint.Positive.Offset == 0);
Assert.Empty(dataPoint.Positive.BucketCounts);
}
}

Assert.Equal(0UL, dataPoint.ZeroCount);
Assert.Equal(20, dataPoint.Scale);
Assert.True(dataPoint.Positive.Offset > 0);
Assert.Equal(1UL, dataPoint.Positive.BucketCounts[0]);
Assert.True(dataPoint.Negative.Offset <= 0);

if (attributes.Length > 0)
{
OtlpTestHelpers.AssertOtlpAttributes(attributes, dataPoint.Attributes);
Expand All @@ -569,6 +601,8 @@ public void TestExponentialHistogramToOtlpMetric(string name, string description
[Theory]
[InlineData("test_histogram", null, null, 123L, null, MetricReaderTemporalityPreference.Cumulative)]
[InlineData("test_histogram", null, null, null, 123.45, MetricReaderTemporalityPreference.Cumulative)]
[InlineData("test_histogram", null, null, -123L, null, MetricReaderTemporalityPreference.Cumulative)]
[InlineData("test_histogram", null, null, null, -123.45, MetricReaderTemporalityPreference.Cumulative)]
[InlineData("test_histogram", null, null, 123L, null, MetricReaderTemporalityPreference.Delta)]
[InlineData("test_histogram", "description", "unit", 123L, null, MetricReaderTemporalityPreference.Cumulative)]
[InlineData("test_histogram", null, null, 123L, null, MetricReaderTemporalityPreference.Delta, "key1", "value1", "key2", 123)]
Expand Down Expand Up @@ -632,6 +666,7 @@ public void TestHistogramToOtlpMetric(string name, string description, string un

Assert.Equal(1UL, dataPoint.Count);

// Known issue: Negative measurements affect the Sum. Per the spec, they should not.
if (longValue.HasValue)
{
Assert.Equal((double)longValue, dataPoint.Sum);
Expand Down
22 changes: 0 additions & 22 deletions test/OpenTelemetry.Instrumentation.SqlClient.Tests/Dockerfile

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\EnabledOnDockerPlatformTheoryAttribute.cs" Link="EnabledOnDockerPlatformTheoryAttribute.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\SkipUnlessEnvVarFoundTheoryAttribute.cs" Link="SkipUnlessEnvVarFoundTheoryAttribute.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\EventSourceTestHelper.cs" Link="EventSourceTestHelper.cs" />
<Compile Include="$(RepoRoot)\test\OpenTelemetry.Tests\Shared\TestEventListener.cs" Link="TestEventListener.cs" />
Expand All @@ -21,6 +22,8 @@
<PackageReference Include="Microsoft.Data.SqlClient" Version="2.1.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(MicrosoftNETTestSdkPkgVer)" />
<PackageReference Include="Moq" Version="$(MoqPkgVer)" />
<PackageReference Include="Testcontainers.MsSql" Version="$(TestcontainersPkgVer)" />
<PackageReference Include="Testcontainers.SqlEdge" Version="$(TestcontainersPkgVer)" />
<PackageReference Include="xunit" Version="$(XUnitPkgVer)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XUnitRunnerVisualStudioPkgVer)">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// <copyright file="SqlClientIntegrationTests.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>

using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using DotNet.Testcontainers.Containers;
using Microsoft.Data.SqlClient;
using OpenTelemetry.Tests;
using OpenTelemetry.Trace;
using Testcontainers.MsSql;
using Testcontainers.SqlEdge;
using Xunit;

namespace OpenTelemetry.Instrumentation.SqlClient.Tests
{
public sealed class SqlClientIntegrationTests : IAsyncLifetime
{
// The Microsoft SQL Server Docker image is not compatible with ARM devices, such as Macs with Apple Silicon.
private readonly IContainer databaseContainer = Architecture.Arm64.Equals(RuntimeInformation.ProcessArchitecture) ? new SqlEdgeBuilder().Build() : new MsSqlBuilder().Build();

public Task InitializeAsync()
{
return this.databaseContainer.StartAsync();
}

public Task DisposeAsync()
{
return this.databaseContainer.DisposeAsync().AsTask();
}

[Trait("CategoryName", "SqlIntegrationTests")]
[EnabledOnDockerPlatformTheory(EnabledOnDockerPlatformTheoryAttribute.DockerPlatform.Linux)]
[InlineData(CommandType.Text, "select 1/1", false)]
[InlineData(CommandType.Text, "select 1/1", false, true)]
[InlineData(CommandType.Text, "select 1/0", false, false, true)]
[InlineData(CommandType.Text, "select 1/0", false, false, true, false, false)]
[InlineData(CommandType.Text, "select 1/0", false, false, true, true, false)]
[InlineData(CommandType.StoredProcedure, "sp_who", false)]
[InlineData(CommandType.StoredProcedure, "sp_who", true)]
public void SuccessfulCommandTest(
CommandType commandType,
string commandText,
bool captureStoredProcedureCommandName,
bool captureTextCommandContent = false,
bool isFailure = false,
bool recordException = false,
bool shouldEnrich = true)
{
#if NETFRAMEWORK
// Disable things not available on netfx
recordException = false;
shouldEnrich = false;
#endif

var sampler = new TestSampler();
var activities = new List<Activity>();
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetSampler(sampler)
.AddInMemoryExporter(activities)
.AddSqlClientInstrumentation(options =>
{
#if !NETFRAMEWORK
options.SetDbStatementForStoredProcedure = captureStoredProcedureCommandName;
options.SetDbStatementForText = captureTextCommandContent;
#else
options.SetDbStatementForText = captureStoredProcedureCommandName || captureTextCommandContent;
#endif
options.RecordException = recordException;
if (shouldEnrich)
{
options.Enrich = SqlClientTests.ActivityEnrichment;
}
})
.Build();

using SqlConnection sqlConnection = new SqlConnection(this.GetConnectionString());

sqlConnection.Open();

string dataSource = sqlConnection.DataSource;

sqlConnection.ChangeDatabase("master");

using SqlCommand sqlCommand = new SqlCommand(commandText, sqlConnection)
{
CommandType = commandType,
};

try
{
sqlCommand.ExecuteNonQuery();
}
catch
{
}

Assert.Single(activities);
var activity = activities[0];

SqlClientTests.VerifyActivityData(commandType, commandText, captureStoredProcedureCommandName, captureTextCommandContent, isFailure, recordException, shouldEnrich, dataSource, activity);
SqlClientTests.VerifySamplingParameters(sampler.LatestSamplingParameters);
}

private string GetConnectionString()
{
switch (this.databaseContainer)
{
case SqlEdgeContainer container:
return container.GetConnectionString();
case MsSqlContainer container:
return container.GetConnectionString();
default:
throw new InvalidOperationException($"Container type ${this.databaseContainer.GetType().Name} not supported.");
}
}
}
}
Loading

0 comments on commit 3484019

Please sign in to comment.