Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
description = 'MeterRegistry implementation for Prometheus using io.prometheus:prometheus-metrics-core. If you have compatibility issues with this module, you can go back to io.micrometer:micrometer-registry-prometheus-simpleclient that uses io.prometheus:simpleclient_common'
description = 'MeterRegistry implementation for Prometheus using io.prometheus:prometheus-metrics-core. If you have compatibility issues with this module, you can go back to io.micrometer:micrometer-registry-prometheus-simpleclient that uses io.prometheus:simpleclient_common.'

dependencies {
api project(':micrometer-core')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,8 @@ String getConventionName() {
return conventionName;
}

Family<T> addSamples(Collection<DataPointSnapshot> dataPointSnapshots) {
for (DataPointSnapshot dataPointSnapshot : dataPointSnapshots) {
this.dataPointSnapshots.add((T) dataPointSnapshot);
}
Family<T> addSamples(Collection<T> dataPointSnapshots) {
this.dataPointSnapshots.addAll(dataPointSnapshots);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*
* @author Jon Schneider
* @author Jonatan Ivanov
* @since 1.13.0
*/
public interface PrometheusConfig extends MeterRegistryConfig {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*
* @author Jon Schneider
* @author Jonatan Ivanov
* @since 1.13.0
*/
public class PrometheusCounter extends AbstractMeter implements Counter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
*
* @author Jon Schneider
* @author Jonatan Ivanov
* @since 1.13.0
*/
public class PrometheusDistributionSummary extends AbstractDistributionSummary {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
*/
package io.micrometer.prometheusmetrics;

/**
* {@link PrometheusNamingConvention} with {@code _duration} suffix for timers.
*
* @author Clint Checketts
* @since 1.13.0
*/
public class PrometheusDurationNamingConvention extends PrometheusNamingConvention {

public PrometheusDurationNamingConvention() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
* @author Jon Schneider
* @author Johnny Lim
* @author Jonatan Ivanov
* @since 1.13.0
*/
public class PrometheusMeterRegistry extends MeterRegistry {

Expand Down Expand Up @@ -128,7 +129,6 @@ public String scrape() {
* @param contentType the scrape Content-Type
* @return the scrape body
* @see ExpositionFormats
* @since 1.7.0
*/
public String scrape(String contentType) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Expand Down Expand Up @@ -175,7 +175,6 @@ private void scrape(OutputStream outputStream, String contentType, MetricSnapsho
* @return Content that should be included in the response body for an endpoint
* designated for Prometheus to scrape from.
* @see ExpositionFormats
* @since 1.7.0
*/
public String scrape(String contentType, @Nullable Set<String> includedNames) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Expand All @@ -197,7 +196,6 @@ public String scrape(String contentType, @Nullable Set<String> includedNames) {
* {@code null}.
* @throws IOException if writing fails
* @see ExpositionFormats
* @since 1.7.0
*/
public void scrape(OutputStream outputStream, String contentType, @Nullable Set<String> includedNames)
throws IOException {
Expand Down Expand Up @@ -258,7 +256,7 @@ public DistributionSummary newDistributionSummary(Meter.Id id,
// TODO: remove this cumulative -> non cumulative conversion
// ClassicHistogramBuckets is not cumulative but the
// histograms we use are cumulative
// so we converting it to non-cumulative just for the
// so we convert it to non-cumulative just for the
// Prometheus client library
// can convert it back to cumulative.
buckets.add(histogramCounts[0].bucket());
Expand Down Expand Up @@ -287,7 +285,7 @@ public DistributionSummary newDistributionSummary(Meter.Id id,
// a bunch of Collector.MetricFamilySamples.Sample
// that has an le label for Prometheus and a vmrange label for
// Victoria.
// That control is gone now, we don’t have control over the
// That control is gone now, so we don’t have control over the
// output and when HistogramDataPointSnapshot is written, the
// bucket name is hardcoded to le.
}
Expand Down Expand Up @@ -482,7 +480,7 @@ private void addDistributionStatisticSamples(Meter.Id id, MicrometerCollector co
// TODO: remove this cumulative -> non cumulative conversion
// ClassicHistogramBuckets is not cumulative but the histograms we
// use are cumulative
// so we converting it to non-cumulative just for the Prometheus
// so we convert it to non-cumulative just for the Prometheus
// client library
// can convert it back to cumulative.
buckets.add(histogramCounts[0].bucket(getBaseTimeUnit()));
Expand Down Expand Up @@ -511,7 +509,7 @@ private void addDistributionStatisticSamples(Meter.Id id, MicrometerCollector co
// a bunch of Collector.MetricFamilySamples.Sample
// that has an le label for Prometheus and a vmrange label for
// Victoria.
// That control is gone now, we don’t have control over the
// That control is gone now, so we don’t have control over the
// output and when HistogramDataPointSnapshot is written, the
// bucket name is hardcoded to le.
}
Expand Down Expand Up @@ -604,7 +602,6 @@ protected DistributionStatisticConfig defaultHistogramConfig() {
* MeterRegistry.Config#onMeterRegistrationFailed(BiConsumer)} when you want meters
* with the same name but different tags to cause an unchecked exception.
* @return This registry
* @since 1.6.0
*/
public PrometheusMeterRegistry throwExceptionOnRegistrationFailure() {
config().onMeterRegistrationFailed((id, reason) -> {
Expand All @@ -616,17 +613,15 @@ public PrometheusMeterRegistry throwExceptionOnRegistrationFailure() {

private enum Format {

TEXT_004("text/plain; version=0.0.4; charset=utf-8"),
OPENMETRICS_100("application/openmetrics-text; version=1.0.0; charset=utf-8"),
PROTOBUF("application/vnd.google.protobuf; proto=io.prometheus.client.MetricFamily; encoding=delimited");
TEXT_004("text/plain; version=0.0.4; charset=utf-8");

private final String contentType;

Format(String contentType) {
this.contentType = contentType;
}

public String getContentType() {
String getContentType() {
return contentType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* docs</a> for a specification of the constraints on metric names and labels
*
* @author Jon Schneider
* @since 1.13.0
*/
public class PrometheusNamingConvention implements NamingConvention {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* name.
*
* @author Tommy Ludwig
* @since 1.13.0
*/
public class PrometheusRenameFilter implements MeterFilter {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*
* @author Jon Schneider
* @author Jonatan Ivanov
* @since 1.13.0
*/
public class PrometheusTimer extends AbstractTimer {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

import static io.micrometer.core.instrument.MockClock.clock;
import static java.util.Collections.emptyList;
Expand Down Expand Up @@ -161,12 +160,10 @@ void typedCustomMeters() {

MetricSnapshot snapshot = registry.getPrometheusRegistry().scrape().get(0);
assertThat(snapshot).describedAs("custom counter with a type of COUNTER").isInstanceOf(CounterSnapshot.class);
assertThat(
snapshot.getDataPoints().get(0).getLabels().stream().map(Label::getName).collect(Collectors.toList()))
.containsExactly("statistic");
assertThat(
snapshot.getDataPoints().get(0).getLabels().stream().map(Label::getValue).collect(Collectors.toList()))
.containsExactly("COUNT");
assertThat(snapshot.getDataPoints().get(0).getLabels().stream().map(Label::getName)).singleElement()
.isEqualTo("statistic");
assertThat(snapshot.getDataPoints().get(0).getLabels().stream().map(Label::getValue)).singleElement()
.isEqualTo("COUNT");
}

@DisplayName("attempts to register different meter types with the same name fail somewhat gracefully")
Expand Down Expand Up @@ -379,7 +376,7 @@ void functionCounterNamingConvention() {
assertThat(registry.scrape()).contains("api_requests_total 1.0");
}

private Condition<? super Iterable<? extends MetricSnapshot>> withNameAndQuantile(String name) {
private Condition<Iterable<? extends MetricSnapshot>> withNameAndQuantile(String name) {
return new Condition<>(
metricSnapshots -> ((MetricSnapshots) metricSnapshots).stream()
.filter(snapshot -> snapshot.getMetadata().getPrometheusName().equals(name))
Expand Down Expand Up @@ -511,8 +508,8 @@ void filteredMetricFamilySamplesWithCounter() {
}

private void assertFilteredMetricSnapshots(String[] includedNames, String[] expectedNames) {
MetricSnapshots snapshots = registry.getPrometheusRegistry()
.scrape(name -> new HashSet<>(Arrays.asList(includedNames)).contains(name));
Set<String> includeNameSet = new HashSet<>(Arrays.asList(includedNames));
MetricSnapshots snapshots = registry.getPrometheusRegistry().scrape(name -> includeNameSet.contains(name));
String[] names = snapshots.stream()
.map(snapshot -> snapshot.getMetadata().getPrometheusName())
.toArray(String[]::new);
Expand Down Expand Up @@ -542,7 +539,7 @@ void filteredMetricFamilySamplesWithTimer() {
@Test
void filteredMetricFamilySamplesWithLongTaskTimer() {
String[] includedNames = { "my_long_task_timer_seconds", "my_long_task_timer_seconds_max" };
String[] expectedNames = { "my_long_task_timer_seconds_max", "my_long_task_timer_seconds" };
String[] expectedNames = { "my_long_task_timer_seconds", "my_long_task_timer_seconds_max" };

LongTaskTimer.builder("my.long.task.timer").register(registry);
assertFilteredMetricSnapshots(includedNames, expectedNames);
Expand Down Expand Up @@ -585,7 +582,7 @@ void timerSumAndMaxHaveCorrectBaseUnit_whenPercentileHistogramEnabled() {

@Test
@Issue("#4988")
void longTaskTimerRecordingsSchouldBeCorrect() {
void longTaskTimerRecordingsShouldBeCorrect() {
LongTaskTimer ltt = LongTaskTimer.builder("test.ltt").publishPercentileHistogram().register(registry);

String result = registry.scrape();
Expand Down Expand Up @@ -870,11 +867,11 @@ void openMetricsScrapeWithExemplars() throws InterruptedException {
timer.record(Duration.ofMillis(150));

Timer timerWithHistogram = Timer.builder("timer.withHistogram").publishPercentileHistogram().register(registry);
timerWithHistogram.record(15, TimeUnit.MILLISECONDS);
timerWithHistogram.record(Duration.ofMillis(15));
sleepToAvoidRateLimiting();
timerWithHistogram.record(150, TimeUnit.MILLISECONDS);
timerWithHistogram.record(Duration.ofMillis(150));
sleepToAvoidRateLimiting();
timerWithHistogram.record(60, TimeUnit.SECONDS);
timerWithHistogram.record(Duration.ofSeconds(60));

Timer timerWithSlos = Timer.builder("timer.withSlos")
.serviceLevelObjectives(Duration.ofMillis(100), Duration.ofMillis(200), Duration.ofMillis(300))
Expand All @@ -899,14 +896,14 @@ void openMetricsScrapeWithExemplars() throws InterruptedException {
sleepToAvoidRateLimiting();
summaryWithHistogram.record(15);

DistributionSummary slos = DistributionSummary.builder("summary.withSlos")
DistributionSummary summaryWithSlos = DistributionSummary.builder("summary.withSlos")
.serviceLevelObjectives(100, 200, 300)
.register(registry);
slos.record(10);
summaryWithSlos.record(10);
sleepToAvoidRateLimiting();
slos.record(1_000);
summaryWithSlos.record(1_000);
sleepToAvoidRateLimiting();
slos.record(250);
summaryWithSlos.record(250);

String scraped = registry.scrape("application/openmetrics-text");
assertThat(scraped).contains("my_counter_total 1.0 # {span_id=\"1\",trace_id=\"2\"} 1.0 ");
Expand Down Expand Up @@ -1010,10 +1007,10 @@ public Properties prometheusProperties() {
}
};

return new PrometheusMeterRegistry(prometheusConfig, prometheusRegistry, clock, new TestSpanContex());
return new PrometheusMeterRegistry(prometheusConfig, prometheusRegistry, clock, new TestSpanContext());
}

static class TestSpanContex implements SpanContext {
static class TestSpanContext implements SpanContext {

private final AtomicLong count = new AtomicLong();

Expand Down