Skip to content

Commit 9f61be4

Browse files
committed
[Tests] Warmup collector endpoints on .NET
1 parent 31a7277 commit 9f61be4

File tree

6 files changed

+126
-6
lines changed

6 files changed

+126
-6
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#if NET
5+
using System.Net;
6+
using Microsoft.AspNetCore.Http;
7+
using Xunit.Abstractions;
8+
9+
namespace IntegrationTests.Helpers;
10+
11+
internal static class MockCollectorHealthZ
12+
{
13+
private static readonly HttpClient HttpClient = new()
14+
{
15+
Timeout = TimeSpan.FromSeconds(1)
16+
};
17+
18+
public static PathHandler CreateHealthZHandler()
19+
{
20+
return new PathHandler(HandleHealthZRequests, "/healthz");
21+
}
22+
23+
public static async Task WarmupHealthZEndpoint(ITestOutputHelper output, string host, int port)
24+
{
25+
var finalHost = host == "*" ? "localhost" : host;
26+
27+
var healthZUrl = new Uri($"http://{finalHost}:{port}/healthz");
28+
const int maxAttempts = 10;
29+
for (var i = 1; i <= maxAttempts; ++i)
30+
{
31+
try
32+
{
33+
var response = await HttpClient.GetAsync(healthZUrl).ConfigureAwait(false);
34+
if (response.StatusCode == HttpStatusCode.OK)
35+
{
36+
return;
37+
}
38+
}
39+
#pragma warning disable CA1031 // Do not catch general exception types. Catching all exceptions to retry.
40+
catch (Exception e)
41+
#pragma warning restore CA1031 // Do not catch general exception types. Catching all exceptions to retry.
42+
{
43+
output.WriteLine($"Exception while calling {healthZUrl}: {e.Message}. Attempt: {i}");
44+
}
45+
46+
if (i == maxAttempts)
47+
{
48+
throw new InvalidOperationException($"Failed to warm up healthz endpoint at {healthZUrl} after {maxAttempts} attempts.");
49+
}
50+
}
51+
}
52+
53+
private static async Task HandleHealthZRequests(HttpContext context)
54+
{
55+
context.Response.StatusCode = 200;
56+
await context.Response.WriteAsync("OK").ConfigureAwait(false);
57+
}
58+
}
59+
#endif

test/IntegrationTests/Helpers/MockLogsCollector.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@ internal sealed class MockLogsCollector : IDisposable
2424

2525
private CollectedExpectation? _collectedExpectation;
2626

27-
public MockLogsCollector(ITestOutputHelper output, string host = "localhost")
27+
private MockLogsCollector(ITestOutputHelper output, string host)
2828
{
2929
_output = output;
3030

3131
#if NETFRAMEWORK
3232
_listener = new(output, HandleHttpRequests, host, "/v1/logs/");
3333
#else
34-
_listener = new(output, nameof(MockLogsCollector), new PathHandler(HandleHttpRequests, "/v1/logs"));
34+
_listener = new(output, nameof(MockLogsCollector), new PathHandler(HandleHttpRequests, "/v1/logs"), MockCollectorHealthZ.CreateHealthZHandler());
3535
#endif
3636
}
3737

@@ -42,6 +42,21 @@ public MockLogsCollector(ITestOutputHelper output, string host = "localhost")
4242

4343
public OtlpResourceExpector ResourceExpector { get; } = new();
4444

45+
#if NET
46+
public static async Task<MockLogsCollector> InitializeAsync(ITestOutputHelper output, string host = "localhost")
47+
#else
48+
public static Task<MockLogsCollector> InitializeAsync(ITestOutputHelper output, string host = "localhost")
49+
#endif
50+
{
51+
var collector = new MockLogsCollector(output, host);
52+
#if NET
53+
await MockCollectorHealthZ.WarmupHealthZEndpoint(output, host, collector.Port);
54+
return collector;
55+
#else
56+
return Task.FromResult(collector);
57+
#endif
58+
}
59+
4560
public void Dispose()
4661
{
4762
WriteOutput($"Shutting down. Total logs requests received: '{_logs.Count}'");

test/IntegrationTests/Helpers/MockMetricsCollector.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ internal sealed class MockMetricsCollector : IDisposable
2525
private readonly BlockingCollection<List<Collected>> _metricsSnapshots = new(10); // bounded to avoid memory leak; contains protobuf type
2626
private Func<ICollection<Collected>, bool>? _additionalEntriesExpectation;
2727

28-
public MockMetricsCollector(ITestOutputHelper output, string host = "localhost")
28+
private MockMetricsCollector(ITestOutputHelper output, string host)
2929
{
3030
_output = output;
3131
#if NETFRAMEWORK
3232
_listener = new(output, HandleHttpRequests, host, "/v1/metrics/");
3333
#else
34-
_listener = new(output, nameof(MockMetricsCollector), new PathHandler(HandleHttpRequests, "/v1/metrics"));
34+
_listener = new(output, nameof(MockMetricsCollector), new PathHandler(HandleHttpRequests, "/v1/metrics"), MockCollectorHealthZ.CreateHealthZHandler());
3535
#endif
3636
}
3737

@@ -42,6 +42,21 @@ public MockMetricsCollector(ITestOutputHelper output, string host = "localhost")
4242

4343
public OtlpResourceExpector ResourceExpector { get; } = new();
4444

45+
#if NET
46+
public static async Task<MockMetricsCollector> InitializeAsync(ITestOutputHelper output, string host = "localhost")
47+
#else
48+
public static Task<MockMetricsCollector> InitializeAsync(ITestOutputHelper output, string host = "localhost")
49+
#endif
50+
{
51+
var collector = new MockMetricsCollector(output, host);
52+
#if NET
53+
await MockCollectorHealthZ.WarmupHealthZEndpoint(output, host, collector.Port);
54+
return collector;
55+
#else
56+
return Task.FromResult(collector);
57+
#endif
58+
}
59+
4560
public void Dispose()
4661
{
4762
WriteOutput("Shutting down.");

test/IntegrationTests/Helpers/MockProfilesCollector.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal sealed class MockProfilesCollector : IDisposable
2525
private readonly BlockingCollection<Collected> _profilesSnapshots = new(10); // bounded to avoid memory leak; contains protobuf type
2626
private CollectedExpectation? _collectedExpectation;
2727

28-
public MockProfilesCollector(ITestOutputHelper output, string host = "localhost")
28+
private MockProfilesCollector(ITestOutputHelper output, string host)
2929
{
3030
_output = output;
3131
#if NETFRAMEWORK
@@ -42,6 +42,21 @@ public MockProfilesCollector(ITestOutputHelper output, string host = "localhost"
4242

4343
public OtlpResourceExpector ResourceExpector { get; } = new();
4444

45+
#if NET
46+
public static async Task<MockProfilesCollector> InitializeAsync(ITestOutputHelper output, string host = "localhost")
47+
#else
48+
public static Task<MockProfilesCollector> InitializeAsync(ITestOutputHelper output, string host = "localhost")
49+
#endif
50+
{
51+
var collector = new MockProfilesCollector(output, host);
52+
#if NET
53+
await MockCollectorHealthZ.WarmupHealthZEndpoint(output, host, collector.Port);
54+
return collector;
55+
#else
56+
return Task.FromResult(collector);
57+
#endif
58+
}
59+
4560
public void Dispose()
4661
{
4762
WriteOutput("Shutting down.");

test/IntegrationTests/Helpers/MockSpansCollector.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ internal sealed class MockSpansCollector : IDisposable
2626
private readonly List<Expectation> _expectations = new();
2727
private Func<ICollection<Collected>, bool>? _collectedExpectation;
2828

29-
public MockSpansCollector(ITestOutputHelper output, string host = "localhost")
29+
private MockSpansCollector(ITestOutputHelper output, string host)
3030
{
3131
_output = output;
3232

@@ -44,6 +44,21 @@ public MockSpansCollector(ITestOutputHelper output, string host = "localhost")
4444

4545
public OtlpResourceExpector ResourceExpector { get; } = new();
4646

47+
#if NET
48+
public static async Task<MockSpansCollector> InitializeAsync(ITestOutputHelper output, string host = "localhost")
49+
#else
50+
public static Task<MockSpansCollector> InitializeAsync(ITestOutputHelper output, string host = "localhost")
51+
#endif
52+
{
53+
var collector = new MockSpansCollector(output, host);
54+
#if NET
55+
await MockCollectorHealthZ.WarmupHealthZEndpoint(output, host, collector.Port).ConfigureAwait(false);
56+
return collector;
57+
#else
58+
return Task.FromResult(collector);
59+
#endif
60+
}
61+
4762
public void Dispose()
4863
{
4964
WriteOutput("Shutting down.");

test/NuGetPackagesTests/NuGetPackagesTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<Compile Include="..\IntegrationTests\Helpers\EnvironmentHelper.cs" />
1717
<Compile Include="..\IntegrationTests\Helpers\EnvironmentTools.cs" />
1818
<Compile Include="..\IntegrationTests\Helpers\InstrumentedProcessHelper.cs" />
19+
<Compile Include="..\IntegrationTests\Helpers\MockCollectorHealthZ.cs" />
1920
<Compile Include="..\IntegrationTests\Helpers\MockSpansCollector.cs" />
2021
<Compile Include="..\IntegrationTests\Helpers\OtlpResourceExpector.cs" />
2122
<Compile Include="..\IntegrationTests\Helpers\OutputHelper.cs" />

0 commit comments

Comments
 (0)