io.micrometer.core.instrument.Meter.Type Java Examples

The following examples show how to use io.micrometer.core.instrument.Meter.Type. 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: MicrometerAtlasIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenTimer_whenEnrichWithQuantile_thenQuantilesComputed() {
    SimpleMeterRegistry registry = new SimpleMeterRegistry();
    Timer timer = Timer
      .builder("test.timer")
      .quantiles(WindowSketchQuantiles
        .quantiles(0.3, 0.5, 0.95)
        .create())
      .register(registry);

    timer.record(2, TimeUnit.SECONDS);
    timer.record(2, TimeUnit.SECONDS);
    timer.record(3, TimeUnit.SECONDS);
    timer.record(4, TimeUnit.SECONDS);
    timer.record(8, TimeUnit.SECONDS);
    timer.record(13, TimeUnit.SECONDS);

    Map<String, Integer> quantileMap = extractTagValueMap(registry, Type.Gauge, 1e9);
    assertThat(quantileMap, allOf(hasEntry("quantile=0.3", 2), hasEntry("quantile=0.5", 3), hasEntry("quantile=0.95", 8)));
}
 
Example #3
Source File: MicrometerAtlasIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
private Map<String, Integer> extractTagValueMap(MeterRegistry registry, Type meterType, double valueDivisor) {
    return registry
      .getMeters()
      .stream()
      .filter(meter -> meter.getType() == meterType)
      .collect(Collectors.toMap(meter -> {
          Tag tag = meter
            .getId()
            .getTags()
            .iterator()
            .next();
          return tag.getKey() + "=" + tag.getValue();
      }, meter -> (int) (meter
        .measure()
        .iterator()
        .next()
        .getValue() / valueDivisor)));
}
 
Example #4
Source File: MicrometerAtlasIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenTimer_whenEnrichWithTimescaleHistogram_thenTimeScaleDataCollected() {
    SimpleMeterRegistry registry = new SimpleMeterRegistry();
    Timer timer = Timer
      .builder("timer")
      .histogram(Histogram.linearTime(TimeUnit.MILLISECONDS, 0, 200, 3))
      .register(registry);

    timer.record(1000, TimeUnit.MILLISECONDS);
    timer.record(23, TimeUnit.MILLISECONDS);
    timer.record(450, TimeUnit.MILLISECONDS);
    timer.record(341, TimeUnit.MILLISECONDS);
    timer.record(500, TimeUnit.MILLISECONDS);

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

    assertThat(histograms, allOf(hasEntry("bucket=0.0", 0), hasEntry("bucket=2.0E8", 1), hasEntry("bucket=4.0E8", 1), hasEntry("bucket=Infinity", 3)));

}
 
Example #5
Source File: MicrometerMetricsPublisherThreadPoolTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
/**
 * Test that thread pool metrics are published.
 */
@Test
void testMetricIds() {
    HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance().getMetricsPublisher();
    HystrixPlugins.reset();
    HystrixPlugins.getInstance().registerMetricsPublisher(new MicrometerMetricsPublisher(registry, metricsPublisher));

    HystrixCommandKey key = HystrixCommandKey.Factory.asKey("MicrometerCOMMAND-A");
    new SampleCommand(key).execute();

    final Tags tags = Tags.of("key", NAME_MICROMETER_GROUP);

    final Set<MeterId> actualMeterIds = registry.getMeters().stream()
        .map(meter -> new MeterId(meter.getId().getName(), meter.getId().getType(), Tags.of(meter.getId().getTags())))
        .collect(Collectors.toSet());

    final Set<MeterId> expectedMeterIds = new HashSet<>(Arrays.asList(
        new MeterId(metricName("threads.active.current.count"), Type.GAUGE, tags),
        new MeterId(metricName("threads.cumulative.count"), Type.COUNTER, tags.and(Tags.of("type", "executed"))),
        new MeterId(metricName("threads.cumulative.count"), Type.COUNTER, tags.and(Tags.of("type", "rejected"))),
        new MeterId(metricName("threads.pool.current.size"), Type.GAUGE, tags),
        new MeterId(metricName("threads.largest.pool.current.size"), Type.GAUGE, tags),
        new MeterId(metricName("threads.max.pool.current.size"), Type.GAUGE, tags),
        new MeterId(metricName("threads.core.pool.current.size"), Type.GAUGE, tags),
        new MeterId(metricName("tasks.cumulative.count"), Type.COUNTER, tags.and(Tags.of("type", "scheduled"))),
        new MeterId(metricName("tasks.cumulative.count"), Type.COUNTER, tags.and(Tags.of("type", "completed"))),
        new MeterId(metricName("queue.current.size"), Type.GAUGE, tags),
        new MeterId(metricName("queue.max.size"), Type.GAUGE, tags),
        new MeterId(metricName("queue.rejection.threshold.size"), Type.GAUGE, tags)
    ));

    assertThat(actualMeterIds).containsAll(expectedMeterIds);
}
 
Example #6
Source File: ServiceLevelObjectiveConfiguration.java    From micrometer with Apache License 2.0 5 votes vote down vote up
@Bean
HealthMeterRegistry healthMeterRegistry() {
    HealthMeterRegistry registry = HealthMeterRegistry.builder(HealthConfig.DEFAULT)
            .serviceLevelObjectives(JvmServiceLevelObjectives.MEMORY)
            .serviceLevelObjectives(OperatingSystemServiceLevelObjectives.DISK)
            .serviceLevelObjectives(
                    ServiceLevelObjective.build("api.error.ratio")
                            .failedMessage("API error ratio")
                            .baseUnit(BaseUnits.PERCENT)
                            .tag("uri.matches", "/api/**")
                            .tag("error.outcome", "SERVER_ERROR")
                            .errorRatio(
                                    s -> s.name("http.server.requests").tag("uri", uri -> uri.startsWith("/api")),
                                    all -> all.tag("outcome", "SERVER_ERROR")
                            ).isLessThan(0.01)
            )
            .build();

    for (ServiceLevelObjective slo : registry.getServiceLevelObjectives()) {
        applicationContext.registerBean(
                camelCasedHealthIndicatorNames.name(slo.getName(), Type.GAUGE),
                HealthContributor.class,
                () -> toHealthContributor(registry, slo)
        );
    }

    return registry;
}
 
Example #7
Source File: ServiceLevelObjectiveConfiguration.java    From micrometer with Apache License 2.0 5 votes vote down vote up
private HealthContributor toHealthContributor(HealthMeterRegistry registry, ServiceLevelObjective slo) {
    if (slo instanceof ServiceLevelObjective.SingleIndicator) {
        return new AbstractHealthIndicator(slo.getFailedMessage()) {
            @Override
            protected void doHealthCheck(Health.Builder builder) {
                ServiceLevelObjective.SingleIndicator singleIndicator = (ServiceLevelObjective.SingleIndicator) slo;
                builder.status(slo.healthy(registry) ? Status.UP : Status.OUT_OF_SERVICE)
                        .withDetail("value", singleIndicator.getValueAsString(registry))
                        .withDetail("mustBe", singleIndicator.getTestDescription());

                for (Tag tag : slo.getTags()) {
                    builder.withDetail(camelCasedHealthIndicatorNames.tagKey(tag.getKey()), tag.getValue());
                }

                if (slo.getBaseUnit() != null) {
                    builder.withDetail("unit", slo.getBaseUnit());
                }
            }
        };
    } else {
        ServiceLevelObjective.MultipleIndicator multipleIndicator = (ServiceLevelObjective.MultipleIndicator) slo;
        Map<String, HealthContributor> objectiveIndicators = Arrays.stream(multipleIndicator.getObjectives())
                .collect(
                        Collectors.toMap(
                                indicator -> camelCasedHealthIndicatorNames.name(indicator.getName(), Type.GAUGE),
                                indicator -> toHealthContributor(registry, indicator)
                        )
                );
        return CompositeHealthContributor.fromMap(objectiveIndicators);
    }
}
 
Example #8
Source File: MicrometerMetricsPublisherThreadPoolTest.java    From micrometer with Apache License 2.0 4 votes vote down vote up
MeterId(final String name, final Type type, final Tags tags) {
    this.name = name;
    this.type = type;
    this.tags = tags;
}
 
Example #9
Source File: NewRelicNamingConventionTest.java    From micrometer with Apache License 2.0 4 votes vote down vote up
@Test
void name() {
  String validString = newRelicNamingConvention.name(VALID_NAME, Type.COUNTER);
  assertThat(validString).isEqualTo(VALID_NAME);
}
 
Example #10
Source File: NewRelicNamingConventionTest.java    From micrometer with Apache License 2.0 4 votes vote down vote up
@Test
void nameShouldStripInvalidCharacters() {
  String validString = newRelicNamingConvention.name(INVALID_NAME, Type.COUNTER);
  assertThat(validString).isEqualTo("invalid_name");
}
 
Example #11
Source File: AzureMonitorNamingConventionTest.java    From micrometer with Apache License 2.0 4 votes vote down vote up
@Test
void testNameContainsDesiredCharacters() {
    assertThat(namingConvention.name("{[email protected]}", Type.GAUGE)).isEqualTo("_custom_Metric_1_");
}
 
Example #12
Source File: NoopMeterRegistry.java    From armeria with Apache License 2.0 4 votes vote down vote up
@Override
protected Meter newMeter(Id id, Type type, Iterable<Measurement> measurements) {
    return new NoopMeter(id);
}
 
Example #13
Source File: DefaultDestinationPublishingMeterRegistry.java    From spring-cloud-stream with Apache License 2.0 4 votes vote down vote up
@Override
protected Meter newMeter(Id id, Type type, Iterable<Measurement> measurements) {
	return new DefaultMeter(id, type, measurements);
}