use of io.opentelemetry.api.metrics.DoubleHistogram 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");
}
use of io.opentelemetry.api.metrics.DoubleHistogram in project opentelemetry-java by open-telemetry.
the class SdkMeterProviderTest method collectAllSyncInstruments.
@Test
void collectAllSyncInstruments() {
InMemoryMetricReader sdkMeterReader = InMemoryMetricReader.create();
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("testLongHistogram").ofLongs().build();
longValueRecorder.record(10, Attributes.empty());
DoubleCounter doubleCounter = sdkMeter.counterBuilder("testDoubleCounter").ofDoubles().build();
doubleCounter.add(10.1, Attributes.empty());
DoubleUpDownCounter doubleUpDownCounter = sdkMeter.upDownCounterBuilder("testDoubleUpDownCounter").ofDoubles().build();
doubleUpDownCounter.add(-10.1, Attributes.empty());
DoubleHistogram doubleValueRecorder = sdkMeter.histogramBuilder("testDoubleHistogram").build();
doubleValueRecorder.record(10.1, Attributes.empty());
assertThat(sdkMeterReader.collectAllMetrics()).allSatisfy(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasDescription("").hasUnit("1")).satisfiesExactlyInAnyOrder(metric -> assertThat(metric).hasName("testDoubleHistogram").hasDoubleHistogram().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasCount(1).hasSum(10.1).hasBucketCounts(0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), metric -> assertThat(metric).hasName("testDoubleCounter").hasDoubleSum().isMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasValue(10.1)), metric -> assertThat(metric).hasName("testLongHistogram").hasDoubleHistogram().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasCount(1).hasSum(10).hasBucketCounts(0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), metric -> assertThat(metric).hasName("testLongUpDownCounter").hasLongSum().isNotMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasValue(-10)), metric -> assertThat(metric).hasName("testLongCounter").hasLongSum().isMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasValue(10)), metric -> assertThat(metric).hasName("testDoubleUpDownCounter").hasDoubleSum().isNotMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasValue(-10.1)));
}
use of io.opentelemetry.api.metrics.DoubleHistogram in project opentelemetry-java by open-telemetry.
the class SdkMeterProviderTest method viewSdk_AllowMulitpleViewsPerSynchronousInstrument.
@Test
void viewSdk_AllowMulitpleViewsPerSynchronousInstrument() {
InstrumentSelector selector = InstrumentSelector.builder().setType(InstrumentType.HISTOGRAM).setName("test").build();
InMemoryMetricReader reader = InMemoryMetricReader.create();
SdkMeterProvider provider = sdkMeterProviderBuilder.registerMetricReader(reader).registerView(selector, View.builder().setName("not_test").setDescription("not_desc").setAggregation(Aggregation.lastValue()).build()).registerView(selector, View.builder().setName("not_test_2").setDescription("not_desc_2").setAggregation(Aggregation.sum()).build()).build();
Meter meter = provider.get(SdkMeterProviderTest.class.getName());
DoubleHistogram histogram = meter.histogramBuilder("test").setDescription("desc").setUnit("unit").build();
histogram.record(1.0);
assertThat(reader.collectAllMetrics()).satisfiesExactlyInAnyOrder(metric -> assertThat(metric).hasName("not_test").hasDescription("not_desc").hasUnit("unit").hasDoubleGauge(), metric -> assertThat(metric).hasName("not_test_2").hasDescription("not_desc_2").hasUnit("unit").hasDoubleSum());
}
use of io.opentelemetry.api.metrics.DoubleHistogram in project opentelemetry-java by open-telemetry.
the class SdkDoubleHistogramTest method boundDoubleHistogramRecord_MonotonicityCheck.
@Test
@SuppressLogger(SdkDoubleHistogram.class)
void boundDoubleHistogramRecord_MonotonicityCheck() {
DoubleHistogram histogram = sdkMeter.histogramBuilder("testHistogram").build();
BoundDoubleHistogram bound = ((SdkDoubleHistogram) histogram).bind(Attributes.empty());
try {
bound.record(-9);
assertThat(sdkMeterReader.collectAllMetrics()).hasSize(0);
logs.assertContains("Histograms can only record non-negative values. Instrument testHistogram has recorded a negative value.");
} finally {
bound.unbind();
}
}
use of io.opentelemetry.api.metrics.DoubleHistogram in project opentelemetry-java by open-telemetry.
the class SdkDoubleHistogramTest method collectMetrics_NoRecords.
@Test
void collectMetrics_NoRecords() {
DoubleHistogram doubleRecorder = sdkMeter.histogramBuilder("testRecorder").build();
BoundDoubleHistogram bound = ((SdkDoubleHistogram) doubleRecorder).bind(Attributes.builder().put("key", "value").build());
try {
assertThat(sdkMeterReader.collectAllMetrics()).isEmpty();
} finally {
bound.unbind();
}
}
Aggregations