Skip to content

Commit 22bb5ef

Browse files
[OTLP] Refactor integration tests
- Refactor tests to use `[MemberData]` to avoid duplication. - Fix code analysis suggestions. - Log event level.
1 parent 6a70665 commit 22bb5ef

File tree

1 file changed

+112
-74
lines changed

1 file changed

+112
-74
lines changed

test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTest/IntegrationTests.cs

Lines changed: 112 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -19,49 +19,106 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests;
1919
public sealed class IntegrationTests : IDisposable
2020
{
2121
private const string CollectorHostnameEnvVarName = "OTEL_COLLECTOR_HOSTNAME";
22-
private const int ExportIntervalMilliseconds = 10000;
22+
private const int ExportIntervalMilliseconds = 10_000;
23+
private const string GrpcEndpointHttp = ":4317";
24+
private const string GrpcEndpointHttps = ":5317";
25+
private const string ProtobufEndpointHttp = ":4318/v1/";
26+
private const string ProtobufEndpointHttps = ":5318/v1/";
27+
2328
private static readonly SdkLimitOptions DefaultSdkLimitOptions = new();
2429
private static readonly ExperimentalOptions DefaultExperimentalOptions = new();
2530
private static readonly string? CollectorHostname = SkipUnlessEnvVarFoundTheoryAttribute.GetEnvironmentVariable(CollectorHostnameEnvVarName);
31+
32+
private static readonly bool[] BooleanValues = [false, true];
33+
private static readonly ExportProcessorType[] ExportProcessorTypes = [ExportProcessorType.Batch, ExportProcessorType.Simple];
34+
2635
private readonly OpenTelemetryEventListener openTelemetryEventListener;
2736

2837
public IntegrationTests(ITestOutputHelper outputHelper)
2938
{
3039
this.openTelemetryEventListener = new(outputHelper);
3140
}
3241

33-
public void Dispose()
42+
public static TheoryData<OtlpExportProtocol, string, ExportProcessorType, bool, string> TraceTestCases()
43+
{
44+
var data = new TheoryData<OtlpExportProtocol, string, ExportProcessorType, bool, string>();
45+
46+
#pragma warning disable CS0618 // Suppressing gRPC obsolete warning
47+
foreach (var exportType in ExportProcessorTypes)
48+
{
49+
foreach (var forceFlush in BooleanValues)
50+
{
51+
data.Add(OtlpExportProtocol.Grpc, GrpcEndpointHttp, exportType, forceFlush, Uri.UriSchemeHttp);
52+
data.Add(OtlpExportProtocol.HttpProtobuf, $"{ProtobufEndpointHttp}traces", exportType, forceFlush, Uri.UriSchemeHttp);
53+
}
54+
}
55+
56+
data.Add(OtlpExportProtocol.Grpc, GrpcEndpointHttps, ExportProcessorType.Simple, true, Uri.UriSchemeHttps);
57+
data.Add(OtlpExportProtocol.HttpProtobuf, $"{ProtobufEndpointHttps}traces", ExportProcessorType.Simple, true, Uri.UriSchemeHttps);
58+
#pragma warning restore CS0618 // Suppressing gRPC obsolete warning
59+
60+
return data;
61+
}
62+
63+
public static TheoryData<OtlpExportProtocol, string, bool, bool, string> MetricsTestCases()
3464
{
35-
this.openTelemetryEventListener.Dispose();
65+
var data = new TheoryData<OtlpExportProtocol, string, bool, bool, string>();
66+
67+
#pragma warning disable CS0618 // Suppressing gRPC obsolete warning
68+
foreach (var useManualExport in BooleanValues)
69+
{
70+
foreach (var forceFlush in BooleanValues)
71+
{
72+
data.Add(OtlpExportProtocol.Grpc, GrpcEndpointHttp, useManualExport, forceFlush, Uri.UriSchemeHttp);
73+
data.Add(OtlpExportProtocol.HttpProtobuf, $"{ProtobufEndpointHttp}metrics", useManualExport, forceFlush, Uri.UriSchemeHttp);
74+
}
75+
}
76+
77+
data.Add(OtlpExportProtocol.Grpc, GrpcEndpointHttps, true, true, Uri.UriSchemeHttps);
78+
data.Add(OtlpExportProtocol.HttpProtobuf, $"{ProtobufEndpointHttps}metrics", true, true, Uri.UriSchemeHttps);
79+
#pragma warning restore CS0618 // Suppressing gRPC obsolete warning
80+
81+
return data;
3682
}
3783

84+
public static TheoryData<OtlpExportProtocol, string, ExportProcessorType, string> LogsTestCases()
85+
{
86+
var data = new TheoryData<OtlpExportProtocol, string, ExportProcessorType, string>();
87+
3888
#pragma warning disable CS0618 // Suppressing gRPC obsolete warning
39-
[InlineData(OtlpExportProtocol.Grpc, ":4317", ExportProcessorType.Batch, false)]
40-
[InlineData(OtlpExportProtocol.HttpProtobuf, ":4318/v1/traces", ExportProcessorType.Batch, false)]
41-
[InlineData(OtlpExportProtocol.Grpc, ":4317", ExportProcessorType.Batch, true)]
42-
[InlineData(OtlpExportProtocol.HttpProtobuf, ":4318/v1/traces", ExportProcessorType.Batch, true)]
43-
[InlineData(OtlpExportProtocol.Grpc, ":4317", ExportProcessorType.Simple, false)]
44-
[InlineData(OtlpExportProtocol.HttpProtobuf, ":4318/v1/traces", ExportProcessorType.Simple, false)]
45-
[InlineData(OtlpExportProtocol.Grpc, ":4317", ExportProcessorType.Simple, true)]
46-
[InlineData(OtlpExportProtocol.HttpProtobuf, ":4318/v1/traces", ExportProcessorType.Simple, true)]
47-
[InlineData(OtlpExportProtocol.Grpc, ":5317", ExportProcessorType.Simple, true, "https")]
48-
[InlineData(OtlpExportProtocol.HttpProtobuf, ":5318/v1/traces", ExportProcessorType.Simple, true, "https")]
89+
foreach (var exportType in ExportProcessorTypes)
90+
{
91+
data.Add(OtlpExportProtocol.Grpc, GrpcEndpointHttp, exportType, Uri.UriSchemeHttp);
92+
data.Add(OtlpExportProtocol.HttpProtobuf, $"{ProtobufEndpointHttp}logs", exportType, Uri.UriSchemeHttp);
93+
}
94+
95+
data.Add(OtlpExportProtocol.Grpc, GrpcEndpointHttps, ExportProcessorType.Simple, Uri.UriSchemeHttps);
96+
data.Add(OtlpExportProtocol.HttpProtobuf, $"{ProtobufEndpointHttps}logs", ExportProcessorType.Simple, Uri.UriSchemeHttps);
4997
#pragma warning restore CS0618 // Suppressing gRPC obsolete warning
98+
99+
return data;
100+
}
101+
102+
public void Dispose() => this.openTelemetryEventListener.Dispose();
103+
50104
[Trait("CategoryName", "CollectorIntegrationTests")]
51105
[SkipUnlessEnvVarFoundTheory(CollectorHostnameEnvVarName)]
52-
public void TraceExportResultIsSuccess(OtlpExportProtocol protocol, string endpoint, ExportProcessorType exportProcessorType, bool forceFlush, string scheme = "http")
106+
[MemberData(nameof(TraceTestCases))]
107+
public void TraceExportResultIsSuccess(
108+
OtlpExportProtocol protocol,
109+
string endpoint,
110+
ExportProcessorType exportProcessorType,
111+
bool forceFlush,
112+
string scheme)
53113
{
54-
using EventWaitHandle handle = new ManualResetEvent(false);
114+
using var handle = new ManualResetEvent(false);
115+
116+
var exporterOptions = CreateExporterOptions(protocol, scheme, endpoint);
55117

56-
var exporterOptions = new OtlpExporterOptions
118+
exporterOptions.ExportProcessorType = exportProcessorType;
119+
exporterOptions.BatchExportProcessorOptions = new()
57120
{
58-
Endpoint = new Uri($"{scheme}://{CollectorHostname}{endpoint}"),
59-
Protocol = protocol,
60-
ExportProcessorType = exportProcessorType,
61-
BatchExportProcessorOptions = new()
62-
{
63-
ScheduledDelayMilliseconds = ExportIntervalMilliseconds,
64-
},
121+
ScheduledDelayMilliseconds = ExportIntervalMilliseconds,
65122
};
66123

67124
DelegatingExporter<Activity>? delegatingExporter = null;
@@ -121,29 +178,19 @@ public void TraceExportResultIsSuccess(OtlpExportProtocol protocol, string endpo
121178
}
122179
}
123180

124-
#pragma warning disable CS0618 // Suppressing gRPC obsolete warning
125-
[InlineData(OtlpExportProtocol.Grpc, ":4317", false, false)]
126-
[InlineData(OtlpExportProtocol.HttpProtobuf, ":4318/v1/metrics", false, false)]
127-
[InlineData(OtlpExportProtocol.Grpc, ":4317", false, true)]
128-
[InlineData(OtlpExportProtocol.HttpProtobuf, ":4318/v1/metrics", false, true)]
129-
[InlineData(OtlpExportProtocol.Grpc, ":4317", true, false)]
130-
[InlineData(OtlpExportProtocol.HttpProtobuf, ":4318/v1/metrics", true, false)]
131-
[InlineData(OtlpExportProtocol.Grpc, ":4317", true, true)]
132-
[InlineData(OtlpExportProtocol.HttpProtobuf, ":4318/v1/metrics", true, true)]
133-
[InlineData(OtlpExportProtocol.Grpc, ":5317", true, true, "https")]
134-
[InlineData(OtlpExportProtocol.HttpProtobuf, ":5318/v1/metrics", true, true, "https")]
135-
#pragma warning restore CS0618 // Suppressing gRPC obsolete warning
136181
[Trait("CategoryName", "CollectorIntegrationTests")]
137182
[SkipUnlessEnvVarFoundTheory(CollectorHostnameEnvVarName)]
138-
public void MetricExportResultIsSuccess(OtlpExportProtocol protocol, string endpoint, bool useManualExport, bool forceFlush, string scheme = "http")
183+
[MemberData(nameof(MetricsTestCases))]
184+
public void MetricExportResultIsSuccess(
185+
OtlpExportProtocol protocol,
186+
string endpoint,
187+
bool useManualExport,
188+
bool forceFlush,
189+
string scheme)
139190
{
140-
using EventWaitHandle handle = new ManualResetEvent(false);
191+
using var handle = new ManualResetEvent(false);
141192

142-
var exporterOptions = new OtlpExporterOptions
143-
{
144-
Endpoint = new Uri($"{scheme}://{CollectorHostname}{endpoint}"),
145-
Protocol = protocol,
146-
};
193+
var exporterOptions = CreateExporterOptions(protocol, scheme, endpoint);
147194

148195
DelegatingExporter<Metric>? delegatingExporter = null;
149196
var exportResults = new List<ExportResult>();
@@ -207,25 +254,18 @@ public void MetricExportResultIsSuccess(OtlpExportProtocol protocol, string endp
207254
}
208255
}
209256

210-
#pragma warning disable CS0618 // Suppressing gRPC obsolete warning
211-
[InlineData(OtlpExportProtocol.Grpc, ":4317", ExportProcessorType.Batch)]
212-
[InlineData(OtlpExportProtocol.HttpProtobuf, ":4318/v1/logs", ExportProcessorType.Batch)]
213-
[InlineData(OtlpExportProtocol.Grpc, ":4317", ExportProcessorType.Simple)]
214-
[InlineData(OtlpExportProtocol.HttpProtobuf, ":4318/v1/logs", ExportProcessorType.Simple)]
215-
[InlineData(OtlpExportProtocol.Grpc, ":5317", ExportProcessorType.Simple, "https")]
216-
[InlineData(OtlpExportProtocol.HttpProtobuf, ":5318/v1/logs", ExportProcessorType.Simple, "https")]
217-
#pragma warning restore CS0618 // Suppressing gRPC obsolete warning
218257
[Trait("CategoryName", "CollectorIntegrationTests")]
219258
[SkipUnlessEnvVarFoundTheory(CollectorHostnameEnvVarName)]
220-
public void LogExportResultIsSuccess(OtlpExportProtocol protocol, string endpoint, ExportProcessorType exportProcessorType, string scheme = "http")
259+
[MemberData(nameof(LogsTestCases))]
260+
public void LogExportResultIsSuccess(
261+
OtlpExportProtocol protocol,
262+
string endpoint,
263+
ExportProcessorType exportProcessorType,
264+
string scheme)
221265
{
222-
using EventWaitHandle handle = new ManualResetEvent(false);
266+
using var handle = new ManualResetEvent(false);
223267

224-
var exporterOptions = new OtlpExporterOptions
225-
{
226-
Endpoint = new Uri($"{scheme}://{CollectorHostname}{endpoint}"),
227-
Protocol = protocol,
228-
};
268+
var exporterOptions = CreateExporterOptions(protocol, scheme, endpoint);
229269

230270
DelegatingExporter<LogRecord> delegatingExporter;
231271
var exportResults = new List<ExportResult>();
@@ -275,24 +315,26 @@ public void LogExportResultIsSuccess(OtlpExportProtocol protocol, string endpoin
275315
Assert.Single(exportResults);
276316
Assert.Equal(ExportResult.Success, exportResults[0]);
277317
break;
318+
278319
case ExportProcessorType.Simple:
279320
Assert.Single(exportResults);
280321
Assert.Equal(ExportResult.Success, exportResults[0]);
281322
break;
323+
282324
default:
283325
throw new NotSupportedException("Unexpected processor type encountered.");
284326
}
285327
}
286328

287-
private sealed class OpenTelemetryEventListener : EventListener
288-
{
289-
private readonly ITestOutputHelper outputHelper;
290-
291-
public OpenTelemetryEventListener(ITestOutputHelper outputHelper)
329+
private static OtlpExporterOptions CreateExporterOptions(OtlpExportProtocol protocol, string scheme, string endpoint) =>
330+
new()
292331
{
293-
this.outputHelper = outputHelper;
294-
}
332+
Endpoint = new($"{scheme}://{CollectorHostname}{endpoint}"),
333+
Protocol = protocol,
334+
};
295335

336+
private sealed class OpenTelemetryEventListener(ITestOutputHelper outputHelper) : EventListener
337+
{
296338
protected override void OnEventSourceCreated(EventSource eventSource)
297339
{
298340
base.OnEventSourceCreated(eventSource);
@@ -305,17 +347,13 @@ protected override void OnEventSourceCreated(EventSource eventSource)
305347

306348
protected override void OnEventWritten(EventWrittenEventArgs eventData)
307349
{
308-
string? message;
309-
if (eventData.Message != null && eventData.Payload != null && eventData.Payload.Count > 0)
310-
{
311-
message = string.Format(CultureInfo.InvariantCulture, eventData.Message, eventData.Payload.ToArray());
312-
}
313-
else
314-
{
315-
message = eventData.Message;
316-
}
350+
var message = eventData.Message != null && eventData.Payload?.Count > 0
351+
? string.Format(CultureInfo.InvariantCulture, eventData.Message, [.. eventData.Payload])
352+
: eventData.Message;
353+
354+
message = string.Format(CultureInfo.InvariantCulture, "[{0}] {1}", eventData.Level, message);
317355

318-
this.outputHelper.WriteLine(message);
356+
outputHelper.WriteLine(message);
319357
}
320358
}
321359
}

0 commit comments

Comments
 (0)