Add tests for TracerProviderSdk (#1077)
* Add tests for TracerProviderSdk * fix * comment
This commit is contained in:
parent
812fe936cf
commit
06ebd1f465
|
|
@ -28,6 +28,12 @@ namespace OpenTelemetry.Trace
|
|||
/// activies created without ActivitySource, into something which closely
|
||||
/// matches the one created using ActivitySource.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This class is meant to be only used when writing new Instrumentation for
|
||||
/// libraries which are already instrumented with DiagnosticSource/Activity
|
||||
/// following this doc:
|
||||
/// https://github.com/dotnet/runtime/blob/master/src/libraries/System.Diagnostics.DiagnosticSource/src/ActivityUserGuide.md.
|
||||
/// </remarks>
|
||||
public class ActivitySourceAdapter
|
||||
{
|
||||
private readonly Sampler sampler;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
// <copyright file="TraceSdkTest.cs" company="OpenTelemetry Authors">
|
||||
// <copyright file="TracerProvideSdkTest.cs" company="OpenTelemetry Authors">
|
||||
// Copyright The OpenTelemetry Authors
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
|
|
@ -14,18 +14,19 @@
|
|||
// limitations under the License.
|
||||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using OpenTelemetry.Tests.Shared;
|
||||
using Xunit;
|
||||
|
||||
namespace OpenTelemetry.Trace.Tests
|
||||
{
|
||||
public class TraceSdkTest
|
||||
public class TracerProvideSdkTest
|
||||
{
|
||||
private const string ActivitySourceName = "TraceSdkTest";
|
||||
|
||||
[Fact]
|
||||
public void TracerSdkInvokesSamplingWithCorrectParameters()
|
||||
public void TracerProviderSdkInvokesSamplingWithCorrectParameters()
|
||||
{
|
||||
var testSampler = new TestSampler();
|
||||
using var activitySource = new ActivitySource(ActivitySourceName);
|
||||
|
|
@ -185,6 +186,97 @@ namespace OpenTelemetry.Trace.Tests
|
|||
Assert.False(endCalled);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TracerProvideSdkCreatesActivitySource()
|
||||
{
|
||||
TestActivityProcessor testActivityProcessor = new TestActivityProcessor();
|
||||
|
||||
bool startCalled = false;
|
||||
bool endCalled = false;
|
||||
|
||||
testActivityProcessor.StartAction =
|
||||
(a) =>
|
||||
{
|
||||
startCalled = true;
|
||||
};
|
||||
|
||||
testActivityProcessor.EndAction =
|
||||
(a) =>
|
||||
{
|
||||
endCalled = true;
|
||||
};
|
||||
|
||||
TestInstrumentation testInstrumentation = null;
|
||||
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
|
||||
.AddProcessor(testActivityProcessor)
|
||||
.AddInstrumentation((adapter) =>
|
||||
{
|
||||
testInstrumentation = new TestInstrumentation(adapter);
|
||||
return testInstrumentation;
|
||||
})
|
||||
.Build();
|
||||
|
||||
var adapter = testInstrumentation.Adapter;
|
||||
Activity activity = new Activity("test");
|
||||
activity.Start();
|
||||
adapter.Start(activity);
|
||||
adapter.Stop(activity);
|
||||
activity.Stop();
|
||||
|
||||
Assert.True(startCalled);
|
||||
Assert.True(endCalled);
|
||||
|
||||
/*
|
||||
* Uncomment when issue 1075 is fixed.
|
||||
TestActivityProcessor testActivityProcessorNew = new TestActivityProcessor();
|
||||
|
||||
bool startCalledNew = false;
|
||||
bool endCalledNew = false;
|
||||
|
||||
testActivityProcessorNew.StartAction =
|
||||
(a) =>
|
||||
{
|
||||
startCalledNew = true;
|
||||
};
|
||||
|
||||
testActivityProcessorNew.EndAction =
|
||||
(a) =>
|
||||
{
|
||||
endCalledNew = true;
|
||||
};
|
||||
|
||||
tracerProvider.AddProcessor(testActivityProcessorNew);
|
||||
Activity activityNew = new Activity("test");
|
||||
activityNew.Start();
|
||||
adapter.Start(activityNew);
|
||||
adapter.Stop(activityNew);
|
||||
activityNew.Stop();
|
||||
|
||||
Assert.True(startCalledNew);
|
||||
Assert.True(endCalledNew);
|
||||
*/
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TracerProvideSdkCreatesAndDiposesInstrumentation()
|
||||
{
|
||||
TestInstrumentation testInstrumentation = null;
|
||||
var tracerProvider = Sdk.CreateTracerProviderBuilder()
|
||||
.AddInstrumentation((adapter) =>
|
||||
{
|
||||
testInstrumentation = new TestInstrumentation(adapter);
|
||||
return testInstrumentation;
|
||||
})
|
||||
.Build();
|
||||
|
||||
Assert.NotNull(testInstrumentation);
|
||||
var adapter = testInstrumentation.Adapter;
|
||||
Assert.NotNull(adapter);
|
||||
Assert.False(testInstrumentation.IsDisposed);
|
||||
tracerProvider.Dispose();
|
||||
Assert.True(testInstrumentation.IsDisposed);
|
||||
}
|
||||
|
||||
private class TestSampler : Sampler
|
||||
{
|
||||
public SamplingResult DesiredSamplingResult { get; set; } = new SamplingResult(SamplingDecision.RecordAndSampled);
|
||||
|
|
@ -197,5 +289,22 @@ namespace OpenTelemetry.Trace.Tests
|
|||
return this.DesiredSamplingResult;
|
||||
}
|
||||
}
|
||||
|
||||
private class TestInstrumentation : IDisposable
|
||||
{
|
||||
public bool IsDisposed;
|
||||
public ActivitySourceAdapter Adapter;
|
||||
|
||||
public TestInstrumentation(ActivitySourceAdapter adapter)
|
||||
{
|
||||
this.Adapter = adapter;
|
||||
this.IsDisposed = false;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
this.IsDisposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue