Skip to content

Commit

Permalink
fix: change Environment Var parsing to not throw FormatException (par…
Browse files Browse the repository at this point in the history
…t1) (#4095)
  • Loading branch information
TimothyMothra authored Jan 21, 2023
1 parent 10389e3 commit 9e00b8f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.Zipkin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Changed EnvironmentVariable parsing to not throw a `FormatException` and
instead log a warning.
([#4095](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4095))

## 1.4.0-rc.2

Released 2023-Jan-09
Expand Down
9 changes: 6 additions & 3 deletions src/OpenTelemetry/Internal/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ public static bool TryGetUriValue(

if (!Uri.TryCreate(stringValue, UriKind.Absolute, out value))
{
throw new FormatException($"{key} environment variable has an invalid value: '{stringValue}'");
OpenTelemetrySdkEventSource.Log.InvalidEnvironmentVariable(key, stringValue);
return false;
}

return true;
Expand All @@ -85,7 +86,8 @@ public static bool TryGetIntValue(

if (!int.TryParse(stringValue, NumberStyles.None, CultureInfo.InvariantCulture, out value))
{
throw new FormatException($"{key} environment variable has an invalid value: '{stringValue}'");
OpenTelemetrySdkEventSource.Log.InvalidEnvironmentVariable(key, stringValue);
return false;
}

return true;
Expand All @@ -108,7 +110,8 @@ public static bool TryGetValue<T>(

if (!tryParseFunc(stringValue!, out value))
{
throw new FormatException($"{key} environment variable has an invalid value: '{stringValue}'");
OpenTelemetrySdkEventSource.Log.InvalidEnvironmentVariable(key, stringValue);
return false;
}

return true;
Expand Down
6 changes: 6 additions & 0 deletions src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,12 @@ public void TracerProviderSdkEvent(string message)
this.WriteEvent(46, message);
}

[Event(47, Message = "{0} environment variable has an invalid value: '{1}'", Level = EventLevel.Warning)]
public void InvalidEnvironmentVariable(string key, string value)
{
this.WriteEvent(47, key, value);
}

#if DEBUG
public class OpenTelemetryEventListener : EventListener
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void JaegerExporterOptions_EnvironmentVariableOverride()
Assert.Equal(new Uri("http://custom-endpoint:12345"), options.Endpoint);
}

[Theory]
[Theory(Skip = "https://github.com/open-telemetry/opentelemetry-dotnet/issues/3690")]
[InlineData(JaegerExporterOptions.OTelAgentPortEnvVarKey)]
[InlineData(JaegerExporterOptions.OTelProtocolEnvVarKey)]
public void JaegerExporterOptions_InvalidEnvironmentVariableOverride(string envVar)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,23 @@ public void OtlpExporterOptions_UsingIConfiguration()
Assert.Equal(OtlpExportProtocol.HttpProtobuf, options.Protocol);
}

[Fact]
[Fact(Skip = "https://github.com/open-telemetry/opentelemetry-dotnet/issues/3690")]
public void OtlpExporterOptions_InvalidEndpointVariableOverride()
{
Environment.SetEnvironmentVariable(OtlpExporterOptions.EndpointEnvVarName, "invalid");

Assert.Throws<FormatException>(() => new OtlpExporterOptions());
}

[Fact]
[Fact(Skip = "https://github.com/open-telemetry/opentelemetry-dotnet/issues/3690")]
public void OtlpExporterOptions_InvalidTimeoutVariableOverride()
{
Environment.SetEnvironmentVariable(OtlpExporterOptions.TimeoutEnvVarName, "invalid");

Assert.Throws<FormatException>(() => new OtlpExporterOptions());
}

[Fact]
[Fact(Skip = "https://github.com/open-telemetry/opentelemetry-dotnet/issues/3690")]
public void OtlpExporterOptions_InvalidProtocolVariableOverride()
{
Environment.SetEnvironmentVariable(OtlpExporterOptions.ProtocolEnvVarName, "invalid");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public void IncodeEndpointConfigTakesPrecedenceOverEnvironmentVariable()
}
}

[Fact]
[Fact(Skip = "https://github.com/open-telemetry/opentelemetry-dotnet/issues/3690")]
public void ErrorGettingUriFromEnvVarSetsDefaultEndpointValue()
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void BatchExportProcessorOptions_UsingIConfiguration()
Assert.Equal(4, options.ScheduledDelayMilliseconds);
}

[Fact]
[Fact(Skip = "https://github.com/open-telemetry/opentelemetry-dotnet/issues/3690")]
public void BatchExportProcessorOptions_InvalidPortEnvironmentVariableOverride()
{
Environment.SetEnvironmentVariable(BatchExportActivityProcessorOptions.ExporterTimeoutEnvVarKey, "invalid");
Expand Down

0 comments on commit 9e00b8f

Please sign in to comment.