-
Notifications
You must be signed in to change notification settings - Fork 862
[sdk-logs] Add BatchExportLogRecordProcessorOptions to SDK #4446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
CodeBlanch
merged 4 commits into
open-telemetry:main
from
CodeBlanch:sdk-logs-batchoptions
Apr 26, 2023
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/OpenTelemetry/Logs/BatchExportLogRecordProcessorOptions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // <copyright file="BatchExportLogRecordProcessorOptions.cs" company="OpenTelemetry Authors"> | ||
| // 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. | ||
| // </copyright> | ||
|
|
||
| #nullable enable | ||
|
|
||
| using Microsoft.Extensions.Configuration; | ||
| using OpenTelemetry.Internal; | ||
|
|
||
| namespace OpenTelemetry.Logs; | ||
|
|
||
| /// <summary> | ||
| /// 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. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Notes: | ||
| /// <list type="bullet"> | ||
| /// <item>The constructor throws <see cref="FormatException"/> if it fails | ||
| /// to parse any of the supported environment variables.</item> | ||
| /// <item>The environment variable keys are currently experimental and | ||
| /// subject to change. See: <see | ||
| /// href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md#batch-logrecord-processor">OpenTelemetry | ||
| /// Environment Variable Specification - Batch LogRecord Processor</see>. | ||
| /// </item> | ||
| /// </list> | ||
| /// </remarks> | ||
| internal class BatchExportLogRecordProcessorOptions : BatchExportProcessorOptions<LogRecord> | ||
| { | ||
| 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"; | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="BatchExportLogRecordProcessorOptions"/> class. | ||
| /// </summary> | ||
| 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; | ||
| } | ||
| } | ||
| } | ||
124 changes: 124 additions & 0 deletions
124
test/OpenTelemetry.Tests/Logs/BatchExportLogRecordProcessorOptionsTest.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // <copyright file="BatchExportLogRecordProcessorOptionsTest.cs" company="OpenTelemetry Authors"> | ||
| // 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. | ||
| // </copyright> | ||
|
|
||
| #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<string, string>() | ||
| { | ||
| [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_InvalidPortEnvironmentVariableOverride() | ||
| { | ||
| Environment.SetEnvironmentVariable(BatchExportLogRecordProcessorOptions.ExporterTimeoutEnvVarKey, "invalid"); | ||
|
|
||
| Assert.Throws<FormatException>(() => new BatchExportLogRecordProcessorOptions()); | ||
| } | ||
|
|
||
| [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); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.