Skip to content

Commit

Permalink
Update the Prometheus JMX Collector and Client (#909)
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Scholz <[email protected]>
  • Loading branch information
scholzj authored Jun 4, 2024
1 parent a43839e commit 46e8df1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 0.30.0

* Dependency updates (Prometheus JMX Collector 1.0.1, Prometheus Client 1.3.1)

## 0.29.0

* Dependency updates (Vert.x 4.5.8, Netty 4.1.110.Final)
Expand Down
13 changes: 7 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@
<opentelemetry-semconv.version>1.21.0-alpha</opentelemetry-semconv.version>
<grpc-netty-shaded.version>1.61.0</grpc-netty-shaded.version>
<micrometer.version>1.12.3</micrometer.version>
<jmx-prometheus-collector.version>0.18.0</jmx-prometheus-collector.version>
<prometheus-simpleclient.version>0.16.0</prometheus-simpleclient.version>
<jmx-prometheus-collector.version>1.0.1</jmx-prometheus-collector.version>
<prometheus-client.version>1.3.1</prometheus-client.version>
<commons-cli.version>1.4</commons-cli.version>
<test-container.version>0.106.0</test-container.version>
<jakarta.version>2.3.2</jakarta.version>
Expand Down Expand Up @@ -292,13 +292,13 @@
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>${prometheus-simpleclient.version}</version>
<artifactId>prometheus-metrics-model</artifactId>
<version>${prometheus-client.version}</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_common</artifactId>
<version>${prometheus-simpleclient.version}</version>
<artifactId>prometheus-metrics-exposition-formats</artifactId>
<version>${prometheus-client.version}</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
Expand Down Expand Up @@ -493,6 +493,7 @@
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<tarLongFileMode>posix</tarLongFileMode>
<descriptors>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</descriptors>
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/io/strimzi/kafka/bridge/JmxCollectorRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@

package io.strimzi.kafka.bridge;

import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.exporter.common.TextFormat;
import io.prometheus.jmx.JmxCollector;
import io.prometheus.metrics.expositionformats.PrometheusTextFormatWriter;
import io.prometheus.metrics.model.registry.PrometheusRegistry;

import javax.management.MalformedObjectNameException;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;

/**
* Allow to collect JMX metrics exposing them in the Prometheus format
*/
public class JmxCollectorRegistry {

private final CollectorRegistry collectorRegistry;
private final PrometheusRegistry collectorRegistry;
private final PrometheusTextFormatWriter textFormatter = new PrometheusTextFormatWriter(true);

/**
* Constructor
Expand All @@ -30,7 +30,7 @@ public class JmxCollectorRegistry {
*/
public JmxCollectorRegistry(String yamlConfig) throws MalformedObjectNameException {
new JmxCollector(yamlConfig).register();
collectorRegistry = CollectorRegistry.defaultRegistry;
collectorRegistry = PrometheusRegistry.defaultRegistry;
}

/**
Expand All @@ -42,20 +42,20 @@ public JmxCollectorRegistry(String yamlConfig) throws MalformedObjectNameExcepti
*/
public JmxCollectorRegistry(File yamlFileConfig) throws MalformedObjectNameException, IOException {
new JmxCollector(yamlFileConfig).register();
collectorRegistry = CollectorRegistry.defaultRegistry;
collectorRegistry = PrometheusRegistry.defaultRegistry;
}

/**
* @return Content that should be included in the response body for an endpoint designated for
* Prometheus to scrape from.
*/
public String scrape() {
Writer writer = new StringWriter();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
TextFormat.write004(writer, collectorRegistry.metricFamilySamples());
textFormatter.write(stream, collectorRegistry.scrape());
} catch (IOException e) {
throw new RuntimeException(e);
}
return writer.toString();
return stream.toString(StandardCharsets.UTF_8);
}
}
26 changes: 26 additions & 0 deletions src/main/resources/jmx_metrics_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@ lowercaseOutputName: true

rules:
# more specific rules to consumer and producer with topic related information
- pattern: kafka.producer<type=(.+), client-id=(.+), topic=(.+)><>([a-z-]+)-total
name: strimzi_bridge_kafka_producer_$4_total
type: COUNTER
labels:
type: "$1"
clientId: "$2"
topic: "$3"
- pattern: kafka.producer<type=(.+), client-id=(.+), topic=(.+)><>([a-z-]+)
name: strimzi_bridge_kafka_producer_$4
type: GAUGE
labels:
type: "$1"
clientId: "$2"
topic: "$3"
- pattern: kafka.consumer<type=(.+), client-id=(.+), topic=(.+)><>([a-z-]+)-total
name: strimzi_bridge_kafka_consumer_$4_total
type: COUNTER
labels:
type: "$1"
clientId: "$2"
topic: "$3"
- pattern: kafka.consumer<type=(.+), client-id=(.+), topic=(.+)><>([a-z-]+)
name: strimzi_bridge_kafka_consumer_$4
type: GAUGE
Expand All @@ -17,6 +31,18 @@ rules:
clientId: "$2"
topic: "$3"
# more general metrics
- pattern: kafka.(\w+)<type=(.+), client-id=(.+)><>([a-z-]+-total-[a-z-]+) # handles the metrics with total in the middle of the metric name
name: strimzi_bridge_kafka_$1_$4
type: GAUGE
labels:
type: "$2"
clientId: "$3"
- pattern: kafka.(\w+)<type=(.+), client-id=(.+)><>([a-z-]+)-total
name: strimzi_bridge_kafka_$1_$4_total
type: COUNTER
labels:
type: "$2"
clientId: "$3"
- pattern: kafka.(\w+)<type=(.+), client-id=(.+)><>([a-z-]+)
name: strimzi_bridge_kafka_$1_$4
type: GAUGE
Expand Down

0 comments on commit 46e8df1

Please sign in to comment.