- API geared aimed at application developers rather than Prometheus experts.
- Name-based metrics; no need to create instances or handle registration.
- Cleaner timer / summary syntax, via resource/try
- Aim is to simplify Prometheus adoption, reduce excessive code intrusion.
- Lexical compatibility with Codahale/Dropwizard Metrics API, simplifying complete migration.
- Regular Prometheus API can always be used directly for more advanced cases (unclear what those might be).
Also @AlertRule
API for defining alert rules in Java code and exporting rules files to disk (Prometheus 1.x format, or YAML for 2.x).
private final PrometheusMetrics metrics;
public void onUserLogin(Object event) {
metrics.gauge("Sessions.open").inc();
metrics.counter("Sessions.total").inc();
}
public String handleLogin() {
try (Context timer = metrics.summary("Sessions.handleLogin").time()) {
return "Login handled!";
}
}
Configure a common prefix...
@Value("${spring.application.name}") // "MyApp"
private String prefixToUse;
@Bean
public PrometheusMetrics prometheusMetrics(CollectorRegistry collector) {
return new PrometheusMetrics(collector, prefixToUse);
}
All created metrics will use that (normalised) prefix:
metrics.counter("counter_1").inc();
assertThat(samplesString(registry)).startsWith("[Name: myapp_counter_1 Type: COUNTER ");
Defaulted if not set:
metrics.timer("transaction"); // ==> "transaction"
metrics.timer("transaction", "Transaction time"); // ==> "Transaction time"
Alternatively, load a Java Properties
file like:
transaction = Transaction time
when you create your PrometheusMetrics object, e.g.
final Properties props = new Properties();
try (Reader r = Files.newReader( new File("descriptions.properties"), Charsets.UTF_8)) {
props.load(r);
}
metrics.setDescriptionMappings(props);
Now you can use the simpler API:
metrics.timer("transaction"); // ==> "Transaction time"
By default, summaries will get the following percentiles, rather than a simple median:
50%, 75%, 90%, 95%, 99%, 99.9%
metrics.error("salesforce");
assertThat(registry.getSampleValue("myapp_errors", \
new String[]{"error_type"}, \
new String[]{"salesforce"})).isEqualTo(1.0d);
// ...
metrics.error("transaction");
assertThat(registry.getSampleValue("myapp_errors", \
new String[]{"error_type"}, \
new String[]{"transaction"})).isEqualTo(1.0d);
- All names lowercased
.
,-
,#
,_