Add support for OTEL_EXPORTER_JAEGER_AGENT_HOST and OTEL_EXPORTER_JAEGER_AGENT_PORT (#2123)

This commit is contained in:
Robert Pająk 2021-07-15 05:46:02 +02:00 committed by GitHub
parent 56357fcd04
commit a9fed4252b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 179 additions and 6 deletions

View File

@ -2,6 +2,12 @@
## Unreleased
* The `JaegerExporterOptions` defaults can be overridden using
`OTEL_EXPORTER_JAEGER_AGENT_HOST` and `OTEL_EXPORTER_JAEGER_AGENT_PORT`
envionmental variables as defined in the
[specification](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md#jaeger-exporter).
([#2123](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2123))
## 1.1.0
Released 2021-Jul-12

View File

@ -16,6 +16,7 @@
using System;
using System.Diagnostics.Tracing;
using System.Security;
using OpenTelemetry.Internal;
namespace OpenTelemetry.Exporter.Jaeger.Implementation
@ -37,10 +38,31 @@ namespace OpenTelemetry.Exporter.Jaeger.Implementation
}
}
[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);
}
}
}

View File

@ -14,7 +14,10 @@
// limitations under the License.
// </copyright>
using System;
using System.Diagnostics;
using System.Security;
using OpenTelemetry.Exporter.Jaeger.Implementation;
namespace OpenTelemetry.Exporter
{
@ -22,6 +25,40 @@ namespace OpenTelemetry.Exporter
{
internal const int DefaultMaxPayloadSizeInBytes = 4096;
internal const string OTelAgentHostEnvVarKey = "OTEL_EXPORTER_JAEGER_AGENT_HOST";
internal const string OTelAgentPortEnvVarKey = "OTEL_EXPORTER_JAEGER_AGENT_PORT";
public JaegerExporterOptions()
{
try
{
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);
}
}
}
catch (SecurityException ex)
{
// The caller does not have the required permission to
// retrieve the value of an environment variable from the current process.
JaegerExporterEventSource.Log.MissingPermissionsToReadEnvironmentVariable(ex);
}
}
/// <summary>
/// Gets or sets the Jaeger agent host. Default value: localhost.
/// </summary>

View File

@ -29,17 +29,22 @@ dotnet add package OpenTelemetry.Exporter.Jaeger
## Configuration
You can configure the `JaegerExporter` through `JaegerExporterOptions`
and environment variables. The `JaegerExporterOptions` setters
take precedence over the environment variables.
## Options Properties
The `JaegerExporter` can be configured using the `JaegerExporterOptions`
properties:
* `AgentHost`: Usually `localhost` since an agent should usually be running on
the same machine as your application or service.
* `AgentPort`: The compact thrift protocol port of the Jaeger Agent (default
`6831`).
* `AgentHost`: The Jaeger Agent host (default `localhost`).
* `AgentPort`: The compact thrift protocol UDP port of the Jaeger Agent
(default `6831`).
* `MaxPayloadSizeInBytes`: The maximum size of each UDP packet that gets
sent to the agent. (default `4096`).
sent to the agent (default `4096`).
* `ExportProcessorType`: Whether the exporter should use
[Batch or Simple exporting processor](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#built-in-span-processors)
.
(default `ExportProcessorType.Batch`).
* `BatchExportProcessorOptions`: Configuration options for the batch exporter.
Only used if ExportProcessorType is set to Batch.
@ -47,6 +52,16 @@ See the
[`TestJaegerExporter.cs`](../../examples/Console/TestJaegerExporter.cs)
for an example of how to use the exporter.
## Environment Variables
The following environment variables can be used to override the default
values of the `JaegerExporterOptions`.
| Environment variable | `JaegerExporterOptions` property |
| --------------------------------- | -------------------------------- |
| `OTEL_EXPORTER_JAEGER_AGENT_HOST` | `AgentHost` |
| `OTEL_EXPORTER_JAEGER_AGENT_PORT` | `AgentPort` |
## References
* [Jaeger](https://www.jaegertracing.io)

View File

@ -0,0 +1,93 @@
// <copyright file="JaegerExporterOptionsTests.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.Exporter.Jaeger.Tests
{
public class JaegerExporterOptionsTests : IDisposable
{
public JaegerExporterOptionsTests()
{
this.ClearEnvVars();
}
public void Dispose()
{
this.ClearEnvVars();
}
[Fact]
public void JaegerExporterOptions_Defaults()
{
var options = new JaegerExporterOptions();
Assert.Equal("localhost", options.AgentHost);
Assert.Equal(6831, options.AgentPort);
Assert.Equal(4096, options.MaxPayloadSizeInBytes);
Assert.Equal(ExportProcessorType.Batch, options.ExportProcessorType);
}
[Fact]
public void JaegerExporterOptions_EnvironmentVariableOverride()
{
Environment.SetEnvironmentVariable(JaegerExporterOptions.OTelAgentHostEnvVarKey, "jeager-host");
Environment.SetEnvironmentVariable(JaegerExporterOptions.OTelAgentPortEnvVarKey, "123");
var options = new JaegerExporterOptions();
Assert.Equal("jeager-host", options.AgentHost);
Assert.Equal(123, options.AgentPort);
}
[Fact]
public void JaegerExporterOptions_InvalidPortEnvironmentVariableOverride()
{
Environment.SetEnvironmentVariable(JaegerExporterOptions.OTelAgentPortEnvVarKey, "invalid");
var options = new JaegerExporterOptions();
Assert.Equal(6831, options.AgentPort); // use default
}
[Fact]
public void JaegerExporterOptions_SetterOverridesEnvironmentVariable()
{
Environment.SetEnvironmentVariable(JaegerExporterOptions.OTelAgentHostEnvVarKey, "envvar-host");
var options = new JaegerExporterOptions
{
AgentHost = "incode-host",
};
Assert.Equal("incode-host", options.AgentHost);
}
[Fact]
public void JaegerExporterOptions_EnvironmentVariableNames()
{
Assert.Equal("OTEL_EXPORTER_JAEGER_AGENT_HOST", JaegerExporterOptions.OTelAgentHostEnvVarKey);
Assert.Equal("OTEL_EXPORTER_JAEGER_AGENT_PORT", JaegerExporterOptions.OTelAgentPortEnvVarKey);
}
private void ClearEnvVars()
{
Environment.SetEnvironmentVariable(JaegerExporterOptions.OTelAgentHostEnvVarKey, null);
Environment.SetEnvironmentVariable(JaegerExporterOptions.OTelAgentPortEnvVarKey, null);
}
}
}