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

Enable zipkin endpoint configuration via environment variable #1924

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
28a9d47
enable ipking enpoint config via env variable
vishweshbankwar Mar 19, 2021
e97cbe4
update changelog
vishweshbankwar Mar 19, 2021
2f16f16
fix md error
vishweshbankwar Mar 19, 2021
27b35ee
Merge branch 'main' into vibankwa/Enable-Zipkin-Endpoint-Configuratio…
vishweshbankwar Mar 19, 2021
d1f58fb
Trigger build
vishweshbankwar Mar 19, 2021
8b335f6
Adding test
vishweshbankwar Mar 22, 2021
dd1b45f
Merge branch 'main' into vibankwa/Enable-Zipkin-Endpoint-Configuratio…
vishweshbankwar Mar 26, 2021
7077fcc
Added more test and updated readme
vishweshbankwar Mar 26, 2021
f370f5f
updating readme
vishweshbankwar Mar 30, 2021
5b1c6fd
Merge branch 'main' into vibankwa/Enable-Zipkin-Endpoint-Configuratio…
vishweshbankwar Mar 30, 2021
ec80f35
updates to zipkin readme
vishweshbankwar Mar 31, 2021
010bf58
Merge branch 'main' into vibankwa/Enable-Zipkin-Endpoint-Configuratio…
vishweshbankwar Mar 31, 2021
f2d0284
resolving PR comments
vishweshbankwar Apr 2, 2021
a0941f4
Merge branch 'vibankwa/Enable-Zipkin-Endpoint-Configuration-via-Env-V…
vishweshbankwar Apr 2, 2021
31fea42
Merge branch 'main' into vibankwa/Enable-Zipkin-Endpoint-Configuratio…
vishweshbankwar Apr 2, 2021
053d335
moved endpoint init to ctor with try catch
vishweshbankwar Apr 13, 2021
13cee05
Merge branch 'main' into vibankwa/Enable-Zipkin-Endpoint-Configuratio…
vishweshbankwar Apr 13, 2021
6bcf977
Merge branch 'main' into vibankwa/Enable-Zipkin-Endpoint-Configuratio…
vishweshbankwar May 25, 2021
a800ae9
Merge branch 'main' into vibankwa/Enable-Zipkin-Endpoint-Configuratio…
vishweshbankwar Jun 23, 2021
ce391ef
Merge branch 'main' into vibankwa/Enable-Zipkin-Endpoint-Configuratio…
vishweshbankwar Jul 10, 2021
827e518
resolving PR comments
vishweshbankwar Jul 10, 2021
cf830b3
Minor change
vishweshbankwar Jul 12, 2021
f345254
resolve PR comments
vishweshbankwar Jul 15, 2021
30f3c0c
Merge branch 'main' into vibankwa/Enable-Zipkin-Endpoint-Configuratio…
vishweshbankwar Jul 15, 2021
bc32815
Merge branch 'main' into vibankwa/Enable-Zipkin-Endpoint-Configuratio…
vishweshbankwar Jul 28, 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.Zipkin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

* Enabling endpoint configuration in ZipkinExporterOptions via
`OTEL_EXPORTER_ZIPKIN_ENDPOINT` environment variable.
([#1453](https://github.com/open-telemetry/opentelemetry-dotnet/issues/1453))

## 1.2.0-alpha1

Released 2021-Jul-23
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,25 @@ public void FailedExport(Exception ex)
}
}

[NonEvent]
public void FailedEndpointInitialization(Exception ex)
{
if (this.IsEnabled(EventLevel.Error, (EventKeywords)(-1)))
{
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);
}
}
}
18 changes: 15 additions & 3 deletions src/OpenTelemetry.Exporter.Zipkin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ method on `TracerProviderBuilder`.

## Configuration

You can configure the `ZipkinExporter` through
`ZipkinExporterOptions` properties:
You can configure the `ZipkinExporter` through `ZipkinExporterOptions`
and environment variables. The `ZipkinExporterOptions` setters
take precedence over the environment variables.

### Configuration using Properties

* `ServiceName`: Name of the service reporting telemetry. If the `Resource`
associated with the telemetry has "service.name" defined, then it'll be
Expand All @@ -41,7 +44,7 @@ See
[`TestZipkinExporter.cs`](../../examples/Console/TestZipkinExporter.cs)
for example use.

## Configuration using Dependency Injection
### Configuration using Dependency Injection

This exporter allows easy configuration of `ZipkinExporterOptions` from
dependency injection container, when used in conjunction with
Expand All @@ -50,6 +53,15 @@ dependency injection container, when used in conjunction with
See the [Startup](../../examples/AspNetCore/Startup.cs) class of the ASP.NET
Core application for example use.

### Configuration using Environment Variables

The following environment variables can be used to override the default
values of the `ZipkinExporterOptions`.

| Environment variable | `ZipkinExporterOptions` property |
| --------------------------------| -------------------------------- |
| `OTEL_EXPORTER_ZIPKIN_ENDPOINT` | `Endpoint` |

## References

* [OpenTelemetry Project](https://opentelemetry.io/)
Expand Down
22 changes: 21 additions & 1 deletion src/OpenTelemetry.Exporter.Zipkin/ZipkinExporterOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.Diagnostics;
using OpenTelemetry.Exporter.Zipkin.Implementation;

namespace OpenTelemetry.Exporter
{
Expand All @@ -25,12 +26,31 @@ namespace OpenTelemetry.Exporter
public sealed class ZipkinExporterOptions
{
internal const int DefaultMaxPayloadSizeInBytes = 4096;
internal const string ZipkinEndpointEnvVar = "OTEL_EXPORTER_ZIPKIN_ENDPOINT";
internal const string DefaultZipkinEndpoint = "http://localhost:9411/api/v2/spans";

/// <summary>
/// Initializes a new instance of the <see cref="ZipkinExporterOptions"/> class.
/// Initializes zipkin endpoint.
/// </summary>
public ZipkinExporterOptions()
{
try
{
this.Endpoint = new Uri(Environment.GetEnvironmentVariable(ZipkinEndpointEnvVar) ?? DefaultZipkinEndpoint);
}
catch (Exception ex)
Copy link
Member

Choose a reason for hiding this comment

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

minor (may follow up in separate PR):
We can do similar to what Jaeger/OTLP does - catch SecurityException for reading env variable. Handle the Uri parse exception separately. Both should be logged as separate EventSource events.
This avoids catching the generic Exception.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure - will create a separate PR for that.

{
this.Endpoint = new Uri(DefaultZipkinEndpoint);
ZipkinExporterEventSource.Log.FailedEndpointInitialization(ex);
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// <summary>
/// Gets or sets Zipkin endpoint address. See https://zipkin.io/zipkin-api/#/default/post_spans.
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved
/// Typically https://zipkin-server-name:9411/api/v2/spans.
/// </summary>
public Uri Endpoint { get; set; } = new Uri("http://localhost:9411/api/v2/spans");
public Uri Endpoint { get; set; }

/// <summary>
/// Gets or sets a value indicating whether short trace id should be used.
Expand Down
54 changes: 54 additions & 0 deletions test/OpenTelemetry.Exporter.Zipkin.Tests/ZipkinExporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,60 @@ public void SuppresssesInstrumentation()
Assert.Equal(1, endCalledCount);
}

[Fact]
public void EndpointConfigurationUsingEnvironmentVariable()
{
try
{
Environment.SetEnvironmentVariable(ZipkinExporterOptions.ZipkinEndpointEnvVar, "http://urifromenvironmentvariable");

var exporterOptions = new ZipkinExporterOptions();

Assert.Equal(new Uri(Environment.GetEnvironmentVariable(ZipkinExporterOptions.ZipkinEndpointEnvVar)).AbsoluteUri, exporterOptions.Endpoint.AbsoluteUri);
}
finally
{
Environment.SetEnvironmentVariable(ZipkinExporterOptions.ZipkinEndpointEnvVar, null);
}
}

[Fact]
public void IncodeEndpointConfigTakesPrecedenceOverEnvironmentVariable()
{
try
{
Environment.SetEnvironmentVariable(ZipkinExporterOptions.ZipkinEndpointEnvVar, "http://urifromenvironmentvariable");

var exporterOptions = new ZipkinExporterOptions
{
Endpoint = new Uri("http://urifromcode"),
};

Assert.Equal(new Uri("http://urifromcode").AbsoluteUri, exporterOptions.Endpoint.AbsoluteUri);
}
finally
{
Environment.SetEnvironmentVariable(ZipkinExporterOptions.ZipkinEndpointEnvVar, null);
}
}

[Fact]
public void ErrorGettingUriFromEnvVarSetsDefaultEndpointValue()
{
try
{
Environment.SetEnvironmentVariable(ZipkinExporterOptions.ZipkinEndpointEnvVar, "InvalidUri");

var exporterOptions = new ZipkinExporterOptions();

Assert.Equal(new Uri(ZipkinExporterOptions.DefaultZipkinEndpoint).AbsoluteUri, exporterOptions.Endpoint.AbsoluteUri);
}
finally
{
Environment.SetEnvironmentVariable(ZipkinExporterOptions.ZipkinEndpointEnvVar, null);
}
}
vishweshbankwar marked this conversation as resolved.
Show resolved Hide resolved

[Theory]
[InlineData(true, false, false)]
[InlineData(false, false, false)]
Expand Down