use of io.opentelemetry.api.metrics.LongCounter in project opentelemetry-java by open-telemetry.
the class SdkMeterTest method testLongCounter.
@Test
void testLongCounter() {
LongCounter longCounter = sdkMeter.counterBuilder("testLongCounter").setDescription("My very own counter").setUnit("metric tonnes").build();
assertThat(longCounter).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 a warning.
sdkMeter.counterBuilder("testLongCounter").setDescription("My very own counter").setUnit("metric tonnes").build();
assertThat(logs.getEvents()).isEmpty();
sdkMeter.counterBuilder("testLongCounter").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.LongCounter in project opentelemetry-java-instrumentation by open-telemetry.
the class MeterTest method longCounter.
@Test
void longCounter() {
LongCounter instrument = meter.counterBuilder("test").setDescription("d").setUnit("u").build();
instrument.add(5, Attributes.of(AttributeKey.stringKey("q"), "r"));
instrument.add(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")).hasLongSum().isMonotonic().points().satisfiesExactly(point -> assertThat(point).hasValue(11).attributes().containsOnly(attributeEntry("q", "r")))));
}
use of io.opentelemetry.api.metrics.LongCounter in project besu by hyperledger.
the class OpenTelemetrySystem method createLabelledCounter.
@Override
public LabelledMetric<Counter> createLabelledCounter(final MetricCategory category, final String name, final String help, final String... labelNames) {
LOG.trace("Creating a counter {}", name);
return cachedCounters.computeIfAbsent(name, (k) -> {
if (isCategoryEnabled(category)) {
final Meter meter = meterSdkProvider.get(category.getName());
final LongCounter counter = meter.counterBuilder(name).setDescription(help).build();
return new OpenTelemetryCounter(counter, labelNames);
} else {
return NoOpMetricsSystem.getCounterLabelledMetric(labelNames.length);
}
});
}
use of io.opentelemetry.api.metrics.LongCounter 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.LongCounter in project opentelemetry-java by open-telemetry.
the class SdkMeterProviderTest method sdkMeterProvider_supportsMultipleCollectorsDelta.
@Test
void sdkMeterProvider_supportsMultipleCollectorsDelta() {
// Note: we use a view to do delta aggregation, but any view ALWAYS uses double-precision right
// now.
InMemoryMetricReader collector1 = InMemoryMetricReader.createDelta();
InMemoryMetricReader collector2 = InMemoryMetricReader.createDelta();
SdkMeterProvider meterProvider = sdkMeterProviderBuilder.registerMetricReader(collector1).registerMetricReader(collector2).registerView(InstrumentSelector.builder().setType(InstrumentType.COUNTER).setName("testSum").build(), View.builder().setAggregation(Aggregation.sum()).build()).build();
Meter sdkMeter = meterProvider.get(SdkMeterProviderTest.class.getName());
LongCounter counter = sdkMeter.counterBuilder("testSum").build();
long startTime = testClock.now();
counter.add(1L);
testClock.advance(Duration.ofSeconds(1));
assertThat(collector1.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasName("testSum").hasLongSum().isDelta().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now()).hasValue(1)));
long collectorOneTimeOne = testClock.now();
counter.add(1L);
testClock.advance(Duration.ofSeconds(1));
// Make sure collector 2 sees the value collector 1 saw
assertThat(collector2.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasName("testSum").hasLongSum().isDelta().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(startTime).hasEpochNanos(testClock.now()).hasValue(2)));
// Make sure Collector 1 sees the same point as 2, when it collects.
assertThat(collector1.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasName("testSum").hasLongSum().isDelta().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(collectorOneTimeOne).hasEpochNanos(testClock.now()).hasValue(1)));
}
Aggregations