Skip to content

Commit

Permalink
Upgrade prometheus to 1.3.2 (#53)
Browse files Browse the repository at this point in the history
Signed-off-by: Mickael Maison <[email protected]>
  • Loading branch information
mimaison authored Oct 24, 2024
1 parent af31146 commit 5c5197e
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 59 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
<sonatype.nexus.staging>1.7.0</sonatype.nexus.staging>

<kafka.version>3.7.0</kafka.version>
<prometheus.version>1.3.1</prometheus.version>
<prometheus.version>1.3.2</prometheus.version>
<yammer.version>2.2.0</yammer.version>
<slf4j.version>2.0.13</slf4j.version>
<junit.version>5.10.2</junit.version>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/io/strimzi/kafka/metrics/MetricsCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public interface MetricsCollector {

/**
* Collect all the metrics added to this Collector
*
* @return the list of metrics of this collector
*/
List<MetricSnapshot> collect();
List<MetricSnapshot<?>> collect();
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void addCollector(MetricsCollector collector) {
*/
@Override
public MetricSnapshots collect() {
List<MetricSnapshot> snapshots = new ArrayList<>();
List<MetricSnapshot<?>> snapshots = new ArrayList<>();
for (MetricsCollector collector : collectors) {
snapshots.addAll(collector.collect());
}
Expand Down
17 changes: 7 additions & 10 deletions src/main/java/io/strimzi/kafka/metrics/kafka/KafkaCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ public void removeMetric(MetricName name) {

/**
* Collect all the metrics added to this Collector
*
* @return the list of metrics of this collector
*/
@Override
public List<MetricSnapshot> collect() {
Map<String, GaugeSnapshot.Builder> gaugeBuilders = new HashMap<>();
Map<String, InfoSnapshot.Builder> infoBuilders = new HashMap<>();
public List<MetricSnapshot<?>> collect() {
Map<String, MetricSnapshot.Builder<?>> builders = new HashMap<>();
for (MetricWrapper metricWrapper : kafkaMetrics.values()) {
String prometheusMetricName = metricWrapper.prometheusName();
Object metricValue = ((KafkaMetric) metricWrapper.metric()).metricValue();
Expand All @@ -93,18 +93,15 @@ public List<MetricSnapshot> collect() {

if (metricValue instanceof Number) {
double value = ((Number) metricValue).doubleValue();
GaugeSnapshot.Builder builder = gaugeBuilders.computeIfAbsent(prometheusMetricName, k -> GaugeSnapshot.builder().name(prometheusMetricName));
GaugeSnapshot.Builder builder = (GaugeSnapshot.Builder) builders.computeIfAbsent(prometheusMetricName, k -> GaugeSnapshot.builder().name(prometheusMetricName));
builder.dataPoint(DataPointSnapshotBuilder.gaugeDataPoint(labels, value));
} else {
InfoSnapshot.Builder builder = infoBuilders.computeIfAbsent(prometheusMetricName, k -> InfoSnapshot.builder().name(prometheusMetricName));
InfoSnapshot.Builder builder = (InfoSnapshot.Builder) builders.computeIfAbsent(prometheusMetricName, k -> InfoSnapshot.builder().name(prometheusMetricName));
builder.dataPoint(DataPointSnapshotBuilder.infoDataPoint(labels, metricValue, metricWrapper.attribute()));
}
}
List<MetricSnapshot> snapshots = new ArrayList<>();
for (GaugeSnapshot.Builder builder : gaugeBuilders.values()) {
snapshots.add(builder.build());
}
for (InfoSnapshot.Builder builder : infoBuilders.values()) {
List<MetricSnapshot<?>> snapshots = new ArrayList<>();
for (MetricSnapshot.Builder<?> builder : builders.values()) {
snapshots.add(builder.build());
}
return snapshots;
Expand Down
33 changes: 11 additions & 22 deletions src/main/java/io/strimzi/kafka/metrics/yammer/YammerCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,13 @@ public void removeMetric(MetricName name) {

/**
* Collect all the metrics added to this Collector
*
* @return the list of metrics of this collector
*/
@SuppressWarnings({"CyclomaticComplexity", "JavaNCSS"})
@Override
public List<MetricSnapshot> collect() {
Map<String, CounterSnapshot.Builder> counterBuilders = new HashMap<>();
Map<String, GaugeSnapshot.Builder> gaugeBuilders = new HashMap<>();
Map<String, InfoSnapshot.Builder> infoBuilders = new HashMap<>();
Map<String, SummarySnapshot.Builder> summaryBuilders = new HashMap<>();
public List<MetricSnapshot<?>> collect() {
Map<String, MetricSnapshot.Builder<?>> builders = new HashMap<>();
for (MetricWrapper metricWrapper : yammerMetrics.values()) {
String prometheusMetricName = metricWrapper.prometheusName();
Object metric = metricWrapper.metric();
Expand All @@ -100,45 +98,36 @@ public List<MetricSnapshot> collect() {

if (metric instanceof Counter) {
Counter counter = (Counter) metric;
CounterSnapshot.Builder builder = counterBuilders.computeIfAbsent(prometheusMetricName, k -> CounterSnapshot.builder().name(prometheusMetricName));
CounterSnapshot.Builder builder = (CounterSnapshot.Builder) builders.computeIfAbsent(prometheusMetricName, k -> CounterSnapshot.builder().name(prometheusMetricName));
builder.dataPoint(DataPointSnapshotBuilder.counterDataPoint(labels, counter.count()));
} else if (metric instanceof Gauge) {
Object valueObj = ((Gauge<?>) metric).value();
if (valueObj instanceof Number) {
double value = ((Number) valueObj).doubleValue();
GaugeSnapshot.Builder builder = gaugeBuilders.computeIfAbsent(prometheusMetricName, k -> GaugeSnapshot.builder().name(prometheusMetricName));
GaugeSnapshot.Builder builder = (GaugeSnapshot.Builder) builders.computeIfAbsent(prometheusMetricName, k -> GaugeSnapshot.builder().name(prometheusMetricName));
builder.dataPoint(DataPointSnapshotBuilder.gaugeDataPoint(labels, value));
} else {
InfoSnapshot.Builder builder = infoBuilders.computeIfAbsent(prometheusMetricName, k -> InfoSnapshot.builder().name(prometheusMetricName));
InfoSnapshot.Builder builder = (InfoSnapshot.Builder) builders.computeIfAbsent(prometheusMetricName, k -> InfoSnapshot.builder().name(prometheusMetricName));
builder.dataPoint(DataPointSnapshotBuilder.infoDataPoint(labels, valueObj, metricWrapper.attribute()));
}
} else if (metric instanceof Timer) {
Timer timer = (Timer) metric;
SummarySnapshot.Builder builder = summaryBuilders.computeIfAbsent(prometheusMetricName, k -> SummarySnapshot.builder().name(prometheusMetricName));
SummarySnapshot.Builder builder = (SummarySnapshot.Builder) builders.computeIfAbsent(prometheusMetricName, k -> SummarySnapshot.builder().name(prometheusMetricName));
builder.dataPoint(DataPointSnapshotBuilder.summaryDataPoint(labels, timer.count(), timer.sum(), quantiles(timer)));
} else if (metric instanceof Histogram) {
Histogram histogram = (Histogram) metric;
SummarySnapshot.Builder builder = summaryBuilders.computeIfAbsent(prometheusMetricName, k -> SummarySnapshot.builder().name(prometheusMetricName));
SummarySnapshot.Builder builder = (SummarySnapshot.Builder) builders.computeIfAbsent(prometheusMetricName, k -> SummarySnapshot.builder().name(prometheusMetricName));
builder.dataPoint(DataPointSnapshotBuilder.summaryDataPoint(labels, histogram.count(), histogram.sum(), quantiles(histogram)));
} else if (metric instanceof Meter) {
Meter meter = (Meter) metric;
CounterSnapshot.Builder builder = counterBuilders.computeIfAbsent(prometheusMetricName, k -> CounterSnapshot.builder().name(prometheusMetricName));
CounterSnapshot.Builder builder = (CounterSnapshot.Builder) builders.computeIfAbsent(prometheusMetricName, k -> CounterSnapshot.builder().name(prometheusMetricName));
builder.dataPoint(DataPointSnapshotBuilder.counterDataPoint(labels, meter.count()));
} else {
LOG.error("The metric {} has an unexpected type: {}", prometheusMetricName, metric.getClass().getName());
}
}
List<MetricSnapshot> snapshots = new ArrayList<>();
for (GaugeSnapshot.Builder builder : gaugeBuilders.values()) {
snapshots.add(builder.build());
}
for (CounterSnapshot.Builder builder : counterBuilders.values()) {
snapshots.add(builder.build());
}
for (InfoSnapshot.Builder builder : infoBuilders.values()) {
snapshots.add(builder.build());
}
for (SummarySnapshot.Builder builder : summaryBuilders.values()) {
List<MetricSnapshot<?>> snapshots = new ArrayList<>();
for (MetricSnapshot.Builder<?> builder : builders.values()) {
snapshots.add(builder.build());
}
return snapshots;
Expand Down
14 changes: 8 additions & 6 deletions src/test/java/io/strimzi/kafka/metrics/MetricsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
/**
* Utility class to create and retrieve metrics
*/
@SuppressWarnings("ClassFanOutComplexity")
public class MetricsUtils {

/**
Expand Down Expand Up @@ -94,7 +95,7 @@ public T value() {
* @param expectedValue the expected value
* @param expectedLabels the expected labels
*/
public static void assertGaugeSnapshot(MetricSnapshot snapshot, double expectedValue, Labels expectedLabels) {
public static void assertGaugeSnapshot(MetricSnapshot<?> snapshot, double expectedValue, Labels expectedLabels) {
assertInstanceOf(GaugeSnapshot.class, snapshot);
GaugeSnapshot gaugeSnapshot = (GaugeSnapshot) snapshot;
assertEquals(1, gaugeSnapshot.getDataPoints().size());
Expand All @@ -109,7 +110,7 @@ public static void assertGaugeSnapshot(MetricSnapshot snapshot, double expectedV
* @param expectedValue the expected value
* @param expectedLabels the expected labels
*/
public static void assertCounterSnapshot(MetricSnapshot snapshot, double expectedValue, Labels expectedLabels) {
public static void assertCounterSnapshot(MetricSnapshot<?> snapshot, double expectedValue, Labels expectedLabels) {
assertInstanceOf(CounterSnapshot.class, snapshot);
CounterSnapshot counterSnapshot = (CounterSnapshot) snapshot;
assertEquals(1, counterSnapshot.getDataPoints().size());
Expand All @@ -125,11 +126,12 @@ public static void assertCounterSnapshot(MetricSnapshot snapshot, double expecte
* @param newLabelName the expected new label name
* @param newLabelValue the expected new label value
*/
public static void assertInfoSnapshot(MetricSnapshot snapshot, Labels labels, String newLabelName, String newLabelValue) {
public static void assertInfoSnapshot(MetricSnapshot<?> snapshot, Labels labels, String newLabelName, String newLabelValue) {
assertInstanceOf(InfoSnapshot.class, snapshot);
assertEquals(1, snapshot.getDataPoints().size());
InfoSnapshot infoSnapshot = (InfoSnapshot) snapshot;
assertEquals(1, infoSnapshot.getDataPoints().size());
Labels expectedLabels = labels.add(newLabelName, newLabelValue);
assertEquals(expectedLabels, snapshot.getDataPoints().get(0).getLabels());
assertEquals(expectedLabels, infoSnapshot.getDataPoints().get(0).getLabels());
}

/**
Expand All @@ -140,7 +142,7 @@ public static void assertInfoSnapshot(MetricSnapshot snapshot, Labels labels, St
* @param expectedLabels the expected labels
* @param expectedQuantiles the expected quantiles
*/
public static void assertSummarySnapshot(MetricSnapshot snapshot, int expectedCount, double expectedSum, Labels expectedLabels, Quantiles expectedQuantiles) {
public static void assertSummarySnapshot(MetricSnapshot<?> snapshot, int expectedCount, double expectedSum, Labels expectedLabels, Quantiles expectedQuantiles) {
assertInstanceOf(SummarySnapshot.class, snapshot);
SummarySnapshot summarySnapshot = (SummarySnapshot) snapshot;
assertEquals(1, summarySnapshot.getDataPoints().size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testCollect() {
Labels labels = Labels.of("l1", "v1", "l2", "v2");
double value = 2.0;
prometheusCollector.addCollector(() -> {
List<MetricSnapshot> snapshots = new ArrayList<>();
List<MetricSnapshot<?>> snapshots = new ArrayList<>();
snapshots.add(GaugeSnapshot.builder()
.name("gauge")
.dataPoint(DataPointSnapshotBuilder.gaugeDataPoint(labels, value))
Expand All @@ -60,7 +60,7 @@ public void testCollect() {
Quantiles quantiles = Quantiles.of(new Quantile(0.9, value));
String metricName = "name";
prometheusCollector.addCollector(() -> {
List<MetricSnapshot> snapshots = new ArrayList<>();
List<MetricSnapshot<?>> snapshots = new ArrayList<>();
snapshots.add(InfoSnapshot.builder()
.name("info")
.dataPoint(DataPointSnapshotBuilder.infoDataPoint(labels, value, metricName))
Expand All @@ -79,8 +79,8 @@ public void testCollect() {
assertSummarySnapshot(findSnapshot(snapshots, SummarySnapshot.class), count, value, labels, quantiles);
}

private MetricSnapshot findSnapshot(MetricSnapshots snapshots, Class<?> clazz) {
for (MetricSnapshot snapshot : snapshots) {
private MetricSnapshot<?> findSnapshot(MetricSnapshots snapshots, Class<?> clazz) {
for (MetricSnapshot<?> snapshot : snapshots) {
if (clazz.isInstance(snapshot)) {
return snapshot;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void setup() {
public void testCollectKafkaMetrics() {
KafkaCollector collector = new KafkaCollector();

List<MetricSnapshot> metrics = collector.collect();
List<? extends MetricSnapshot<?>> metrics = collector.collect();
assertEquals(0, metrics.size());

// Adding a metric
Expand All @@ -56,15 +56,13 @@ public void testCollectKafkaMetrics() {

metrics = collector.collect();
assertEquals(1, metrics.size());
MetricSnapshot snapshot = metrics.get(0);
assertGaugeSnapshot(snapshot, value.get(), labels);
assertGaugeSnapshot(metrics.get(0), value.get(), labels);

// Updating the value of the metric
value.set(3);
metrics = collector.collect();
assertEquals(1, metrics.size());
MetricSnapshot updatedSnapshot = metrics.get(0);
assertGaugeSnapshot(updatedSnapshot, 3, labels);
assertGaugeSnapshot(metrics.get(0), 3, labels);

// Removing a metric
collector.removeMetric(metricName);
Expand All @@ -76,7 +74,7 @@ public void testCollectKafkaMetrics() {
public void testCollectNonNumericKafkaMetric() {
KafkaCollector collector = new KafkaCollector();

List<MetricSnapshot> metrics = collector.collect();
List<? extends MetricSnapshot<?>> metrics = collector.collect();
assertEquals(0, metrics.size());

// Adding a non-numeric metric converted
Expand All @@ -87,7 +85,7 @@ public void testCollectNonNumericKafkaMetric() {
metrics = collector.collect();

assertEquals(1, metrics.size());
MetricSnapshot snapshot = metrics.get(0);
MetricSnapshot<?> snapshot = metrics.get(0);
assertEquals(metricWrapper.prometheusName(), snapshot.getMetadata().getName());
assertInfoSnapshot(snapshot, labels, "name", nonNumericValue);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void setup() {
public void testCollectYammerMetrics() {
YammerCollector collector = new YammerCollector();

List<MetricSnapshot> metrics = collector.collect();
List<? extends MetricSnapshot<?>> metrics = collector.collect();
assertEquals(0, metrics.size());

// Adding a metric
Expand All @@ -52,15 +52,13 @@ public void testCollectYammerMetrics() {

metrics = collector.collect();
assertEquals(1, metrics.size());
MetricSnapshot snapshot = metrics.get(0);
assertGaugeSnapshot(snapshot, value.get(), labels);
assertGaugeSnapshot(metrics.get(0), value.get(), labels);

// Updating the value of the metric
value.set(3);
metrics = collector.collect();
assertEquals(1, metrics.size());
MetricSnapshot updatedSnapshot = metrics.get(0);
assertGaugeSnapshot(updatedSnapshot, 3, labels);
assertGaugeSnapshot(metrics.get(0), 3, labels);

// Removing the metric
collector.removeMetric(metricName);
Expand All @@ -72,7 +70,7 @@ public void testCollectYammerMetrics() {
public void testCollectNonNumericYammerMetrics() {
YammerCollector collector = new YammerCollector();

List<MetricSnapshot> metrics = collector.collect();
List<? extends MetricSnapshot<?>> metrics = collector.collect();
assertEquals(0, metrics.size());

String nonNumericValue = "value";
Expand All @@ -82,7 +80,7 @@ public void testCollectNonNumericYammerMetrics() {
metrics = collector.collect();

assertEquals(1, metrics.size());
MetricSnapshot snapshot = metrics.get(0);
MetricSnapshot<?> snapshot = metrics.get(0);
assertEquals(metricWrapper.prometheusName(), snapshot.getMetadata().getName());
assertInfoSnapshot(snapshot, labels, "name", nonNumericValue);
}
Expand Down

0 comments on commit 5c5197e

Please sign in to comment.