[OTLP] Dispose exporter if setup fails (#6398)

Co-authored-by: Rajkumar Rangaraj <rajrang@microsoft.com>
This commit is contained in:
Martin Costello 2025-07-30 14:40:11 +01:00 committed by GitHub
parent 4dc60007ca
commit 570a6fa687
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 29 deletions

View File

@ -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;
}
}

View File

@ -151,23 +151,31 @@ public static class OtlpTraceExporterHelperExtensions
BaseExporter<Activity> 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;
}
}
}