From 4ebbccfe7b16e65470f56b22c25d7a60561730a5 Mon Sep 17 00:00:00 2001 From: Mikel Blanchard Date: Wed, 26 Apr 2023 15:51:57 -0700 Subject: [PATCH] [sdk-logs] Add BatchExportLogRecordProcessorOptions to SDK (#4446) --- ...viderBuilderServiceCollectionExtensions.cs | 3 +- .../BatchExportLogRecordProcessorOptions.cs | 82 +++++++++++++ ...atchExportLogRecordProcessorOptionsTest.cs | 116 ++++++++++++++++++ 3 files changed, 200 insertions(+), 1 deletion(-) create mode 100644 src/OpenTelemetry/Logs/BatchExportLogRecordProcessorOptions.cs create mode 100644 test/OpenTelemetry.Tests/Logs/BatchExportLogRecordProcessorOptionsTest.cs diff --git a/src/OpenTelemetry/Internal/Builder/ProviderBuilderServiceCollectionExtensions.cs b/src/OpenTelemetry/Internal/Builder/ProviderBuilderServiceCollectionExtensions.cs index d57911333..c8ddae0b9 100644 --- a/src/OpenTelemetry/Internal/Builder/ProviderBuilderServiceCollectionExtensions.cs +++ b/src/OpenTelemetry/Internal/Builder/ProviderBuilderServiceCollectionExtensions.cs @@ -21,6 +21,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection.Extensions; using OpenTelemetry; using OpenTelemetry.Internal; +using OpenTelemetry.Logs; using OpenTelemetry.Metrics; using OpenTelemetry.Trace; @@ -34,7 +35,7 @@ internal static class ProviderBuilderServiceCollectionExtensions // TODO: // services!.TryAddSingleton(); - // services!.RegisterOptionsFactory(configuration => new BatchExportLogRecordProcessorOptions(configuration)); + services!.RegisterOptionsFactory(configuration => new BatchExportLogRecordProcessorOptions(configuration)); return services!; } diff --git a/src/OpenTelemetry/Logs/BatchExportLogRecordProcessorOptions.cs b/src/OpenTelemetry/Logs/BatchExportLogRecordProcessorOptions.cs new file mode 100644 index 000000000..d17d4f65d --- /dev/null +++ b/src/OpenTelemetry/Logs/BatchExportLogRecordProcessorOptions.cs @@ -0,0 +1,82 @@ +// +// 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. +// + +#nullable enable + +using Microsoft.Extensions.Configuration; +using OpenTelemetry.Internal; + +namespace OpenTelemetry.Logs; + +/// +/// Batch log processor options. OTEL_BLRP_MAX_QUEUE_SIZE, +/// OTEL_BLRP_MAX_EXPORT_BATCH_SIZE, OTEL_BLRP_EXPORT_TIMEOUT, +/// OTEL_BLRP_SCHEDULE_DELAY environment variables are parsed during object +/// construction. +/// +/// +/// Notes: +/// +/// The constructor throws if it fails to +/// parse any of the supported environment variables. +/// The environment variable keys are currently experimental and subject +/// to change. See: OpenTelemetry +/// Environment Variable Specification - Batch LogRecord Processor. +/// +/// +/// +internal class BatchExportLogRecordProcessorOptions : BatchExportProcessorOptions +{ + internal const string MaxQueueSizeEnvVarKey = "OTEL_BLRP_MAX_QUEUE_SIZE"; + + internal const string MaxExportBatchSizeEnvVarKey = "OTEL_BLRP_MAX_EXPORT_BATCH_SIZE"; + + internal const string ExporterTimeoutEnvVarKey = "OTEL_BLRP_EXPORT_TIMEOUT"; + + internal const string ScheduledDelayEnvVarKey = "OTEL_BLRP_SCHEDULE_DELAY"; + + /// + /// Initializes a new instance of the class. + /// + public BatchExportLogRecordProcessorOptions() + : this(new ConfigurationBuilder().AddEnvironmentVariables().Build()) + { + } + + internal BatchExportLogRecordProcessorOptions(IConfiguration configuration) + { + if (configuration.TryGetIntValue(ExporterTimeoutEnvVarKey, out var value)) + { + this.ExporterTimeoutMilliseconds = value; + } + + if (configuration.TryGetIntValue(MaxExportBatchSizeEnvVarKey, out value)) + { + this.MaxExportBatchSize = value; + } + + if (configuration.TryGetIntValue(MaxQueueSizeEnvVarKey, out value)) + { + this.MaxQueueSize = value; + } + + if (configuration.TryGetIntValue(ScheduledDelayEnvVarKey, out value)) + { + this.ScheduledDelayMilliseconds = value; + } + } +} diff --git a/test/OpenTelemetry.Tests/Logs/BatchExportLogRecordProcessorOptionsTest.cs b/test/OpenTelemetry.Tests/Logs/BatchExportLogRecordProcessorOptionsTest.cs new file mode 100644 index 000000000..35e89e97b --- /dev/null +++ b/test/OpenTelemetry.Tests/Logs/BatchExportLogRecordProcessorOptionsTest.cs @@ -0,0 +1,116 @@ +// +// 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. +// + +#nullable enable + +using Microsoft.Extensions.Configuration; +using Xunit; + +namespace OpenTelemetry.Logs.Tests; + +public sealed class BatchExportLogRecordProcessorOptionsTest : IDisposable +{ + public BatchExportLogRecordProcessorOptionsTest() + { + ClearEnvVars(); + } + + public void Dispose() + { + ClearEnvVars(); + GC.SuppressFinalize(this); + } + + [Fact] + public void BatchExportLogRecordProcessorOptions_Defaults() + { + var options = new BatchExportLogRecordProcessorOptions(); + + Assert.Equal(30000, options.ExporterTimeoutMilliseconds); + Assert.Equal(512, options.MaxExportBatchSize); + Assert.Equal(2048, options.MaxQueueSize); + Assert.Equal(5000, options.ScheduledDelayMilliseconds); + } + + [Fact] + public void BatchExportLogRecordProcessorOptions_EnvironmentVariableOverride() + { + Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.ExporterTimeoutEnvVarKey, "1"); + Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.MaxExportBatchSizeEnvVarKey, "2"); + Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.MaxQueueSizeEnvVarKey, "3"); + Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.ScheduledDelayEnvVarKey, "4"); + + var options = new BatchExportLogRecordProcessorOptions(); + + Assert.Equal(1, options.ExporterTimeoutMilliseconds); + Assert.Equal(2, options.MaxExportBatchSize); + Assert.Equal(3, options.MaxQueueSize); + Assert.Equal(4, options.ScheduledDelayMilliseconds); + } + + [Fact] + public void ExportLogRecordProcessorOptions_UsingIConfiguration() + { + var values = new Dictionary() + { + [BatchExportLogRecordProcessorOptions.MaxQueueSizeEnvVarKey] = "1", + [BatchExportLogRecordProcessorOptions.MaxExportBatchSizeEnvVarKey] = "2", + [BatchExportLogRecordProcessorOptions.ExporterTimeoutEnvVarKey] = "3", + [BatchExportLogRecordProcessorOptions.ScheduledDelayEnvVarKey] = "4", + }; + + var configuration = new ConfigurationBuilder() + .AddInMemoryCollection(values) + .Build(); + + var options = new BatchExportLogRecordProcessorOptions(configuration); + + Assert.Equal(1, options.MaxQueueSize); + Assert.Equal(2, options.MaxExportBatchSize); + Assert.Equal(3, options.ExporterTimeoutMilliseconds); + Assert.Equal(4, options.ScheduledDelayMilliseconds); + } + + [Fact] + public void BatchExportLogRecordProcessorOptions_SetterOverridesEnvironmentVariable() + { + Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.ExporterTimeoutEnvVarKey, "123"); + + var options = new BatchExportLogRecordProcessorOptions + { + ExporterTimeoutMilliseconds = 89000, + }; + + Assert.Equal(89000, options.ExporterTimeoutMilliseconds); + } + + [Fact] + public void BatchExportLogRecordProcessorOptions_EnvironmentVariableNames() + { + Assert.Equal("OTEL_BLRP_EXPORT_TIMEOUT", BatchExportLogRecordProcessorOptions.ExporterTimeoutEnvVarKey); + Assert.Equal("OTEL_BLRP_MAX_EXPORT_BATCH_SIZE", BatchExportLogRecordProcessorOptions.MaxExportBatchSizeEnvVarKey); + Assert.Equal("OTEL_BLRP_MAX_QUEUE_SIZE", BatchExportLogRecordProcessorOptions.MaxQueueSizeEnvVarKey); + Assert.Equal("OTEL_BLRP_SCHEDULE_DELAY", BatchExportLogRecordProcessorOptions.ScheduledDelayEnvVarKey); + } + + private static void ClearEnvVars() + { + Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.ExporterTimeoutEnvVarKey, null); + Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.MaxExportBatchSizeEnvVarKey, null); + Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.MaxQueueSizeEnvVarKey, null); + Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.ScheduledDelayEnvVarKey, null); + } +}