io.micrometer.core.instrument.Statistic Java Examples

The following examples show how to use io.micrometer.core.instrument.Statistic. 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: StatsdLongTaskTimer.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@Override
public void poll() {
    long active = activeTasks();
    if (alwaysPublish || lastActive.getAndSet(active) != active) {
        sink.next(lineBuilder.gauge(active, Statistic.ACTIVE_TASKS));
    }

    double duration = duration(TimeUnit.MILLISECONDS);
    if (alwaysPublish || lastDuration.getAndSet(duration) != duration) {
        sink.next(lineBuilder.gauge(duration, Statistic.DURATION));
    }

    double max = max(TimeUnit.MILLISECONDS);
    if (alwaysPublish || lastDuration.getAndSet(duration) != duration) {
        sink.next(lineBuilder.gauge(max, Statistic.MAX));
    }
}
 
Example #2
Source File: AtlasUtils.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@Nullable
static com.netflix.spectator.api.Statistic toSpectatorStatistic(Statistic stat) {
    switch (stat) {
        case COUNT:
            return count;
        case TOTAL_TIME:
            return totalTime;
        case TOTAL:
            return totalAmount;
        case VALUE:
            return gauge;
        case ACTIVE_TASKS:
            return activeTasks;
        case DURATION:
            return duration;
        case MAX:
            return max;
    }
    return null;
}
 
Example #3
Source File: MossMetricsEndpoint.java    From Moss with Apache License 2.0 6 votes vote down vote up
public MetricResponse metric(String requiredMetricName, @Nullable List<String> tag) {
    List<Tag> tags = parseTags(tag);
    Collection<Meter> meters = findFirstMatchingMeters(this.registry,
            requiredMetricName, tags);
    if (meters.isEmpty()) {
        return null;
    }
    Map<Statistic, Double> samples = getSamples(meters);
    Map<String, Set<String>> availableTags = getAvailableTags(meters);
    tags.forEach((t) -> availableTags.remove(t.getKey()));
    Meter.Id meterId = meters.iterator().next().getId();
    return new MetricResponse(requiredMetricName, meterId.getDescription(),
            meterId.getBaseUnit(), asList(samples, Sample::new),
            asList(availableTags, AvailableTag::new));


}
 
Example #4
Source File: SysdigStatsdLineBuilderTest.java    From micrometer with Apache License 2.0 6 votes vote down vote up
@Issue("#970")
@Test
void sanitizeAllNonAlphaNumericCharacters() {
    String badCounterName = "\"\';^*()!~`_./?a{counter}:with123 weirdChars";
    String badTagName = "\"\';^*()!~`_./?a{tag}:with123 weirdChars";
    String badValueName = "\"\';^*()!~`_./?a{value}:with123 weirdChars";
    Counter c = registry.counter(badCounterName,
            badTagName,
            badValueName);
    SysdigStatsdLineBuilder lb = new SysdigStatsdLineBuilder(c.getId(), registry.config());

    registry.config().namingConvention(NamingConvention.dot);
    StringBuilder expected = new StringBuilder();
    expected.append("___________.__a_counter__with123_weirdChars")
            .append("#statistic=count,___________.__a_tag__with123_weirdChars")
            .append("=___________.__a_value__with123_weirdChars:1|c");


    assertThat(lb.line("1", Statistic.COUNT, "c"))
            .isEqualTo(expected.toString());
}
 
Example #5
Source File: SysdigStatsdLineBuilderTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Issue("#739")
@Test
void sanitizeColons() {
    Counter c = registry.counter("my:counter", "my:tag", "my:value");
    SysdigStatsdLineBuilder lb = new SysdigStatsdLineBuilder(c.getId(), registry.config());

    registry.config().namingConvention(NamingConvention.dot);
    assertThat(lb.line("1", Statistic.COUNT, "c")).isEqualTo("my_counter#statistic=count,my_tag=my_value:1|c");
}
 
Example #6
Source File: KairosMeterRegistryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void writeCustomMetricWhenCustomMeterHasMixedFiniteAndNonFiniteValuesShouldSkipOnlyNonFiniteValues() {
    Measurement measurement1 = new Measurement(() -> Double.POSITIVE_INFINITY, Statistic.VALUE);
    Measurement measurement2 = new Measurement(() -> Double.NEGATIVE_INFINITY, Statistic.VALUE);
    Measurement measurement3 = new Measurement(() -> Double.NaN, Statistic.VALUE);
    Measurement measurement4 = new Measurement(() -> 1d, Statistic.VALUE);
    Measurement measurement5 = new Measurement(() -> 2d, Statistic.VALUE);
    List<Measurement> measurements = Arrays.asList(measurement1, measurement2, measurement3, measurement4, measurement5);
    Meter meter = Meter.builder("my.meter", Meter.Type.GAUGE, measurements).register(this.meterRegistry);
    assertThat(meterRegistry.writeCustomMetric(meter)).hasSize(2);
}
 
Example #7
Source File: KairosMeterRegistryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void writeCustomMetricWhenCustomMeterHasOnlyNonFiniteValuesShouldNotBeWritten() {
    Measurement measurement1 = new Measurement(() -> Double.POSITIVE_INFINITY, Statistic.VALUE);
    Measurement measurement2 = new Measurement(() -> Double.NEGATIVE_INFINITY, Statistic.VALUE);
    Measurement measurement3 = new Measurement(() -> Double.NaN, Statistic.VALUE);
    List<Measurement> measurements = Arrays.asList(measurement1, measurement2, measurement3);
    Meter meter = Meter.builder("my.meter", Meter.Type.GAUGE, measurements).register(this.meterRegistry);
    assertThat(meterRegistry.writeCustomMetric(meter)).isEmpty();
}
 
Example #8
Source File: DatadogMetricMetadata.java    From micrometer with Apache License 2.0 5 votes vote down vote up
static String sanitizeType(Statistic statistic) {
    switch (statistic) {
        case COUNT:
        case TOTAL:
        case TOTAL_TIME:
            return "count";
        default:
            return "gauge";
    }
}
 
Example #9
Source File: DatadogStatsdLineBuilder.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private String tagsByStatistic(@Nullable Statistic stat) {
    if (stat == null) {
        return tagsNoStat;
    }
    String tags = this.tags.get(stat);
    if (tags != null) {
        return tags;
    }
    synchronized (conventionTagsLock) {
        return this.tags.computeIfAbsent(stat, (key) -> tags(key, conventionTags, ":", "|#"));
    }
}
 
Example #10
Source File: EtsyStatsdLineBuilderTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Issue("#739")
@Test
void sanitizeColons() {
    Counter c = registry.counter("my:counter", "my:tag", "my:value");
    EtsyStatsdLineBuilder lb = new EtsyStatsdLineBuilder(c.getId(), registry.config(), HierarchicalNameMapper.DEFAULT);

    registry.config().namingConvention(NamingConvention.dot);
    assertThat(lb.line("1", Statistic.COUNT, "c")).isEqualTo("my_counter.my_tag.my_value.statistic.count:1|c");
}
 
Example #11
Source File: EtsyStatsdLineBuilder.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private String nameByStatistic(@Nullable Statistic stat) {
    if (stat == null) {
        if (this.nameNoStat == null) {
            this.nameNoStat = etsyName(null);
        }
        //noinspection ConstantConditions
        return nameNoStat;
    }
    return names.computeIfAbsent(stat, this::etsyName);
}
 
Example #12
Source File: EtsyStatsdLineBuilderTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void changingNamingConvention() {
    Counter c = registry.counter("my.counter", "my.tag", "value");
    EtsyStatsdLineBuilder lb = new EtsyStatsdLineBuilder(c.getId(), registry.config(), HierarchicalNameMapper.DEFAULT);

    registry.config().namingConvention(NamingConvention.dot);
    assertThat(lb.line("1", Statistic.COUNT, "c")).isEqualTo("my.counter.my.tag.value.statistic.count:1|c");

    registry.config().namingConvention(NamingConvention.camelCase);
    assertThat(lb.line("1", Statistic.COUNT, "c")).isEqualTo("myCounter.myTag.value.statistic.count:1|c");
}
 
Example #13
Source File: GrpcMetricsIntegrationTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static Double findClientMeter(
        String method, String suffix, Statistic type, String... keyValues) {
    final MeterIdPrefix prefix = new MeterIdPrefix(
            "client." + suffix + '#' + type.getTagValueRepresentation(),
            "service", "armeria.grpc.testing.TestService",
            "method", method,
            "http.status", "200");
    final String meterIdStr = prefix.withTags(keyValues).toString();
    return MoreMeters.measureAll(registry).get(meterIdStr);
}
 
Example #14
Source File: SysdigStatsdLineBuilderTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void changingNamingConvention() {
    Counter c = registry.counter("my.counter", "my.tag", "value");
    SysdigStatsdLineBuilder lb = new SysdigStatsdLineBuilder(c.getId(), registry.config());

    registry.config().namingConvention(NamingConvention.dot);
    assertThat(lb.line("1", Statistic.COUNT, "c")).isEqualTo("my.counter#statistic=count,my.tag=value:1|c");

    registry.config().namingConvention(NamingConvention.camelCase);
    assertThat(lb.line("1", Statistic.COUNT, "c")).isEqualTo("myCounter#statistic=count,myTag=value:1|c");
}
 
Example #15
Source File: DatadogStatsdLineBuilderTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Issue("#1998")
@Test
void allowColonsInTagValues() {
    Counter c = registry.counter("my:counter", "my:tag", "my:value", "other_tag", "some:value:", "123.another.tag", "123:value");
    DatadogStatsdLineBuilder lb = new DatadogStatsdLineBuilder(c.getId(), registry.config());

    registry.config().namingConvention(NamingConvention.dot);
    assertThat(lb.line("1", Statistic.COUNT, "c"))
            .isEqualTo("my_counter:1|c|#statistic:count,m.123.another.tag:123:value,my_tag:my:value,other_tag:some:value_");
}
 
Example #16
Source File: DatadogStatsdLineBuilderTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void interpretEmptyTagValuesAsValuelessTags() {
    Counter c = registry.counter("my:counter", "my:tag", "");
    DatadogStatsdLineBuilder lb = new DatadogStatsdLineBuilder(c.getId(), registry.config());

    registry.config().namingConvention(NamingConvention.dot);
    assertThat(lb.line("1", Statistic.COUNT, "c")).isEqualTo("my_counter:1|c|#statistic:count,my_tag");
}
 
Example #17
Source File: DatadogStatsdLineBuilderTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Issue("#739")
@Test
void sanitizeColonsInTagKeys() {
    Counter c = registry.counter("my:counter", "my:tag", "my_value");
    DatadogStatsdLineBuilder lb = new DatadogStatsdLineBuilder(c.getId(), registry.config());

    registry.config().namingConvention(NamingConvention.dot);
    assertThat(lb.line("1", Statistic.COUNT, "c")).isEqualTo("my_counter:1|c|#statistic:count,my_tag:my_value");
}
 
Example #18
Source File: TelegrafStatsdLineBuilderTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Issue("#644")
@Test
void escapeCharactersForTelegraf() {
    Counter c = registry.counter("hikari.pools", "pool", "poolname = abc,::hikari");
    TelegrafStatsdLineBuilder lineBuilder = new TelegrafStatsdLineBuilder(c.getId(), registry.config());
    assertThat(lineBuilder.count(1, Statistic.COUNT)).isEqualTo("hikari_pools,statistic=count,pool=poolname_=_abc___hikari:1|c");
}
 
Example #19
Source File: TelegrafStatsdLineBuilderTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void changingNamingConvention() {
    Counter c = registry.counter("my.counter", "my.tag", "value");
    TelegrafStatsdLineBuilder lb = new TelegrafStatsdLineBuilder(c.getId(), registry.config());

    registry.config().namingConvention(NamingConvention.dot);
    assertThat(lb.line("1", Statistic.COUNT, "c")).isEqualTo("my.counter,statistic=count,my.tag=value:1|c");

    registry.config().namingConvention(NamingConvention.camelCase);
    assertThat(lb.line("1", Statistic.COUNT, "c")).isEqualTo("myCounter,statistic=count,myTag=value:1|c");
}
 
Example #20
Source File: TelegrafStatsdLineBuilderTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Issue("#739")
@Test
void sanitizeColons() {
    Counter c = registry.counter("my:counter", "my:tag", "my:value");
    TelegrafStatsdLineBuilder lb = new TelegrafStatsdLineBuilder(c.getId(), registry.config());

    registry.config().namingConvention(NamingConvention.dot);
    assertThat(lb.line("1", Statistic.COUNT, "c")).isEqualTo("my_counter,statistic=count,my_tag=my_value:1|c");
}
 
Example #21
Source File: DatadogStatsdLineBuilderTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void changingNamingConvention() {
    Counter c = registry.counter("my.counter", "my.tag", "value");
    DatadogStatsdLineBuilder lb = new DatadogStatsdLineBuilder(c.getId(), registry.config());

    registry.config().namingConvention(NamingConvention.dot);
    assertThat(lb.line("1", Statistic.COUNT, "c")).isEqualTo("my.counter:1|c|#statistic:count,my.tag:value");

    registry.config().namingConvention(NamingConvention.camelCase);
    assertThat(lb.line("1", Statistic.COUNT, "c")).isEqualTo("myCounter:1|c|#statistic:count,myTag:value");
}
 
Example #22
Source File: GrpcMetricsIntegrationTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Nullable
private static Double findServerMeter(
        String method, String suffix, Statistic type, String... keyValues) {
    final MeterIdPrefix prefix = new MeterIdPrefix(
            "server." + suffix + '#' + type.getTagValueRepresentation(),
            "service", "armeria.grpc.testing.TestService",
            "method", method,
            "hostname.pattern", "*");
    final String meterIdStr = prefix.withTags(keyValues).toString();
    return MoreMeters.measureAll(registry).get(meterIdStr);
}
 
Example #23
Source File: SkywalkingMeterRegistry.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
protected <T> FunctionCounter newFunctionCounter(Meter.Id id, T obj, ToDoubleFunction<T> countFunction) {
    final MeterId meterId = convertId(id);
    FunctionCounter fc = new CumulativeFunctionCounter<>(id, obj, countFunction);

    new SkywalkingCustomCounter.Builder(meterId, new Measurement(() -> countFunction.applyAsDouble(obj), Statistic.COUNT), config).build();
    return fc;
}
 
Example #24
Source File: SkywalkingMeterRegistryTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Test
public void testNewMeterSum() {
    // sum
    testNewMeter("test_meter", Meter.Type.GAUGE, Statistic.TOTAL, data -> {
        assertCounter((Counter) data.getMeter(),
            "test_meter_sum", data.getTags(), 1d);
    });

    // count
    testNewMeter("test_meter", Meter.Type.COUNTER, Statistic.COUNT, data -> {
        assertCounter((Counter) data.getMeter(),
            "test_meter", data.getTags(), 1d);
    });

    // max
    testNewMeter("test_meter", Meter.Type.GAUGE, Statistic.MAX, data -> {
        assertGauge((Gauge) data.getMeter(),
            "test_meter_max", data.getTags(), 1d);
    });

    // activeCount
    testNewMeter("test_meter", Meter.Type.GAUGE, Statistic.ACTIVE_TASKS, data -> {
        assertGauge((Gauge) data.getMeter(),
            "test_meter_active_count", data.getTags(), 1d);
    });

    // durationSum
    testNewMeter("test_meter", Meter.Type.GAUGE, Statistic.DURATION, data -> {
        assertGauge((Gauge) data.getMeter(),
            "test_meter_duration_sum", data.getTags(), 1d);
    });

    // others
    testNewMeter("test_meter", Meter.Type.GAUGE, Statistic.VALUE, data -> {
        assertGauge((Gauge) data.getMeter(),
            "test_meter", data.getTags(), 1d);
    });
}
 
Example #25
Source File: AppOpticsMeterRegistryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void writeMeterWhenCustomMeterHasMixedFiniteAndNonFiniteValuesShouldSkipOnlyNonFiniteValues() {
    Measurement measurement1 = new Measurement(() -> Double.POSITIVE_INFINITY, Statistic.VALUE);
    Measurement measurement2 = new Measurement(() -> Double.NEGATIVE_INFINITY, Statistic.VALUE);
    Measurement measurement3 = new Measurement(() -> Double.NaN, Statistic.VALUE);
    Measurement measurement4 = new Measurement(() -> 1d, Statistic.VALUE);
    Measurement measurement5 = new Measurement(() -> 2d, Statistic.VALUE);
    List<Measurement> measurements = Arrays.asList(measurement1, measurement2, measurement3, measurement4, measurement5);
    Meter meter = Meter.builder("my.meter", Meter.Type.GAUGE, measurements).register(this.meterRegistry);
    assertThat(meterRegistry.writeMeter(meter)).hasValue("{\"name\":\"my.meter\",\"period\":60,\"value\":1.0,\"tags\":{\"statistic\":\"value\"}},{\"name\":\"my.meter\",\"period\":60,\"value\":2.0,\"tags\":{\"statistic\":\"value\"}}");
}
 
Example #26
Source File: AppOpticsMeterRegistryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void writeMeterWhenCustomMeterHasOnlyNonFiniteValuesShouldNotBeWritten() {
    Measurement measurement1 = new Measurement(() -> Double.POSITIVE_INFINITY, Statistic.VALUE);
    Measurement measurement2 = new Measurement(() -> Double.NEGATIVE_INFINITY, Statistic.VALUE);
    Measurement measurement3 = new Measurement(() -> Double.NaN, Statistic.VALUE);
    List<Measurement> measurements = Arrays.asList(measurement1, measurement2, measurement3);
    Meter meter = Meter.builder("my.meter", Meter.Type.GAUGE, measurements).register(this.meterRegistry);
    assertThat(meterRegistry.writeMeter(meter)).isNotPresent();
}
 
Example #27
Source File: WavefrontMeterRegistryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void publishMeterWhenCustomMeterHasMixedFiniteAndNonFiniteValuesShouldSkipOnlyNonFiniteValues() throws IOException {
    Measurement measurement1 = new Measurement(() -> Double.POSITIVE_INFINITY, Statistic.VALUE);
    Measurement measurement2 = new Measurement(() -> Double.NEGATIVE_INFINITY, Statistic.VALUE);
    Measurement measurement3 = new Measurement(() -> Double.NaN, Statistic.VALUE);
    Measurement measurement4 = new Measurement(() -> 1d, Statistic.VALUE);
    Measurement measurement5 = new Measurement(() -> 2d, Statistic.VALUE);
    List<Measurement> measurements = Arrays.asList(measurement1, measurement2, measurement3, measurement4, measurement5);
    Meter meter = Meter.builder("my.meter", Meter.Type.GAUGE, measurements).register(this.registry);
    registry.publishMeter(meter);
    verify(wavefrontSender, times(1)).sendMetric("my.meter", 1d, clock.wallTime(), "host", Collections.singletonMap("statistic", "value"));
    verify(wavefrontSender, times(1)).sendMetric("my.meter", 2d, clock.wallTime(), "host", Collections.singletonMap("statistic", "value"));
    verifyNoMoreInteractions(wavefrontSender);
}
 
Example #28
Source File: WavefrontMeterRegistryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void publishMeterWhenCustomMeterHasOnlyNonFiniteValuesShouldNotBeWritten() {
    Measurement measurement1 = new Measurement(() -> Double.POSITIVE_INFINITY, Statistic.VALUE);
    Measurement measurement2 = new Measurement(() -> Double.NEGATIVE_INFINITY, Statistic.VALUE);
    Measurement measurement3 = new Measurement(() -> Double.NaN, Statistic.VALUE);
    List<Measurement> measurements = Arrays.asList(measurement1, measurement2, measurement3);
    Meter meter = Meter.builder("my.meter", Meter.Type.GAUGE, measurements).register(this.registry);
    registry.publishMeter(meter);
    verifyNoInteractions(wavefrontSender);
}
 
Example #29
Source File: HumioMeterRegistryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void writeMeterWhenCustomMeterHasMixedFiniteAndNonFiniteValuesShouldSkipOnlyNonFiniteValues() {
    Measurement measurement1 = new Measurement(() -> Double.POSITIVE_INFINITY, Statistic.VALUE);
    Measurement measurement2 = new Measurement(() -> Double.NEGATIVE_INFINITY, Statistic.VALUE);
    Measurement measurement3 = new Measurement(() -> Double.NaN, Statistic.VALUE);
    Measurement measurement4 = new Measurement(() -> 1d, Statistic.VALUE);
    Measurement measurement5 = new Measurement(() -> 2d, Statistic.VALUE);
    List<Measurement> measurements = Arrays.asList(measurement1, measurement2, measurement3, measurement4, measurement5);
    Meter meter = Meter.builder("my.meter", Meter.Type.GAUGE, measurements).register(this.meterRegistry);
    assertThat(createBatch().writeMeter(meter))
            .isEqualTo("{\"timestamp\":\"1970-01-01T00:00:00.001Z\",\"attributes\":{\"name\":\"my_meter\",\"value\":1,\"value\":2}}");
}
 
Example #30
Source File: HumioMeterRegistryTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Test
void writeMeterWhenCustomMeterHasOnlyNonFiniteValuesShouldNotBeWritten() {
    Measurement measurement1 = new Measurement(() -> Double.POSITIVE_INFINITY, Statistic.VALUE);
    Measurement measurement2 = new Measurement(() -> Double.NEGATIVE_INFINITY, Statistic.VALUE);
    Measurement measurement3 = new Measurement(() -> Double.NaN, Statistic.VALUE);
    List<Measurement> measurements = Arrays.asList(measurement1, measurement2, measurement3);
    Meter meter = Meter.builder("my.meter", Meter.Type.GAUGE, measurements).register(this.meterRegistry);
    assertThat(createBatch().writeMeter(meter)).isNull();
}