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

Consolidate environment variable parsing #2500

Merged
merged 26 commits into from
Oct 26, 2021
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
210c110
Introduce EnvironmentVariableHelper
pellared Oct 20, 2021
0dbcd71
EnvironmentVariableHelperTests implements IDisposable
pellared Oct 20, 2021
6afa561
Reuse EnvironmentVariableHelper in resource detectors
pellared Oct 20, 2021
edbdf8e
Fix final newline
pellared Oct 20, 2021
728d26f
Interpret empty env var value as it was unset
pellared Oct 20, 2021
53f3c31
Throw FormatException instead of AgumentException
pellared Oct 20, 2021
d219e13
Reuse EnvironmentVariableHelper in Jaeger exporter
pellared Oct 20, 2021
c9a93ec
Fix Benchmarks build
pellared Oct 20, 2021
c78bb2a
Reuse EnvironmentVariableHelper in OTLP exporter
pellared Oct 20, 2021
d376162
Add EnvironmentVariableHelper.LoadUri
pellared Oct 20, 2021
afb1e7b
Reuse EnvironmentVariableHelper in Zipkin exporter
pellared Oct 20, 2021
9afdb73
Merge branch 'main' into refine-env-vars-parsing
pellared Oct 20, 2021
a60a5ac
Add doc comments to ExporterOptions
pellared Oct 20, 2021
2523936
Update exporter readmes
pellared Oct 20, 2021
81949ee
Update changelogs
pellared Oct 20, 2021
921a405
Add doc comments to EnvironmentVariableHelper
pellared Oct 20, 2021
86a3878
Fix grammar error
pellared Oct 20, 2021
c917863
Fix lint errors
pellared Oct 20, 2021
b6d34fe
Fix typos
pellared Oct 20, 2021
0fe88c9
LoadNumeric handles -1
pellared Oct 21, 2021
da979a3
Update EnvironmentVariableHelper.cs
pellared Oct 21, 2021
6a4f9af
Apply suggestions from code review
pellared Oct 21, 2021
28e583c
Update EnvironmentVariableHelper.cs
pellared Oct 21, 2021
22acae8
I am unable to read with understanding
pellared Oct 21, 2021
dd7d0a2
Merge branch 'main' into refine-env-vars-parsing
pellared Oct 26, 2021
6cc2ce9
Merge branch 'main' into refine-env-vars-parsing
cijothomas Oct 26, 2021
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.Jaeger/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,10 @@

## Unreleased

* Changed `JaegerExporterOptions` constructor to throw
`FormatException` if it fails to parse any of the supported environment
variables.

## 1.2.0-beta1

Released 2021-Oct-08
Original file line number Diff line number Diff line change
@@ -16,7 +16,6 @@

using System;
using System.Diagnostics.Tracing;
using System.Security;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Exporter.Jaeger.Implementation
@@ -38,31 +37,10 @@ public void FailedExport(Exception ex)
}
}

[NonEvent]
public void MissingPermissionsToReadEnvironmentVariable(SecurityException ex)
{
if (this.IsEnabled(EventLevel.Warning, EventKeywords.All))
{
this.MissingPermissionsToReadEnvironmentVariable(ex.ToInvariantString());
}
}

[Event(1, Message = "Failed to send spans: '{0}'", Level = EventLevel.Error)]
public void FailedExport(string exception)
{
this.WriteEvent(1, exception);
}

[Event(2, Message = "Failed to parse environment variable: '{0}', value: '{1}'.", Level = EventLevel.Warning)]
public void FailedToParseEnvironmentVariable(string name, string value)
{
this.WriteEvent(2, name, value);
}

[Event(3, Message = "Missing permissions to read environment variable: '{0}'", Level = EventLevel.Warning)]
public void MissingPermissionsToReadEnvironmentVariable(string exception)
{
this.WriteEvent(3, exception);
}
}
}
40 changes: 15 additions & 25 deletions src/OpenTelemetry.Exporter.Jaeger/JaegerExporterOptions.cs
Original file line number Diff line number Diff line change
@@ -16,12 +16,20 @@

using System;
using System.Diagnostics;
using System.Security;
using OpenTelemetry.Exporter.Jaeger.Implementation;
using OpenTelemetry.Internal;
using OpenTelemetry.Trace;

namespace OpenTelemetry.Exporter
{
/// <summary>
/// Jaeger exporter options.
/// OTEL_EXPORTER_JAEGER_AGENT_HOST, OTEL_EXPORTER_JAEGER_AGENT_PORT
/// environment variables are parsed during object construction.
/// </summary>
/// <remarks>
/// The constructor throws <see cref="FormatException"/> if it fails to parse
/// any of the supported environment variables.
/// </remarks>
public class JaegerExporterOptions
{
internal const int DefaultMaxPayloadSizeInBytes = 4096;
@@ -31,32 +39,14 @@ public class JaegerExporterOptions

public JaegerExporterOptions()
{
try
if (EnvironmentVariableHelper.LoadString(OTelAgentHostEnvVarKey, out string agentHostEnvVar))
{
string agentHostEnvVar = Environment.GetEnvironmentVariable(OTelAgentHostEnvVarKey);
if (!string.IsNullOrEmpty(agentHostEnvVar))
{
this.AgentHost = agentHostEnvVar;
}

string agentPortEnvVar = Environment.GetEnvironmentVariable(OTelAgentPortEnvVarKey);
if (!string.IsNullOrEmpty(agentPortEnvVar))
{
if (int.TryParse(agentPortEnvVar, out var agentPortValue))
{
this.AgentPort = agentPortValue;
}
else
{
JaegerExporterEventSource.Log.FailedToParseEnvironmentVariable(OTelAgentPortEnvVarKey, agentPortEnvVar);
}
}
this.AgentHost = agentHostEnvVar;
}
catch (SecurityException ex)

if (EnvironmentVariableHelper.LoadNumeric(OTelAgentPortEnvVarKey, out int agentPortEnvVar))
{
// The caller does not have the required permission to
// retrieve the value of an environment variable from the current process.
JaegerExporterEventSource.Log.MissingPermissionsToReadEnvironmentVariable(ex);
this.AgentPort = agentPortEnvVar;
}
}

Original file line number Diff line number Diff line change
@@ -26,6 +26,8 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\IActivityEnumerator.cs" Link="Includes\IActivityEnumerator.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\EnumerationHelper.cs" Link="Includes\EnumerationHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\EnvironmentVariableHelper.cs" Link="Includes\EnvironmentVariableHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\OpenTelemetrySdkEventSource.cs" Link="Includes\OpenTelemetrySdkEventSource.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PooledList.cs" Link="Includes\PooledList.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeerServiceResolver.cs" Link="Includes\PeerServiceResolver.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ResourceSemanticConventions.cs" Link="Includes\ResourceSemanticConventions.cs" />
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Exporter.Jaeger/README.md
Original file line number Diff line number Diff line change
@@ -62,6 +62,9 @@ values of the `JaegerExporterOptions`.
| `OTEL_EXPORTER_JAEGER_AGENT_HOST` | `AgentHost` |
| `OTEL_EXPORTER_JAEGER_AGENT_PORT` | `AgentPort` |

`FormatException` is thrown in case of an invalid value for any of the
supported environment variables.

## References

* [Jaeger](https://www.jaegertracing.io)
Original file line number Diff line number Diff line change
@@ -2,6 +2,10 @@

## Unreleased

* Changed `OtlpExporterOptions` constructor to throw
`FormatException` if it fails to parse any of the supported environment
variables.

## 1.2.0-beta1

Released 2021-Oct-08
Original file line number Diff line number Diff line change
@@ -16,7 +16,6 @@

using System;
using System.Diagnostics.Tracing;
using System.Security;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation
@@ -26,24 +25,6 @@ internal class OpenTelemetryProtocolExporterEventSource : EventSource
{
public static readonly OpenTelemetryProtocolExporterEventSource Log = new OpenTelemetryProtocolExporterEventSource();

[NonEvent]
public void MissingPermissionsToReadEnvironmentVariable(SecurityException ex)
{
if (this.IsEnabled(EventLevel.Warning, EventKeywords.All))
{
this.MissingPermissionsToReadEnvironmentVariable(ex.ToInvariantString());
}
}

[NonEvent]
public void FailedToConvertToProtoDefinitionError(Exception ex)
{
if (Log.IsEnabled(EventLevel.Error, EventKeywords.All))
{
this.FailedToConvertToProtoDefinitionError(ex.ToInvariantString());
}
}

[NonEvent]
public void FailedToReachCollector(Exception ex)
{
@@ -62,12 +43,6 @@ public void ExportMethodException(Exception ex)
}
}

[Event(1, Message = "Exporter failed to convert SpanData content into gRPC proto definition. Data will not be sent. Exception: {0}", Level = EventLevel.Error)]
public void FailedToConvertToProtoDefinitionError(string ex)
{
this.WriteEvent(1, ex);
}

[Event(2, Message = "Exporter failed send data to collector. Data will not be sent. Exception: {0}", Level = EventLevel.Error)]
public void FailedToReachCollector(string ex)
{
@@ -92,18 +67,6 @@ public void CouldNotTranslateMetric(string className, string methodName)
this.WriteEvent(5, className, methodName);
}

[Event(6, Message = "Failed to parse environment variable: '{0}', value: '{1}'.", Level = EventLevel.Warning)]
public void FailedToParseEnvironmentVariable(string name, string value)
{
this.WriteEvent(6, name, value);
}

[Event(7, Message = "Missing permissions to read environment variable: '{0}'", Level = EventLevel.Warning)]
public void MissingPermissionsToReadEnvironmentVariable(string exception)
{
this.WriteEvent(7, exception);
}

[Event(8, Message = "Unsupported value for protocol '{0}' is configured, default protocol 'grpc' will be used.", Level = EventLevel.Warning)]
public void UnsupportedProtocol(string protocol)
{
Original file line number Diff line number Diff line change
@@ -43,6 +43,8 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\IActivityEnumerator.cs" Link="Includes\IActivityEnumerator.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\EnumerationHelper.cs" Link="Includes\EnumerationHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\EnvironmentVariableHelper.cs" Link="Includes\EnvironmentVariableHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\OpenTelemetrySdkEventSource.cs" Link="Includes\OpenTelemetrySdkEventSource.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PooledList.cs" Link="Includes\PooledList.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeerServiceResolver.cs" Link="Includes\PeerServiceResolver.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ResourceSemanticConventions.cs" Link="Includes\ResourceSemanticConventions.cs" />
Original file line number Diff line number Diff line change
@@ -16,16 +16,21 @@

using System;
using System.Diagnostics;
using System.Security;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Internal;
using OpenTelemetry.Metrics;
using OpenTelemetry.Trace;

namespace OpenTelemetry.Exporter
{
/// <summary>
/// Configuration options for the OpenTelemetry Protocol (OTLP) exporter.
/// OpenTelemetry Protocol (OTLP) exporter options.
/// OTEL_EXPORTER_OTLP_ENDPOINT, OTEL_EXPORTER_OTLP_HEADERS, OTEL_EXPORTER_OTLP_TIMEOUT, OTEL_EXPORTER_OTLP_PROTOCOL
/// environment variables are parsed during object construction.
/// </summary>
/// <remarks>
/// The constructor throws <see cref="FormatException"/> if it fails to parse
/// any of the supported environment variables.
/// </remarks>
public class OtlpExporterOptions
{
internal const string EndpointEnvVarName = "OTEL_EXPORTER_OTLP_ENDPOINT";
@@ -41,60 +46,33 @@ public class OtlpExporterOptions
/// </summary>
public OtlpExporterOptions()
{
try
if (EnvironmentVariableHelper.LoadUri(EndpointEnvVarName, out Uri endpoint))
{
string endpointEnvVar = Environment.GetEnvironmentVariable(EndpointEnvVarName);
if (!string.IsNullOrEmpty(endpointEnvVar))
{
if (Uri.TryCreate(endpointEnvVar, UriKind.Absolute, out var endpoint))
{
this.Endpoint = endpoint;
}
else
{
OpenTelemetryProtocolExporterEventSource.Log.FailedToParseEnvironmentVariable(EndpointEnvVarName, endpointEnvVar);
}
}
this.Endpoint = endpoint;
}

string headersEnvVar = Environment.GetEnvironmentVariable(HeadersEnvVarName);
if (!string.IsNullOrEmpty(headersEnvVar))
{
this.Headers = headersEnvVar;
}
if (EnvironmentVariableHelper.LoadString(HeadersEnvVarName, out string headersEnvVar))
{
this.Headers = headersEnvVar;
}

string timeoutEnvVar = Environment.GetEnvironmentVariable(TimeoutEnvVarName);
if (!string.IsNullOrEmpty(timeoutEnvVar))
if (EnvironmentVariableHelper.LoadNumeric(TimeoutEnvVarName, out int timeout))
{
this.TimeoutMilliseconds = timeout;
}

if (EnvironmentVariableHelper.LoadString(ProtocolEnvVarName, out string protocolEnvVar))
{
var protocol = protocolEnvVar.ToOtlpExportProtocol();
if (protocol.HasValue)
{
if (int.TryParse(timeoutEnvVar, out var timeout))
{
this.TimeoutMilliseconds = timeout;
}
else
{
OpenTelemetryProtocolExporterEventSource.Log.FailedToParseEnvironmentVariable(TimeoutEnvVarName, timeoutEnvVar);
}
this.Protocol = protocol.Value;
}

string protocolEnvVar = Environment.GetEnvironmentVariable(ProtocolEnvVarName);
if (!string.IsNullOrEmpty(protocolEnvVar))
else
{
var protocol = protocolEnvVar.ToOtlpExportProtocol();
if (protocol.HasValue)
{
this.Protocol = protocol.Value;
}
else
{
OpenTelemetryProtocolExporterEventSource.Log.UnsupportedProtocol(protocolEnvVar);
}
throw new FormatException($"{ProtocolEnvVarName} environment variable has an invalid value: '${protocolEnvVar}'");
}
}
catch (SecurityException ex)
{
// The caller does not have the required permission to
// retrieve the value of an environment variable from the current process.
OpenTelemetryProtocolExporterEventSource.Log.MissingPermissionsToReadEnvironmentVariable(ex);
}
}

/// <summary>
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/README.md
Original file line number Diff line number Diff line change
@@ -54,6 +54,9 @@ values of the `OtlpExporterOptions`
| `OTEL_EXPORTER_OTLP_TIMEOUT` | `TimeoutMilliseconds` |
| `OTEL_EXPORTER_OTLP_PROTOCOL` | `Protocol` (grpc or http/protobuf)|

`FormatException` is thrown in case of an invalid value for any of the
supported environment variables.

## OTLP Logs

This package currently only supports exporting traces and metrics. Once the
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.Zipkin/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,10 @@

## Unreleased

* Changed `ZipkinExporterOptions` constructor to throw
`FormatException` if it fails to parse any of the supported environment
variables.

## 1.2.0-beta1

Released 2021-Oct-08
Original file line number Diff line number Diff line change
@@ -37,25 +37,10 @@ public void FailedExport(Exception ex)
}
}

[NonEvent]
public void FailedEndpointInitialization(Exception ex)
{
if (this.IsEnabled(EventLevel.Error, EventKeywords.All))
{
this.FailedEndpointInitialization(ex.ToInvariantString());
}
}

[Event(1, Message = "Failed to export activities: '{0}'", Level = EventLevel.Error)]
public void FailedExport(string exception)
{
this.WriteEvent(1, exception);
}

[Event(2, Message = "Error initializing Zipkin endpoint, falling back to default value: '{0}'", Level = EventLevel.Error)]
public void FailedEndpointInitialization(string exception)
{
this.WriteEvent(2, exception);
}
}
}
Original file line number Diff line number Diff line change
@@ -17,6 +17,8 @@
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\IActivityEnumerator.cs" Link="Includes\IActivityEnumerator.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\EnumerationHelper.cs" Link="Includes\EnumerationHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry.Api\Internal\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\EnvironmentVariableHelper.cs" Link="Includes\EnvironmentVariableHelper.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\OpenTelemetrySdkEventSource.cs" Link="Includes\OpenTelemetrySdkEventSource.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PooledList.cs" Link="Includes\PooledList.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\PeerServiceResolver.cs" Link="Includes\PeerServiceResolver.cs" />
<Compile Include="$(RepoRoot)\src\OpenTelemetry\Internal\ResourceSemanticConventions.cs" Link="Includes\ResourceSemanticConventions.cs" />
3 changes: 3 additions & 0 deletions src/OpenTelemetry.Exporter.Zipkin/README.md
Original file line number Diff line number Diff line change
@@ -62,6 +62,9 @@ values of the `ZipkinExporterOptions`.
| --------------------------------| -------------------------------- |
| `OTEL_EXPORTER_ZIPKIN_ENDPOINT` | `Endpoint` |

`FormatException` is thrown in case of an invalid value for any of the
supported environment variables.

## References

* [OpenTelemetry Project](https://opentelemetry.io/)
Loading