Skip to content

Commit 268185d

Browse files
Add test for distro (Azure#34697)
* draft * rename * do not et transport to null * comments * address pr comment * Update AzureMonitorOptions.cs
1 parent 49dae6f commit 268185d

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

sdk/monitor/Azure.Monitor.OpenTelemetry/Azure.Monitor.OpenTelemetry.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Monitor.OpenTelemetry
1818
EndProject
1919
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Monitor.OpenTelemetry.Demo", "tests\Azure.Monitor.OpenTelemetry.Demo\Azure.Monitor.OpenTelemetry.Demo.csproj", "{6F094CAA-F6BB-4A9C-9D1F-1165F0758C99}"
2020
EndProject
21+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Azure.Monitor.OpenTelemetry.Tests", "tests\Azure.Monitor.OpenTelemetry.Tests\Azure.Monitor.OpenTelemetry.Tests.csproj", "{E047E7C5-F248-49DF-BC2C-A690BC5F0F71}"
22+
EndProject
2123
Global
2224
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2325
Debug|Any CPU = Debug|Any CPU
@@ -36,6 +38,10 @@ Global
3638
{6F094CAA-F6BB-4A9C-9D1F-1165F0758C99}.Debug|Any CPU.Build.0 = Debug|Any CPU
3739
{6F094CAA-F6BB-4A9C-9D1F-1165F0758C99}.Release|Any CPU.ActiveCfg = Release|Any CPU
3840
{6F094CAA-F6BB-4A9C-9D1F-1165F0758C99}.Release|Any CPU.Build.0 = Release|Any CPU
41+
{E047E7C5-F248-49DF-BC2C-A690BC5F0F71}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
42+
{E047E7C5-F248-49DF-BC2C-A690BC5F0F71}.Debug|Any CPU.Build.0 = Debug|Any CPU
43+
{E047E7C5-F248-49DF-BC2C-A690BC5F0F71}.Release|Any CPU.ActiveCfg = Release|Any CPU
44+
{E047E7C5-F248-49DF-BC2C-A690BC5F0F71}.Release|Any CPU.Build.0 = Release|Any CPU
3945
EndGlobalSection
4046
GlobalSection(SolutionProperties) = preSolution
4147
HideSolutionNode = FALSE

sdk/monitor/Azure.Monitor.OpenTelemetry/src/AzureMonitorOptions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
#nullable disable
55

66
using Azure.Core;
7+
using Azure.Core.Pipeline;
78
using Azure.Monitor.OpenTelemetry.Exporter;
89
using Microsoft.Extensions.DependencyInjection;
910
using Microsoft.Extensions.Options;
1011
using System;
12+
using System.Net;
1113

1214
namespace Azure.Monitor.OpenTelemetry
1315
{
@@ -59,6 +61,9 @@ public class AzureMonitorOptions
5961
/// </summary>
6062
public string StorageDirectory { get; set; }
6163

64+
// Used for testing purpose
65+
internal HttpPipelineTransport Transport;
66+
6267
internal AzureMonitorOptions Clone(AzureMonitorOptions options)
6368
{
6469
if (options != null)
@@ -69,6 +74,7 @@ internal AzureMonitorOptions Clone(AzureMonitorOptions options)
6974
EnableMetrics = options.EnableMetrics;
7075
EnableTraces = options.EnableTraces;
7176
StorageDirectory = options.StorageDirectory;
77+
Transport = options.Transport;
7278
}
7379

7480
return this;
@@ -86,6 +92,10 @@ internal void SetValueToExporterOptions(AzureMonitorExporterOptions exporterOpti
8692
exporterOptions.Credential = Credential;
8793
exporterOptions.DisableOfflineStorage = DisableOfflineStorage;
8894
exporterOptions.StorageDirectory = StorageDirectory;
95+
if (Transport != null)
96+
{
97+
exporterOptions.Transport = Transport;
98+
}
8999
}
90100
}
91101
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System.Runtime.CompilerServices;
5+
6+
[assembly: InternalsVisibleTo("Azure.Monitor.OpenTelemetry.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100d15ddcb29688295338af4b7686603fe614abd555e09efba8fb88ee09e1f7b1ccaeed2e8f823fa9eef3fdd60217fc012ea67d2479751a0b8c087a4185541b851bd8b16f8d91b840e51b1cb0ba6fe647997e57429265e85ef62d565db50a69ae1647d54d7bd855e4db3d8a91510e5bcbd0edfbbecaa20a7bd9ae74593daa7b11b4")]
7+
// Moq
8+
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2, PublicKey=0024000004800000940000000602000000240000525341310004000001000100c547cac37abd99c8db225ef2f6c8a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15605093924cceaf74c4861eff62abf69b9291ed0a340e113be11e6a7d3113e92484cf7045cc7")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="xunit" />
9+
<PackageReference Include="xunit.runner.visualstudio" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" />
11+
<PackageReference Include="Moq" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="$(AzureCoreTestFramework)" />
16+
<ProjectReference Include="..\..\src\Azure.Monitor.OpenTelemetry.csproj" />
17+
</ItemGroup>
18+
19+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
#if NET6_0_OR_GREATER
5+
using System.Threading;
6+
using System.Threading.Tasks;
7+
using Microsoft.AspNetCore.Builder;
8+
using Microsoft.Extensions.Logging;
9+
using Xunit;
10+
using Azure.Core.TestFramework;
11+
using System.Net.Http;
12+
using System;
13+
14+
namespace Azure.Monitor.OpenTelemetry.Tests
15+
{
16+
public class AzureMonitorDistroTests
17+
{
18+
[Fact]
19+
public async Task ValidateTelemetryExport()
20+
{
21+
var builder = WebApplication.CreateBuilder();
22+
builder.Logging.ClearProviders();
23+
var mockResponse = new MockResponse(200).SetContent("Ok");
24+
var transport = new MockTransport(mockResponse);
25+
builder.Services.AddAzureMonitor(o =>
26+
{
27+
o.Transport = transport;
28+
o.ConnectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000";
29+
o.EnableLogs = false;
30+
o.EnableMetrics = false;
31+
});
32+
33+
var app = builder.Build();
34+
app.MapGet("/", () => "Hello");
35+
36+
_ = app.RunAsync("http://localhost:9999");
37+
38+
// Send request
39+
using var httpClient = new HttpClient();
40+
var res = await httpClient.GetStringAsync("http://localhost:9999").ConfigureAwait(false);
41+
Assert.NotNull(res);
42+
43+
// Wait for the backend/ingestion to receive requests
44+
WaitForRequest(transport);
45+
46+
// Assert
47+
// TODO: Validate request content
48+
Assert.True(transport.Requests.Count > 0);
49+
50+
await app.DisposeAsync();
51+
}
52+
53+
private void WaitForRequest(MockTransport transport)
54+
{
55+
SpinWait.SpinUntil(
56+
condition: () =>
57+
{
58+
Thread.Sleep(10);
59+
return transport.Requests.Count > 0;
60+
},
61+
timeout: TimeSpan.FromSeconds(10));
62+
}
63+
}
64+
}
65+
#endif

0 commit comments

Comments
 (0)