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

Add unit tests to AspNet instrumentation #1935

Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,108 @@ public void AspNetRequestsAreCollectedSuccessfully(
Assert.Equal(HttpContext.Current.Request.UserAgent, span.GetTagValue(SemanticConventions.AttributeHttpUserAgent) as string);
}

[Theory]
[InlineData(SamplingDecision.Drop)]
[InlineData(SamplingDecision.RecordOnly)]
[InlineData(SamplingDecision.RecordAndSample)]

public void ExtractContextIrrespectiveOfSamplingDecision(SamplingDecision samplingDecision)
{
HttpContext.Current = new HttpContext(
new HttpRequest(string.Empty, "http://localhost/", string.Empty)
{
RequestContext = new RequestContext()
{
RouteData = new RouteData(),
},
},
new HttpResponse(new StringWriter()));

bool isPropagatorCalled = false;
var propagator = new Mock<TextMapPropagator>();
propagator.Setup(m => m.Extract(It.IsAny<PropagationContext>(), It.IsAny<HttpRequest>(), It.IsAny<Func<HttpRequest, string, IEnumerable<string>>>()))
.Returns(() =>
{
isPropagatorCalled = true;
return default(PropagationContext);
});

var activity = new Activity(HttpInListener.ActivityOperationName);

var activityProcessor = new Mock<BaseProcessor<Activity>>();
Sdk.SetDefaultTextMapPropagator(propagator.Object);
using (var openTelemetry = Sdk.CreateTracerProviderBuilder()
.SetSampler(new TestSampler(samplingDecision))
.AddAspNetInstrumentation()
.AddProcessor(activityProcessor.Object).Build())
{
activity.Start();

using (var inMemoryEventListener = new InMemoryEventListener(AspNetInstrumentationEventSource.Log))
{
this.fakeAspNetDiagnosticSource.Write("Start", null);
}

this.fakeAspNetDiagnosticSource.Write("Stop", null);
activity.Stop();
}

Assert.True(isPropagatorCalled);
Copy link
Member

Choose a reason for hiding this comment

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

Compared to PR 1934, this is only checking if propagator is called or not. That alone is not sufficient, as the actual thing to be validated is if the Baggage.Current and Activity.Current is setup based on incoming headers.

(This can be added as a follow up.)

}

[Fact]
public void ExtractContextIrrespectiveOfTheFilterApplied()
{
HttpContext.Current = new HttpContext(
new HttpRequest(string.Empty, "http://localhost/", string.Empty)
{
RequestContext = new RequestContext()
{
RouteData = new RouteData(),
},
},
new HttpResponse(new StringWriter()));

bool isPropagatorCalled = false;
var propagator = new Mock<TextMapPropagator>();
propagator.Setup(m => m.Extract(It.IsAny<PropagationContext>(), It.IsAny<HttpRequest>(), It.IsAny<Func<HttpRequest, string, IEnumerable<string>>>()))
.Returns(() =>
{
isPropagatorCalled = true;
return default(PropagationContext);
});

var activity = new Activity(HttpInListener.ActivityOperationName);

bool isFilterCalled = false;
var activityProcessor = new Mock<BaseProcessor<Activity>>();
Sdk.SetDefaultTextMapPropagator(propagator.Object);
using (var openTelemetry = Sdk.CreateTracerProviderBuilder()
.AddAspNetInstrumentation(options =>
{
options.Filter = context =>
{
isFilterCalled = true;
return false;
};
})
.AddProcessor(activityProcessor.Object).Build())
{
activity.Start();

using (var inMemoryEventListener = new InMemoryEventListener(AspNetInstrumentationEventSource.Log))
{
this.fakeAspNetDiagnosticSource.Write("Start", null);
}

this.fakeAspNetDiagnosticSource.Write("Stop", null);
activity.Stop();
}

Assert.True(isFilterCalled);
Assert.True(isPropagatorCalled);
}

private static Action<Activity, string, object> GetEnrichmentAction(Status statusToBeSet)
{
Action<Activity, string, object> enrichAction;
Expand Down Expand Up @@ -385,5 +487,20 @@ public void Dispose()
this.listener.Dispose();
}
}

private class TestSampler : Sampler
{
private SamplingDecision samplingDecision;

public TestSampler(SamplingDecision samplingDecision)
{
this.samplingDecision = samplingDecision;
}

public override SamplingResult ShouldSample(in SamplingParameters samplingParameters)
{
return new SamplingResult(this.samplingDecision);
}
}
}
}