-
Notifications
You must be signed in to change notification settings - Fork 782
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for OTEL_BSP_EXPORT_* environmental variables (#2219)
- Loading branch information
Showing
13 changed files
with
269 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,2 @@ | ||
OpenTelemetry.Trace.BatchExportActivityProcessorOptions | ||
OpenTelemetry.Trace.BatchExportActivityProcessorOptions.BatchExportActivityProcessorOptions() -> void |
This file contains 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,2 @@ | ||
OpenTelemetry.Trace.BatchExportActivityProcessorOptions | ||
OpenTelemetry.Trace.BatchExportActivityProcessorOptions.BatchExportActivityProcessorOptions() -> void |
This file contains 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
This file contains 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
This file contains 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
91 changes: 91 additions & 0 deletions
91
src/OpenTelemetry/Trace/BatchExportActivityProcessorOptions.cs
This file contains 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,91 @@ | ||
// <copyright file="BatchExportActivityProcessorOptions.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> | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Security; | ||
using OpenTelemetry.Internal; | ||
|
||
namespace OpenTelemetry.Trace | ||
{ | ||
public class BatchExportActivityProcessorOptions : BatchExportProcessorOptions<Activity> | ||
{ | ||
internal const string MaxQueueSizeEnvVarKey = "OTEL_BSP_MAX_QUEUE_SIZE"; | ||
|
||
internal const string MaxExportBatchSizeEnvVarKey = "OTEL_BSP_MAX_EXPORT_BATCH_SIZE"; | ||
|
||
internal const string ExporterTimeoutEnvVarKey = "OTEL_BSP_EXPORT_TIMEOUT"; | ||
|
||
internal const string ScheduledDelayEnvVarKey = "OTEL_BSP_SCHEDULE_DELAY"; | ||
|
||
public BatchExportActivityProcessorOptions() | ||
{ | ||
int value; | ||
|
||
if (TryLoadEnvVarInt(ExporterTimeoutEnvVarKey, out value)) | ||
{ | ||
this.ExporterTimeoutMilliseconds = value; | ||
} | ||
|
||
if (TryLoadEnvVarInt(MaxExportBatchSizeEnvVarKey, out value)) | ||
{ | ||
this.MaxExportBatchSize = value; | ||
} | ||
|
||
if (TryLoadEnvVarInt(MaxQueueSizeEnvVarKey, out value)) | ||
{ | ||
this.MaxQueueSize = value; | ||
} | ||
|
||
if (TryLoadEnvVarInt(ScheduledDelayEnvVarKey, out value)) | ||
{ | ||
this.ScheduledDelayMilliseconds = value; | ||
} | ||
} | ||
|
||
private static bool TryLoadEnvVarInt(string envVarKey, out int result) | ||
{ | ||
result = 0; | ||
|
||
string value; | ||
try | ||
{ | ||
value = Environment.GetEnvironmentVariable(envVarKey); | ||
} | ||
catch (SecurityException ex) | ||
{ | ||
// The caller does not have the required permission to | ||
// retrieve the value of an environment variable from the current process. | ||
OpenTelemetrySdkEventSource.Log.MissingPermissionsToReadEnvironmentVariable(ex); | ||
return false; | ||
} | ||
|
||
if (string.IsNullOrEmpty(value)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (!int.TryParse(value, out var parsedValue)) | ||
{ | ||
OpenTelemetrySdkEventSource.Log.FailedToParseEnvironmentVariable(envVarKey, value); | ||
return false; | ||
} | ||
|
||
result = parsedValue; | ||
return true; | ||
} | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
test/OpenTelemetry.Tests/Trace/BatchExportActivityProcessorOptionsTest.cs
This file contains 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,101 @@ | ||
// <copyright file="BatchExportActivityProcessorOptionsTest.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> | ||
|
||
using System; | ||
using Xunit; | ||
|
||
namespace OpenTelemetry.Trace.Tests | ||
{ | ||
public class BatchExportActivityProcessorOptionsTest : IDisposable | ||
{ | ||
public BatchExportActivityProcessorOptionsTest() | ||
{ | ||
this.ClearEnvVars(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
this.ClearEnvVars(); | ||
} | ||
|
||
[Fact] | ||
public void BatchExportProcessorOptions_Defaults() | ||
{ | ||
var options = new BatchExportActivityProcessorOptions(); | ||
|
||
Assert.Equal(30000, options.ExporterTimeoutMilliseconds); | ||
Assert.Equal(512, options.MaxExportBatchSize); | ||
Assert.Equal(2048, options.MaxQueueSize); | ||
Assert.Equal(5000, options.ScheduledDelayMilliseconds); | ||
} | ||
|
||
[Fact] | ||
public void BatchExportProcessorOptions_EnvironmentVariableOverride() | ||
{ | ||
Environment.SetEnvironmentVariable(BatchExportActivityProcessorOptions.ExporterTimeoutEnvVarKey, "1"); | ||
Environment.SetEnvironmentVariable(BatchExportActivityProcessorOptions.MaxExportBatchSizeEnvVarKey, "2"); | ||
Environment.SetEnvironmentVariable(BatchExportActivityProcessorOptions.MaxQueueSizeEnvVarKey, "3"); | ||
Environment.SetEnvironmentVariable(BatchExportActivityProcessorOptions.ScheduledDelayEnvVarKey, "4"); | ||
|
||
var options = new BatchExportActivityProcessorOptions(); | ||
|
||
Assert.Equal(1, options.ExporterTimeoutMilliseconds); | ||
Assert.Equal(2, options.MaxExportBatchSize); | ||
Assert.Equal(3, options.MaxQueueSize); | ||
Assert.Equal(4, options.ScheduledDelayMilliseconds); | ||
} | ||
|
||
[Fact] | ||
public void BatchExportProcessorOptions_InvalidPortEnvironmentVariableOverride() | ||
{ | ||
Environment.SetEnvironmentVariable(BatchExportActivityProcessorOptions.ExporterTimeoutEnvVarKey, "invalid"); | ||
|
||
var options = new BatchExportActivityProcessorOptions(); | ||
|
||
Assert.Equal(30000, options.ExporterTimeoutMilliseconds); // use default | ||
} | ||
|
||
[Fact] | ||
public void BatchExportProcessorOptions_SetterOverridesEnvironmentVariable() | ||
{ | ||
Environment.SetEnvironmentVariable(BatchExportActivityProcessorOptions.ExporterTimeoutEnvVarKey, "123"); | ||
|
||
var options = new BatchExportActivityProcessorOptions | ||
{ | ||
ExporterTimeoutMilliseconds = 89000, | ||
}; | ||
|
||
Assert.Equal(89000, options.ExporterTimeoutMilliseconds); | ||
} | ||
|
||
[Fact] | ||
public void BatchExportProcessorOptions_EnvironmentVariableNames() | ||
{ | ||
Assert.Equal("OTEL_BSP_EXPORT_TIMEOUT", BatchExportActivityProcessorOptions.ExporterTimeoutEnvVarKey); | ||
Assert.Equal("OTEL_BSP_MAX_EXPORT_BATCH_SIZE", BatchExportActivityProcessorOptions.MaxExportBatchSizeEnvVarKey); | ||
Assert.Equal("OTEL_BSP_MAX_QUEUE_SIZE", BatchExportActivityProcessorOptions.MaxQueueSizeEnvVarKey); | ||
Assert.Equal("OTEL_BSP_SCHEDULE_DELAY", BatchExportActivityProcessorOptions.ScheduledDelayEnvVarKey); | ||
} | ||
|
||
private void ClearEnvVars() | ||
{ | ||
Environment.SetEnvironmentVariable(BatchExportActivityProcessorOptions.ExporterTimeoutEnvVarKey, null); | ||
Environment.SetEnvironmentVariable(BatchExportActivityProcessorOptions.MaxExportBatchSizeEnvVarKey, null); | ||
Environment.SetEnvironmentVariable(BatchExportActivityProcessorOptions.MaxQueueSizeEnvVarKey, null); | ||
Environment.SetEnvironmentVariable(BatchExportActivityProcessorOptions.ScheduledDelayEnvVarKey, null); | ||
} | ||
} | ||
} |