use of io.opentelemetry.api.metrics.LongCounter in project opentelemetry-java by open-telemetry.
the class SdkMeterProviderTest method viewSdk_capturesBaggageFromContext.
@Test
void viewSdk_capturesBaggageFromContext() {
InstrumentSelector selector = InstrumentSelector.builder().setType(InstrumentType.COUNTER).setName("test").build();
InMemoryMetricReader reader = InMemoryMetricReader.create();
SdkMeterProvider provider = sdkMeterProviderBuilder.registerMetricReader(reader).registerView(selector, ((ViewBuilderImpl) View.builder().setAggregation(Aggregation.sum())).appendAllBaggageAttributes().build()).build();
Meter meter = provider.get(SdkMeterProviderTest.class.getName());
Baggage baggage = Baggage.builder().put("baggage", "value").build();
Context context = Context.root().with(baggage);
LongCounter counter = meter.counterBuilder("test").build();
// Make sure whether or not we explicitly pass baggage, all values have it appended.
counter.add(1, Attributes.empty(), context);
// Also check implicit context
try (Scope ignored = context.makeCurrent()) {
counter.add(1, Attributes.empty());
}
// Now make sure all metrics have baggage appended.
// Implicitly we should have ONLY ONE metric data point that has baggage appended.
assertThat(reader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasName("test").hasLongSum().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasAttributes(Attributes.builder().put("baggage", "value").build())));
}
use of io.opentelemetry.api.metrics.LongCounter 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.LongCounter in project opentelemetry-java by open-telemetry.
the class SdkLongCounterTest method collectMetrics_NoRecords.
@Test
void collectMetrics_NoRecords() {
LongCounter longCounter = sdkMeter.counterBuilder("Counter").build();
BoundLongCounter bound = ((SdkLongCounter) longCounter).bind(Attributes.builder().put("foo", "bar").build());
try {
assertThat(sdkMeterReader.collectAllMetrics()).isEmpty();
} finally {
bound.unbind();
}
}
use of io.opentelemetry.api.metrics.LongCounter in project opentelemetry-java by open-telemetry.
the class SdkLongCounterTest method collectMetrics_WithEmptyAttributes.
@Test
void collectMetrics_WithEmptyAttributes() {
LongCounter longCounter = sdkMeter.counterBuilder("testCounter").setDescription("description").setUnit("By").build();
testClock.advance(Duration.ofNanos(SECOND_NANOS));
longCounter.add(12, Attributes.empty());
longCounter.add(12);
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testCounter").hasDescription("description").hasUnit("By").hasLongSum().isMonotonic().isCumulative().points().satisfiesExactly(point -> assertThat(point).hasStartEpochNanos(testClock.now() - SECOND_NANOS).hasEpochNanos(testClock.now()).hasAttributes(Attributes.empty()).hasValue(24)));
}
use of io.opentelemetry.api.metrics.LongCounter in project opentelemetry-java by open-telemetry.
the class SdkLongCounterTest method stressTest.
@Test
void stressTest() {
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(2_000, 1, new OperationUpdaterDirectCall(longCounter, "K", "V")));
stressTestBuilder.addOperation(StressTestRunner.Operation.create(2_000, 1, new OperationUpdaterWithBinding(((SdkLongCounter) longCounter).bind(Attributes.builder().put("K", "V").build()))));
}
stressTestBuilder.build().run();
assertThat(sdkMeterReader.collectAllMetrics()).satisfiesExactly(metric -> assertThat(metric).hasResource(RESOURCE).hasInstrumentationLibrary(INSTRUMENTATION_LIBRARY_INFO).hasName("testCounter").hasLongSum().isCumulative().isMonotonic().points().satisfiesExactlyInAnyOrder(point -> assertThat(point).hasStartEpochNanos(testClock.now()).hasEpochNanos(testClock.now()).hasValue(160_000).attributes().hasSize(1).containsEntry("K", "V")));
}
Aggregations