Class MetricAttributesData
Attributes and optionally a LongAdder
for each of that attributes for high-throughput metric recording scenarios.
This class is used by MetricEntityState subclasses to cache both the attributes
and the accumulator for observable counter metrics (MetricType.ASYNC_COUNTER_FOR_HIGH_PERF_CASES
and MetricType.ASYNC_UP_DOWN_COUNTER_FOR_HIGH_PERF_CASES). For other metric types,
only the attributes are used and the adder remains null.
The LongAdder provides high-throughput recording capability by minimizing contention
across threads. The accumulated value is read during OpenTelemetry's metric collection callback
via sum() (for ObservableLongCounter/UpDownCounter callbacks that must report cumulative values).
-
Constructor Summary
ConstructorsConstructorDescriptionMetricAttributesData(io.opentelemetry.api.common.Attributes attributes) Creates a MetricAttributesData with only attributes (for non-observable-counter metrics).MetricAttributesData(io.opentelemetry.api.common.Attributes attributes, boolean createAdder) Creates a MetricAttributesData with attributes and optionally a LongAdder. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(long value) Adds the given value to the internal LongAdder.io.opentelemetry.api.common.AttributesReturns the OpenTelemetry attributes.booleanhasAdder()Returns whether this holder has a LongAdder for high-throughput recording.longsum()Returns the current cumulative sum without resetting.
-
Constructor Details
-
MetricAttributesData
public MetricAttributesData(io.opentelemetry.api.common.Attributes attributes) Creates a MetricAttributesData with only attributes (for non-observable-counter metrics).- Parameters:
attributes- the OpenTelemetry attributes for this metric dimension combination
-
MetricAttributesData
public MetricAttributesData(io.opentelemetry.api.common.Attributes attributes, boolean createAdder) Creates a MetricAttributesData with attributes and optionally a LongAdder.- Parameters:
attributes- the OpenTelemetry attributes for this metric dimension combinationcreateAdder- if true, creates a LongAdder for high-throughput recording
-
-
Method Details
-
getAttributes
public io.opentelemetry.api.common.Attributes getAttributes()Returns the OpenTelemetry attributes. -
hasAdder
public boolean hasAdder()Returns whether this holder has a LongAdder for high-throughput recording. -
add
public void add(long value) Adds the given value to the internal LongAdder. This is a fast operation optimized for high contention scenarios. Only call this for observable counter metrics where adder is guaranteed non-null.- Parameters:
value- the value to add
-
sum
public long sum()Returns the current cumulative sum without resetting. This is the correct method to use in OpenTelemetry's ObservableLongCounter/UpDownCounter callbacks, which must report cumulative values per the OTel spec. The SDK handles delta computation internally based on the configured aggregation temporality.Using
sumThenReset()in observable counter callbacks causes the SDK to compute delta-of-delta (because it subtracts the previous observation from the current one, butsumThenReset()already returns a delta), producing negative counter values when traffic varies between collection intervals.- Returns:
- the current cumulative sum
-