Adding more tests to AspNetCore (#787)

Co-authored-by: Cijo Thomas <cithomas@microsoft.com>
This commit is contained in:
Eddy Nakamura 2020-07-09 16:39:23 -03:00 committed by GitHub
parent 8ec93bd0b7
commit 1c5872ee26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 9 deletions

View File

@ -110,10 +110,13 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Implementation
activity.AddTag(SpanAttributeConstants.HttpMethodKey, request.Method);
activity.AddTag(SpanAttributeConstants.HttpPathKey, path);
activity.AddTag(SpanAttributeConstants.HttpUrlKey, GetUri(request));
var userAgent = request.Headers["User-Agent"].FirstOrDefault();
activity.AddTag(SpanAttributeConstants.HttpUserAgentKey, userAgent);
activity.AddTag(SpanAttributeConstants.HttpUrlKey, GetUri(request));
if (!string.IsNullOrEmpty(userAgent))
{
activity.AddTag(SpanAttributeConstants.HttpUserAgentKey, userAgent);
}
}
}

View File

@ -18,11 +18,15 @@ using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using OpenTelemetry.Trace;
using OpenTelemetry.Trace.Configuration;
using OpenTelemetry.Trace.Export;
@ -45,8 +49,14 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Tests
this.factory = factory;
}
[Fact]
public async Task SuccessfulTemplateControllerCallGeneratesASpan()
[Theory]
[InlineData("/api/values", "user-agent", 503, "503")]
[InlineData("/api/values", null, 503, null)]
public async Task SuccessfulTemplateControllerCallGeneratesASpan(
string urlPath,
string userAgent,
int statusCode,
string reasonPhrase)
{
var spanProcessor = new Mock<ActivityProcessor>();
@ -55,7 +65,7 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Tests
.WithWebHostBuilder(builder =>
builder.ConfigureTestServices((IServiceCollection services) =>
{
services.AddSingleton<CallbackMiddleware.CallbackMiddlewareImpl>(new TestCallbackMiddlewareImpl());
services.AddSingleton<CallbackMiddleware.CallbackMiddlewareImpl>(new TestCallbackMiddlewareImpl(statusCode, reasonPhrase));
services.AddOpenTelemetry((builder) => builder.AddRequestInstrumentation()
.AddProcessorPipeline(p => p.AddProcessor(n => spanProcessor.Object)));
}))
@ -63,8 +73,13 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Tests
{
try
{
if (!string.IsNullOrEmpty(userAgent))
{
client.DefaultRequestHeaders.Add("User-Agent", userAgent);
}
// Act
var response = await client.GetAsync("/api/values");
var response = await client.GetAsync(urlPath);
}
catch (Exception)
{
@ -89,15 +104,45 @@ namespace OpenTelemetry.Instrumentation.AspNetCore.Tests
var span = (Activity)spanProcessor.Invocations[1].Arguments[0];
Assert.Equal(ActivityKind.Server, span.Kind);
Assert.Equal("/api/values", span.Tags.FirstOrDefault(i => i.Key == SpanAttributeConstants.HttpPathKey).Value);
Assert.Equal("503", span.Tags.FirstOrDefault(i => i.Key == SpanAttributeConstants.HttpStatusCodeKey).Value);
Assert.Equal("localhost:", span.Tags.FirstOrDefault(i => i.Key == SpanAttributeConstants.HttpHostKey).Value);
Assert.Equal("GET", span.Tags.FirstOrDefault(i => i.Key == SpanAttributeConstants.HttpMethodKey).Value);
Assert.Equal(urlPath, span.Tags.FirstOrDefault(i => i.Key == SpanAttributeConstants.HttpPathKey).Value);
Assert.Equal($"http://localhost{urlPath}", span.Tags.FirstOrDefault(i => i.Key == SpanAttributeConstants.HttpUrlKey).Value);
Assert.Equal(statusCode.ToString(), span.Tags.FirstOrDefault(i => i.Key == SpanAttributeConstants.HttpStatusCodeKey).Value);
Status status = SpanHelper.ResolveSpanStatusForHttpStatusCode(statusCode);
Assert.Equal(SpanHelper.GetCachedCanonicalCodeString(status.CanonicalCode), span.Tags.FirstOrDefault(i => i.Key == SpanAttributeConstants.StatusCodeKey).Value);
this.ValidateTagValue(span, SpanAttributeConstants.StatusDescriptionKey, reasonPhrase);
this.ValidateTagValue(span, SpanAttributeConstants.HttpUserAgentKey, userAgent);
}
private void ValidateTagValue(Activity activity, string attribute, string expectedValue)
{
if (string.IsNullOrEmpty(expectedValue))
{
Assert.True(activity.Tags.All(i => i.Key != attribute));
}
else
{
Assert.Equal(expectedValue, activity.Tags.FirstOrDefault(i => i.Key == attribute).Value);
}
}
public class TestCallbackMiddlewareImpl : CallbackMiddleware.CallbackMiddlewareImpl
{
private readonly int statusCode;
private readonly string reasonPhrase;
public TestCallbackMiddlewareImpl(int statusCode, string reasonPhrase)
{
this.statusCode = statusCode;
this.reasonPhrase = reasonPhrase;
}
public override async Task<bool> ProcessAsync(HttpContext context)
{
context.Response.StatusCode = 503;
context.Response.StatusCode = this.statusCode;
context.Response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = this.reasonPhrase;
await context.Response.WriteAsync("empty");
return false;
}