Java Code Examples for io.micrometer.core.instrument.distribution.DistributionStatisticConfig

The following examples show how to use io.micrometer.core.instrument.distribution.DistributionStatisticConfig. These examples are extracted from open source projects. 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 Project: skywalking   Source File: SkywalkingTimer.java    License: Apache License 2.0 6 votes vote down vote up
protected SkywalkingTimer(Id id, MeterId meterId, SkywalkingConfig config, Clock clock,
                          DistributionStatisticConfig distributionStatisticConfig, PauseDetector pauseDetector,
                          TimeUnit baseTimeUnit, boolean supportsAggregablePercentiles) {
    super(id, clock, distributionStatisticConfig, pauseDetector, baseTimeUnit, supportsAggregablePercentiles);

    // meter base name
    String baseName = meterId.getName();

    this.counter = MeterBuilder.buildCounter(meterId.copyTo(baseName + "_count", MeterId.MeterType.COUNTER), config);
    this.sum = MeterBuilder.buildCounter(meterId.copyTo(baseName + "_sum", MeterId.MeterType.COUNTER), config);
    this.maxAdder = new DoubleAccumulator((a, b) -> a > b ? a : b, 0.000);
    this.max = MeterFactory.gauge(meterId.copyTo(baseName + "_max", MeterId.MeterType.GAUGE),
        () -> maxAdder.doubleValue()).build();

    this.histogram = MeterBuilder.buildHistogram(meterId, supportsAggregablePercentiles, distributionStatisticConfig, true);
}
 
Example 2
Source Project: foremast   Source File: PropertiesMeterFilter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public DistributionStatisticConfig configure(Meter.Id id,
                                             DistributionStatisticConfig config) {
    Distribution distribution = this.properties.getDistribution();
    return DistributionStatisticConfig.builder()
            .percentilesHistogram(
                    lookupWithFallbackToAll(distribution.getPercentilesHistogram(), id, null))
            .percentiles(
                    lookupWithFallbackToAll(distribution.getPercentiles(), id, null))
            .sla(convertSla(id.getType(), lookup(distribution.getSla(), id, null)))
            .minimumExpectedValue(convertMeterValue(id.getType(),
                    lookup(distribution.getMinimumExpectedValue(), id, null)))
            .maximumExpectedValue(convertMeterValue(id.getType(),
                    lookup(distribution.getMaximumExpectedValue(), id, null)))
            .build().merge(config);
}
 
Example 3
Source Project: foremast   Source File: PropertiesMeterFilter.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public DistributionStatisticConfig configure(Meter.Id id,
                                             DistributionStatisticConfig config) {
    Distribution distribution = this.properties.getDistribution();
    return DistributionStatisticConfig.builder()
            .percentilesHistogram(
                    lookupWithFallbackToAll(distribution.getPercentilesHistogram(), id, null))
            .percentiles(
                    lookupWithFallbackToAll(distribution.getPercentiles(), id, null))
            .sla(convertSla(id.getType(), lookup(distribution.getSla(), id, null)))
            .minimumExpectedValue(convertMeterValue(id.getType(),
                    lookup(distribution.getMinimumExpectedValue(), id, null)))
            .maximumExpectedValue(convertMeterValue(id.getType(),
                    lookup(distribution.getMaximumExpectedValue(), id, null)))
            .build().merge(config);
}
 
Example 4
Source Project: micrometer   Source File: SimpleMeterRegistry.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected DistributionSummary newDistributionSummary(Meter.Id id, DistributionStatisticConfig distributionStatisticConfig, double scale) {
    DistributionStatisticConfig merged = distributionStatisticConfig.merge(DistributionStatisticConfig.builder()
            .expiry(config.step())
            .build());

    DistributionSummary summary;
    switch (config.mode()) {
        case CUMULATIVE:
            summary = new CumulativeDistributionSummary(id, clock, merged, scale, false);
            break;
        case STEP:
        default:
            summary = new StepDistributionSummary(id, clock, merged, scale, config.step().toMillis(), false);
            break;
    }

    HistogramGauges.registerWithCommonFormat(summary, this);

    return summary;
}
 
Example 5
Source Project: micrometer   Source File: SimpleMeterRegistry.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected Timer newTimer(Meter.Id id, DistributionStatisticConfig distributionStatisticConfig, PauseDetector pauseDetector) {
    DistributionStatisticConfig merged = distributionStatisticConfig.merge(DistributionStatisticConfig.builder()
            .expiry(config.step())
            .build());

    Timer timer;
    switch (config.mode()) {
        case CUMULATIVE:
            timer = new CumulativeTimer(id, clock, merged, pauseDetector, getBaseTimeUnit(), false);
            break;
        case STEP:
        default:
            timer = new StepTimer(id, clock, merged, pauseDetector, getBaseTimeUnit(), config.step().toMillis(), false);
            break;
    }

    HistogramGauges.registerWithCommonFormat(timer, this);

    return timer;
}
 
Example 6
Source Project: skywalking   Source File: SkywalkingDistributionSummary.java    License: Apache License 2.0 6 votes vote down vote up
protected SkywalkingDistributionSummary(Id id, MeterId meterId, SkywalkingConfig config, Clock clock,
                                        DistributionStatisticConfig distributionStatisticConfig, double scale,
                                        boolean supportsAggregablePercentiles) {
    super(id, clock, distributionStatisticConfig, scale, supportsAggregablePercentiles);

    // meter base name
    String baseName = meterId.getName();

    this.counter = MeterBuilder.buildCounter(meterId.copyTo(baseName + "_count", MeterId.MeterType.COUNTER), config);
    this.sum = MeterBuilder.buildCounter(meterId.copyTo(baseName + "_sum", MeterId.MeterType.COUNTER), config);
    this.maxAdder = new DoubleAccumulator((a, b) -> a > b ? a : b, 0.000);
    this.max = MeterFactory.gauge(meterId.copyTo(baseName + "_max", MeterId.MeterType.GAUGE),
        () -> maxAdder.doubleValue()).build();

    this.histogram = MeterBuilder.buildHistogram(meterId, supportsAggregablePercentiles, distributionStatisticConfig, false);
}
 
Example 7
Source Project: skywalking   Source File: MeterBuilder.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Build the histogram
 * @return return histogram if support
 */
public static Optional<Histogram> buildHistogram(MeterId meterId, boolean supportsAggregablePercentiles,
                                                 DistributionStatisticConfig distributionStatisticConfig,
                                                 boolean useNanoTime) {
    if (!distributionStatisticConfig.isPublishingHistogram()) {
        return Optional.empty();
    }

    final NavigableSet<Double> buckets = distributionStatisticConfig.getHistogramBuckets(supportsAggregablePercentiles);
    final List<Double> steps = buckets.stream().sorted(Double::compare)
        .map(t -> useNanoTime ? TimeUtils.nanosToUnit(t, TimeUnit.MILLISECONDS) : t).collect(Collectors.toList());

    final Histogram.Builder histogramBuilder = MeterFactory.histogram(
        meterId.copyTo(meterId.getName() + "_histogram", MeterId.MeterType.HISTOGRAM)).steps(steps);
    final Double minimumExpectedValueAsDouble = distributionStatisticConfig.getMinimumExpectedValueAsDouble();
    if (minimumExpectedValueAsDouble != null) {
        histogramBuilder.minValue(useNanoTime ?
            TimeUtils.nanosToUnit(minimumExpectedValueAsDouble, TimeUnit.MILLISECONDS) : minimumExpectedValueAsDouble);
    }
    return Optional.of(histogramBuilder.build());
}
 
Example 8
Source Project: micrometer   Source File: StepTimerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Issue("#1814")
@Test
void meanShouldWorkIfTotalTimeNotCalled() {
    Duration stepDuration = Duration.ofMillis(10);
    MockClock clock = new MockClock();
    StepTimer timer = new StepTimer(
            mock(Meter.Id.class),
            clock,
            DistributionStatisticConfig.builder().expiry(stepDuration).bufferLength(2).build(),
            mock(PauseDetector.class),
            TimeUnit.MILLISECONDS,
            stepDuration.toMillis(),
            false
    );

    clock.add(stepDuration);
    assertThat(timer.mean(TimeUnit.MILLISECONDS)).isEqualTo(0.0);

    clock.add(Duration.ofMillis(1));
    timer.record(Duration.ofMillis(50));
    timer.record(Duration.ofMillis(100));

    clock.add(stepDuration);
    assertThat(timer.mean(TimeUnit.MILLISECONDS)).isEqualTo(75.0);
}
 
Example 9
Source Project: summerframework   Source File: MyRegistryCustomizer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticConfig config) {
    if (types.contains(id.getType())) {
        return DistributionStatisticConfig.builder().percentiles(getPercentiles()).build().merge(config);
    }
    return config;
}
 
Example 10
Source Project: micrometer   Source File: DropwizardMeterRegistry.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected LongTaskTimer newLongTaskTimer(Meter.Id id, DistributionStatisticConfig distributionStatisticConfig) {
    LongTaskTimer ltt = new DefaultLongTaskTimer(id, clock, getBaseTimeUnit(), distributionStatisticConfig, false);
    registry.register(hierarchicalName(id.withTag(Statistic.ACTIVE_TASKS)), (Gauge<Integer>) ltt::activeTasks);
    registry.register(hierarchicalName(id.withTag(Statistic.DURATION)), (Gauge<Double>) () -> ltt.duration(TimeUnit.NANOSECONDS));
    registry.register(hierarchicalName(id.withTag(Statistic.MAX)), (Gauge<Double>) () -> ltt.max(TimeUnit.NANOSECONDS));
    HistogramGauges.registerWithCommonFormat(ltt, this);
    return ltt;
}
 
Example 11
Source Project: micrometer   Source File: DropwizardMeterRegistry.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected DistributionStatisticConfig defaultHistogramConfig() {
    return DistributionStatisticConfig.builder()
            .expiry(dropwizardConfig.step())
            .build()
            .merge(DistributionStatisticConfig.DEFAULT);
}
 
Example 12
Source Project: micrometer   Source File: CumulativeTimer.java    License: Apache License 2.0 5 votes vote down vote up
public CumulativeTimer(Id id, Clock clock, DistributionStatisticConfig distributionStatisticConfig,
                       PauseDetector pauseDetector, TimeUnit baseTimeUnit, boolean supportsAggregablePercentiles) {
    super(id, clock, distributionStatisticConfig, pauseDetector, baseTimeUnit, supportsAggregablePercentiles);
    this.count = new AtomicLong();
    this.total = new AtomicLong();
    this.max = new TimeWindowMax(clock, distributionStatisticConfig);
}
 
Example 13
Source Project: micrometer   Source File: CumulativeDistributionSummary.java    License: Apache License 2.0 5 votes vote down vote up
public CumulativeDistributionSummary(Id id, Clock clock, DistributionStatisticConfig distributionStatisticConfig,
                                     double scale, boolean supportsAggregablePercentiles) {
    super(id, clock, distributionStatisticConfig, scale, supportsAggregablePercentiles);
    this.count = new AtomicLong();
    this.total = new DoubleAdder();
    this.max = new TimeWindowMax(clock, distributionStatisticConfig);
}
 
Example 14
Source Project: micrometer   Source File: StatsdTimer.java    License: Apache License 2.0 5 votes vote down vote up
StatsdTimer(Id id, StatsdLineBuilder lineBuilder, FluxSink<String> sink, Clock clock,
            DistributionStatisticConfig distributionStatisticConfig, PauseDetector pauseDetector, TimeUnit baseTimeUnit, long stepMillis) {
    super(id, clock, distributionStatisticConfig, pauseDetector, baseTimeUnit, false);
    this.max = new StepDouble(clock, stepMillis);
    this.lineBuilder = lineBuilder;
    this.sink = sink;
}
 
Example 15
Source Project: micrometer   Source File: StatsdMeterRegistry.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected DistributionSummary newDistributionSummary(Meter.Id id, DistributionStatisticConfig
        distributionStatisticConfig, double scale) {

    // Adds an infinity bucket for SLO violation calculation
    if (distributionStatisticConfig.getServiceLevelObjectiveBoundaries() != null) {
        distributionStatisticConfig = addInfBucket(distributionStatisticConfig);
    }

    DistributionSummary summary = new StatsdDistributionSummary(id, lineBuilder(id), fluxSink, clock, distributionStatisticConfig, scale);
    HistogramGauges.registerWithCommonFormat(summary, this);
    return summary;
}
 
Example 16
Source Project: micrometer   Source File: StepMeterRegistry.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected DistributionSummary newDistributionSummary(Meter.Id id, DistributionStatisticConfig distributionStatisticConfig, double scale) {
    DistributionSummary summary = new StepDistributionSummary(id, clock, distributionStatisticConfig, scale,
        config.step().toMillis(),false);
    HistogramGauges.registerWithCommonFormat(summary, this);
    return summary;
}
 
Example 17
Source Project: skywalking   Source File: MeterBuilderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBuildHistogram() {
    final MeterId meterId = new MeterId("test", MeterId.MeterType.COUNTER,
        Arrays.asList(new MeterId.Tag("k1", "v1")));

    // Build a new distribution config
    final DistributionStatisticConfig statisticConfig = DistributionStatisticConfig.builder()
        .serviceLevelObjectives(Duration.ofMillis(1).toNanos(), Duration.ofMillis(5).toNanos(), Duration.ofMillis(10).toNanos())
        .minimumExpectedValue(0d).build();

    // Check buckets
    final Optional<Histogram> histogramOptional = MeterBuilder.buildHistogram(meterId, true, statisticConfig, true);
    final Histogram histogram = histogramOptional.orElse(null);
    Assert.assertNotNull(histogram);
    final Histogram.Bucket[] buckets = histogram.getBuckets();
    Assert.assertEquals(4, buckets.length);
    Assert.assertEquals(0d, buckets[0].getBucket(), 0.0);
    Assert.assertEquals(1d, buckets[1].getBucket(), 0.0);
    Assert.assertEquals(5d, buckets[2].getBucket(), 0.0);
    Assert.assertEquals(10d, buckets[3].getBucket(), 0.0);

    // Check meter id
    Assert.assertEquals("test_histogram", histogram.getMeterId().getName());
    Assert.assertEquals(MeterId.MeterType.HISTOGRAM, histogram.getMeterId().getType());
    Assert.assertEquals(Arrays.asList(new MeterId.Tag("k1", "v1")), histogram.getMeterId().getTags());

    // Don't need the histogram
    Assert.assertNull(MeterBuilder.buildHistogram(meterId, true, DistributionStatisticConfig.DEFAULT, true).orElse(null));
}
 
Example 18
Source Project: micrometer   Source File: MeterRegistry.java    License: Apache License 2.0 5 votes vote down vote up
private <M extends Meter> M registerMeterIfNecessary(Class<M> meterClass, Meter.Id id,
                                                     @Nullable DistributionStatisticConfig config, BiFunction<Meter.Id, DistributionStatisticConfig, M> builder,
                                                     Function<Meter.Id, M> noopBuilder) {
    Id mappedId = getMappedId(id);
    Meter m = getOrCreateMeter(config, builder, id, mappedId, noopBuilder);

    if (!meterClass.isInstance(m)) {
        throw new IllegalArgumentException("There is already a registered meter of a different type with the same name");
    }
    return meterClass.cast(m);
}
 
Example 19
Source Project: micrometer   Source File: MeterRegistry.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Only used by {@link LongTaskTimer#builder(String)}.
 *
 * @param id The identifier for this long task timer.
 * @return A new or existing long task timer.
 */
LongTaskTimer longTaskTimer(Meter.Id id, DistributionStatisticConfig distributionStatisticConfig) {
    return registerMeterIfNecessary(LongTaskTimer.class, id, distributionStatisticConfig, (id2, filteredConfig) -> {
        Meter.Id withUnit = id2.withBaseUnit(getBaseTimeUnitStr());
        return newLongTaskTimer(withUnit, filteredConfig.merge(defaultHistogramConfig()));
    }, NoopLongTaskTimer::new);
}
 
Example 20
Source Project: micrometer   Source File: StatsdMeterRegistry.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected LongTaskTimer newLongTaskTimer(Meter.Id id, DistributionStatisticConfig distributionStatisticConfig) {
    StatsdLongTaskTimer ltt = new StatsdLongTaskTimer(id, lineBuilder(id), fluxSink, clock, statsdConfig.publishUnchangedMeters(),
            distributionStatisticConfig, getBaseTimeUnit());
    HistogramGauges.registerWithCommonFormat(ltt, this);
    pollableMeters.put(id, ltt);
    return ltt;
}
 
Example 21
Source Project: micrometer   Source File: SimpleMeterRegistry.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected DistributionStatisticConfig defaultHistogramConfig() {
    return DistributionStatisticConfig.builder()
            .expiry(config.step())
            .build()
            .merge(DistributionStatisticConfig.DEFAULT);
}
 
Example 22
Source Project: micrometer   Source File: MeterFilter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Set a maximum expected value on any {@link Timer} whose name begins with the given prefix.
 *
 * @param prefix Apply the maximum only to timers whose name begins with this prefix.
 * @param max    The maximum expected value of the timer.
 * @return A filter that applies a maximum expected value to a timer.
 */
static MeterFilter maxExpected(String prefix, Duration max) {
    return new MeterFilter() {
        @Override
        public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticConfig config) {
            if (id.getType() == Meter.Type.TIMER && id.getName().startsWith(prefix)) {
                return DistributionStatisticConfig.builder()
                        .maximumExpectedValue((double) max.toNanos())
                        .build()
                        .merge(config);
            }
            return config;
        }
    };
}
 
Example 23
Source Project: micrometer   Source File: MeterFilter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Set a maximum expected value on any {@link DistributionSummary} whose name begins with the given prefix.
 *
 * @param prefix Apply the maximum only to distribution summaries whose name begins with this prefix.
 * @param max    The maximum expected value of the distribution summary.
 * @return A filter that applies a maximum expected value to a distribution summary.
 * @since 1.4.0
 */
static MeterFilter maxExpected(String prefix, double max) {
    return new MeterFilter() {
        @Override
        public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticConfig config) {
            if (id.getType() == Meter.Type.DISTRIBUTION_SUMMARY && id.getName().startsWith(prefix)) {
                return DistributionStatisticConfig.builder()
                        .maximumExpectedValue(max)
                        .build()
                        .merge(config);
            }
            return config;
        }
    };
}
 
Example 24
Source Project: micrometer   Source File: MeterFilter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Set a minimum expected value on any {@link Timer} whose name begins with the given prefix.
 *
 * @param prefix Apply the minimum only to timers whose name begins with this prefix.
 * @param min    The minimum expected value of the timer.
 * @return A filter that applies a minimum expected value to a timer.
 */
static MeterFilter minExpected(String prefix, Duration min) {
    return new MeterFilter() {
        @Override
        public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticConfig config) {
            if (id.getType() == Meter.Type.TIMER && id.getName().startsWith(prefix)) {
                return DistributionStatisticConfig.builder()
                        .minimumExpectedValue((double) min.toNanos())
                        .build()
                        .merge(config);
            }
            return config;
        }
    };
}
 
Example 25
Source Project: micrometer   Source File: MeterFilter.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Set a minimum expected value on any {@link DistributionSummary} whose name begins with the given prefix.
 *
 * @param prefix Apply the minimum only to distribution summaries whose name begins with this prefix.
 * @param min    The minimum expected value of the distribution summary.
 * @return A filter that applies a minimum expected value to a distribution summary.
 * @since 1.4.0
 */
static MeterFilter minExpected(String prefix, double min) {
    return new MeterFilter() {
        @Override
        public DistributionStatisticConfig configure(Meter.Id id, DistributionStatisticConfig config) {
            if (id.getType() == Meter.Type.DISTRIBUTION_SUMMARY && id.getName().startsWith(prefix)) {
                return DistributionStatisticConfig.builder()
                        .minimumExpectedValue(min)
                        .build()
                        .merge(config);
            }
            return config;
        }
    };
}
 
Example 26
Source Project: micrometer   Source File: MeterFilterTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void minExpectedOnSummary() {
    MeterFilter filter = MeterFilter.minExpected("name", 100.0);
    Meter.Id timer = new Meter.Id("name", Tags.empty(), null, null, Meter.Type.DISTRIBUTION_SUMMARY);

    assertThat(filter.configure(timer, DistributionStatisticConfig.DEFAULT))
            .satisfies(conf -> assertThat(conf.getMinimumExpectedValueAsDouble()).isEqualTo(100));
}
 
Example 27
Source Project: micrometer   Source File: MeterFilterTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void maxExpectedOnSummary() {
    MeterFilter filter = MeterFilter.maxExpected("name", 100.0);
    Meter.Id timer = new Meter.Id("name", Tags.empty(), null, null, Meter.Type.DISTRIBUTION_SUMMARY);

    assertThat(filter.configure(timer, DistributionStatisticConfig.DEFAULT))
            .satisfies(conf -> assertThat(conf.getMaximumExpectedValueAsDouble()).isEqualTo(100));
}
 
Example 28
Source Project: micrometer   Source File: MeterFilterTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void minExpectedOnTimer() {
    MeterFilter filter = MeterFilter.minExpected("name", Duration.ofNanos(100));
    Meter.Id timer = new Meter.Id("name", Tags.empty(), null, null, Meter.Type.TIMER);

    assertThat(filter.configure(timer, DistributionStatisticConfig.DEFAULT))
            .satisfies(conf -> assertThat(conf.getMinimumExpectedValueAsDouble()).isEqualTo(100));
}
 
Example 29
Source Project: micrometer   Source File: MeterFilterTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void maxExpectedOnTimer() {
    MeterFilter filter = MeterFilter.maxExpected("name", Duration.ofNanos(100));
    Meter.Id timer = new Meter.Id("name", Tags.empty(), null, null, Meter.Type.TIMER);

    assertThat(filter.configure(timer, DistributionStatisticConfig.DEFAULT))
            .satisfies(conf -> assertThat(conf.getMaximumExpectedValueAsDouble()).isEqualTo(100));
}
 
Example 30
Source Project: micrometer   Source File: TimedAspectTest.java    License: Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
protected Timer newTimer(@NonNull Id id,
                         @NonNull DistributionStatisticConfig distributionStatisticConfig,
                         @NonNull PauseDetector pauseDetector) {
    throw new RuntimeException();
}