-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimalApiTests.cs
72 lines (58 loc) · 2.38 KB
/
MinimalApiTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
#if NET
using IntegrationTests.Helpers;
using OpenTelemetry.Proto.Logs.V1;
using Xunit.Abstractions;
namespace IntegrationTests;
public class MinimalApiTests : TestHelper
{
public MinimalApiTests(ITestOutputHelper output)
: base("MinimalApi", output)
{
}
[SkippableTheory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, true)]
[Trait("Category", "EndToEnd")]
public async Task SubmitsLogsWithoutDuplicates(bool enableByteCodeInstrumentation, bool enableHostingStartupAssembly)
{
using var collector = new MockLogsCollector(Output);
SetExporter(collector);
SetEnvironmentVariable("OTEL_DOTNET_AUTO_LOGS_INCLUDE_FORMATTED_MESSAGE", "true");
if (enableByteCodeInstrumentation)
{
EnableBytecodeInstrumentation();
collector.ExpectCollected(ValidateCombinedLogsExport, "All log records should be exported once.");
}
if (enableHostingStartupAssembly)
{
SetEnvironmentVariable("ASPNETCORE_HOSTINGSTARTUPASSEMBLIES", "OpenTelemetry.AutoInstrumentation.AspNetCoreBootstrapper");
if (!enableByteCodeInstrumentation)
{
// otherwise expectation was already set
collector.ExpectCollected(ValidateSingleAppLogExport, "Application log records should be exported once.");
}
}
RunTestApplication();
// wait for fixed amount of time for logs to be collected before asserting
await Task.Delay(TimeSpan.FromSeconds(10));
collector.AssertCollected();
}
private static bool ValidateCombinedLogsExport(IEnumerable<LogRecord> records)
{
return ValidateSingleAppLogExport(records) && ValidateSingleBeforeHostLogRecord(records);
}
private static bool ValidateSingleBeforeHostLogRecord(IEnumerable<LogRecord> records)
{
var beforeHostLogCount = records.Count(lr => Convert.ToString(lr.Body) == "{ \"stringValue\": \"Logged before host is built.\" }");
return beforeHostLogCount == 1;
}
private static bool ValidateSingleAppLogExport(IEnumerable<LogRecord> records)
{
var appLogCount = records.Count(lr => Convert.ToString(lr.Body) == "{ \"stringValue\": \"Request received.\" }");
return appLogCount == 1;
}
}
#endif