Java Code Examples for io.micrometer.core.instrument.DistributionSummary#record()

The following examples show how to use io.micrometer.core.instrument.DistributionSummary#record() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MicrometerAtlasIntegrationTest.java    From tutorials with MIT License 7 votes vote down vote up
@Test
public void givenDistributionSummary_whenEnrichWithHistograms_thenDataAggregated() {
    SimpleMeterRegistry registry = new SimpleMeterRegistry();
    DistributionSummary hist = DistributionSummary
      .builder("summary")
      .histogram(Histogram.linear(0, 10, 5))
      .register(registry);

    hist.record(3);
    hist.record(8);
    hist.record(20);
    hist.record(40);
    hist.record(13);
    hist.record(26);

    Map<String, Integer> histograms = extractTagValueMap(registry, Type.Counter, 1.0);

    assertThat(histograms, allOf(hasEntry("bucket=0.0", 0), hasEntry("bucket=10.0", 2), hasEntry("bucket=20.0", 2), hasEntry("bucket=30.0", 1), hasEntry("bucket=40.0", 1), hasEntry("bucket=Infinity", 0)));
}
 
Example 2
Source File: DistributionSummaryTest.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@Test
@DisplayName("multiple recordings are maintained")
default void record(MeterRegistry registry) {
    DistributionSummary ds = registry.summary("my.summary");

    ds.record(10);
    clock(registry).add(step());

    ds.count();

    assertAll(() -> assertEquals(1L, ds.count()),
            () -> assertEquals(10L, ds.totalAmount()));

    ds.record(10);
    ds.record(10);
    clock(registry).add(step());

    assertAll(() -> assertTrue(ds.count() >= 2L),
            () -> assertTrue(ds.totalAmount() >= 20L));
}
 
Example 3
Source File: SkywalkingDistributionSummaryTest.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Test
public void testSimple() {
    // Creating a simplify distribution summary
    final SkywalkingMeterRegistry registry = new SkywalkingMeterRegistry();
    final DistributionSummary summary = registry.summary("test_simple_distribution_summary", "skywalking", "test");

    // Check Skywalking type
    Assert.assertTrue(summary instanceof SkywalkingDistributionSummary);
    final List<MeterId.Tag> tags = Arrays.asList(new MeterId.Tag("skywalking", "test"));

    // Multiple record data
    summary.record(10d);
    summary.record(13d);
    summary.record(2d);

    // Check micrometer data
    Assert.assertEquals(3, summary.count());
    Assert.assertEquals(25d, summary.totalAmount(), 0.0);
    Assert.assertEquals(13d, summary.max(), 0.0);

    // Check Skywalking data
    assertCounter(Whitebox.getInternalState(summary, "counter"), "test_simple_distribution_summary_count", tags, 3d);
    assertCounter(Whitebox.getInternalState(summary, "sum"), "test_simple_distribution_summary_sum", tags, 25d);
    assertGauge(Whitebox.getInternalState(summary, "max"), "test_simple_distribution_summary_max", tags, 13d);
    assertHistogramNull(Whitebox.getInternalState(summary, "histogram"));
}
 
Example 4
Source File: DropwizardMeterRegistriesTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
void filteredGaugesDoNotAffectOthers() {
    final CompositeMeterRegistry micrometer = new CompositeMeterRegistry();
    final PrometheusMeterRegistry prometheus = PrometheusMeterRegistries.newRegistry();
    final DropwizardMeterRegistry dropwizard = DropwizardMeterRegistries.newRegistry();
    micrometer.add(prometheus).add(dropwizard);
    final DistributionSummary summary = DistributionSummary.builder("summary")
                                                           .publishPercentiles(0.5, 0.99)
                                                           .register(micrometer);
    summary.record(42);

    // Make sure Dropwizard registry does not have unwanted gauges.
    assertThat(dropwizard.getDropwizardRegistry().getMetrics()).containsOnlyKeys("summary");

    // Make sure Prometheus registry collects all samples.
    final MetricFamilySamples prometheusSamples = findPrometheusSample(prometheus, "summary");
    assertThat(prometheusSamples.samples).containsExactly(
            new Sample("summary", ImmutableList.of("quantile"), ImmutableList.of("0.5"), 42),
            new Sample("summary", ImmutableList.of("quantile"), ImmutableList.of("0.99"), 42),
            new Sample("summary_count", ImmutableList.of(), ImmutableList.of(), 1),
            new Sample("summary_sum", ImmutableList.of(), ImmutableList.of(), 42));
}
 
Example 5
Source File: SimpleMeterRegistryTest.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@Issue("#370")
@Test
void serviceLevelObjectivesOnlyNoPercentileHistogram() {
    DistributionSummary summary = DistributionSummary.builder("my.summary")
            .serviceLevelObjectives(1.0, 2)
            .register(registry);

    summary.record(1);

    Timer timer = Timer.builder("my.timer").serviceLevelObjectives(Duration.ofMillis(1)).register(registry);
    timer.record(1, TimeUnit.MILLISECONDS);

    Gauge summaryHist1 = registry.get("my.summary.histogram").tags("le", "1").gauge();
    Gauge summaryHist2 = registry.get("my.summary.histogram").tags("le", "2").gauge();
    Gauge timerHist = registry.get("my.timer.histogram").tags("le", "0.001").gauge();

    assertThat(summaryHist1.value()).isEqualTo(1);
    assertThat(summaryHist2.value()).isEqualTo(1);
    assertThat(timerHist.value()).isEqualTo(1);

    clock.add(SimpleConfig.DEFAULT.step());

    assertThat(summaryHist1.value()).isEqualTo(0);
    assertThat(summaryHist2.value()).isEqualTo(0);
    assertThat(timerHist.value()).isEqualTo(0);
}
 
Example 6
Source File: DistributionSummaryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("scale samples by a fixed factor")
default void scale(MeterRegistry registry) {
    DistributionSummary ds = DistributionSummary.builder("my.summary")
            .scale(2.0)
            .register(registry);

    ds.record(1);

    clock(registry).add(step());
    assertThat(ds.totalAmount()).isEqualTo(2.0);
}
 
Example 7
Source File: DistributionSummaryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Deprecated
@Test
default void percentiles(MeterRegistry registry) {
    DistributionSummary s = DistributionSummary.builder("my.summary")
            .publishPercentiles(1)
            .register(registry);

    s.record(1);
    assertThat(s.percentile(1)).isEqualTo(1, Offset.offset(0.3));
    assertThat(s.percentile(0.5)).isNaN();
}
 
Example 8
Source File: DistributionSummaryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Deprecated
@Test
default void histogramCounts(MeterRegistry registry) {
    DistributionSummary s = DistributionSummary.builder("my.summmary")
            .serviceLevelObjectives(1.0)
            .register(registry);

    s.record(1);
    assertThat(s.histogramCountAtValue(1)).isEqualTo(1);
    assertThat(s.histogramCountAtValue(2)).isNaN();
}
 
Example 9
Source File: MicrometerChannelMetricsRecorder.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
public void recordDataReceived(SocketAddress remoteAddress, long bytes) {
	String address = reactor.netty.Metrics.formatSocketAddress(remoteAddress);
	DistributionSummary ds = dataReceivedCache.computeIfAbsent(address,
			key -> filter(dataReceivedBuilder.tag(REMOTE_ADDRESS, address)
			                                 .register(REGISTRY)));
	if (ds != null) {
		ds.record(bytes);
	}
}
 
Example 10
Source File: MicrometerChannelMetricsRecorder.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
public void recordDataSent(SocketAddress remoteAddress, long bytes) {
	String address = reactor.netty.Metrics.formatSocketAddress(remoteAddress);
	DistributionSummary ds = dataSentCache.computeIfAbsent(address,
			key -> filter(dataSentBuilder.tag(REMOTE_ADDRESS, address)
			                             .register(REGISTRY)));
	if (ds != null) {
		ds.record(bytes);
	}
}
 
Example 11
Source File: MicrometerHttpMetricsRecorder.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
public void recordDataReceived(SocketAddress remoteAddress, String uri, long bytes) {
	String address = Metrics.formatSocketAddress(remoteAddress);
	DistributionSummary dataReceived = dataReceivedCache.computeIfAbsent(new MeterKey(uri, address, null, null),
			key -> filter(dataReceivedBuilder.tags(REMOTE_ADDRESS, address, URI, uri)
			                                 .register(REGISTRY)));
	if (dataReceived != null) {
		dataReceived.record(bytes);
	}
}
 
Example 12
Source File: MicrometerHttpMetricsRecorder.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
public void recordDataSent(SocketAddress remoteAddress, String uri, long bytes) {
	String address = Metrics.formatSocketAddress(remoteAddress);
	DistributionSummary dataSent = dataSentCache.computeIfAbsent(new MeterKey(uri, address, null, null),
			key -> filter(dataSentBuilder.tags(REMOTE_ADDRESS, address, URI, uri)
			                             .register(REGISTRY)));
	if (dataSent != null) {
		dataSent.record(bytes);
	}
}
 
Example 13
Source File: MicrometerHttpServerMetricsRecorder.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
public void recordDataReceived(SocketAddress remoteAddress, String uri, long bytes) {
	DistributionSummary dataReceived = dataReceivedCache.computeIfAbsent(new MeterKey(uri, null, null, null),
			key -> filter(dataReceivedBuilder.tags(URI, uri)
			                                 .register(REGISTRY)));
	if (dataReceived != null) {
		dataReceived.record(bytes);
	}
}
 
Example 14
Source File: MicrometerHttpServerMetricsRecorder.java    From reactor-netty with Apache License 2.0 5 votes vote down vote up
@Override
public void recordDataSent(SocketAddress remoteAddress, String uri, long bytes) {
	DistributionSummary dataSent = dataSentCache.computeIfAbsent(new MeterKey(uri, null, null, null),
			key -> filter(dataSentBuilder.tags(URI, uri)
			                             .register(REGISTRY)));
	if (dataSent != null) {
		dataSent.record(bytes);
	}
}
 
Example 15
Source File: DropwizardMeterRegistriesTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void unwantedGaugesAreFilteredOut() {
    final DropwizardMeterRegistry micrometer = DropwizardMeterRegistries.newRegistry();
    final MetricRegistry dropwizard = micrometer.getDropwizardRegistry();

    final DistributionSummary percentileSummary = DistributionSummary.builder("percentileSummary")
                                                                     .publishPercentiles(0.5, 0.99)
                                                                     .register(micrometer);
    final DistributionSummary histogramSummary = DistributionSummary.builder("histogramSummary")
                                                                    .sla(10, 100)
                                                                    .register(micrometer);
    final Timer percentileTimer = Timer.builder("percentileTimer")
                                       .publishPercentiles(0.5, 0.99)
                                       .register(micrometer);
    final Timer histogramTimer = Timer.builder("histogramTimer")
                                      .sla(Duration.ofSeconds(10), Duration.ofSeconds(100))
                                      .register(micrometer);
    percentileSummary.record(42);
    histogramSummary.record(42);
    percentileTimer.record(42, TimeUnit.SECONDS);
    histogramTimer.record(42, TimeUnit.SECONDS);

    final Map<String, Double> measurements = MoreMeters.measureAll(micrometer);
    measurements.forEach((key, value) -> assertThat(key).doesNotContain(".percentile")
                                                        .doesNotContain(".histogram")
                                                        .doesNotContain("phi=")
                                                        .doesNotContain("le="));

    // Must be exported as 2 Histograms and 2 Timers only.
    assertThat(dropwizard.getHistograms()).hasSize(2);
    assertThat(dropwizard.getTimers()).hasSize(2);
}
 
Example 16
Source File: DistributionSummaryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("record zero")
default void recordZero(MeterRegistry registry) {
    DistributionSummary ds = registry.summary("my.summary");

    ds.record(0);
    clock(registry).add(step());

    assertAll(() -> assertEquals(1L, ds.count()),
            () -> assertEquals(0L, ds.totalAmount()));
}
 
Example 17
Source File: DistributionSummaryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("negative quantities are ignored")
default void recordNegative(MeterRegistry registry) {
    DistributionSummary ds = registry.summary("my.summary");

    ds.record(-10);
    assertAll(() -> assertEquals(0, ds.count()),
            () -> assertEquals(0L, ds.totalAmount()));
}
 
Example 18
Source File: MicrometerAtlasIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenDistributionSummary_whenRecord_thenSummarized() {
    SimpleMeterRegistry registry = new SimpleMeterRegistry();
    DistributionSummary distributionSummary = DistributionSummary
      .builder("request.size")
      .baseUnit("bytes")
      .register(registry);
    distributionSummary.record(3);
    distributionSummary.record(4);
    distributionSummary.record(5);

    assertTrue(3 == distributionSummary.count());
    assertTrue(12 == distributionSummary.totalAmount());
}
 
Example 19
Source File: DropwizardMeterRegistriesTest.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Test
void micrometerAddsUnwantedGauges() {
    final DropwizardMeterRegistry micrometer = new DropwizardMeterRegistry(
            DEFAULT_DROPWIZARD_CONFIG, new MetricRegistry(), DEFAULT_NAME_MAPPER, Clock.SYSTEM) {
        @Override
        protected Double nullGaugeValue() {
            return 0.0;
        }
    };
    final MetricRegistry dropwizard = micrometer.getDropwizardRegistry();

    final DistributionSummary percentileSummary = DistributionSummary.builder("percentileSummary")
                                                                     .publishPercentiles(0.5)
                                                                     .register(micrometer);
    final DistributionSummary histogramSummary = DistributionSummary.builder("histogramSummary")
                                                                    .sla(10)
                                                                    .register(micrometer);
    final Timer percentileTimer = Timer.builder("percentileTimer")
                                       .publishPercentiles(0.5)
                                       .register(micrometer);
    final Timer histogramTimer = Timer.builder("histogramTimer")
                                      .sla(Duration.ofSeconds(10))
                                      .register(micrometer);
    percentileSummary.record(42);
    histogramSummary.record(42);
    percentileTimer.record(42, TimeUnit.SECONDS);
    histogramTimer.record(42, TimeUnit.SECONDS);

    // Make sure Micrometer by default registers the unwanted gauges.
    final Map<String, Double> measurements = MoreMeters.measureAll(micrometer);
    assertThat(measurements).containsKeys(
            "percentileSummary.percentile#value{phi=0.5}",
            "histogramSummary.histogram#value{le=10}",
            "percentileTimer.percentile#value{phi=0.5}",
            "histogramTimer.histogram#value{le=10000}");

    assertThat(dropwizard.getGauges()).containsKeys(
            "percentileSummaryPercentile.phi:0.5",
            "histogramSummaryHistogram.le:10",
            "percentileTimerPercentile.phi:0.5",
            "histogramTimerHistogram.le:10000");
}