From 570a6fa68763688b005eda08793e0eed37b1c785 Mon Sep 17 00:00:00 2001 From: Martin Costello Date: Wed, 30 Jul 2025 14:40:11 +0100 Subject: [PATCH] [OTLP] Dispose exporter if setup fails (#6398) Co-authored-by: Rajkumar Rangaraj --- .../OtlpLogExporterHelperExtensions.cs | 40 +++++++++++-------- .../OtlpTraceExporterHelperExtensions.cs | 34 ++++++++++------ 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpLogExporterHelperExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpLogExporterHelperExtensions.cs index c56d8e6f0..851a77292 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpLogExporterHelperExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpLogExporterHelperExtensions.cs @@ -327,25 +327,33 @@ public static class OtlpLogExporterHelperExtensions experimentalOptions!); #pragma warning restore CA2000 // Dispose objects before losing scope - if (configureExporterInstance != null) + try { - otlpExporter = configureExporterInstance(otlpExporter); - } + if (configureExporterInstance != null) + { + otlpExporter = configureExporterInstance(otlpExporter); + } - if (processorOptions!.ExportProcessorType == ExportProcessorType.Simple) - { - return new SimpleLogRecordExportProcessor(otlpExporter); - } - else - { - var batchOptions = processorOptions.BatchExportProcessorOptions; + if (processorOptions!.ExportProcessorType == ExportProcessorType.Simple) + { + return new SimpleLogRecordExportProcessor(otlpExporter); + } + else + { + var batchOptions = processorOptions.BatchExportProcessorOptions; - return new BatchLogRecordExportProcessor( - otlpExporter, - batchOptions.MaxQueueSize, - batchOptions.ScheduledDelayMilliseconds, - batchOptions.ExporterTimeoutMilliseconds, - batchOptions.MaxExportBatchSize); + return new BatchLogRecordExportProcessor( + otlpExporter, + batchOptions.MaxQueueSize, + batchOptions.ScheduledDelayMilliseconds, + batchOptions.ExporterTimeoutMilliseconds, + batchOptions.MaxExportBatchSize); + } + } + catch + { + otlpExporter.Dispose(); + throw; } } diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpTraceExporterHelperExtensions.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpTraceExporterHelperExtensions.cs index c91bbafaf..831a1380b 100644 --- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpTraceExporterHelperExtensions.cs +++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/OtlpTraceExporterHelperExtensions.cs @@ -151,23 +151,31 @@ public static class OtlpTraceExporterHelperExtensions BaseExporter otlpExporter = new OtlpTraceExporter(exporterOptions!, sdkLimitOptions!, experimentalOptions!); #pragma warning restore CA2000 // Dispose objects before losing scope - if (configureExporterInstance != null) + try { - otlpExporter = configureExporterInstance(otlpExporter); - } + if (configureExporterInstance != null) + { + otlpExporter = configureExporterInstance(otlpExporter); + } - if (exportProcessorType == ExportProcessorType.Simple) - { - return new SimpleActivityExportProcessor(otlpExporter); + if (exportProcessorType == ExportProcessorType.Simple) + { + return new SimpleActivityExportProcessor(otlpExporter); + } + else + { + return new BatchActivityExportProcessor( + otlpExporter, + batchExportProcessorOptions!.MaxQueueSize, + batchExportProcessorOptions.ScheduledDelayMilliseconds, + batchExportProcessorOptions.ExporterTimeoutMilliseconds, + batchExportProcessorOptions.MaxExportBatchSize); + } } - else + catch { - return new BatchActivityExportProcessor( - otlpExporter, - batchExportProcessorOptions!.MaxQueueSize, - batchExportProcessorOptions.ScheduledDelayMilliseconds, - batchExportProcessorOptions.ExporterTimeoutMilliseconds, - batchExportProcessorOptions.MaxExportBatchSize); + otlpExporter.Dispose(); + throw; } } }