Skip to content

Commit

Permalink
OTLP LogExporter limits configuration by env. var. (#4887)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kielek authored Sep 27, 2023
1 parent bc1badf commit 6e2f442
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
not AOT-compatible. Removed the dependency on `System.Reflection.Emit.Lightweight`.
([#4859](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4859))

* Added support for `OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT`
and `OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT`.
([#4887](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4887))

## 1.6.0

Released 2023-Sep-05
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord, SdkLimitO
otlpLogRecord.SeverityText = logRecord.Severity.Value.ToShortName();
}

var attributeValueLengthLimit = sdkLimitOptions.AttributeValueLengthLimit;
var attributeCountLimit = sdkLimitOptions.AttributeCountLimit ?? int.MaxValue;
var attributeValueLengthLimit = sdkLimitOptions.LogRecordAttributeValueLengthLimit;
var attributeCountLimit = sdkLimitOptions.LogRecordAttributeCountLimit ?? int.MaxValue;

/*
// Removing this temporarily for stable release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ internal sealed class SdkLimitOptions
private bool spanEventAttributeCountLimitSet;
private int? spanLinkAttributeCountLimit;
private bool spanLinkAttributeCountLimitSet;
private int? logRecordAttributeValueLengthLimit;
private bool logRecordAttributeValueLengthLimitSet;
private int? logRecordAttributeCountLimit;
private bool logRecordAttributeCountLimitSet;

public SdkLimitOptions()
: this(new ConfigurationBuilder().AddEnvironmentVariables().Build())
Expand All @@ -38,17 +42,21 @@ public SdkLimitOptions()

internal SdkLimitOptions(IConfiguration configuration)
{
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/sdk-environment-variables.md#attribute-limits
// https://github.com/open-telemetry/opentelemetry-specification/blob/v1.25.0/specification/configuration/sdk-environment-variables.md#attribute-limits
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
// https://github.com/open-telemetry/opentelemetry-specification/blob/v1.25.0/specification/configuration/sdk-environment-variables.md#span-limits
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);

// https://github.com/open-telemetry/opentelemetry-specification/blob/v1.25.0/specification/configuration/sdk-environment-variables.md#logrecord-limits
SetIntConfigValue(configuration, "OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT", value => this.LogRecordAttributeValueLengthLimit = value, null);
SetIntConfigValue(configuration, "OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT", value => this.LogRecordAttributeCountLimit = value, null);
}

/// <summary>
Expand Down Expand Up @@ -135,6 +143,38 @@ public int? SpanLinkAttributeCountLimit
}
}

/// <summary>
/// Gets or sets the maximum allowed log record attribute value size.
/// </summary>
/// <remarks>
/// Note: Overrides the <see cref="AttributeValueLengthLimit"/> setting for log records if specified.
/// </remarks>
public int? LogRecordAttributeValueLengthLimit
{
get => this.logRecordAttributeValueLengthLimitSet ? this.logRecordAttributeValueLengthLimit : this.AttributeValueLengthLimit;
set
{
this.logRecordAttributeValueLengthLimitSet = true;
this.logRecordAttributeValueLengthLimit = value;
}
}

/// <summary>
/// Gets or sets the maximum allowed log record attribute count.
/// </summary>
/// <remarks>
/// Note: Overrides the <see cref="AttributeCountLimit"/> setting for log records if specified.
/// </remarks>
public int? LogRecordAttributeCountLimit
{
get => this.logRecordAttributeCountLimitSet ? this.logRecordAttributeCountLimit : this.AttributeCountLimit;
set
{
this.logRecordAttributeCountLimitSet = true;
this.logRecordAttributeCountLimit = value;
}
}

private static void SetIntConfigValue(IConfiguration configuration, string key, Action<int?> setter, int? defaultValue)
{
if (configuration.TryGetIntValue(key, out var result))
Expand Down
11 changes: 9 additions & 2 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,14 @@ specification](https://github.com/open-telemetry/opentelemetry-specification/blo

The following environment variables can be used to override the default
values of the attribute limits
(following the [OpenTelemetry specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.15.0/specification/sdk-environment-variables.md#attribute-limits)).
(following the [OpenTelemetry specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.25.0/specification/configuration/sdk-environment-variables.md#attribute-limits)).

* `OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT`
* `OTEL_ATTRIBUTE_COUNT_LIMIT`

The following environment variables can be used to override the default
values of the span limits
(following the [OpenTelemetry specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.15.0/specification/sdk-environment-variables.md#span-limits)).
(following the [OpenTelemetry specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.25.0/specification/configuration/sdk-environment-variables.md#span-limits)).

* `OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT`
* `OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT`
Expand All @@ -213,6 +213,13 @@ values of the span limits
* `OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT`
* `OTEL_LINK_ATTRIBUTE_COUNT_LIMIT`

The following environment variables can be used to override the default
values of the log record limits
(following the [OpenTelemetry specification](https://github.com/open-telemetry/opentelemetry-specification/blob/v1.25.0/specification/configuration/sdk-environment-variables.md#logrecord-limits)).

* `OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT`
* `OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT`

## Configure HttpClient

The `HttpClientFactory` option is provided on `OtlpExporterOptions` for users
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public void SdkLimitOptionsDefaults()
Assert.Equal(128, options.SpanLinkCountLimit);
Assert.Equal(128, options.SpanEventAttributeCountLimit);
Assert.Equal(128, options.SpanLinkAttributeCountLimit);
Assert.Null(options.LogRecordAttributeValueLengthLimit);
Assert.Equal(128, options.LogRecordAttributeCountLimit);
}

[Fact]
Expand Down Expand Up @@ -79,14 +81,19 @@ public void SpanAttributeValueLengthLimitFallback()
options.AttributeValueLengthLimit = 10;
Assert.Equal(10, options.AttributeValueLengthLimit);
Assert.Equal(10, options.SpanAttributeValueLengthLimit);
Assert.Equal(10, options.LogRecordAttributeValueLengthLimit);

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

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

[Fact]
Expand All @@ -99,6 +106,7 @@ public void SpanAttributeCountLimitFallback()
Assert.Equal(10, options.SpanAttributeCountLimit);
Assert.Equal(10, options.SpanEventAttributeCountLimit);
Assert.Equal(10, options.SpanLinkAttributeCountLimit);
Assert.Equal(10, options.LogRecordAttributeCountLimit);

options.SpanAttributeCountLimit = 20;
Assert.Equal(10, options.AttributeCountLimit);
Expand Down Expand Up @@ -150,6 +158,8 @@ public void SdkLimitOptionsUsingIConfiguration()
["OTEL_SPAN_LINK_COUNT_LIMIT"] = "28",
["OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT"] = "29",
["OTEL_LINK_ATTRIBUTE_COUNT_LIMIT"] = "30",
["OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT"] = "31",
["OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT"] = "32",
};

var configuration = new ConfigurationBuilder()
Expand All @@ -166,6 +176,8 @@ public void SdkLimitOptionsUsingIConfiguration()
Assert.Equal(28, options.SpanLinkCountLimit);
Assert.Equal(29, options.SpanEventAttributeCountLimit);
Assert.Equal(30, options.SpanLinkAttributeCountLimit);
Assert.Equal(31, options.LogRecordAttributeValueLengthLimit);
Assert.Equal(32, options.LogRecordAttributeCountLimit);
}

private static void ClearEnvVars()
Expand All @@ -178,5 +190,7 @@ private static void ClearEnvVars()
Environment.SetEnvironmentVariable("OTEL_SPAN_LINK_COUNT_LIMIT", null);
Environment.SetEnvironmentVariable("OTEL_EVENT_ATTRIBUTE_COUNT_LIMIT", null);
Environment.SetEnvironmentVariable("OTEL_LINK_ATTRIBUTE_COUNT_LIMIT", null);
Environment.SetEnvironmentVariable("OTEL_LOGRECORD_ATTRIBUTE_VALUE_LENGTH_LIMIT", null);
Environment.SetEnvironmentVariable("OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT", null);
}
}

0 comments on commit 6e2f442

Please sign in to comment.