This is an automated email from the ASF dual-hosted git repository.
jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/karaf-decanter.git
The following commit(s) were added to refs/heads/main by this push:
new 978cd70a [KARAF-7841] Use Prometheus labels and create 1 event per
metric in the Prometheus-Collector (#337)
978cd70a is described below
commit 978cd70aeaaf6f4bbcd11c37c1ab90ebb3e14c6f
Author: François Papon <[email protected]>
AuthorDate: Sat Mar 15 07:37:56 2025 +0100
[KARAF-7841] Use Prometheus labels and create 1 event per metric in the
Prometheus-Collector (#337)
---
.../collector/prometheus/PrometheusCollector.java | 43 ++++++++++++++--------
.../prometheus/PrometheusCollectorTest.java | 18 ++++++++-
collector/prometheus/src/test/resources/sample.txt | 28 ++++----------
pom.xml | 1 +
4 files changed, 52 insertions(+), 38 deletions(-)
diff --git
a/collector/prometheus/src/main/java/org/apache/karaf/decanter/collector/prometheus/PrometheusCollector.java
b/collector/prometheus/src/main/java/org/apache/karaf/decanter/collector/prometheus/PrometheusCollector.java
index 386c4550..7563c5dd 100644
---
a/collector/prometheus/src/main/java/org/apache/karaf/decanter/collector/prometheus/PrometheusCollector.java
+++
b/collector/prometheus/src/main/java/org/apache/karaf/decanter/collector/prometheus/PrometheusCollector.java
@@ -31,9 +31,11 @@ import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
+import java.util.Arrays;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;
+import java.util.stream.Stream;
@Component(
name = "org.apache.karaf.decanter.collector.prometheus",
@@ -53,6 +55,7 @@ public class PrometheusCollector implements Runnable {
private Dictionary<String, Object> properties;
private URL prometheusURL;
+ private String type = "prometheus";
@Activate
public void activate(ComponentContext componentContext) throws Exception {
@@ -71,28 +74,38 @@ public class PrometheusCollector implements Runnable {
public void run() {
try {
URLConnection connection = prometheusURL.openConnection();
- Map<String, Object> data = new HashMap<>();
- data.put("type", "prometheus");
+ String topic = (properties.get(EventConstants.EVENT_TOPIC) !=
null) ? (String) properties.get(EventConstants.EVENT_TOPIC) :
"decanter/collect/prometheus";
try (BufferedReader reader = new BufferedReader(new
InputStreamReader(connection.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
- if (line.matches("# TYPE .* gauge")) {
- line = reader.readLine();
- if (line == null) {
- break;
- }
- String[] split = line.split(" ");
- if (split.length == 2) {
- String property = split[0];
- double value = Double.parseDouble(split[1]);
- data.put(property, value);
+ if (!line.matches("# .*")) {
+ Map<String, Object> data = new HashMap<>();
+ data.put("type", type);
+ if (line.contains("{")) {
+ final String name = line.substring(0,
line.indexOf("{"));
+ String[] labels =
line.substring(line.indexOf("{")+1, line.indexOf("}")).split(",");
+ Stream.of(labels).forEach(it -> {
+ String labelName = it.substring(0,
it.indexOf("=")).replace("\"", "");
+ String labelValue =
it.substring(it.indexOf("=")+1).replace("\"", "");
+ data.put(labelName, labelValue);
+ });
+
+ String value = line.substring(line.indexOf("}")+2);
+ Double parseValue = Double.parseDouble(value);
+ data.put(name, parseValue);
+ } else {
+ String[] split = line.split(" ");
+ if (split.length == 2) {
+ String property = split[0];
+ double value = Double.parseDouble(split[1]);
+ data.put(property, value);
+ }
}
+ PropertiesPreparator.prepare(data, properties);
+ dispatcher.postEvent(new Event(topic, data));
}
}
}
- PropertiesPreparator.prepare(data, properties);
- String topic = (properties.get(EventConstants.EVENT_TOPIC) !=
null) ? (String) properties.get(EventConstants.EVENT_TOPIC) :
"decanter/collect/prometheus";
- dispatcher.postEvent(new Event(topic, data));
} catch (Exception e) {
LOGGER.warn("Can't get Prometheus metrics", e);
e.printStackTrace();
diff --git
a/collector/prometheus/src/test/java/org/apache/karaf/decanter/collector/prometheus/PrometheusCollectorTest.java
b/collector/prometheus/src/test/java/org/apache/karaf/decanter/collector/prometheus/PrometheusCollectorTest.java
index b5e200a6..551baa49 100644
---
a/collector/prometheus/src/test/java/org/apache/karaf/decanter/collector/prometheus/PrometheusCollectorTest.java
+++
b/collector/prometheus/src/test/java/org/apache/karaf/decanter/collector/prometheus/PrometheusCollectorTest.java
@@ -38,11 +38,25 @@ public class PrometheusCollectorTest {
prometheusCollector.run();
- Assert.assertEquals(1, dispatcher.postedEvents.size());
+ Assert.assertEquals(5, dispatcher.postedEvents.size());
Assert.assertEquals(0.0,
dispatcher.postedEvents.get(0).getProperty("Test1"));
- Assert.assertEquals(8.0,
dispatcher.postedEvents.get(0).getProperty("Test2"));
Assert.assertEquals("prometheus",
dispatcher.postedEvents.get(0).getProperty("type"));
+
+ Assert.assertEquals(8.0,
dispatcher.postedEvents.get(1).getProperty("Test2"));
+ Assert.assertEquals("prometheus",
dispatcher.postedEvents.get(1).getProperty("type"));
+
+ Assert.assertEquals(1.0,
dispatcher.postedEvents.get(2).getProperty("request_time_total_seconds"));
+ Assert.assertEquals("/send",
dispatcher.postedEvents.get(2).getProperty("endpoint"));
+ Assert.assertEquals("prometheus",
dispatcher.postedEvents.get(2).getProperty("type"));
+
+ Assert.assertEquals(2.2561752E7,
dispatcher.postedEvents.get(3).getProperty("memoryPool_usage_bytes"));
+ Assert.assertEquals("Compressed Class Space",
dispatcher.postedEvents.get(3).getProperty("name"));
+ Assert.assertEquals("prometheus",
dispatcher.postedEvents.get(3).getProperty("type"));
+
+ Assert.assertEquals(1.2582912E7,
dispatcher.postedEvents.get(4).getProperty("memoryPool_usage_bytes"));
+ Assert.assertEquals("G1 Eden Space",
dispatcher.postedEvents.get(4).getProperty("name"));
+ Assert.assertEquals("prometheus",
dispatcher.postedEvents.get(4).getProperty("type"));
}
class MockDispatcher implements EventAdmin {
diff --git a/collector/prometheus/src/test/resources/sample.txt
b/collector/prometheus/src/test/resources/sample.txt
index eca383f9..52662482 100644
--- a/collector/prometheus/src/test/resources/sample.txt
+++ b/collector/prometheus/src/test/resources/sample.txt
@@ -1,27 +1,13 @@
-<!--
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you 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
-
- http://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.
--->
# HELP Test1 Test1
# TYPE Test1 gauge
Test1 0.0
# HELP Test2 Test2
# TYPE Test2 gauge
Test2 8.0
-# HELP Test3 Test3
-# TYPE Test3 histogram
-Test3 ...
\ No newline at end of file
+# HELP request_time_total
+# TYPE request_time_total counter
+request_time_total_seconds{api="my-api",endpoint="/send",method="POST"} 1.0
+# HELP memoryPool_usage_bytes Current usage of the memory pool
+# TYPE memoryPool_usage_bytes gauge
+memoryPool_usage_bytes{name="Compressed Class Space"} 2.2561752E7
+memoryPool_usage_bytes{name="G1 Eden Space"} 1.2582912E7
diff --git a/pom.xml b/pom.xml
index 34e4675c..cf724305 100644
--- a/pom.xml
+++ b/pom.xml
@@ -477,6 +477,7 @@
<exclude>**/*.svg</exclude>
<exclude>**/*.less</exclude>
<exclude>**/build.txt</exclude>
+ <exclude>**/sample.txt</exclude>
<exclude>**/components/**/*</exclude>
<exclude>**/bower_components/**/*</exclude>
<exclude>**/resources/config</exclude>