Skip to content

Commit

Permalink
Improving code coverage (#939)
Browse files Browse the repository at this point in the history
Adding tests for zpages and hosting extensions

Testing enableConnectionLevelAttributes from SqlClient

updating assert

Adding tests for SqlClientInstrumentationOptions

removing extra test

undoing changes

updating changelog

renaming file to be complicant with other exporters

renaming files and adding more tests
  • Loading branch information
eddynaka authored Jul 29, 2020
1 parent b25d741 commit 3f01bdc
Show file tree
Hide file tree
Showing 16 changed files with 235 additions and 32 deletions.
22 changes: 22 additions & 0 deletions src/OpenTelemetry.Exporter.ZPages/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// <copyright file="AssemblyInfo.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.Runtime.CompilerServices;

#if SIGNED
[assembly: InternalsVisibleTo("OpenTelemetry.Exporter.ZPages.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010051c1562a090fb0c9f391012a32198b5e5d9a60e9b80fa2d7b434c9e5ccb7259bd606e66f9660676afc6692b8cdc6793d190904551d2103b7b22fa636dcbb8208839785ba402ea08fc00c8f1500ccef28bbf599aa64ffb1e1d5dc1bf3420a3777badfe697856e9d52070a50c3ea5821c80bef17ca3acffa28f89dd413f096f898")]
#else
[assembly: InternalsVisibleTo("OpenTelemetry.Exporter.ZPages.Tests")]
#endif
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="OpenTelemetryBuilderExtensions.cs" company="OpenTelemetry Authors">
// <copyright file="TracerProviderBuilderExtensions.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -23,7 +23,7 @@ namespace OpenTelemetry.Trace
/// <summary>
/// Extension methods to simplify registering of Zipkin exporter.
/// </summary>
public static class OpenTelemetryBuilderExtensions
public static class TracerProviderBuilderExtensions
{
/// <summary>
/// Registers a Zipkin exporter that will receive <see cref="System.Diagnostics.Activity"/> instances.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@ internal class AspNetInstrumentation : IDisposable

private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber;

/// <summary>
/// Initializes a new instance of the <see cref="AspNetInstrumentation"/> class.
/// </summary>
/// <param name="activitySource">ActivitySource adapter instance.</param>
public AspNetInstrumentation(ActivitySourceAdapter activitySource)
: this(activitySource, new AspNetInstrumentationOptions())
{
}

/// <summary>
/// Initializes a new instance of the <see cref="AspNetInstrumentation"/> class.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ internal class AspNetCoreInstrumentation : IDisposable
{
private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber;

/// <summary>
/// Initializes a new instance of the <see cref="AspNetCoreInstrumentation"/> class.
/// </summary>
/// <param name="activitySource">ActivitySource adapter instance.</param>
public AspNetCoreInstrumentation(ActivitySourceAdapter activitySource)
: this(activitySource, new AspNetCoreInstrumentationOptions())
{
}

/// <summary>
/// Initializes a new instance of the <see cref="AspNetCoreInstrumentation"/> class.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/OpenTelemetry.Instrumentation.SqlClient/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

* Renamed from `AddSqlClientDependencyInstrumentation` to `AddSqlClientInstrumentation`

## 0.3.0-beta

Released 2020-07-23
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static class TracerProviderBuilderExtensions
/// <param name="builder"><see cref="TracerProviderBuilder"/> being configured.</param>
/// <param name="configureSqlClientInstrumentationOptions">SqlClient configuration options.</param>
/// <returns>The instance of <see cref="TracerProviderBuilder"/> to chain the calls.</returns>
public static TracerProviderBuilder AddSqlClientDependencyInstrumentation(
public static TracerProviderBuilder AddSqlClientInstrumentation(
this TracerProviderBuilder builder,
Action<SqlClientInstrumentationOptions> configureSqlClientInstrumentationOptions = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;
using System.Diagnostics;
using OpenTelemetry.Exporter.Jaeger.Implementation;
Expand All @@ -25,6 +26,13 @@ namespace OpenTelemetry.Exporter.Jaeger.Tests
{
public class JaegerExporterTests
{
[Fact]
public void JaegerExporter_BadArgs()
{
TracerProviderBuilder builder = null;
Assert.Throws<ArgumentNullException>(() => builder.UseJaegerExporter());
}

[Fact]
public void UseJaegerExporterWithCustomActivityProcessor()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// <copyright file="ActivityExtensionsTest.cs" company="OpenTelemetry Authors">
// <copyright file="OtlpExporterTest.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -31,9 +31,9 @@

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests
{
public class ActivityExtensionsTest
public class OtlpExporterTest
{
static ActivityExtensionsTest()
static OtlpExporterTest()
{
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;
Expand All @@ -48,6 +48,13 @@ static ActivityExtensionsTest()
ActivitySource.AddActivityListener(listener);
}

[Fact]
public void OtlpExporter_BadArgs()
{
TracerProviderBuilder builder = null;
Assert.Throws<ArgumentNullException>(() => builder.UseOtlpExporter());
}

[Fact]
public void ToOtlpResourceSpansTest()
{
Expand Down
79 changes: 79 additions & 0 deletions test/OpenTelemetry.Exporter.ZPages.Tests/ZPagesExporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Threading;
using System.Threading.Tasks;
using OpenTelemetry;
using OpenTelemetry.Exporter.ZPages.Implementation;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

Expand All @@ -45,6 +46,13 @@ static ZPagesExporterTests()
ActivitySource.AddActivityListener(listener);
}

[Fact]
public void ZPagesExporter_BadArgs()
{
TracerProviderBuilder builder = null;
Assert.Throws<ArgumentNullException>(() => builder.UseZPagesExporter());
}

[Fact]
public async Task ZPagesExporter_Integration()
{
Expand Down Expand Up @@ -93,6 +101,77 @@ public void ZPagesExporter_CustomActivityProcessor()
Assert.True(endCalled);
}

[Fact]
public void ZPagesExporter_CheckingCustomOptions()
{
ZPagesExporterOptions options = new ZPagesExporterOptions
{
RetentionTime = 100_000,
Url = "http://localhost:7284/rpcz/",
};

ZPagesExporter exporter = new ZPagesExporter(options);

Assert.Equal(options.Url, exporter.Options.Url);
Assert.Equal(options.RetentionTime, exporter.Options.RetentionTime);
}

[Fact]
public void ZPagesExporter_ZPagesProcessor()
{
const string ActivitySourceName = "zpages.test";
Guid requestId = Guid.NewGuid();
ZPagesExporterOptions options = new ZPagesExporterOptions
{
RetentionTime = 100_000,
Url = "http://localhost:7284/rpcz/",
};
ZPagesExporter exporter = new ZPagesExporter(options);
var zpagesProcessor = new ZPagesProcessor(exporter);

var openTelemetrySdk = Sdk.CreateTracerProvider(b => b
.AddActivitySource(ActivitySourceName)
.UseZPagesExporter(
processorConfigure: p => p.AddProcessor((next) => zpagesProcessor)));

var source = new ActivitySource(ActivitySourceName);
var activity0 = source.StartActivity("Test Zipkin Activity");

// checking size of dictionaries from ZPagesActivityTracker
Assert.Equal(1, ZPagesActivityTracker.ProcessingList.First().Value);
Assert.Equal(1, ZPagesActivityTracker.TotalCount.First().Value);
Assert.Single(ZPagesActivityTracker.TotalEndedCount);
Assert.Single(ZPagesActivityTracker.TotalErrorCount);
Assert.Single(ZPagesActivityTracker.TotalLatency);

var activity1 = source.StartActivity("Test Zipkin Activity");

// checking size of dictionaries from ZPagesActivityTracker
Assert.Equal(2, ZPagesActivityTracker.ProcessingList.First().Value);
Assert.Equal(2, ZPagesActivityTracker.TotalCount.First().Value);
Assert.Single(ZPagesActivityTracker.TotalEndedCount);
Assert.Single(ZPagesActivityTracker.TotalErrorCount);
Assert.Single(ZPagesActivityTracker.TotalLatency);

var activity2 = source.StartActivity("Test Zipkin Activity 2");

// checking size of dictionaries from ZPagesActivityTracker
Assert.Equal(2, ZPagesActivityTracker.ProcessingList.Count);
Assert.Equal(2, ZPagesActivityTracker.TotalCount.Count);
Assert.Equal(2, ZPagesActivityTracker.TotalEndedCount.Count);
Assert.Equal(2, ZPagesActivityTracker.TotalErrorCount.Count);
Assert.Equal(2, ZPagesActivityTracker.TotalLatency.Count);

activity0?.Stop();
activity1?.Stop();
activity2?.Stop();

// checking if activities were processed
Assert.Equal(0, ZPagesActivityTracker.ProcessingList.First().Value);
Assert.Equal(0, ZPagesActivityTracker.ProcessingList.Last().Value);
Assert.Empty(ZPagesActivityTracker.ZQueue);
}

internal static Activity CreateTestActivity(
bool setAttributes = true,
Dictionary<string, object> additionalAttributes = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ public void Dispose()
this.testServer.Dispose();
}

[Fact]
public void ZipkinExporter_BadArgs()
{
TracerProviderBuilder builder = null;
Assert.Throws<ArgumentNullException>(() => builder.UseZipkinExporter());
}

[Theory]
[InlineData(true)]
[InlineData(false)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void AddOpenTelemetry_ServiceProviderArgument_ServicesRegistered()
services.AddSingleton(testInstrumentation);
services.AddOpenTelemetry((provider, builder) =>
{
builder.AddInstrumentation<TestInstrumentation>((activitySource) => provider.GetRequiredService<TestInstrumentation>());
builder.AddInstrumentation((activitySource) => provider.GetRequiredService<TestInstrumentation>());
});

var serviceProvider = services.BuildServiceProvider();
Expand All @@ -104,6 +104,18 @@ public void AddOpenTelemetry_ServiceProviderArgument_ServicesRegistered()
Assert.True(testInstrumentation.Disposed);
}

[Fact]
public void AddOpenTelemetry_BadArgs()
{
ServiceCollection services = null;
Assert.Throws<ArgumentNullException>(() => services.AddOpenTelemetry());
Assert.Throws<ArgumentNullException>(() =>
services.AddOpenTelemetry((provider, builder) =>
{
builder.AddInstrumentation((activitySource) => provider.GetRequiredService<TestInstrumentation>());
}));
}

internal class TestInstrumentation : IDisposable
{
public bool Disposed { get; private set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,12 @@ public void GrpcAndHttpClientInstrumentationIsInvoked()
Assert.Equal($"HTTP POST", httpSpan.DisplayName);
Assert.Equal(grpcSpan.SpanId, httpSpan.ParentSpanId);
}

[Fact]
public void Grpc_BadArgs()
{
TracerProviderBuilder builder = null;
Assert.Throws<ArgumentNullException>(() => builder.AddGrpcClientInstrumentation());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,31 @@
// limitations under the License.
// </copyright>

using System.Diagnostics;
using System.Linq;
using Moq;
using OpenTelemetry.Trace;
using Xunit;

namespace OpenTelemetry.Instrumentation.SqlClient.Tests
{
public class SqlClientInstrumentationOptionsTests
{
static SqlClientInstrumentationOptionsTests()
{
Activity.DefaultIdFormat = ActivityIdFormat.W3C;
Activity.ForceDefaultIdFormat = true;

var listener = new ActivityListener
{
ShouldListenTo = _ => true,
GetRequestedDataUsingParentId = (ref ActivityCreationOptions<string> options) => ActivityDataRequest.AllData,
GetRequestedDataUsingContext = (ref ActivityCreationOptions<ActivityContext> options) => ActivityDataRequest.AllData,
};

ActivitySource.AddActivityListener(listener);
}

[Theory]
[InlineData("localhost", "localhost", null, null, null)]
[InlineData("127.0.0.1", null, "127.0.0.1", null, null)]
Expand All @@ -42,5 +61,41 @@ public void ParseDataSourceTests(
Assert.Equal(expectedInstanceName, sqlConnectionDetails.InstanceName);
Assert.Equal(expectedPort, sqlConnectionDetails.Port);
}

[Theory]
[InlineData(true, "localhost", "localhost", null, null, null)]
[InlineData(true, "127.0.0.1,1433", null, "127.0.0.1", null, null)]
[InlineData(true, "127.0.0.1,1434", null, "127.0.0.1", null, "1434")]
[InlineData(true, "127.0.0.1\\instanceName, 1818", null, "127.0.0.1", "instanceName", "1818")]
[InlineData(false, "localhost", "localhost", null, null, null)]
public void SqlClientInstrumentationOptions_EnableConnectionLevelAttributes(
bool enableConnectionLevelAttributes,
string dataSource,
string expectedServerHostName,
string expectedServerIpAddress,
string expectedInstanceName,
string expectedPort)
{
var source = new ActivitySource("sql-client-instrumentation");
var activity = source.StartActivity("Test Sql Activity");
var options = new SqlClientInstrumentationOptions
{
EnableConnectionLevelAttributes = enableConnectionLevelAttributes,
};
options.AddConnectionLevelDetailsToActivity(dataSource, activity);

if (!enableConnectionLevelAttributes)
{
Assert.Equal(expectedServerHostName, activity.Tags.FirstOrDefault(t => t.Key == SemanticConventions.AttributePeerService).Value);
}
else
{
Assert.Equal(expectedServerHostName, activity.Tags.FirstOrDefault(t => t.Key == SemanticConventions.AttributeNetPeerName).Value);
}

Assert.Equal(expectedServerIpAddress, activity.Tags.FirstOrDefault(t => t.Key == SemanticConventions.AttributeNetPeerIp).Value);
Assert.Equal(expectedInstanceName, activity.Tags.FirstOrDefault(t => t.Key == SemanticConventions.AttributeDbMsSqlInstanceName).Value);
Assert.Equal(expectedPort, activity.Tags.FirstOrDefault(t => t.Key == SemanticConventions.AttributeNetPeerPort).Value);
}
}
}
Loading

0 comments on commit 3f01bdc

Please sign in to comment.