Skip to content

Commit

Permalink
[SDK + Otlp] Support loading envvars from IConfiguration (#3760)
Browse files Browse the repository at this point in the history
* Updated Otlp Trace & Metrics exporters to load envvars from IConfiguration.

* Patch CHANGELOGs.

* Fix up otlp log exporter for SdkOptions changes.

* Revert SdkOptions public api.

* Restore tests.

* Fix benchmarks.

* SdkLimitOptions IConfiguration test.

* OtlpExporterOptions IConfiguration test.

* MetricReaderOptions IConfiguration test.

* Bug fix.

* Nit.
  • Loading branch information
CodeBlanch committed Oct 17, 2022
1 parent 727aab0 commit 1a2103d
Show file tree
Hide file tree
Showing 26 changed files with 459 additions and 271 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 @@ -16,6 +16,10 @@ Released 2022-Oct-17
are configured via the environment variables defined in the specification.
([#3684](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3684))

* Added support for loading environment variables from `IConfiguration` when
using the `AddOtlpExporter` extensions
([#3760](https://github.com/open-telemetry/opentelemetry-dotnet/pull/3760))

## 1.4.0-beta.1

Released 2022-Sep-29
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
using System.Runtime.CompilerServices;
using Google.Protobuf;
using Google.Protobuf.Collections;
using OpenTelemetry.Configuration;
using OpenTelemetry.Internal;
using OpenTelemetry.Proto.Collector.Trace.V1;
using OpenTelemetry.Proto.Common.V1;
Expand All @@ -42,6 +41,7 @@ internal static class ActivityExtensions

internal static void AddBatch(
this ExportTraceServiceRequest request,
SdkLimitOptions sdkLimitOptions,
Resource processResource,
in Batch<Activity> activityBatch)
{
Expand All @@ -54,7 +54,7 @@ internal static class ActivityExtensions

foreach (var activity in activityBatch)
{
Span span = activity.ToOtlpSpan();
Span span = activity.ToOtlpSpan(sdkLimitOptions);
if (span == null)
{
OpenTelemetryProtocolExporterEventSource.Log.CouldNotTranslateActivity(
Expand Down Expand Up @@ -116,7 +116,7 @@ internal static ScopeSpans GetSpanListFromPool(string name, string version)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static Span ToOtlpSpan(this Activity activity)
internal static Span ToOtlpSpan(this Activity activity, SdkLimitOptions sdkLimitOptions)
{
if (activity.IdFormat != ActivityIdFormat.W3C)
{
Expand Down Expand Up @@ -157,9 +157,10 @@ internal static Span ToOtlpSpan(this Activity activity)

TagEnumerationState otlpTags = new()
{
SdkLimitOptions = sdkLimitOptions,
Span = otlpSpan,
};
otlpTags.EnumerateTags(activity, SdkConfiguration.Instance.SpanAttributeCountLimit ?? int.MaxValue);
otlpTags.EnumerateTags(activity, sdkLimitOptions.SpanAttributeCountLimit ?? int.MaxValue);

if (activity.Kind == ActivityKind.Client || activity.Kind == ActivityKind.Producer)
{
Expand All @@ -180,15 +181,17 @@ internal static Span ToOtlpSpan(this Activity activity)

EventEnumerationState otlpEvents = new()
{
SdkLimitOptions = sdkLimitOptions,
Span = otlpSpan,
};
otlpEvents.EnumerateEvents(activity, SdkConfiguration.Instance.SpanEventCountLimit ?? int.MaxValue);
otlpEvents.EnumerateEvents(activity, sdkLimitOptions.SpanEventCountLimit ?? int.MaxValue);

LinkEnumerationState otlpLinks = new()
{
SdkLimitOptions = sdkLimitOptions,
Span = otlpSpan,
};
otlpLinks.EnumerateLinks(activity, SdkConfiguration.Instance.SpanLinkCountLimit ?? int.MaxValue);
otlpLinks.EnumerateLinks(activity, sdkLimitOptions.SpanLinkCountLimit ?? int.MaxValue);

return otlpSpan;
}
Expand Down Expand Up @@ -236,7 +239,7 @@ private static OtlpTrace.Status ToOtlpStatus(this Activity activity, ref TagEnum
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Span.Types.Link ToOtlpLink(in ActivityLink activityLink)
private static Span.Types.Link ToOtlpLink(in ActivityLink activityLink, SdkLimitOptions sdkLimitOptions)
{
byte[] traceIdBytes = new byte[16];
byte[] spanIdBytes = new byte[8];
Expand All @@ -250,10 +253,10 @@ private static Span.Types.Link ToOtlpLink(in ActivityLink activityLink)
SpanId = UnsafeByteOperations.UnsafeWrap(spanIdBytes),
};

int maxTags = SdkConfiguration.Instance.LinkAttributeCountLimit ?? int.MaxValue;
int maxTags = sdkLimitOptions.SpanLinkAttributeCountLimit ?? int.MaxValue;
foreach (ref readonly var tag in activityLink.EnumerateTagObjects())
{
if (OtlpKeyValueTransformer.Instance.TryTransformTag(tag, out var attribute, SdkConfiguration.Instance.AttributeValueLengthLimit))
if (OtlpKeyValueTransformer.Instance.TryTransformTag(tag, out var attribute, sdkLimitOptions.AttributeValueLengthLimit))
{
if (otlpLink.Attributes.Count < maxTags)
{
Expand All @@ -270,18 +273,18 @@ private static Span.Types.Link ToOtlpLink(in ActivityLink activityLink)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Span.Types.Event ToOtlpEvent(in ActivityEvent activityEvent)
private static Span.Types.Event ToOtlpEvent(in ActivityEvent activityEvent, SdkLimitOptions sdkLimitOptions)
{
var otlpEvent = new Span.Types.Event
{
Name = activityEvent.Name,
TimeUnixNano = (ulong)activityEvent.Timestamp.ToUnixTimeNanoseconds(),
};

int maxTags = SdkConfiguration.Instance.EventAttributeCountLimit ?? int.MaxValue;
int maxTags = sdkLimitOptions.SpanEventAttributeCountLimit ?? int.MaxValue;
foreach (ref readonly var tag in activityEvent.EnumerateTagObjects())
{
if (OtlpKeyValueTransformer.Instance.TryTransformTag(tag, out var attribute, SdkConfiguration.Instance.AttributeValueLengthLimit))
if (OtlpKeyValueTransformer.Instance.TryTransformTag(tag, out var attribute, sdkLimitOptions.AttributeValueLengthLimit))
{
if (otlpEvent.Attributes.Count < maxTags)
{
Expand Down Expand Up @@ -320,6 +323,8 @@ private static Span.Types.Event ToOtlpEvent(in ActivityEvent activityEvent)

private struct TagEnumerationState : PeerServiceResolver.IPeerServiceState
{
public SdkLimitOptions SdkLimitOptions;

public Span Span;

public string StatusCode;
Expand Down Expand Up @@ -357,7 +362,7 @@ public void EnumerateTags(Activity activity, int maxTags)
continue;
}

if (OtlpKeyValueTransformer.Instance.TryTransformTag(tag, out var attribute, SdkConfiguration.Instance.AttributeValueLengthLimit))
if (OtlpKeyValueTransformer.Instance.TryTransformTag(tag, out var attribute, this.SdkLimitOptions.AttributeValueLengthLimit))
{
if (this.Span.Attributes.Count < maxTags)
{
Expand All @@ -384,6 +389,8 @@ public void EnumerateTags(Activity activity, int maxTags)

private struct EventEnumerationState
{
public SdkLimitOptions SdkLimitOptions;

public Span Span;

public void EnumerateEvents(Activity activity, int maxEvents)
Expand All @@ -392,7 +399,7 @@ public void EnumerateEvents(Activity activity, int maxEvents)
{
if (this.Span.Events.Count < maxEvents)
{
this.Span.Events.Add(ToOtlpEvent(in @event));
this.Span.Events.Add(ToOtlpEvent(in @event, this.SdkLimitOptions));
}
else
{
Expand All @@ -404,6 +411,8 @@ public void EnumerateEvents(Activity activity, int maxEvents)

private struct LinkEnumerationState
{
public SdkLimitOptions SdkLimitOptions;

public Span Span;

public void EnumerateLinks(Activity activity, int maxLinks)
Expand All @@ -412,7 +421,7 @@ public void EnumerateLinks(Activity activity, int maxLinks)
{
if (this.Span.Links.Count < maxLinks)
{
this.Span.Links.Add(ToOtlpLink(in link));
this.Span.Links.Add(ToOtlpLink(in link, this.SdkLimitOptions));
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
using System.Runtime.CompilerServices;
using Google.Protobuf;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Configuration;
using OpenTelemetry.Internal;
using OpenTelemetry.Logs;
using OpenTelemetry.Trace;
Expand All @@ -39,6 +38,7 @@ internal static class LogRecordExtensions

internal static void AddBatch(
this OtlpCollector.ExportLogsServiceRequest request,
SdkLimitOptions sdkLimitOptions,
OtlpResource.Resource processResource,
in Batch<LogRecord> logRecordBatch)
{
Expand All @@ -53,7 +53,7 @@ internal static class LogRecordExtensions

foreach (var logRecord in logRecordBatch)
{
var otlpLogRecord = logRecord.ToOtlpLog();
var otlpLogRecord = logRecord.ToOtlpLog(sdkLimitOptions);
if (otlpLogRecord != null)
{
scopeLogs.LogRecords.Add(otlpLogRecord);
Expand All @@ -62,7 +62,7 @@ internal static class LogRecordExtensions
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord)
internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord, SdkLimitOptions sdkLimitOptions)
{
OtlpLogs.LogRecord otlpLogRecord = null;

Expand All @@ -75,8 +75,8 @@ internal static OtlpLogs.LogRecord ToOtlpLog(this LogRecord logRecord)
SeverityText = LogLevels[(int)logRecord.LogLevel],
};

var attributeValueLengthLimit = SdkConfiguration.Instance.AttributeValueLengthLimit;
var attributeCountLimit = SdkConfiguration.Instance.AttributeCountLimit ?? int.MaxValue;
var attributeValueLengthLimit = sdkLimitOptions.AttributeValueLengthLimit;
var attributeCountLimit = sdkLimitOptions.AttributeCountLimit ?? int.MaxValue;

// First add the generic attributes like category, eventid and exception, so they are less likely being dropped because of AttributeCountLimit

Expand Down
Loading

0 comments on commit 1a2103d

Please sign in to comment.