From fb8e5494175e4924ab362a74a218031de07f7ad3 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Tue, 8 Oct 2024 12:12:37 +0200 Subject: [PATCH] Limit attribute count and truncate values in ElasticActivityListener (#2461) 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 --- .../OpenTelemetry/ElasticActivityListener.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Elastic.Apm/OpenTelemetry/ElasticActivityListener.cs b/src/Elastic.Apm/OpenTelemetry/ElasticActivityListener.cs index 0ebbe1fdc..d045e8ceb 100644 --- a/src/Elastic.Apm/OpenTelemetry/ElasticActivityListener.cs +++ b/src/Elastic.Apm/OpenTelemetry/ElasticActivityListener.cs @@ -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(); 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)