forked from Azure/azure-sdk-for-net
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Azure Monitor Exporter: Basic Integration Tests (Azure#15539)
* adding empty IntegrationTest projects * demo tests working with webapp * update basic tests * refactor AzureMonitorTransmitter * extra controller * saving work in progress * saving work in progress * saving work in progress * saving work in progress * progress * saving progress * cleanup * cleanup * cleanup * cleanup * ForceFlush * cleanup * change * cleanup * cleanup * cleanup * resolving conflicts * new projects * suppress warning NetSDK1005 * code review cleanup * remove Microsoft.AspNetCore.App to support net461 * fix references * cleanup * addressing code review comments * removing packagereference versions * renaming projects * cleanup controllers * testing a fix for test project * testing fix for webapp project * testing fix for build error * testing build fix * rename * fixing merge conflicts. tests are failing * more renamings, investigating build fail * fix namespace * fix other test * test to disable parallel tests * testing fixes for POC * refactor * cleanup * saving progress, tests working * cleanup * fix merge conflict * cleanup * cleanup
- Loading branch information
1 parent
3b4fe10
commit 2d44823
Showing
9 changed files
with
185 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
sdk/monitor/OpenTelemetry.Exporter.AzureMonitor/src/ITransmitter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using OpenTelemetry.Exporter.AzureMonitor.Models; | ||
|
||
namespace OpenTelemetry.Exporter.AzureMonitor | ||
{ | ||
internal interface ITransmitter | ||
{ | ||
/// <summary> | ||
/// Sent telemetry and return the number of items Accepted. | ||
/// </summary> | ||
ValueTask<int> TrackAsync(IEnumerable<TelemetryItem> telemetryItems, bool async, CancellationToken cancellationToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
...Monitor/tests/OpenTelemetry.Exporter.AzureMonitor.Integration.Tests/OpenTelemetryTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
using OpenTelemetry.Exporter.AzureMonitor.Integration.Tests.TestFramework; | ||
|
||
using Xunit; | ||
using Xunit.Abstractions; | ||
|
||
namespace OpenTelemetry.Exporter.AzureMonitor.Integration.Tests | ||
{ | ||
public class OpenTelemetryTests : IClassFixture<OpenTelemetryWebApplicationFactory<AspNetCoreWebApp.Startup>> | ||
{ | ||
private readonly OpenTelemetryWebApplicationFactory<AspNetCoreWebApp.Startup> factory; | ||
private readonly ITestOutputHelper output; | ||
|
||
public OpenTelemetryTests(OpenTelemetryWebApplicationFactory<AspNetCoreWebApp.Startup> factory, ITestOutputHelper output) | ||
{ | ||
this.factory = factory; | ||
this.output = output; | ||
} | ||
|
||
/// <summary> | ||
/// This test validates that when an app instrumented with the AzureMonitorExporter receives an HTTP request, | ||
/// A TelemetryItem is created matching that request. | ||
/// </summary> | ||
[Fact] | ||
public async Task ProofOfConcept() | ||
{ | ||
string testValue = Guid.NewGuid().ToString(); | ||
|
||
// Arrange | ||
var client = this.factory.CreateClient(); | ||
|
||
//// Act | ||
var response = await client.GetAsync($"api/home/{testValue}"); | ||
|
||
// Shutdown | ||
response.EnsureSuccessStatusCode(); | ||
Task.Delay(100).Wait(); //TODO: HOW TO REMOVE THIS WAIT? | ||
this.factory.ForceFlush(); | ||
|
||
// Assert | ||
Assert.True(this.factory.TelemetryItems.Any(), "test project did not capture telemetry"); | ||
|
||
PrintTelemetryItems(this.factory.TelemetryItems); | ||
var item = this.factory.TelemetryItems.Single(); | ||
var baseData = (Models.RequestData)item.Data.BaseData; | ||
Assert.True(baseData.Url.EndsWith(testValue), "it is expected that the recorded TelemetryItem matches the value of testValue."); | ||
} | ||
|
||
/// <summary> | ||
/// This uses the XUnit ITestOutputHelper to print details to the output of the test run. | ||
/// </summary> | ||
private void PrintTelemetryItems(IEnumerable<Models.TelemetryItem> telemetryItems) | ||
{ | ||
foreach (var item in telemetryItems) | ||
{ | ||
this.output.WriteLine(item.Name); | ||
|
||
if (item.Data.BaseData is Models.RequestData requestData) | ||
{ | ||
this.output.WriteLine($"\t{requestData.Url}"); | ||
} | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...or/tests/OpenTelemetry.Exporter.AzureMonitor.Integration.Tests/Properties/AssemblyInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using Xunit; | ||
[assembly: CollectionBehavior(CollectionBehavior.CollectionPerAssembly, DisableTestParallelization = true)] |
28 changes: 28 additions & 0 deletions
28
...ts/OpenTelemetry.Exporter.AzureMonitor.Integration.Tests/TestFramework/MockTransmitter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
using OpenTelemetry.Exporter.AzureMonitor.Models; | ||
|
||
namespace OpenTelemetry.Exporter.AzureMonitor.Integration.Tests.TestFramework | ||
{ | ||
public class MockTransmitter : ITransmitter | ||
{ | ||
public ConcurrentBag<TelemetryItem> TelemetryItems = new ConcurrentBag<TelemetryItem>(); | ||
|
||
public ValueTask<int> TrackAsync(IEnumerable<TelemetryItem> telemetryItems, bool async, CancellationToken cancellationToken) | ||
{ | ||
foreach (var telemetryItem in telemetryItems) | ||
{ | ||
this.TelemetryItems.Add(telemetryItem); | ||
} | ||
|
||
return new ValueTask<int>(Task.FromResult(telemetryItems.Count())); | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...porter.AzureMonitor.Integration.Tests/TestFramework/OpenTelemetryWebApplicationFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Concurrent; | ||
|
||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc.Testing; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
using OpenTelemetry.Exporter.AzureMonitor.Models; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace OpenTelemetry.Exporter.AzureMonitor.Integration.Tests.TestFramework | ||
{ | ||
/// <summary> | ||
/// This class implements <see cref="WebApplicationFactory"/> and will configure the <see cref="IServiceCollection"/> for OpenTelemetry and the <see cref="AzureMonitorTraceExporter"/>. | ||
/// Here we mock the <see cref="ServiceRestClient"/> to capture telemetry that would have been sent to the ingestion service. | ||
/// We also mock the <see cref="TrackResponse"/> which would have been received from the ingestion service. | ||
/// </summary> | ||
/// <typeparam name="TStartup">Startup class from the application to be used during this test.</typeparam> | ||
public class OpenTelemetryWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class | ||
{ | ||
public ConcurrentBag<TelemetryItem> TelemetryItems => this.Transmitter.TelemetryItems; | ||
|
||
private const string EmptyConnectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000"; | ||
private readonly MockTransmitter Transmitter = new MockTransmitter(); | ||
private ActivityProcessor ActivityProcessor; | ||
|
||
protected override void ConfigureWebHost(IWebHostBuilder builder) | ||
{ | ||
this.ActivityProcessor = new BatchExportActivityProcessor(new AzureMonitorTraceExporter( | ||
options: new AzureMonitorExporterOptions | ||
{ | ||
ConnectionString = EmptyConnectionString, | ||
}, | ||
transmitter: this.Transmitter)); | ||
|
||
builder.ConfigureServices(services => services.AddOpenTelemetryTracing((builder) => builder | ||
.AddAspNetCoreInstrumentation() | ||
.AddHttpClientInstrumentation() | ||
.AddProcessor(this.ActivityProcessor))); | ||
} | ||
|
||
public void ForceFlush() => this.ActivityProcessor.ForceFlush(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters