use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.
the class DoubleHistogramAggregatorTest method diffAccumulation.
@Test
void diffAccumulation() {
Attributes attributes = Attributes.builder().put("test", "value").build();
ExemplarData exemplar = DoubleExemplarData.create(attributes, 2L, SpanContext.create("00000000000000000000000000000001", "0000000000000002", TraceFlags.getDefault(), TraceState.getDefault()), 1);
List<ExemplarData> exemplars = Collections.singletonList(exemplar);
List<ExemplarData> previousExemplars = Collections.singletonList(DoubleExemplarData.create(attributes, 1L, SpanContext.create("00000000000000000000000000000001", "0000000000000002", TraceFlags.getDefault(), TraceState.getDefault()), 2));
HistogramAccumulation previousAccumulation = HistogramAccumulation.create(2, new long[] { 1, 1, 2 }, previousExemplars);
HistogramAccumulation nextAccumulation = HistogramAccumulation.create(5, new long[] { 2, 2, 2 }, exemplars);
// Assure most recent exemplars are kept.
assertThat(aggregator.diff(previousAccumulation, nextAccumulation)).isEqualTo(HistogramAccumulation.create(3, new long[] { 1, 1, 0 }, exemplars));
}
use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.
the class DoubleSumAggregatorTest method testExemplarsInAccumulation.
@Test
void testExemplarsInAccumulation() {
Attributes attributes = Attributes.builder().put("test", "value").build();
ExemplarData exemplar = DoubleExemplarData.create(attributes, 2L, SpanContext.create("00000000000000000000000000000001", "0000000000000002", TraceFlags.getDefault(), TraceState.getDefault()), 1);
List<ExemplarData> exemplars = Collections.singletonList(exemplar);
Mockito.when(reservoir.collectAndReset(Attributes.empty())).thenReturn(exemplars);
DoubleSumAggregator aggregator = new DoubleSumAggregator(InstrumentDescriptor.create("instrument_name", "instrument_description", "instrument_unit", InstrumentType.COUNTER, InstrumentValueType.DOUBLE), () -> reservoir);
AggregatorHandle<DoubleAccumulation> aggregatorHandle = aggregator.createHandle();
aggregatorHandle.recordDouble(0, attributes, Context.root());
assertThat(aggregatorHandle.accumulateThenReset(Attributes.empty())).isEqualTo(DoubleAccumulation.create(0, exemplars));
}
use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.
the class DoubleSumAggregatorTest method mergeAndDiff.
@Test
void mergeAndDiff() {
Attributes attributes = Attributes.builder().put("test", "value").build();
ExemplarData exemplar = DoubleExemplarData.create(attributes, 2L, SpanContext.create("00000000000000000000000000000001", "0000000000000002", TraceFlags.getDefault(), TraceState.getDefault()), 1);
List<ExemplarData> exemplars = Collections.singletonList(exemplar);
List<ExemplarData> previousExemplars = Collections.singletonList(DoubleExemplarData.create(attributes, 1L, SpanContext.create("00000000000000000000000000000001", "0000000000000002", TraceFlags.getDefault(), TraceState.getDefault()), 2));
for (InstrumentType instrumentType : InstrumentType.values()) {
for (AggregationTemporality temporality : AggregationTemporality.values()) {
DoubleSumAggregator aggregator = new DoubleSumAggregator(InstrumentDescriptor.create("name", "description", "unit", instrumentType, InstrumentValueType.LONG), ExemplarReservoir::noSamples);
DoubleAccumulation merged = aggregator.merge(DoubleAccumulation.create(1.0d, previousExemplars), DoubleAccumulation.create(2.0d, exemplars));
assertThat(merged.getValue()).withFailMessage("Invalid merge result for instrumentType %s, temporality %s: %s", instrumentType, temporality, merged).isEqualTo(3.0d);
assertThat(merged.getExemplars()).containsExactly(exemplar);
DoubleAccumulation diffed = aggregator.diff(DoubleAccumulation.create(1d), DoubleAccumulation.create(2d, exemplars));
assertThat(diffed.getValue()).withFailMessage("Invalid diff result for instrumentType %s, temporality %s: %s", instrumentType, temporality, merged).isEqualTo(1d);
assertThat(diffed.getExemplars()).containsExactly(exemplar);
}
}
}
use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.
the class SdkObservableDoubleUpDownCounterTest method collectMetrics_DeltaSumAggregator.
@Test
void collectMetrics_DeltaSumAggregator() {
InMemoryMetricReader sdkMeterReader = InMemoryMetricReader.createDelta();
SdkMeterProvider sdkMeterProvider = sdkMeterProviderBuilder.registerMetricReader(sdkMeterReader).registerView(InstrumentSelector.builder().setType(InstrumentType.OBSERVABLE_UP_DOWN_COUNTER).build(), View.builder().setAggregation(Aggregation.sum()).build()).build();
sdkMeterProvider.get(getClass().getName()).upDownCounterBuilder("testObserver").ofDoubles().buildWithCallback(result -> result.record(12.1d, Attributes.builder().put("k", "v").build()));
testClock.advance(Duration.ofNanos(SECOND_NANOS));
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testObserver").hasDoubleSum().isDelta().isNotMonotonic().points().satisfiesExactlyInAnyOrder(point -> assertThat(point).hasStartEpochNanos(testClock.now() - SECOND_NANOS).hasEpochNanos(testClock.now()).hasValue(12.1).attributes().hasSize(1).containsEntry("k", "v")));
testClock.advance(Duration.ofNanos(SECOND_NANOS));
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testObserver").hasDoubleSum().isDelta().isNotMonotonic().points().satisfiesExactlyInAnyOrder(point -> assertThat(point).hasStartEpochNanos(testClock.now() - SECOND_NANOS).hasEpochNanos(testClock.now()).hasValue(0).attributes().hasSize(1).containsEntry("k", "v")));
}
use of io.opentelemetry.api.common.Attributes in project opentelemetry-java by open-telemetry.
the class SdkObservableLongCounterTest method collectMetrics_WithOneRecord.
@Test
void collectMetrics_WithOneRecord() {
InMemoryMetricReader sdkMeterReader = InMemoryMetricReader.create();
SdkMeterProvider sdkMeterProvider = sdkMeterProviderBuilder.registerMetricReader(sdkMeterReader).build();
sdkMeterProvider.get(getClass().getName()).counterBuilder("testObserver").buildWithCallback(result -> result.record(12, Attributes.builder().put("k", "v").build()));
testClock.advance(Duration.ofNanos(SECOND_NANOS));
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testObserver").hasLongSum().isMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now() - SECOND_NANOS).hasEpochNanos(testClock.now()).hasValue(12).attributes().hasSize(1).containsEntry("k", "v")));
testClock.advance(Duration.ofNanos(SECOND_NANOS));
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testObserver").hasLongSum().isMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now() - 2 * SECOND_NANOS).hasEpochNanos(testClock.now()).hasValue(12).attributes().hasSize(1).containsEntry("k", "v")));
}
Aggregations