Skip to content
Closed
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
15 changes: 15 additions & 0 deletions implementations/micrometer-registry-prometheus_native/build.gradle
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')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
compatibleVersion=SKIP
Copy link
Contributor

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?

Copy link
Member

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.

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();
}

}
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();
}

}
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();
}

}
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();
}

}
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;
}

}
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();
}

}
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());
}

}
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;
}

}
Loading