use of io.opentelemetry.api.metrics.LongCounter in project opentelemetry-java by open-telemetry.
the class SdkLongCounterTest method longCounterAdd_Monotonicity.
@Test
@SuppressLogger(SdkLongCounter.class)
void longCounterAdd_Monotonicity() {
LongCounter longCounter = sdkMeter.counterBuilder("testCounter").build();
longCounter.add(-45);
assertThat(sdkMeterReader.collectAllMetrics()).hasSize(0);
logs.assertContains("Counters can only increase. Instrument testCounter has recorded a negative value.");
}
use of io.opentelemetry.api.metrics.LongCounter in project opentelemetry-java by open-telemetry.
the class SdkLongCounterTest method stressTest_WithDifferentLabelSet.
@Test
void stressTest_WithDifferentLabelSet() {
String[] keys = { "Key_1", "Key_2", "Key_3", "Key_4" };
String[] values = { "Value_1", "Value_2", "Value_3", "Value_4" };
LongCounter longCounter = sdkMeter.counterBuilder("testCounter").build();
StressTestRunner.Builder stressTestBuilder = StressTestRunner.builder().setInstrument((SdkLongCounter) longCounter).setCollectionIntervalMs(100);
for (int i = 0; i < 4; i++) {
stressTestBuilder.addOperation(StressTestRunner.Operation.create(1_000, 2, new OperationUpdaterDirectCall(longCounter, keys[i], values[i])));
stressTestBuilder.addOperation(StressTestRunner.Operation.create(1_000, 2, new OperationUpdaterWithBinding(((SdkLongCounter) longCounter).bind(Attributes.builder().put(keys[i], values[i]).build()))));
}
stressTestBuilder.build().run();
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testCounter").hasLongSum().isCumulative().isMonotonic().points().allSatisfy(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasValue(20_000)).extracting(PointData::getAttributes).containsExactlyInAnyOrder(Attributes.of(stringKey(keys[0]), values[0]), Attributes.of(stringKey(keys[1]), values[1]), Attributes.of(stringKey(keys[2]), values[2]), Attributes.of(stringKey(keys[3]), values[3])));
}
use of io.opentelemetry.api.metrics.LongCounter in project opentelemetry-java by open-telemetry.
the class SdkLongCounterTest method collectMetrics_WithMultipleCollects.
@Test
void collectMetrics_WithMultipleCollects() {
long startTime = testClock.now();
LongCounter longCounter = sdkMeter.counterBuilder("testCounter").build();
BoundLongCounter bound = ((SdkLongCounter) longCounter).bind(Attributes.builder().put("K", "V").build());
try {
// Do some records using bounds and direct calls and bindings.
longCounter.add(12, Attributes.empty());
bound.add(123);
longCounter.add(21, Attributes.empty());
// Advancing time here should not matter.
testClock.advance(Duration.ofNanos(SECOND_NANOS));
bound.add(321);
longCounter.add(111, Attributes.builder().put("K", "V").build());
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testCounter").hasLongSum().isMonotonic().isCumulative().points().allSatisfy(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now())).satisfiesExactlyInAnyOrder(point -> assertThat(point).hasAttributes(Attributes.empty()).hasValue(33), point -> assertThat(point).hasAttributes(Attributes.of(stringKey("K"), "V")).hasValue(555)));
// Repeat to prove we keep previous values.
testClock.advance(Duration.ofNanos(SECOND_NANOS));
bound.add(222);
longCounter.add(11, Attributes.empty());
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testCounter").hasLongSum().isMonotonic().isCumulative().points().allSatisfy(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now())).satisfiesExactlyInAnyOrder(point -> assertThat(point).hasAttributes(Attributes.empty()).hasValue(44), point -> assertThat(point).hasAttributes(Attributes.of(stringKey("K"), "V")).hasValue(777)));
} finally {
bound.unbind();
}
}
use of io.opentelemetry.api.metrics.LongCounter in project opentelemetry-java by open-telemetry.
the class SdkLongCounterTest method boundLongCounterAdd_Monotonicity.
@Test
@SuppressLogger(SdkLongCounter.class)
void boundLongCounterAdd_Monotonicity() {
LongCounter longCounter = sdkMeter.counterBuilder("testCounter").build();
BoundLongCounter bound = ((SdkLongCounter) longCounter).bind(Attributes.empty());
try {
bound.add(-9);
assertThat(sdkMeterReader.collectAllMetrics()).hasSize(0);
logs.assertContains("Counters can only increase. Instrument testCounter has recorded a negative value.");
} finally {
bound.unbind();
}
}
use of io.opentelemetry.api.metrics.LongCounter in project opentelemetry-java by open-telemetry.
the class CardinalityTest method staleMetricsDropped_synchronousInstrument.
/**
* Records to sync instruments, with distinct attributes each time. Validates that stale metrics
* are dropped for delta and cumulative readers. Stale metrics are those with attributes that did
* not receive recordings in the most recent collection.
*
* <p>Effectively, we make sure we cap-out at attribute size = 2000 (constant in
* MetricStorageutils).
*/
@Test
void staleMetricsDropped_synchronousInstrument() {
LongCounter syncCounter = meter.counterBuilder("sync-counter").build();
// Note: This constant comes from MetricStorageUtils, but it's package-private.
for (int i = 1; i <= 2000; i++) {
syncCounter.add(1, Attributes.builder().put("key", "num_" + i).build());
// DELTA reader only has latest
assertThat(deltaReader.collectAllMetrics()).as("Delta collection " + i).hasSize(1).satisfiesExactly(metricData -> assertThat(metricData).hasName("sync-counter").hasLongSum().isDelta().points().hasSize(1));
// Make sure we preserve previous cumulatives
int currentSize = i;
assertThat(cumulativeReader.collectAllMetrics()).as("Cumulative collection " + i).hasSize(1).satisfiesExactly(metricData -> assertThat(metricData).hasName("sync-counter").hasLongSum().isCumulative().points().hasSize(currentSize));
}
// Now punch the limit and ONLY metrics we just recorded stay, due to simplistic GC.
for (int i = 2001; i <= 2010; i++) {
syncCounter.add(1, Attributes.builder().put("key", "num_" + i).build());
}
assertThat(deltaReader.collectAllMetrics()).as("Delta collection - post limit @ 10").hasSize(1).satisfiesExactly(metricData -> assertThat(metricData).hasName("sync-counter").hasLongSum().isDelta().points().hasSize(10));
assertThat(cumulativeReader.collectAllMetrics()).as("Cumulative collection - post limit @ 10").hasSize(1).satisfiesExactly(metricData -> assertThat(metricData).hasName("sync-counter").hasLongSum().isCumulative().points().hasSize(10));
}
Aggregations