use of io.opentelemetry.api.metrics.DoubleUpDownCounter in project opentelemetry-java by open-telemetry.
the class SdkDoubleUpDownCounterTest 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" };
DoubleUpDownCounter doubleUpDownCounter = sdkMeter.upDownCounterBuilder("testUpDownCounter").ofDoubles().build();
StressTestRunner.Builder stressTestBuilder = StressTestRunner.builder().setInstrument((SdkDoubleUpDownCounter) doubleUpDownCounter).setCollectionIntervalMs(100);
for (int i = 0; i < 4; i++) {
stressTestBuilder.addOperation(StressTestRunner.Operation.create(2_000, 1, new OperationUpdaterDirectCall(doubleUpDownCounter, keys[i], values[i])));
stressTestBuilder.addOperation(StressTestRunner.Operation.create(2_000, 1, new OperationUpdaterWithBinding(((SdkDoubleUpDownCounter) doubleUpDownCounter).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("testUpDownCounter").hasDoubleSum().isCumulative().isNotMonotonic().points().allSatisfy(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasValue(40_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.DoubleUpDownCounter in project opentelemetry-java by open-telemetry.
the class SdkDoubleUpDownCounterTest method collectMetrics_NoRecords.
@Test
void collectMetrics_NoRecords() {
DoubleUpDownCounter doubleUpDownCounter = sdkMeter.upDownCounterBuilder("testUpDownCounter").ofDoubles().build();
BoundDoubleUpDownCounter bound = ((SdkDoubleUpDownCounter) doubleUpDownCounter).bind(Attributes.builder().put("foo", "bar").build());
try {
assertThat(sdkMeterReader.collectAllMetrics()).isEmpty();
} finally {
bound.unbind();
}
}
use of io.opentelemetry.api.metrics.DoubleUpDownCounter in project opentelemetry-java by open-telemetry.
the class SdkMeterTest method testDoubleUpDownCounter.
@Test
void testDoubleUpDownCounter() {
DoubleUpDownCounter doubleUpDownCounter = sdkMeter.upDownCounterBuilder("testDoubleUpDownCounter").ofDoubles().setDescription("My very own counter").setUnit("metric tonnes").build();
assertThat(doubleUpDownCounter).isNotNull();
// Note: We no longer get the same instrument instance as these instances are lightweight
// objects backed by storage now. Here we just make sure it doesn't log an error.
sdkMeter.upDownCounterBuilder("testDoubleUpDownCounter").ofDoubles().setDescription("My very own counter").setUnit("metric tonnes").build();
assertThat(logs.getEvents()).isEmpty();
sdkMeter.upDownCounterBuilder("testDoubleUpDownCounter").ofDoubles().build();
assertThat(logs.assertContains(loggingEvent -> loggingEvent.getLevel().equals(WARN), "Failed to register metric.").getThrowable()).hasMessageContaining("Metric with same name and different descriptor already created.");
}
use of io.opentelemetry.api.metrics.DoubleUpDownCounter in project opentelemetry-java-instrumentation by open-telemetry.
the class MeterTest method doubleUpDownCounter.
@Test
void doubleUpDownCounter() {
DoubleUpDownCounter instrument = meter.upDownCounterBuilder("test").ofDoubles().setDescription("d").setUnit("u").build();
instrument.add(5.5, Attributes.of(AttributeKey.stringKey("q"), "r"));
instrument.add(6.6, Attributes.of(AttributeKey.stringKey("q"), "r"));
testing.waitAndAssertMetrics(instrumentationName, "test", metrics -> metrics.anySatisfy(metric -> assertThat(metric).hasDescription("d").hasUnit("u").hasInstrumentationLibrary(InstrumentationLibraryInfo.create(instrumentationName, "1.2.3")).hasDoubleSum().isNotMonotonic().points().satisfiesExactly(point -> assertThat(point).hasValue(12.1).attributes().containsOnly(attributeEntry("q", "r")))));
}
use of io.opentelemetry.api.metrics.DoubleUpDownCounter in project opentelemetry-java by open-telemetry.
the class SdkMeterProviderTest method collectAllSyncInstruments_DeltaHistogram.
@Test
void collectAllSyncInstruments_DeltaHistogram() {
registerViewForAllTypes(sdkMeterProviderBuilder, Aggregation.explicitBucketHistogram(Collections.emptyList()));
InMemoryMetricReader sdkMeterReader = InMemoryMetricReader.createDelta();
SdkMeterProvider sdkMeterProvider = sdkMeterProviderBuilder.registerMetricReader(sdkMeterReader).build();
Meter sdkMeter = sdkMeterProvider.get(SdkMeterProviderTest.class.getName());
LongCounter longCounter = sdkMeter.counterBuilder("testLongCounter").build();
longCounter.add(10, Attributes.empty());
LongUpDownCounter longUpDownCounter = sdkMeter.upDownCounterBuilder("testLongUpDownCounter").build();
longUpDownCounter.add(10, Attributes.empty());
LongHistogram longValueRecorder = sdkMeter.histogramBuilder("testLongValueRecorder").ofLongs().build();
longValueRecorder.record(10, Attributes.empty());
DoubleCounter doubleCounter = sdkMeter.counterBuilder("testDoubleCounter").ofDoubles().build();
doubleCounter.add(10, Attributes.empty());
DoubleUpDownCounter doubleUpDownCounter = sdkMeter.upDownCounterBuilder("testDoubleUpDownCounter").ofDoubles().build();
doubleUpDownCounter.add(10, Attributes.empty());
DoubleHistogram doubleValueRecorder = sdkMeter.histogramBuilder("testDoubleValueRecorder").build();
doubleValueRecorder.record(10, Attributes.empty());
testClock.advance(Duration.ofSeconds(1));
assertThat(sdkMeterReader.collectAllMetrics()).allSatisfy(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasDescription("").hasUnit("1").hasDoubleHistogram().isDelta().points().satisfiesExactlyInAnyOrder(point -> assertThat(point).hasStartEpochNanos(testClock.now() - 1000000000).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasBucketCounts(1))).extracting(MetricData::getName).containsExactlyInAnyOrder("testLongCounter", "testDoubleCounter", "testLongUpDownCounter", "testDoubleUpDownCounter", "testLongValueRecorder", "testDoubleValueRecorder");
testClock.advance(Duration.ofSeconds(1));
longCounter.add(10, Attributes.empty());
longUpDownCounter.add(10, Attributes.empty());
longValueRecorder.record(10, Attributes.empty());
doubleCounter.add(10, Attributes.empty());
doubleUpDownCounter.add(10, Attributes.empty());
doubleValueRecorder.record(10, Attributes.empty());
assertThat(sdkMeterReader.collectAllMetrics()).allSatisfy(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasDescription("").hasUnit("1").hasDoubleHistogram().isDelta().points().satisfiesExactlyInAnyOrder(point -> assertThat(point).hasStartEpochNanos(testClock.now() - 1000000000).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasBucketCounts(1))).extracting(MetricData::getName).containsExactlyInAnyOrder("testLongCounter", "testDoubleCounter", "testLongUpDownCounter", "testDoubleUpDownCounter", "testLongValueRecorder", "testDoubleValueRecorder");
}
Aggregations