This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch mm
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 4f585e2a4a73583eec163a81f266b01dac87ac1f
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Wed Nov 15 21:29:28 2023 +0100

    CAMEL-20105: camel-micromemter - Make it easier to configure for camel-main
---
 components/camel-micrometer-prometheus/pom.xml     |  6 ++-
 .../services/org/apache/camel/other.properties     |  2 +-
 .../generated/resources/micrometer-prometheus.json |  2 +-
 .../prometheus/MicrometerPrometheus.java           | 48 ++++++++++++++++++++--
 4 files changed, 51 insertions(+), 7 deletions(-)

diff --git a/components/camel-micrometer-prometheus/pom.xml 
b/components/camel-micrometer-prometheus/pom.xml
index 0bbb96068f9..72cb76f0fa3 100644
--- a/components/camel-micrometer-prometheus/pom.xml
+++ b/components/camel-micrometer-prometheus/pom.xml
@@ -29,7 +29,7 @@
     <artifactId>camel-micrometer-prometheus</artifactId>
     <packaging>jar</packaging>
     <name>Camel :: Micrometer Prometheus</name>
-    <description>Camel Micrometer Prometheus</description>
+    <description>Camel Micrometer Prometheus for Camel Main</description>
 
     <properties>
         <firstVersion>4.3.0</firstVersion>
@@ -41,6 +41,10 @@
             <groupId>org.apache.camel</groupId>
             <artifactId>camel-micrometer</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.apache.camel</groupId>
+            <artifactId>camel-platform-http-main</artifactId>
+        </dependency>
 
         <dependency>
             <groupId>io.micrometer</groupId>
diff --git 
a/components/camel-micrometer-prometheus/src/generated/resources/META-INF/services/org/apache/camel/other.properties
 
b/components/camel-micrometer-prometheus/src/generated/resources/META-INF/services/org/apache/camel/other.properties
index bb8c34ad8e2..5c5e29db9e2 100644
--- 
a/components/camel-micrometer-prometheus/src/generated/resources/META-INF/services/org/apache/camel/other.properties
+++ 
b/components/camel-micrometer-prometheus/src/generated/resources/META-INF/services/org/apache/camel/other.properties
@@ -4,4 +4,4 @@ groupId=org.apache.camel
 artifactId=camel-micrometer-prometheus
 version=4.3.0-SNAPSHOT
 projectName=Camel :: Micrometer Prometheus
-projectDescription=Camel Micrometer Prometheus
+projectDescription=Camel Micrometer Prometheus for Camel Main
diff --git 
a/components/camel-micrometer-prometheus/src/generated/resources/micrometer-prometheus.json
 
b/components/camel-micrometer-prometheus/src/generated/resources/micrometer-prometheus.json
index de860450126..429ab2db753 100644
--- 
a/components/camel-micrometer-prometheus/src/generated/resources/micrometer-prometheus.json
+++ 
b/components/camel-micrometer-prometheus/src/generated/resources/micrometer-prometheus.json
@@ -3,7 +3,7 @@
     "kind": "other",
     "name": "micrometer-prometheus",
     "title": "Micrometer Prometheus",
-    "description": "Camel Micrometer Prometheus",
+    "description": "Camel Micrometer Prometheus for Camel Main",
     "deprecated": false,
     "firstVersion": "4.3.0",
     "supportLevel": "Preview",
diff --git 
a/components/camel-micrometer-prometheus/src/main/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheus.java
 
b/components/camel-micrometer-prometheus/src/main/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheus.java
index 36946b45423..f909665a4d6 100644
--- 
a/components/camel-micrometer-prometheus/src/main/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheus.java
+++ 
b/components/camel-micrometer-prometheus/src/main/java/org/apache/camel/component/micrometer/prometheus/MicrometerPrometheus.java
@@ -18,6 +18,11 @@ package org.apache.camel.component.micrometer.prometheus;
 
 import io.micrometer.prometheus.PrometheusConfig;
 import io.micrometer.prometheus.PrometheusMeterRegistry;
+import io.vertx.core.Handler;
+import io.vertx.core.http.HttpMethod;
+import io.vertx.ext.web.Route;
+import io.vertx.ext.web.RoutingContext;
+import io.vertx.ext.web.impl.BlockingHandlerDecorator;
 import org.apache.camel.CamelContext;
 import org.apache.camel.StaticService;
 import org.apache.camel.api.management.ManagedResource;
@@ -25,6 +30,9 @@ import 
org.apache.camel.component.micrometer.eventnotifier.MicrometerExchangeEve
 import 
org.apache.camel.component.micrometer.eventnotifier.MicrometerRouteEventNotifier;
 import 
org.apache.camel.component.micrometer.messagehistory.MicrometerMessageHistoryFactory;
 import 
org.apache.camel.component.micrometer.routepolicy.MicrometerRoutePolicyFactory;
+import org.apache.camel.component.platform.http.PlatformHttpComponent;
+import org.apache.camel.component.platform.http.vertx.VertxPlatformHttpRouter;
+import org.apache.camel.component.platform.http.vertx.VertxPlatformHttpServer;
 import org.apache.camel.spi.CamelMetricsService;
 import org.apache.camel.spi.Configurer;
 import org.apache.camel.spi.ManagementStrategy;
@@ -41,9 +49,12 @@ public class MicrometerPrometheus extends ServiceSupport 
implements CamelMetrics
 
     private static final Logger LOG = 
LoggerFactory.getLogger(MicrometerPrometheus.class);
 
-    // TODO: option to configure prometheus
-    // TODO: context-path / port for http scrape service
-    // TODO: use platform-http-main
+    private VertxPlatformHttpServer server;
+    private VertxPlatformHttpRouter router;
+    private PlatformHttpComponent platformHttpComponent;
+
+    // TODO: option to configure prometheus (on or off)
+    // TODO: 0.0.4 or 1.0.0 text format
     // TODO: dev console
 
     private CamelContext camelContext;
@@ -149,6 +160,35 @@ public class MicrometerPrometheus extends ServiceSupport 
implements CamelMetrics
             camelContext.setMessageHistoryFactory(factory);
         }
 
-        LOG.info("MicrometerPrometheus enabled");
+        server = camelContext.hasService(VertxPlatformHttpServer.class);
+        router = VertxPlatformHttpRouter.lookup(camelContext);
+        platformHttpComponent = camelContext.getComponent("platform-http", 
PlatformHttpComponent.class);
+
+        if (server != null && router != null && platformHttpComponent != null) 
{
+            setupHttpScraper();
+            LOG.info("MicrometerPrometheus enabled with HTTP scraping on 
/q/metrics");
+        } else {
+            LOG.info("MicrometerPrometheus enabled");
+        }
+    }
+
+    protected void setupHttpScraper() {
+        Route metrcis = router.route("/q/metrics");
+        metrcis.method(HttpMethod.GET);
+        metrcis.produces("text/plain;version=0.0.4;charset=utf-8");
+
+        Handler<RoutingContext> handler = new Handler<RoutingContext>() {
+            @Override
+            public void handle(RoutingContext ctx) {
+                ctx.response().putHeader("content-type", 
"text/plain;version=0.0.4;charset=utf-8");
+                String data = meterRegistry.scrape();
+                ctx.response().write(data);
+            }
+        };
+
+        // use blocking handler as the task can take longer time to complete
+        metrcis.handler(new BlockingHandlerDecorator(handler, true));
+
+        platformHttpComponent.addHttpEndpoint("/q/metrics", null, null);
     }
 }

Reply via email to