otel4s icon indicating copy to clipboard operation
otel4s copied to clipboard

Trace SDK: support span limits

Open iRevive opened this issue 1 year ago • 2 comments

Reference Link
Spec https://opentelemetry.io/docs/specs/otel/trace/sdk/#span-limits
Java implementation SpanLimits.java

The purpose is to keep the number of a) Attributes, b) Links, and c) Events under the configured limit. Aside from dropping the overflowing ones, we should also track how many have been dropped.

1) Design SpanLimits and SpanLimits.Builder

There is a draft implementation which can be helpful.

Tests should include the discipline's HashTest. The example - RetryPolicySuite.

2) Design a generic collection container with fixed bounds

OpenTelemetry Java, for example, keeps track of dropped attributes right into the span's (or event's or link's) state. The example, and the simplified version:

class SpanLink {
  def attributes: Attributes
  def totalAttributesCount: Int // how many attributes were submitted to be recorded
  def droppedAttribuesCount: Int = totalAttributesCount - attributes.size
}

To keep track of dropped attributes, links, or events, we can use the generic container:

sealed trait Bounded[A, C <: immutable.Iterable[x]] {
  def limit: Int // the max size
  def dropped: Int // how many elements have been dropped
  def elements: C
  def append(a: A): Bounded[A, C]
}

object Bounded {
  def attributes(limit: Int): Bounded[Attribute[_], Attributes]
  def links(limit: Int): Bounded[LinkData, Vector[LinkData]]
  def events(limit: Int): Bounded[EventData, Vector[EventData]]
}

We may bikeshed the name 🙂 e.g: Bounded, Fixed, Limited, etc.

3) Use Bounded container in the SDK implementation:

For example - https://github.com/typelevel/otel4s/pull/325/files#diff-b00a1cedc0fb0cc94255f20aea84be95187a261e07f8ad033de3375f70d43f8cR151-R160.

4) Add the number of dropped elements to the proto encoders

The SpansProtoEncoder. The proto models already have the properties and we need to populate them.

The change would be similar to:

SpanProto.Link(
  traceId = ByteString.copyFrom(data.spanContext.traceId.toArray),
  spanId = ByteString.copyFrom(data.spanContext.spanId.toArray),
  traceState = traceState,
- attributes = ProtoEncoder.encode(data.attributes),
+ attributes = ProtoEncoder.encode(data.attributes.elements),
+ droppedAttributesCount = data.attributes.dropped
)

5) Implement SpanLimitsAutoConfigure

We need this to autoconfigure the SpanLimits using configuration properties.

The example - TelemetryResourceAutoConfigure.

The configuration options - https://opentelemetry.io/docs/specs/otel/configuration/sdk-environment-variables/#span-limits.

iRevive avatar Feb 06 '24 08:02 iRevive

Hi. Can I try to implement this or is it already being done by someone?

bio-aeon avatar Jul 23 '24 22:07 bio-aeon

Hey @bio-aeon, you can work on it. Thanks in advance!

iRevive avatar Jul 24 '24 18:07 iRevive