Java源码示例:io.micrometer.core.instrument.Meter.Type
示例1
@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)));
}
示例2
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)));
}
示例3
@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)));
}
示例4
@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)));
}
示例5
/**
* 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);
}
示例6
@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;
}
示例7
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);
}
}
示例8
MeterId(final String name, final Type type, final Tags tags) {
this.name = name;
this.type = type;
this.tags = tags;
}
示例9
@Test
void name() {
String validString = newRelicNamingConvention.name(VALID_NAME, Type.COUNTER);
assertThat(validString).isEqualTo(VALID_NAME);
}
示例10
@Test
void nameShouldStripInvalidCharacters() {
String validString = newRelicNamingConvention.name(INVALID_NAME, Type.COUNTER);
assertThat(validString).isEqualTo("invalid_name");
}
示例11
@Test
void testNameContainsDesiredCharacters() {
assertThat(namingConvention.name("{[email protected]}", Type.GAUGE)).isEqualTo("_custom_Metric_1_");
}
示例12
@Override
protected Meter newMeter(Id id, Type type, Iterable<Measurement> measurements) {
return new NoopMeter(id);
}
示例13
@Override
protected Meter newMeter(Id id, Type type, Iterable<Measurement> measurements) {
return new DefaultMeter(id, type, measurements);
}