ActivitySource minor optimization and test (#1105)
* ActivitySource minor optimization and test * comment * More optimize
This commit is contained in:
parent
68c16da46f
commit
d13ef78b06
|
|
@ -14,6 +14,7 @@
|
|||
// limitations under the License.
|
||||
// </copyright>
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using OpenTelemetry.Resources;
|
||||
|
||||
|
|
@ -39,10 +40,34 @@ namespace OpenTelemetry.Trace
|
|||
private readonly Sampler sampler;
|
||||
private readonly Resource resource;
|
||||
private ActivityProcessor activityProcessor;
|
||||
private Action<Activity> getRequestedDataAction;
|
||||
|
||||
internal ActivitySourceAdapter(Sampler sampler, ActivityProcessor activityProcessor, Resource resource)
|
||||
{
|
||||
if (sampler == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(sampler));
|
||||
}
|
||||
|
||||
if (resource == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(resource));
|
||||
}
|
||||
|
||||
this.sampler = sampler;
|
||||
if (this.sampler is AlwaysOnSampler)
|
||||
{
|
||||
this.getRequestedDataAction = this.RunGetRequestedDataAlwaysOnSampler;
|
||||
}
|
||||
else if (this.sampler is AlwaysOffSampler)
|
||||
{
|
||||
this.getRequestedDataAction = this.RunGetRequestedDataAlwaysOffSampler;
|
||||
}
|
||||
else
|
||||
{
|
||||
this.getRequestedDataAction = this.RunGetRequestedDataOtherSampler;
|
||||
}
|
||||
|
||||
this.activityProcessor = activityProcessor;
|
||||
this.resource = resource;
|
||||
}
|
||||
|
|
@ -57,11 +82,11 @@ namespace OpenTelemetry.Trace
|
|||
/// <param name="activity"><see cref="Activity"/> to be started.</param>
|
||||
public void Start(Activity activity)
|
||||
{
|
||||
this.RunGetRequestedData(activity);
|
||||
this.getRequestedDataAction(activity);
|
||||
if (activity.IsAllDataRequested)
|
||||
{
|
||||
activity.SetResource(this.resource);
|
||||
this.activityProcessor.OnStart(activity);
|
||||
this.activityProcessor?.OnStart(activity);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -73,7 +98,7 @@ namespace OpenTelemetry.Trace
|
|||
{
|
||||
if (activity.IsAllDataRequested)
|
||||
{
|
||||
this.activityProcessor.OnEnd(activity);
|
||||
this.activityProcessor?.OnEnd(activity);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -82,7 +107,18 @@ namespace OpenTelemetry.Trace
|
|||
this.activityProcessor = processor;
|
||||
}
|
||||
|
||||
private void RunGetRequestedData(Activity activity)
|
||||
private void RunGetRequestedDataAlwaysOnSampler(Activity activity)
|
||||
{
|
||||
activity.IsAllDataRequested = true;
|
||||
activity.ActivityTraceFlags |= ActivityTraceFlags.Recorded;
|
||||
}
|
||||
|
||||
private void RunGetRequestedDataAlwaysOffSampler(Activity activity)
|
||||
{
|
||||
activity.IsAllDataRequested = false;
|
||||
}
|
||||
|
||||
private void RunGetRequestedDataOtherSampler(Activity activity)
|
||||
{
|
||||
ActivityContext parentContext;
|
||||
if (string.IsNullOrEmpty(activity.ParentId))
|
||||
|
|
|
|||
|
|
@ -272,6 +272,31 @@ namespace OpenTelemetry.Trace.Tests
|
|||
Assert.True(endCalledNew);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TracerProvideSdkCreatesActivitySourceWhenNoProcessor()
|
||||
{
|
||||
TestInstrumentation testInstrumentation = null;
|
||||
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
|
||||
.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();
|
||||
|
||||
// No asserts here. Validates that no exception
|
||||
// gets thrown when processors are not added,
|
||||
// TODO: Refactor to have more proper unit test
|
||||
// to target each individual classes.
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TracerProvideSdkCreatesAndDiposesInstrumentation()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue