Skip to content
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

fix: change Environment Var parsing to not throw FormatException (part1) #4095

Merged
merged 3 commits into from
Jan 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 9 additions & 3 deletions src/OpenTelemetry/Internal/ConfigurationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ 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);
value = default;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should value be touched here? (I guess no)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch! it's an out param so it needs to be set, but it's already been set by the Try method in the if statement.

return false;
}

return true;
Expand All @@ -85,7 +87,9 @@ 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);
value = default;
return false;
}

return true;
Expand All @@ -108,7 +112,9 @@ 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);
value = default;
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