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

Add support for OTEL_BSP_EXPORT_* environmental variables #2219

Merged
merged 19 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 11 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
8 changes: 8 additions & 0 deletions src/OpenTelemetry.Exporter.Jaeger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Unreleased

* `JaegerExporterOptions.BatchExportProcessorOptions` is initialized with
`BatchSpanExportProcessorOptions` which supports field value overriding
using `OTEL_BSP_SCHEDULE_DELAY`, `OTEL_BSP_EXPORT_TIMEOUT`,
`OTEL_BSP_MAX_QUEUE_SIZE`, `OTEL_BSP_MAX_EXPORT_BATCH_SIZE`
envionmental variables as defined in the
[specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.5.0/specification/sdk-environment-variables.md#batch-span-processor).
([#2219](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2219))

## 1.2.0-alpha1

Released 2021-Jul-23
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Diagnostics;
using System.Security;
using OpenTelemetry.Exporter.Jaeger.Implementation;
using OpenTelemetry.Trace;

namespace OpenTelemetry.Exporter
{
Expand Down Expand Up @@ -82,6 +83,6 @@ public JaegerExporterOptions()
/// <summary>
/// Gets or sets the BatchExportProcessor options. Ignored unless ExportProcessorType is BatchExporter.
/// </summary>
public BatchExportProcessorOptions<Activity> BatchExportProcessorOptions { get; set; } = new BatchExportProcessorOptions<Activity>();
public BatchExportProcessorOptions<Activity> BatchExportProcessorOptions { get; set; } = new BatchSpanExportProcessorOptions();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Unreleased

* `OtlpExporterOptions.BatchExportProcessorOptions` is initialized with
`BatchSpanExportProcessorOptions` which supports field value overriding
using `OTEL_BSP_SCHEDULE_DELAY`, `OTEL_BSP_EXPORT_TIMEOUT`,
`OTEL_BSP_MAX_QUEUE_SIZE`, `OTEL_BSP_MAX_EXPORT_BATCH_SIZE`
envionmental variables as defined in the
[specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.5.0/specification/sdk-environment-variables.md#batch-span-processor).
([#2219](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2219))

* The `OtlpExporterOptions` defaults can be overridden using
`OTEL_EXPORTER_OTLP_ENDPOINT`, `OTEL_EXPORTER_OTLP_HEADERS` and `OTEL_EXPORTER_OTLP_TIMEOUT`
envionmental variables as defined in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.Diagnostics;
using System.Security;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using OpenTelemetry.Trace;

namespace OpenTelemetry.Exporter
{
Expand Down Expand Up @@ -104,7 +105,7 @@ public OtlpExporterOptions()
/// <summary>
/// Gets or sets the BatchExportProcessor options. Ignored unless ExportProcessorType is Batch.
/// </summary>
public BatchExportProcessorOptions<Activity> BatchExportProcessorOptions { get; set; } = new BatchExportProcessorOptions<Activity>();
public BatchExportProcessorOptions<Activity> BatchExportProcessorOptions { get; set; } = new BatchSpanExportProcessorOptions();

/// <summary>
/// Gets or sets the metric export interval in milliseconds. The default value is 1000 milliseconds.
Expand Down
8 changes: 8 additions & 0 deletions src/OpenTelemetry.Exporter.Zipkin/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Unreleased

* `ZipkinExporterOptions.BatchExportProcessorOptions` is initialized with
`BatchSpanExportProcessorOptions` which supports field value overriding
using `OTEL_BSP_SCHEDULE_DELAY`, `OTEL_BSP_EXPORT_TIMEOUT`,
`OTEL_BSP_MAX_QUEUE_SIZE`, `OTEL_BSP_MAX_EXPORT_BATCH_SIZE`
envionmental variables as defined in the
[specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.5.0/specification/sdk-environment-variables.md#batch-span-processor).
([#2219](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2219))

* Enabling endpoint configuration in ZipkinExporterOptions via
`OTEL_EXPORTER_ZIPKIN_ENDPOINT` environment variable.
([#1453](https://github.com/open-telemetry/opentelemetry-dotnet/issues/1453))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Diagnostics;
using OpenTelemetry.Exporter.Zipkin.Implementation;
using OpenTelemetry.Trace;

namespace OpenTelemetry.Exporter
{
Expand Down Expand Up @@ -70,6 +71,6 @@ public ZipkinExporterOptions()
/// <summary>
/// Gets or sets the BatchExportProcessor options. Ignored unless ExportProcessorType is BatchExporter.
/// </summary>
public BatchExportProcessorOptions<Activity> BatchExportProcessorOptions { get; set; } = new BatchExportProcessorOptions<Activity>();
public BatchExportProcessorOptions<Activity> BatchExportProcessorOptions { get; set; } = new BatchSpanExportProcessorOptions();
}
}
7 changes: 7 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

## Unreleased

* Add `BatchSpanExportProcessorOptions` which supports field value overriding
using `OTEL_BSP_SCHEDULE_DELAY`, `OTEL_BSP_EXPORT_TIMEOUT`,
`OTEL_BSP_MAX_QUEUE_SIZE`, `OTEL_BSP_MAX_EXPORT_BATCH_SIZE`
envionmental variables as defined in the
[specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.5.0/specification/sdk-environment-variables.md#batch-span-processor).
([#2219](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2219))

* `ResourceBuilder.AddEnvironmentVariableDetector` handles `OTEL_SERVICE_NAME`
environmental variable. ([#2209](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2209))

Expand Down
22 changes: 22 additions & 0 deletions src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#endif
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.Security;

namespace OpenTelemetry.Internal
{
Expand Down Expand Up @@ -125,6 +126,15 @@ public void TracerProviderException(string evnt, Exception ex)
}
}

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

[Event(1, Message = "Span processor queue size reached maximum. Throttling spans.", Level = EventLevel.Warning)]
public void SpanProcessorQueueIsExhausted()
{
Expand Down Expand Up @@ -287,6 +297,18 @@ public void TracerProviderException(string evnt, string ex)
this.WriteEvent(28, evnt, ex);
}

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

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

#if DEBUG
public class OpenTelemetryEventListener : EventListener
{
Expand Down
14 changes: 14 additions & 0 deletions src/OpenTelemetry/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,24 @@ purposes, the SDK provides the following built-in processors:
* [BatchExportProcessor&lt;T&gt;](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#batching-processor)
: This is an exporting processor which batches the telemetry before sending to
the configured exporter.

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

<!-- markdownlint-disable MD013 -->
| Environment variable | `BatchSpanExportProcessorOptions` property |
| -------------------------------- | ---------------------------------------------- |
| `OTEL_BSP_SCHEDULE_DELAY` | `ScheduledDelayMilliseconds` |
| `OTEL_BSP_EXPORT_TIMEOUT` | `ExporterTimeoutMilliseconds` |
| `OTEL_BSP_MAX_QUEUE_SIZE` | `MaxQueueSize` |
| `OTEL_BSP_MAX_EXPORT_BATCH_SIZE` | `MaxExportBatchSizeEnvVarKey` |
<!-- markdownlint-enable MD013 -->

* [CompositeProcessor&lt;T&gt;](../../src/OpenTelemetry/CompositeProcessor.cs)
: This is a processor which can be composed from multiple processors. This is
typically used to construct multiple processing pipelines, each ending with
its own exporter.

* [SimpleExportProcessor&lt;T&gt;](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#simple-processor)
: This is an exporting processor which passes telemetry to the configured
exporter without any batching.
Expand Down
91 changes: 91 additions & 0 deletions src/OpenTelemetry/Trace/BatchSpanExportProcessorOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// <copyright file="BatchSpanExportProcessorOptions.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 System.Diagnostics;
using System.Security;
using OpenTelemetry.Internal;

namespace OpenTelemetry.Trace
{
public class BatchSpanExportProcessorOptions : BatchExportProcessorOptions<Activity>
pellared marked this conversation as resolved.
Show resolved Hide resolved
{
internal const string MaxQueueSizeEnvVarKey = "OTEL_BSP_MAX_QUEUE_SIZE";

internal const string MaxExportBatchSizeEnvVarKey = "OTEL_BSP_MAX_EXPORT_BATCH_SIZE";

internal const string ExporterTimeoutEnvVarKey = "OTEL_BSP_EXPORT_TIMEOUT";

internal const string ScheduledDelayEnvVarKey = "OTEL_BSP_SCHEDULE_DELAY";

public BatchSpanExportProcessorOptions()
pellared marked this conversation as resolved.
Show resolved Hide resolved
{
int value;

if (TryLoadEnvVarInt(ExporterTimeoutEnvVarKey, out value))
{
this.ExporterTimeoutMilliseconds = value;
}

if (TryLoadEnvVarInt(MaxExportBatchSizeEnvVarKey, out value))
{
this.MaxExportBatchSize = value;
}

if (TryLoadEnvVarInt(MaxQueueSizeEnvVarKey, out value))
{
this.MaxQueueSize = value;
}

if (TryLoadEnvVarInt(ScheduledDelayEnvVarKey, out value))
{
this.ScheduledDelayMilliseconds = value;
}
}

private static bool TryLoadEnvVarInt(string envVarKey, out int result)
{
result = 0;

string value;
try
{
value = Environment.GetEnvironmentVariable(envVarKey);
}
catch (SecurityException ex)
{
// The caller does not have the required permission to
// retrieve the value of an environment variable from the current process.
OpenTelemetrySdkEventSource.Log.MissingPermissionsToReadEnvironmentVariable(ex);
return false;
}

if (string.IsNullOrEmpty(value))
{
return false;
}

if (!int.TryParse(value, out var parsedValue))
{
OpenTelemetrySdkEventSource.Log.FailedToParseEnvironmentVariable(envVarKey, value);
return false;
}

result = parsedValue;
return true;
}
}
}
101 changes: 101 additions & 0 deletions test/OpenTelemetry.Tests/Trace/BatchSpanExportProcessorOptionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// <copyright file="BatchSpanExportProcessorOptionsTest.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.Trace.Tests
{
public class BatchSpanExportProcessorOptionsTest : IDisposable
{
public BatchSpanExportProcessorOptionsTest()
{
this.ClearEnvVars();
}

public void Dispose()
{
this.ClearEnvVars();
}

[Fact]
public void BatchExportProcessorOptions_Defaults()
{
var options = new BatchSpanExportProcessorOptions();

Assert.Equal(30000, options.ExporterTimeoutMilliseconds);
Assert.Equal(512, options.MaxExportBatchSize);
Assert.Equal(2048, options.MaxQueueSize);
Assert.Equal(5000, options.ScheduledDelayMilliseconds);
}

[Fact]
public void BatchExportProcessorOptions_EnvironmentVariableOverride()
{
Environment.SetEnvironmentVariable(BatchSpanExportProcessorOptions.ExporterTimeoutEnvVarKey, "1");
Environment.SetEnvironmentVariable(BatchSpanExportProcessorOptions.MaxExportBatchSizeEnvVarKey, "2");
Environment.SetEnvironmentVariable(BatchSpanExportProcessorOptions.MaxQueueSizeEnvVarKey, "3");
Environment.SetEnvironmentVariable(BatchSpanExportProcessorOptions.ScheduledDelayEnvVarKey, "4");

var options = new BatchSpanExportProcessorOptions();

Assert.Equal(1, options.ExporterTimeoutMilliseconds);
Assert.Equal(2, options.MaxExportBatchSize);
Assert.Equal(3, options.MaxQueueSize);
Assert.Equal(4, options.ScheduledDelayMilliseconds);
}

[Fact]
public void BatchExportProcessorOptions_InvalidPortEnvironmentVariableOverride()
{
Environment.SetEnvironmentVariable(BatchSpanExportProcessorOptions.ExporterTimeoutEnvVarKey, "invalid");

var options = new BatchSpanExportProcessorOptions();

Assert.Equal(30000, options.ExporterTimeoutMilliseconds); // use default
}

[Fact]
public void BatchExportProcessorOptions_SetterOverridesEnvironmentVariable()
{
Environment.SetEnvironmentVariable(BatchSpanExportProcessorOptions.ExporterTimeoutEnvVarKey, "123");

var options = new BatchSpanExportProcessorOptions
{
ExporterTimeoutMilliseconds = 89000,
};

Assert.Equal(89000, options.ExporterTimeoutMilliseconds);
}

[Fact]
public void BatchExportProcessorOptions_EnvironmentVariableNames()
{
Assert.Equal("OTEL_BSP_EXPORT_TIMEOUT", BatchSpanExportProcessorOptions.ExporterTimeoutEnvVarKey);
Assert.Equal("OTEL_BSP_MAX_EXPORT_BATCH_SIZE", BatchSpanExportProcessorOptions.MaxExportBatchSizeEnvVarKey);
Assert.Equal("OTEL_BSP_MAX_QUEUE_SIZE", BatchSpanExportProcessorOptions.MaxQueueSizeEnvVarKey);
Assert.Equal("OTEL_BSP_SCHEDULE_DELAY", BatchSpanExportProcessorOptions.ScheduledDelayEnvVarKey);
}

private void ClearEnvVars()
{
Environment.SetEnvironmentVariable(BatchSpanExportProcessorOptions.ExporterTimeoutEnvVarKey, null);
Environment.SetEnvironmentVariable(BatchSpanExportProcessorOptions.MaxExportBatchSizeEnvVarKey, null);
Environment.SetEnvironmentVariable(BatchSpanExportProcessorOptions.MaxQueueSizeEnvVarKey, null);
Environment.SetEnvironmentVariable(BatchSpanExportProcessorOptions.ScheduledDelayEnvVarKey, null);
}
}
}