Skip to content

Commit

Permalink
Removed usage of Mock<BaseProcessor> (#5095)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngruson authored Nov 29, 2023
1 parent 6a829dd commit 79aa39f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,7 @@ public void AddHttpClientInstrumentation_BadArgs()
[InlineData(false)]
public async Task InjectsHeadersAsync(bool shouldEnrich)
{
var processor = new Mock<BaseProcessor<Activity>>();
processor.Setup(x => x.OnStart(It.IsAny<Activity>())).Callback<Activity>(c =>
{
c.SetTag("enrichedWithHttpWebRequest", "no");
c.SetTag("enrichedWithHttpWebResponse", "no");
c.SetTag("enrichedWithHttpRequestMessage", "no");
c.SetTag("enrichedWithHttpResponseMessage", "no");
});
var exportedItems = new List<Activity>();

using var request = new HttpRequestMessage
{
Expand Down Expand Up @@ -167,15 +160,15 @@ public async Task InjectsHeadersAsync(bool shouldEnrich)
};
}
})
.AddProcessor(processor.Object)
.AddInMemoryExporter(exportedItems)
.Build())
{
using var c = new HttpClient();
await c.SendAsync(request);
}

Assert.Equal(5, processor.Invocations.Count); // SetParentProvider/OnStart/OnEnd/OnShutdown/Dispose called.
var activity = (Activity)processor.Invocations[2].Arguments[0];
Assert.Single(exportedItems);
var activity = exportedItems[0];

Assert.Equal(ActivityKind.Client, activity.Kind);
Assert.Equal(parent.TraceId, activity.Context.TraceId);
Expand All @@ -198,17 +191,33 @@ public async Task InjectsHeadersAsync(bool shouldEnrich)
#endif

#if NETFRAMEWORK
Assert.Equal(shouldEnrich ? "yes" : "no", activity.Tags.Where(tag => tag.Key == "enrichedWithHttpWebRequest").FirstOrDefault().Value);
Assert.Equal(shouldEnrich ? "yes" : "no", activity.Tags.Where(tag => tag.Key == "enrichedWithHttpWebResponse").FirstOrDefault().Value);
if (shouldEnrich)
{
Assert.Equal("yes", activity.Tags.Where(tag => tag.Key == "enrichedWithHttpWebRequest").FirstOrDefault().Value);
Assert.Equal("yes", activity.Tags.Where(tag => tag.Key == "enrichedWithHttpWebResponse").FirstOrDefault().Value);
}
else
{
Assert.DoesNotContain(activity.Tags, tag => tag.Key == "enrichedWithHttpWebRequest");
Assert.DoesNotContain(activity.Tags, tag => tag.Key == "enrichedWithHttpWebResponse");
}

Assert.Equal("no", activity.Tags.Where(tag => tag.Key == "enrichedWithHttpRequestMessage").FirstOrDefault().Value);
Assert.Equal("no", activity.Tags.Where(tag => tag.Key == "enrichedWithHttpResponseMessage").FirstOrDefault().Value);
Assert.DoesNotContain(activity.Tags, tag => tag.Key == "enrichedWithHttpRequestMessage");
Assert.DoesNotContain(activity.Tags, tag => tag.Key == "enrichedWithHttpResponseMessage");
#else
Assert.Equal("no", activity.Tags.Where(tag => tag.Key == "enrichedWithHttpWebRequest").FirstOrDefault().Value);
Assert.Equal("no", activity.Tags.Where(tag => tag.Key == "enrichedWithHttpWebResponse").FirstOrDefault().Value);
Assert.DoesNotContain(activity.Tags, tag => tag.Key == "enrichedWithHttpWebRequest");
Assert.DoesNotContain(activity.Tags, tag => tag.Key == "enrichedWithHttpWebResponse");

Assert.Equal(shouldEnrich ? "yes" : "no", activity.Tags.Where(tag => tag.Key == "enrichedWithHttpRequestMessage").FirstOrDefault().Value);
Assert.Equal(shouldEnrich ? "yes" : "no", activity.Tags.Where(tag => tag.Key == "enrichedWithHttpResponseMessage").FirstOrDefault().Value);
if (shouldEnrich)
{
Assert.Equal("yes", activity.Tags.Where(tag => tag.Key == "enrichedWithHttpRequestMessage").FirstOrDefault().Value);
Assert.Equal("yes", activity.Tags.Where(tag => tag.Key == "enrichedWithHttpResponseMessage").FirstOrDefault().Value);
}
else
{
Assert.DoesNotContain(activity.Tags, tag => tag.Key == "enrichedWithHttpRequestMessage");
Assert.DoesNotContain(activity.Tags, tag => tag.Key == "enrichedWithHttpResponseMessage");
}
#endif
}

Expand All @@ -223,7 +232,7 @@ public async Task InjectsHeadersAsync_CustomFormat()
action(message, "custom_tracestate", Activity.Current.TraceStateString);
});

var processor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();

using var request = new HttpRequestMessage
{
Expand All @@ -241,15 +250,15 @@ public async Task InjectsHeadersAsync_CustomFormat()

using (Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation()
.AddProcessor(processor.Object)
.AddInMemoryExporter(exportedItems)
.Build())
{
using var c = new HttpClient();
await c.SendAsync(request);
}

Assert.Equal(5, processor.Invocations.Count); // SetParentProvider/OnStart/OnEnd/OnShutdown/Dispose called.
var activity = (Activity)processor.Invocations[2].Arguments[0];
Assert.Single(exportedItems);
var activity = exportedItems[0];

Assert.Equal(ActivityKind.Client, activity.Kind);
Assert.Equal(parent.TraceId, activity.Context.TraceId);
Expand Down Expand Up @@ -291,7 +300,7 @@ public async Task RespectsSuppress()
action(message, "custom_tracestate", Activity.Current.TraceStateString);
});

var processor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();

using var request = new HttpRequestMessage
{
Expand All @@ -309,7 +318,7 @@ public async Task RespectsSuppress()

using (Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation()
.AddProcessor(processor.Object)
.AddInMemoryExporter(exportedItems)
.Build())
{
using var c = new HttpClient();
Expand All @@ -321,7 +330,7 @@ public async Task RespectsSuppress()

// If suppressed, activity is not emitted and
// propagation is also not performed.
Assert.Equal(3, processor.Invocations.Count); // SetParentProvider/OnShutdown/Dispose called.
Assert.Empty(exportedItems);
Assert.False(request.Headers.Contains("custom_traceparent"));
Assert.False(request.Headers.Contains("custom_tracestate"));
}
Expand All @@ -345,7 +354,7 @@ public async Task ExportsSpansCreatedForRetries()
Method = new HttpMethod("GET"),
};

using var traceprovider = Sdk.CreateTracerProviderBuilder()
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation()
.AddInMemoryExporter(exportedItems)
.Build();
Expand All @@ -357,7 +366,7 @@ public async Task ExportsSpansCreatedForRetries()
await httpClient.SendAsync(request);

// number of exported spans should be 3(maxRetries)
Assert.Equal(maxRetries, exportedItems.Count());
Assert.Equal(maxRetries, exportedItems.Count);

var spanid1 = exportedItems[0].SpanId;
var spanid2 = exportedItems[1].SpanId;
Expand Down Expand Up @@ -390,7 +399,7 @@ public async Task HttpRequestMethodIsSetOnActivityAsPerSpec(string originalMetho
Method = new HttpMethod(originalMethod),
};

using var traceprovider = Sdk.CreateTracerProviderBuilder()
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation()
.AddInMemoryExporter(exportedItems)
.Build();
Expand Down Expand Up @@ -447,7 +456,7 @@ public async Task HttpRequestMethodIsSetonRequestDurationMetricAsPerSpec(string
Method = new HttpMethod(originalMethod),
};

using var meterprovider = Sdk.CreateMeterProviderBuilder()
using var meterProvider = Sdk.CreateMeterProviderBuilder()
.AddHttpClientInstrumentation()
.AddInMemoryExporter(metricItems)
.Build();
Expand All @@ -463,7 +472,7 @@ public async Task HttpRequestMethodIsSetonRequestDurationMetricAsPerSpec(string
// ignore error.
}

meterprovider.Dispose();
meterProvider.Dispose();

var metric = metricItems.FirstOrDefault(m => m.Name == "http.client.request.duration");

Expand Down Expand Up @@ -493,10 +502,10 @@ public async Task HttpRequestMethodIsSetonRequestDurationMetricAsPerSpec(string
[Fact]
public async Task RedirectTest()
{
var processor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();
using (Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation()
.AddProcessor(processor.Object)
.AddInMemoryExporter(exportedItems)
.Build())
{
using var c = new HttpClient();
Expand All @@ -509,18 +518,12 @@ public async Task RedirectTest()
// good way to produce two spans when redirecting that we have
// found. For now, this is not supported.

Assert.Equal(5, processor.Invocations.Count); // SetParentProvider/OnStart/OnEnd/OnShutdown/Dispose called.

var firstActivity = (Activity)processor.Invocations[2].Arguments[0]; // First OnEnd
Assert.Contains(firstActivity.TagObjects, t => t.Key == "http.response.status_code" && (int)t.Value == 200);
Assert.Single(exportedItems);
Assert.Contains(exportedItems[0].TagObjects, t => t.Key == "http.response.status_code" && (int)t.Value == 200);
#else
Assert.Equal(7, processor.Invocations.Count); // SetParentProvider/OnStart/OnEnd/OnStart/OnEnd/OnShutdown/Dispose called.

var firstActivity = (Activity)processor.Invocations[2].Arguments[0]; // First OnEnd
Assert.Contains(firstActivity.TagObjects, t => t.Key == "http.response.status_code" && (int)t.Value == 302);

var secondActivity = (Activity)processor.Invocations[4].Arguments[0]; // Second OnEnd
Assert.Contains(secondActivity.TagObjects, t => t.Key == "http.response.status_code" && (int)t.Value == 200);
Assert.Equal(2, exportedItems.Count);
Assert.Contains(exportedItems[0].TagObjects, t => t.Key == "http.response.status_code" && (int)t.Value == 302);
Assert.Contains(exportedItems[1].TagObjects, t => t.Key == "http.response.status_code" && (int)t.Value == 200);
#endif
}

Expand Down Expand Up @@ -595,7 +598,7 @@ public async Task ReportsExceptionEventForNetworkFailuresWithGetAsync()
var exportedItems = new List<Activity>();
bool exceptionThrown = false;

using var traceprovider = Sdk.CreateTracerProviderBuilder()
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation(o => o.RecordException = true)
.AddInMemoryExporter(exportedItems)
.Build();
Expand All @@ -621,7 +624,7 @@ public async Task DoesNotReportExceptionEventOnErrorResponseWithGetAsync()
var exportedItems = new List<Activity>();
bool exceptionThrown = false;

using var traceprovider = Sdk.CreateTracerProviderBuilder()
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation(o => o.RecordException = true)
.AddInMemoryExporter(exportedItems)
.Build();
Expand Down Expand Up @@ -652,7 +655,7 @@ public async Task DoesNotReportExceptionEventOnErrorResponseWithGetStringAsync()
Method = new HttpMethod("GET"),
};

using var traceprovider = Sdk.CreateTracerProviderBuilder()
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation(o => o.RecordException = true)
.AddInMemoryExporter(exportedItems)
.Build();
Expand Down Expand Up @@ -706,7 +709,7 @@ public async Task CustomPropagatorCalled(bool sample, bool createParentActivity)

var exportedItems = new List<Activity>();

using (var traceprovider = Sdk.CreateTracerProviderBuilder()
using (var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation()
.AddInMemoryExporter(exportedItems)
.SetSampler(sample ? new ParentBasedSampler(new AlwaysOnSampler()) : new AlwaysOffSampler())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ public void Dispose()
[Fact]
public async Task BacksOffIfAlreadyInstrumented()
{
var activityProcessor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddProcessor(activityProcessor.Object)
.AddInMemoryExporter(exportedItems)
.AddHttpClientInstrumentation()
.Build();

Expand All @@ -90,12 +90,9 @@ public async Task BacksOffIfAlreadyInstrumented()
using var response = await request.GetResponseAsync();

#if NETFRAMEWORK
// Note: Back-off is part of the .NET Framework reflection only and
// is needed to prevent issues when the same request is re-used for
// things like redirects or SSL negotiation.
Assert.Single(activityProcessor.Invocations); // SetParentProvider called
Assert.Empty(exportedItems);
#else
Assert.Equal(3, activityProcessor.Invocations.Count); // SetParentProvider/Begin/End called
Assert.Single(exportedItems);
#endif
}

Expand All @@ -105,7 +102,7 @@ public async Task RequestNotCollectedWhenInstrumentationFilterApplied()
bool httpWebRequestFilterApplied = false;
bool httpRequestMessageFilterApplied = false;

List<Activity> exportedItems = new();
var exportedItems = new List<Activity>();

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddInMemoryExporter(exportedItems)
Expand Down Expand Up @@ -145,7 +142,7 @@ public async Task RequestNotCollectedWhenInstrumentationFilterApplied()
[Fact]
public async Task RequestNotCollectedWhenInstrumentationFilterThrowsException()
{
List<Activity> exportedItems = new();
var exportedItems = new List<Activity>();

using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddInMemoryExporter(exportedItems)
Expand Down Expand Up @@ -174,9 +171,9 @@ public async Task RequestNotCollectedWhenInstrumentationFilterThrowsException()
[Fact]
public async Task InjectsHeadersAsync()
{
var activityProcessor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddProcessor(activityProcessor.Object)
.AddInMemoryExporter(exportedItems)
.AddHttpClientInstrumentation()
.Build();

Expand All @@ -192,8 +189,8 @@ public async Task InjectsHeadersAsync()

using var response = await request.GetResponseAsync();

Assert.Equal(3, activityProcessor.Invocations.Count); // SetParentProvider/Begin/End called
var activity = (Activity)activityProcessor.Invocations[2].Arguments[0];
Assert.Single(exportedItems);
var activity = exportedItems[0];

Assert.Equal(parent.TraceId, activity.Context.TraceId);
Assert.Equal(parent.SpanId, activity.ParentSpanId);
Expand Down Expand Up @@ -312,13 +309,11 @@ public void AddHttpClientInstrumentationUsesOptionsApi(string name)

int configurationDelegateInvocations = 0;

var activityProcessor = new Mock<BaseProcessor<Activity>>();
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.ConfigureServices(services =>
{
services.Configure<HttpClientInstrumentationOptions>(name, o => configurationDelegateInvocations++);
})
.AddProcessor(activityProcessor.Object)
.AddHttpClientInstrumentation(name, options =>
{
Assert.IsType<HttpClientInstrumentationOptions>(options);
Expand All @@ -334,7 +329,7 @@ public async Task ReportsExceptionEventForNetworkFailures()
var exportedItems = new List<Activity>();
bool exceptionThrown = false;

using var traceprovider = Sdk.CreateTracerProviderBuilder()
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation(o => o.RecordException = true)
.AddInMemoryExporter(exportedItems)
.Build();
Expand Down Expand Up @@ -363,7 +358,7 @@ public async Task ReportsExceptionEventOnErrorResponse()
var exportedItems = new List<Activity>();
bool exceptionThrown = false;

using var traceprovider = Sdk.CreateTracerProviderBuilder()
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddHttpClientInstrumentation(o => o.RecordException = true)
.AddInMemoryExporter(exportedItems)
.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using System.Diagnostics;
using System.Net;
using System.Text.Json;
using Moq;
using OpenTelemetry.Tests;
using OpenTelemetry.Trace;
using Xunit;
Expand Down Expand Up @@ -49,9 +48,9 @@ public void HttpOutCallsAreCollectedSuccessfully(HttpTestData.HttpOutTestCase tc
bool enrichWithHttpResponseMessageCalled = false;
bool enrichWithExceptionCalled = false;

var activityProcessor = new Mock<BaseProcessor<Activity>>();
var exportedItems = new List<Activity>();
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddProcessor(activityProcessor.Object)
.AddInMemoryExporter(exportedItems)
.AddHttpClientInstrumentation(options =>
{
options.EnrichWithHttpWebRequest = (activity, httpWebRequest) => { enrichWithHttpWebRequestCalled = true; };
Expand Down Expand Up @@ -92,8 +91,8 @@ public void HttpOutCallsAreCollectedSuccessfully(HttpTestData.HttpOutTestCase tc
tc.ResponseExpected = false;
}

Assert.Equal(3, activityProcessor.Invocations.Count); // SetParentProvider/Begin/End called
var activity = (Activity)activityProcessor.Invocations[2].Arguments[0];
Assert.Single(exportedItems);
var activity = exportedItems[0];
ValidateHttpWebRequestActivity(activity);
Assert.Equal(tc.SpanName, activity.DisplayName);

Expand Down

0 comments on commit 79aa39f

Please sign in to comment.