-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add Support for Prometheus Native Histograms #3987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+1,267
−1
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
implementations/micrometer-registry-prometheus_native/build.gradle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| repositories { | ||
| mavenLocal() | ||
| mavenCentral() | ||
| } | ||
|
|
||
| dependencies { | ||
| api project(':micrometer-core') | ||
|
|
||
| api 'io.prometheus:prometheus-metrics-core:1.0.0-alpha-2' | ||
| api 'io.prometheus:prometheus-metrics-model:1.0.0-alpha-2' | ||
| api 'io.prometheus:prometheus-metrics-exposition-formats:1.0.0-alpha-2' | ||
| api 'io.prometheus:prometheus-metrics-exporter-servlet-jakarta:1.0.0-alpha-2' | ||
|
|
||
| testImplementation project(':micrometer-test') | ||
| } |
1 change: 1 addition & 0 deletions
1
implementations/micrometer-registry-prometheus_native/gradle.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| compatibleVersion=SKIP | ||
62 changes: 62 additions & 0 deletions
62
...crometer-registry-prometheus_native/src/main/java/io/micrometer/prometheusnative/Max.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * Copyright 2023 VMware, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micrometer.prometheusnative; | ||
|
|
||
| import io.micrometer.core.instrument.distribution.DistributionStatisticConfig; | ||
| import io.prometheus.metrics.core.metrics.SlidingWindow; | ||
|
|
||
| /** | ||
| * Micrometer distributions have a {@code max} value, which is not provided out-of-the-box | ||
| * by Prometheus histograms or summaries. | ||
| * <p> | ||
| * Max is used to track these {@code max} values. | ||
| */ | ||
| public class Max { | ||
|
|
||
| private static class MaxObserver { | ||
|
|
||
| private double current = Double.NaN; | ||
|
|
||
| public void observe(double value) { | ||
| if (Double.isNaN(current) || current < value) { | ||
| current = value; | ||
| } | ||
| } | ||
|
|
||
| public double get() { | ||
| return current; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private final SlidingWindow<MaxObserver> slidingWindow; | ||
|
|
||
| public Max(DistributionStatisticConfig config) { | ||
| long maxAgeSeconds = config.getExpiry() != null ? config.getExpiry().toMillis() / 1000L : 60; | ||
| int ageBuckets = config.getBufferLength() != null ? config.getBufferLength() : 3; | ||
| slidingWindow = new SlidingWindow<>(MaxObserver.class, MaxObserver::new, MaxObserver::observe, maxAgeSeconds, | ||
| ageBuckets); | ||
| } | ||
|
|
||
| public void observe(double value) { | ||
| slidingWindow.observe(value); | ||
| } | ||
|
|
||
| public double get() { | ||
| return slidingWindow.current().get(); | ||
| } | ||
|
|
||
| } |
35 changes: 35 additions & 0 deletions
35
...stry-prometheus_native/src/main/java/io/micrometer/prometheusnative/PrometheusConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| * Copyright 2023 VMware, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micrometer.prometheusnative; | ||
|
|
||
| import io.micrometer.core.instrument.config.MeterRegistryConfig; | ||
| import io.prometheus.metrics.config.PrometheusProperties; | ||
|
|
||
| public interface PrometheusConfig extends MeterRegistryConfig { | ||
|
|
||
| @Override | ||
| default String prefix() { | ||
| return "prometheus"; | ||
| } | ||
|
|
||
| default PrometheusProperties getPrometheusProperties() { | ||
| // To be discussed: Make Prometheus properties configurable via Spring's | ||
| // application.properties | ||
| // under the `management.metrics.export.prometheus` prefix? | ||
| return PrometheusProperties.get(); | ||
| } | ||
|
|
||
| } |
49 changes: 49 additions & 0 deletions
49
...try-prometheus_native/src/main/java/io/micrometer/prometheusnative/PrometheusCounter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * Copyright 2023 VMware, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micrometer.prometheusnative; | ||
|
|
||
| import io.micrometer.common.lang.Nullable; | ||
| import io.prometheus.metrics.core.datapoints.CounterDataPoint; | ||
| import io.prometheus.metrics.core.metrics.Counter; | ||
| import io.prometheus.metrics.model.snapshots.CounterSnapshot.CounterDataPointSnapshot; | ||
| import io.prometheus.metrics.model.snapshots.Exemplar; | ||
|
|
||
| public class PrometheusCounter extends PrometheusMeter<Counter, CounterDataPointSnapshot> | ||
| implements io.micrometer.core.instrument.Counter { | ||
|
|
||
| private final CounterDataPoint dataPoint; | ||
|
|
||
| PrometheusCounter(Id id, Counter counter, CounterDataPoint dataPoint) { | ||
| super(id, counter); | ||
| this.dataPoint = dataPoint; | ||
| } | ||
|
|
||
| @Override | ||
| public void increment(double amount) { | ||
| dataPoint.inc(amount); | ||
| } | ||
|
|
||
| @Override | ||
| public double count() { | ||
| return collect().getValue(); | ||
| } | ||
|
|
||
| @Nullable | ||
| Exemplar exemplar() { | ||
| return collect().getExemplar(); | ||
| } | ||
|
|
||
| } |
34 changes: 34 additions & 0 deletions
34
...us_native/src/main/java/io/micrometer/prometheusnative/PrometheusCounterWithCallback.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Copyright 2023 VMware, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micrometer.prometheusnative; | ||
|
|
||
| import io.micrometer.core.instrument.FunctionCounter; | ||
| import io.prometheus.metrics.core.metrics.CounterWithCallback; | ||
| import io.prometheus.metrics.model.snapshots.CounterSnapshot.CounterDataPointSnapshot; | ||
|
|
||
| public class PrometheusCounterWithCallback extends PrometheusMeter<CounterWithCallback, CounterDataPointSnapshot> | ||
| implements FunctionCounter { | ||
|
|
||
| public PrometheusCounterWithCallback(Id id, CounterWithCallback counter) { | ||
| super(id, counter); | ||
| } | ||
|
|
||
| @Override | ||
| public double count() { | ||
| return collect().getValue(); | ||
| } | ||
|
|
||
| } |
47 changes: 47 additions & 0 deletions
47
...ometheus_native/src/main/java/io/micrometer/prometheusnative/PrometheusFunctionTimer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * Copyright 2023 VMware, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micrometer.prometheusnative; | ||
|
|
||
| import io.micrometer.core.instrument.FunctionTimer; | ||
| import io.micrometer.core.instrument.Meter; | ||
| import io.prometheus.metrics.core.metrics.SummaryWithCallback; | ||
| import io.prometheus.metrics.model.snapshots.SummarySnapshot.SummaryDataPointSnapshot; | ||
|
|
||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| public class PrometheusFunctionTimer extends PrometheusMeter<SummaryWithCallback, SummaryDataPointSnapshot> | ||
| implements FunctionTimer { | ||
|
|
||
| public PrometheusFunctionTimer(Meter.Id id, SummaryWithCallback summary) { | ||
| super(id, summary); | ||
| } | ||
|
|
||
| @Override | ||
| public double count() { | ||
| return collect().getCount(); | ||
| } | ||
|
|
||
| @Override | ||
| public double totalTime(TimeUnit unit) { | ||
| return toUnit(collect().getSum(), unit); | ||
| } | ||
|
|
||
| @Override | ||
| public TimeUnit baseTimeUnit() { | ||
| return TimeUnit.SECONDS; | ||
| } | ||
|
|
||
| } |
34 changes: 34 additions & 0 deletions
34
...heus_native/src/main/java/io/micrometer/prometheusnative/PrometheusGaugeWithCallback.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Copyright 2023 VMware, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micrometer.prometheusnative; | ||
|
|
||
| import io.micrometer.core.instrument.Gauge; | ||
| import io.prometheus.metrics.core.metrics.GaugeWithCallback; | ||
| import io.prometheus.metrics.model.snapshots.GaugeSnapshot.GaugeDataPointSnapshot; | ||
|
|
||
| public class PrometheusGaugeWithCallback extends PrometheusMeter<GaugeWithCallback, GaugeDataPointSnapshot> | ||
| implements Gauge { | ||
|
|
||
| public PrometheusGaugeWithCallback(Id id, GaugeWithCallback gauge) { | ||
| super(id, gauge); | ||
| } | ||
|
|
||
| @Override | ||
| public double value() { | ||
| return collect().getValue(); | ||
| } | ||
|
|
||
| } |
63 changes: 63 additions & 0 deletions
63
...y-prometheus_native/src/main/java/io/micrometer/prometheusnative/PrometheusHistogram.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * Copyright 2023 VMware, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micrometer.prometheusnative; | ||
|
|
||
| import io.micrometer.core.instrument.DistributionSummary; | ||
| import io.micrometer.core.instrument.distribution.HistogramSnapshot; | ||
| import io.prometheus.metrics.core.datapoints.DistributionDataPoint; | ||
| import io.prometheus.metrics.core.metrics.Histogram; | ||
| import io.prometheus.metrics.model.snapshots.HistogramSnapshot.HistogramDataPointSnapshot; | ||
|
|
||
| public class PrometheusHistogram extends PrometheusMeter<Histogram, HistogramDataPointSnapshot> | ||
| implements DistributionSummary { | ||
|
|
||
| private final Max max; | ||
|
|
||
| private final DistributionDataPoint dataPoint; | ||
|
|
||
| public PrometheusHistogram(Id id, Max max, Histogram histogram, DistributionDataPoint dataPoint) { | ||
| super(id, histogram); | ||
| this.max = max; | ||
| this.dataPoint = dataPoint; | ||
| } | ||
|
|
||
| @Override | ||
| public void record(double amount) { | ||
| dataPoint.observe(amount); | ||
| max.observe(amount); | ||
| } | ||
|
|
||
| @Override | ||
| public long count() { | ||
| return collect().getCount(); | ||
| } | ||
|
|
||
| @Override | ||
| public double totalAmount() { | ||
| return collect().getSum(); | ||
| } | ||
|
|
||
| @Override | ||
| public double max() { | ||
| return max.get(); | ||
| } | ||
|
|
||
| @Override | ||
| public HistogramSnapshot takeSnapshot() { | ||
| return HistogramSnapshot.empty(count(), totalAmount(), max()); | ||
| } | ||
|
|
||
| } |
44 changes: 44 additions & 0 deletions
44
...y-prometheus_native/src/main/java/io/micrometer/prometheusnative/PrometheusInfoGauge.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Copyright 2023 VMware, Inc. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.micrometer.prometheusnative; | ||
|
|
||
| import io.micrometer.core.instrument.Gauge; | ||
| import io.micrometer.core.instrument.Meter; | ||
|
|
||
| /** | ||
| * Prometheus Info metric. | ||
| * <p> | ||
| * Micrometer implements Info metrics as Gauge. | ||
| */ | ||
| public class PrometheusInfoGauge implements Gauge { | ||
|
|
||
| private final Meter.Id id; | ||
|
|
||
| public PrometheusInfoGauge(Id id) { | ||
| this.id = id; | ||
| } | ||
|
|
||
| @Override | ||
| public double value() { | ||
| return 1.0; | ||
| } | ||
|
|
||
| @Override | ||
| public Id getId() { | ||
| return id; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where is the version number?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for the API/ABI compatibility check. Since there isn't a previous version of this module to compare with, this configuration skips the compatibility check so the build doesn't fail.