Skip to content

Commit fb8e549

Browse files
authored
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
1 parent af6d159 commit fb8e549

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/Elastic.Apm/OpenTelemetry/ElasticActivityListener.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,20 @@ private static void UpdateOTelAttributes(Activity activity, OTel otel)
228228
{
229229
if (!activity.TagObjects.Any()) return;
230230

231+
// https://opentelemetry.io/docs/specs/otel/common/#attribute-limits
232+
// copy max 128 keys and truncate values to 10k chars (the current maximum for e.g. statement.db).
233+
var i = 0;
231234
otel.Attributes ??= new Dictionary<string, object>();
232235
foreach (var (key, value) in activity.TagObjects)
233-
otel.Attributes[key] = value;
236+
{
237+
if (i >= 128) break;
238+
239+
if (value is string s)
240+
otel.Attributes[key] = s.Truncate(10_000);
241+
else
242+
otel.Attributes[key] = value;
243+
i++;
244+
}
234245
}
235246

236247
private static void UpdateSpan(Activity activity, Span span)

0 commit comments

Comments
 (0)