diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTests.cs b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTests.cs index 91466b01299..b8206f03936 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTests.cs +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/IntegrationTests.cs @@ -15,6 +15,7 @@ // using System; +using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.Metrics; using System.Diagnostics.Tracing; @@ -79,7 +80,8 @@ public void TraceExportResultIsSuccess(OtlpExportProtocol protocol, string endpo }, }; - DelegatingTestExporter delegatingExporter = null; + DelegatingExporter delegatingExporter = null; + var exportResults = new List(); var activitySourceName = "otlp.collector.test"; @@ -93,7 +95,16 @@ public void TraceExportResultIsSuccess(OtlpExportProtocol protocol, string endpo serviceProvider: null, configureExporterInstance: otlpExporter => { - delegatingExporter = new DelegatingTestExporter(otlpExporter, onExportAction: () => handle.Set()); + delegatingExporter = new DelegatingExporter + { + OnExportFunc = (batch) => + { + var result = otlpExporter.Export(batch); + exportResults.Add(result); + handle.Set(); + return result; + }, + }; return delegatingExporter; }); @@ -108,21 +119,21 @@ public void TraceExportResultIsSuccess(OtlpExportProtocol protocol, string endpo if (forceFlush) { Assert.True(tracerProvider.ForceFlush()); - Assert.Single(delegatingExporter.ExportResults); - Assert.Equal(ExportResult.Success, delegatingExporter.ExportResults[0]); + Assert.Single(exportResults); + Assert.Equal(ExportResult.Success, exportResults[0]); } else if (exporterOptions.ExportProcessorType == ExportProcessorType.Batch) { Assert.True(handle.WaitOne(ExportIntervalMilliseconds * 2)); - Assert.Single(delegatingExporter.ExportResults); - Assert.Equal(ExportResult.Success, delegatingExporter.ExportResults[0]); + Assert.Single(exportResults); + Assert.Equal(ExportResult.Success, exportResults[0]); } } if (!forceFlush && exportProcessorType == ExportProcessorType.Simple) { - Assert.Single(delegatingExporter.ExportResults); - Assert.Equal(ExportResult.Success, delegatingExporter.ExportResults[0]); + Assert.Single(exportResults); + Assert.Equal(ExportResult.Success, exportResults[0]); } } @@ -155,7 +166,8 @@ public void MetricExportResultIsSuccess(OtlpExportProtocol protocol, string endp Protocol = protocol, }; - DelegatingTestExporter delegatingExporter = null; + DelegatingExporter delegatingExporter = null; + var exportResults = new List(); var meterName = "otlp.collector.test"; @@ -174,7 +186,16 @@ public void MetricExportResultIsSuccess(OtlpExportProtocol protocol, string endp serviceProvider: null, configureExporterInstance: otlpExporter => { - delegatingExporter = new DelegatingTestExporter(otlpExporter, onExportAction: () => handle.Set()); + delegatingExporter = new DelegatingExporter + { + OnExportFunc = (batch) => + { + var result = otlpExporter.Export(batch); + exportResults.Add(result); + handle.Set(); + return result; + }, + }; return delegatingExporter; }); @@ -191,21 +212,21 @@ public void MetricExportResultIsSuccess(OtlpExportProtocol protocol, string endp if (forceFlush) { Assert.True(meterProvider.ForceFlush()); - Assert.Single(delegatingExporter.ExportResults); - Assert.Equal(ExportResult.Success, delegatingExporter.ExportResults[0]); + Assert.Single(exportResults); + Assert.Equal(ExportResult.Success, exportResults[0]); } else if (!useManualExport) { Assert.True(handle.WaitOne(ExportIntervalMilliseconds * 2)); - Assert.Single(delegatingExporter.ExportResults); - Assert.Equal(ExportResult.Success, delegatingExporter.ExportResults[0]); + Assert.Single(exportResults); + Assert.Equal(ExportResult.Success, exportResults[0]); } } if (!forceFlush && useManualExport) { - Assert.Single(delegatingExporter.ExportResults); - Assert.Equal(ExportResult.Success, delegatingExporter.ExportResults[0]); + Assert.Single(exportResults); + Assert.Equal(ExportResult.Success, exportResults[0]); } } diff --git a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests.csproj b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests.csproj index 2ddffb5be2a..271f7649e84 100644 --- a/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests.csproj +++ b/test/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests/OpenTelemetry.Exporter.OpenTelemetryProtocol.Tests.csproj @@ -27,7 +27,7 @@ - + diff --git a/test/OpenTelemetry.Tests/Shared/DelegatingExporter.cs b/test/OpenTelemetry.Tests/Shared/DelegatingExporter.cs new file mode 100644 index 00000000000..13cef6924d9 --- /dev/null +++ b/test/OpenTelemetry.Tests/Shared/DelegatingExporter.cs @@ -0,0 +1,27 @@ +// +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +using System; + +namespace OpenTelemetry.Tests; + +internal sealed class DelegatingExporter : BaseExporter + where T : class +{ + public Func, ExportResult> OnExportFunc { get; set; } = (batch) => default; + + public override ExportResult Export(in Batch batch) => this.OnExportFunc(batch); +} diff --git a/test/OpenTelemetry.Tests/Shared/DelegatingTestExporter.cs b/test/OpenTelemetry.Tests/Shared/DelegatingTestExporter.cs deleted file mode 100644 index 10800369105..00000000000 --- a/test/OpenTelemetry.Tests/Shared/DelegatingTestExporter.cs +++ /dev/null @@ -1,46 +0,0 @@ -// -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -using System; -using System.Collections.Generic; - -namespace OpenTelemetry.Tests -{ - public class DelegatingTestExporter : BaseExporter - where T : class - { - private readonly BaseExporter exporter; - private readonly Action onExportAction; - - public DelegatingTestExporter( - BaseExporter exporter, - Action onExportAction = null) - { - this.exporter = exporter; - this.onExportAction = onExportAction; - } - - public List ExportResults { get; } = new(); - - public override ExportResult Export(in Batch batch) - { - var result = this.exporter.Export(batch); - this.ExportResults.Add(result); - this.onExportAction?.Invoke(); - return result; - } - } -}