Throw an exception when parsing an OTEL_BSP_* env var fails (#2395)

This commit is contained in:
Robert Pająk 2021-10-12 18:59:19 +02:00 committed by GitHub
parent cfa01ba1b3
commit ed814d1d6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 12 deletions

View File

@ -21,6 +21,11 @@ using OpenTelemetry.Internal;
namespace OpenTelemetry.Trace
{
/// <summary>
/// Batch span processor options.
/// OTEL_BSP_MAX_QUEUE_SIZE, OTEL_BSP_MAX_EXPORT_BATCH_SIZE, OTEL_BSP_EXPORT_TIMEOUT, OTEL_BSP_SCHEDULE_DELAY
/// environment variables are parsed during object construction.
/// </summary>
public class BatchExportActivityProcessorOptions : BatchExportProcessorOptions<Activity>
{
internal const string MaxQueueSizeEnvVarKey = "OTEL_BSP_MAX_QUEUE_SIZE";
@ -35,28 +40,28 @@ namespace OpenTelemetry.Trace
{
int value;
if (TryLoadEnvVarInt(ExporterTimeoutEnvVarKey, out value))
if (LoadEnvVarInt(ExporterTimeoutEnvVarKey, out value))
{
this.ExporterTimeoutMilliseconds = value;
}
if (TryLoadEnvVarInt(MaxExportBatchSizeEnvVarKey, out value))
if (LoadEnvVarInt(MaxExportBatchSizeEnvVarKey, out value))
{
this.MaxExportBatchSize = value;
}
if (TryLoadEnvVarInt(MaxQueueSizeEnvVarKey, out value))
if (LoadEnvVarInt(MaxQueueSizeEnvVarKey, out value))
{
this.MaxQueueSize = value;
}
if (TryLoadEnvVarInt(ScheduledDelayEnvVarKey, out value))
if (LoadEnvVarInt(ScheduledDelayEnvVarKey, out value))
{
this.ScheduledDelayMilliseconds = value;
}
}
private static bool TryLoadEnvVarInt(string envVarKey, out int result)
private static bool LoadEnvVarInt(string envVarKey, out int result)
{
result = 0;
@ -78,13 +83,11 @@ namespace OpenTelemetry.Trace
return false;
}
if (!int.TryParse(value, out var parsedValue))
if (!int.TryParse(value, out result))
{
OpenTelemetrySdkEventSource.Log.FailedToParseEnvironmentVariable(envVarKey, value);
return false;
throw new ArgumentException($"{envVarKey} environment variable has an invalid value: '${value}'");
}
result = parsedValue;
return true;
}
}

View File

@ -63,9 +63,7 @@ namespace OpenTelemetry.Trace.Tests
{
Environment.SetEnvironmentVariable(BatchExportActivityProcessorOptions.ExporterTimeoutEnvVarKey, "invalid");
var options = new BatchExportActivityProcessorOptions();
Assert.Equal(30000, options.ExporterTimeoutMilliseconds); // use default
Assert.Throws<ArgumentException>(() => new BatchExportActivityProcessorOptions());
}
[Fact]