Skip to content

Commit

Permalink
Limit attribute count and truncate values in ElasticActivityListener
Browse files Browse the repository at this point in the history
Implement limits to comply with OpenTelemetry specs, capping attributes at 128 and truncating string values to 10,000 characters. This ensures payload size remains manageable and adheres to the defined standards.

https://opentelemetry.io/docs/specs/otel/common/#attribute-limits
  • Loading branch information
Mpdreamz committed Oct 7, 2024
1 parent af6d159 commit 36eb68c
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Elastic.Apm/OpenTelemetry/ElasticActivityListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,20 @@ private static void UpdateOTelAttributes(Activity activity, OTel otel)
{
if (!activity.TagObjects.Any()) return;

// https://opentelemetry.io/docs/specs/otel/common/#attribute-limits
// copy max 128 keys and truncate values to 10k chars (the current maximum for e.g. statement.db).
var i = 0;
otel.Attributes ??= new Dictionary<string, object>();
foreach (var (key, value) in activity.TagObjects)
otel.Attributes[key] = value;
{
if (i >= 128) break;

if (value is string s)
otel.Attributes[key] = s.Truncate(10_000);
else
otel.Attributes[key] = value;
i++;
}
}

private static void UpdateSpan(Activity activity, Span span)
Expand Down

0 comments on commit 36eb68c

Please sign in to comment.