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

[Exporter.OpenTelemetryProtocol] SdkOptionLimits - fix defaults #3978

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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
11 changes: 10 additions & 1 deletion src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@

## Unreleased

* Fix default values for `OTEL_ATTRIBUTE_COUNT_LIMIT`,
`OTEL_ATTRIBUTE_COUNT_LIMIT`,
`OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT`,
`OTEL_SPAN_EVENT_COUNT_LIMIT`,
`OTEL_SPAN_LINK_COUNT_LIMIT`,
`OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT`,
`OTEL_LINK_ATTRIBUTE_COUNT_LIMIT`. All of them are defaulted to `128`.
([#3978](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3978))

## 1.4.0-beta.3

Released 2022-Nov-07

* Log Exporter modified to no longer prefix scope-depth when exporting ILogger
scopes as attributes. Empty keys and {OriginalFormat} key will be ignored from
scopes.
([3843](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3843))
([#3843](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3843))

## 1.4.0-beta.2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,15 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;

internal sealed class SdkLimitOptions
{
private const int DefaultSdkLimit = 128;
private int? spanAttributeValueLengthLimit;
private bool spanAttributeValueLengthLimitSet;
private int? spanAttributeCountLimit;
private bool spanAttributeCountLimitSet;
private int? spanEventAttributeCountLimit;
private bool spanEventAttributeCountLimitSet;
private int? spanLinkAttributeCountLimit;
private bool spanLinkAttributeCountLimitSet;

internal SdkLimitOptions()
: this(new ConfigurationBuilder().AddEnvironmentVariables().Build())
Expand All @@ -34,16 +39,16 @@ internal SdkLimitOptions()
internal SdkLimitOptions(IConfiguration configuration)
{
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md#attribute-limits
SetIntConfigValue(configuration, "OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT", value => this.AttributeValueLengthLimit = value);
SetIntConfigValue(configuration, "OTEL_ATTRIBUTE_COUNT_LIMIT", value => this.AttributeCountLimit = value);
SetIntConfigValue(configuration, "OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT", value => this.AttributeValueLengthLimit = value, null);
SetIntConfigValue(configuration, "OTEL_ATTRIBUTE_COUNT_LIMIT", value => this.AttributeCountLimit = value, DefaultSdkLimit);

// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md#span-limits
SetIntConfigValue(configuration, "OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT", value => this.SpanAttributeValueLengthLimit = value);
SetIntConfigValue(configuration, "OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT", value => this.SpanAttributeCountLimit = value);
SetIntConfigValue(configuration, "OTEL_SPAN_EVENT_COUNT_LIMIT", value => this.SpanEventCountLimit = value);
SetIntConfigValue(configuration, "OTEL_SPAN_LINK_COUNT_LIMIT", value => this.SpanLinkCountLimit = value);
SetIntConfigValue(configuration, "OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT", value => this.SpanEventAttributeCountLimit = value);
SetIntConfigValue(configuration, "OTEL_LINK_ATTRIBUTE_COUNT_LIMIT", value => this.SpanLinkAttributeCountLimit = value);
SetIntConfigValue(configuration, "OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT", value => this.SpanAttributeValueLengthLimit = value, null);
SetIntConfigValue(configuration, "OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT", value => this.SpanAttributeCountLimit = value, null);
SetIntConfigValue(configuration, "OTEL_SPAN_EVENT_COUNT_LIMIT", value => this.SpanEventCountLimit = value, DefaultSdkLimit);
SetIntConfigValue(configuration, "OTEL_SPAN_LINK_COUNT_LIMIT", value => this.SpanLinkCountLimit = value, DefaultSdkLimit);
SetIntConfigValue(configuration, "OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT", value => this.SpanEventAttributeCountLimit = value, null);
SetIntConfigValue(configuration, "OTEL_LINK_ATTRIBUTE_COUNT_LIMIT", value => this.SpanLinkAttributeCountLimit = value, null);
}

/// <summary>
Expand All @@ -64,8 +69,12 @@ internal SdkLimitOptions(IConfiguration configuration)
/// </remarks>
public int? SpanAttributeValueLengthLimit
{
get => this.spanAttributeValueLengthLimit ?? this.AttributeValueLengthLimit;
set => this.spanAttributeValueLengthLimit = value;
get => this.spanAttributeValueLengthLimitSet ? this.spanAttributeValueLengthLimit : this.AttributeValueLengthLimit;
set
{
this.spanAttributeValueLengthLimitSet = true;
this.spanAttributeValueLengthLimit = value;
}
}

/// <summary>
Expand All @@ -76,8 +85,12 @@ public int? SpanAttributeValueLengthLimit
/// </remarks>
public int? SpanAttributeCountLimit
{
get => this.spanAttributeCountLimit ?? this.AttributeCountLimit;
set => this.spanAttributeCountLimit = value;
get => this.spanAttributeCountLimitSet ? this.spanAttributeCountLimit : this.AttributeCountLimit;
set
{
this.spanAttributeCountLimitSet = true;
this.spanAttributeCountLimit = value;
}
}

/// <summary>
Expand All @@ -98,8 +111,12 @@ public int? SpanAttributeCountLimit
/// </remarks>
public int? SpanEventAttributeCountLimit
{
get => this.spanEventAttributeCountLimit ?? this.SpanAttributeCountLimit;
set => this.spanEventAttributeCountLimit = value;
get => this.spanEventAttributeCountLimitSet ? this.spanEventAttributeCountLimit : this.SpanAttributeCountLimit;
set
{
this.spanEventAttributeCountLimitSet = true;
this.spanEventAttributeCountLimit = value;
}
}

/// <summary>
Expand All @@ -110,15 +127,23 @@ public int? SpanEventAttributeCountLimit
/// </remarks>
public int? SpanLinkAttributeCountLimit
{
get => this.spanLinkAttributeCountLimit ?? this.SpanAttributeCountLimit;
set => this.spanLinkAttributeCountLimit = value;
get => this.spanLinkAttributeCountLimitSet ? this.spanLinkAttributeCountLimit : this.SpanAttributeCountLimit;
set
{
this.spanLinkAttributeCountLimitSet = true;
this.spanLinkAttributeCountLimit = value;
}
}

private static void SetIntConfigValue(IConfiguration configuration, string key, Action<int> setter)
private static void SetIntConfigValue(IConfiguration configuration, string key, Action<int?> setter, int? defaultValue)
{
if (configuration.TryGetIntValue(key, out var result))
{
setter(result);
}
else if (defaultValue.HasValue)
{
setter(defaultValue);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
// limitations under the License.
// </copyright>

using System;
using System.Collections.Generic;
using Microsoft.Extensions.Configuration;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;
using Xunit;
Expand All @@ -37,16 +35,16 @@ public void Dispose()
[Fact]
public void SdkLimitOptionsDefaults()
{
var config = new SdkLimitOptions();

Assert.Null(config.AttributeValueLengthLimit);
Assert.Null(config.AttributeCountLimit);
Assert.Null(config.SpanAttributeValueLengthLimit);
Assert.Null(config.SpanAttributeCountLimit);
Assert.Null(config.SpanEventCountLimit);
Assert.Null(config.SpanLinkCountLimit);
Assert.Null(config.SpanEventAttributeCountLimit);
Assert.Null(config.SpanLinkAttributeCountLimit);
var options = new SdkLimitOptions();

Assert.Null(options.AttributeValueLengthLimit);
Assert.Equal(128, options.AttributeCountLimit);
Assert.Null(options.SpanAttributeValueLengthLimit);
Assert.Equal(128, options.SpanAttributeCountLimit);
Assert.Equal(128, options.SpanEventCountLimit);
Assert.Equal(128, options.SpanLinkCountLimit);
Assert.Equal(128, options.SpanEventAttributeCountLimit);
Assert.Equal(128, options.SpanLinkAttributeCountLimit);
}

[Fact]
Expand All @@ -61,66 +59,88 @@ public void SdkLimitOptionsIsInitializedFromEnvironment()
Environment.SetEnvironmentVariable("OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT", "30");
Environment.SetEnvironmentVariable("OTEL_LINK_ATTRIBUTE_COUNT_LIMIT", "30");

var config = new SdkLimitOptions();
var options = new SdkLimitOptions();

Assert.Equal(10, config.AttributeValueLengthLimit);
Assert.Equal(10, config.AttributeCountLimit);
Assert.Equal(20, config.SpanAttributeValueLengthLimit);
Assert.Equal(20, config.SpanAttributeCountLimit);
Assert.Equal(10, config.SpanEventCountLimit);
Assert.Equal(10, config.SpanLinkCountLimit);
Assert.Equal(30, config.SpanEventAttributeCountLimit);
Assert.Equal(30, config.SpanLinkAttributeCountLimit);
Assert.Equal(10, options.AttributeValueLengthLimit);
Assert.Equal(10, options.AttributeCountLimit);
Assert.Equal(20, options.SpanAttributeValueLengthLimit);
Assert.Equal(20, options.SpanAttributeCountLimit);
Assert.Equal(10, options.SpanEventCountLimit);
Assert.Equal(10, options.SpanLinkCountLimit);
Assert.Equal(30, options.SpanEventAttributeCountLimit);
Assert.Equal(30, options.SpanLinkAttributeCountLimit);
}

[Fact]
public void SpanAttributeValueLengthLimitFallback()
{
var config = new SdkLimitOptions();
var options = new SdkLimitOptions();

options.AttributeValueLengthLimit = 10;
Assert.Equal(10, options.AttributeValueLengthLimit);
Assert.Equal(10, options.SpanAttributeValueLengthLimit);

config.AttributeValueLengthLimit = 10;
Assert.Equal(10, config.AttributeValueLengthLimit);
Assert.Equal(10, config.SpanAttributeValueLengthLimit);
options.SpanAttributeValueLengthLimit = 20;
Assert.Equal(10, options.AttributeValueLengthLimit);
Assert.Equal(20, options.SpanAttributeValueLengthLimit);

config.SpanAttributeValueLengthLimit = 20;
Assert.Equal(10, config.AttributeValueLengthLimit);
Assert.Equal(20, config.SpanAttributeValueLengthLimit);
options.SpanAttributeValueLengthLimit = null;
Assert.Equal(10, options.AttributeValueLengthLimit);
Assert.Null(options.SpanAttributeValueLengthLimit);
}

[Fact]
public void SpanAttributeCountLimitFallback()
{
var config = new SdkLimitOptions();

config.AttributeCountLimit = 10;
Assert.Equal(10, config.AttributeCountLimit);
Assert.Equal(10, config.SpanAttributeCountLimit);
Assert.Equal(10, config.SpanEventAttributeCountLimit);
Assert.Equal(10, config.SpanLinkAttributeCountLimit);

config.SpanAttributeCountLimit = 20;
Assert.Equal(10, config.AttributeCountLimit);
Assert.Equal(20, config.SpanAttributeCountLimit);
Assert.Equal(20, config.SpanEventAttributeCountLimit);
Assert.Equal(20, config.SpanLinkAttributeCountLimit);

config.SpanEventAttributeCountLimit = 30;
Assert.Equal(10, config.AttributeCountLimit);
Assert.Equal(20, config.SpanAttributeCountLimit);
Assert.Equal(30, config.SpanEventAttributeCountLimit);
Assert.Equal(20, config.SpanLinkAttributeCountLimit);

config.SpanLinkAttributeCountLimit = 40;
Assert.Equal(10, config.AttributeCountLimit);
Assert.Equal(20, config.SpanAttributeCountLimit);
Assert.Equal(30, config.SpanEventAttributeCountLimit);
Assert.Equal(40, config.SpanLinkAttributeCountLimit);
var options = new SdkLimitOptions();

options.AttributeCountLimit = 10;
Assert.Equal(10, options.AttributeCountLimit);
Assert.Equal(10, options.SpanAttributeCountLimit);
Assert.Equal(10, options.SpanEventAttributeCountLimit);
Assert.Equal(10, options.SpanLinkAttributeCountLimit);

options.SpanAttributeCountLimit = 20;
Assert.Equal(10, options.AttributeCountLimit);
Assert.Equal(20, options.SpanAttributeCountLimit);
Assert.Equal(20, options.SpanEventAttributeCountLimit);
Assert.Equal(20, options.SpanLinkAttributeCountLimit);

options.SpanEventAttributeCountLimit = 30;
Assert.Equal(10, options.AttributeCountLimit);
Assert.Equal(20, options.SpanAttributeCountLimit);
Assert.Equal(30, options.SpanEventAttributeCountLimit);
Assert.Equal(20, options.SpanLinkAttributeCountLimit);

options.SpanLinkAttributeCountLimit = 40;
Assert.Equal(10, options.AttributeCountLimit);
Assert.Equal(20, options.SpanAttributeCountLimit);
Assert.Equal(30, options.SpanEventAttributeCountLimit);
Assert.Equal(40, options.SpanLinkAttributeCountLimit);

options.SpanLinkAttributeCountLimit = null;
Assert.Equal(10, options.AttributeCountLimit);
Assert.Equal(20, options.SpanAttributeCountLimit);
Assert.Equal(30, options.SpanEventAttributeCountLimit);
Assert.Null(options.SpanLinkAttributeCountLimit);

options.SpanEventAttributeCountLimit = null;
Assert.Equal(10, options.AttributeCountLimit);
Assert.Equal(20, options.SpanAttributeCountLimit);
Assert.Null(options.SpanEventAttributeCountLimit);
Assert.Null(options.SpanLinkAttributeCountLimit);

options.SpanAttributeCountLimit = null;
Assert.Equal(10, options.AttributeCountLimit);
Assert.Null(options.SpanAttributeCountLimit);
Assert.Null(options.SpanEventAttributeCountLimit);
Assert.Null(options.SpanLinkAttributeCountLimit);
}

[Fact]
public void SdkLimitOptionsUsingIConfiguration()
{
var values = new Dictionary<string, string>()
var values = new Dictionary<string, string>
{
["OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT"] = "23",
["OTEL_ATTRIBUTE_COUNT_LIMIT"] = "24",
Expand Down