Add application metrics in the catalog backend 60/128360/4
authorandre.schmid <andre.schmid@est.tech>
Wed, 13 Oct 2021 15:03:43 +0000 (16:03 +0100)
committerMichael Morris <michael.morris@est.tech>
Thu, 14 Apr 2022 09:48:51 +0000 (09:48 +0000)
A new endpoint was introduced sdc2/rest/actuator/prometheus in the
catalog backend, that returns application metrics in a format
consumable by prometheus, using the micrometer library.

Change-Id: I03542e1c1a9b8b12d4e00f86e5b02c597740934b
Issue-ID: SDC-3957
Signed-off-by: andre.schmid <andre.schmid@est.tech>
catalog-be/pom.xml
catalog-be/src/main/java/org/openecomp/sdc/be/servlets/PrometheusMetricsServlet.java [new file with mode: 0644]
catalog-be/src/main/java/org/openecomp/sdc/config/MicrometerSpringConfig.java [new file with mode: 0644]
catalog-be/src/main/resources/application-context.xml

index 9df0805..2d74490 100644 (file)
@@ -19,6 +19,7 @@
         <maven-dependency-plugin.version>3.2.0</maven-dependency-plugin.version>
         <replacer.plugin.version>1.5.3</replacer.plugin.version>
         <ui.version>${project.version}</ui.version>
+        <micrometer.version>1.8.4</micrometer.version>
     </properties>
 
     <dependencies>
             </exclusions>
         </dependency>
 
+      <dependency>
+          <groupId>io.micrometer</groupId>
+          <artifactId>micrometer-registry-prometheus</artifactId>
+          <version>${micrometer.version}</version>
+      </dependency>
+
         <dependency>
             <groupId>ch.qos.logback</groupId>
             <artifactId>logback-classic</artifactId>
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/PrometheusMetricsServlet.java b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/PrometheusMetricsServlet.java
new file mode 100644 (file)
index 0000000..a90164a
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * -
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2022 Nordix Foundation.
+ *  ================================================================================
+ *  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
+ *
+ *       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.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdc.be.servlets;
+
+import io.micrometer.core.instrument.binder.jetty.JettyConnectionMetrics;
+import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics;
+import io.micrometer.core.instrument.binder.jvm.JvmCompilationMetrics;
+import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics;
+import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics;
+import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics;
+import io.micrometer.core.instrument.binder.system.FileDescriptorMetrics;
+import io.micrometer.core.instrument.binder.system.ProcessorMetrics;
+import io.micrometer.core.instrument.binder.system.UptimeMetrics;
+import io.micrometer.prometheus.PrometheusMeterRegistry;
+import io.prometheus.client.exporter.common.TextFormat;
+import io.swagger.v3.oas.annotations.Operation;
+import io.swagger.v3.oas.annotations.Parameter;
+import io.swagger.v3.oas.annotations.servers.Server;
+import io.swagger.v3.oas.annotations.tags.Tag;
+import javax.ws.rs.GET;
+import javax.ws.rs.HeaderParam;
+import javax.ws.rs.Path;
+import org.openecomp.sdc.common.api.Constants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Controller;
+
+@Controller
+@Path("/actuator")
+@Tag(name = "SDCE-2 APIs")
+@Server(url = "/sdc2/rest")
+public class PrometheusMetricsServlet {
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(PrometheusMetricsServlet.class);
+
+    private final PrometheusMeterRegistry prometheusRegistry;
+
+    public PrometheusMetricsServlet(final PrometheusMeterRegistry prometheusRegistry) {
+        this.prometheusRegistry = prometheusRegistry;
+        new ClassLoaderMetrics().bindTo(prometheusRegistry);
+        try (final JvmGcMetrics jvmGcMetrics = new JvmGcMetrics()) {
+            jvmGcMetrics.bindTo(prometheusRegistry);
+        } catch (final Exception e) {
+            LOGGER.error("Could not instantiate the JVM GC Metrics", e);
+        }
+        new JvmMemoryMetrics().bindTo(prometheusRegistry);
+        new JvmCompilationMetrics().bindTo(prometheusRegistry);
+        new JvmThreadMetrics().bindTo(prometheusRegistry);
+        new ProcessorMetrics().bindTo(prometheusRegistry);
+        new UptimeMetrics().bindTo(prometheusRegistry);
+        new FileDescriptorMetrics().bindTo(prometheusRegistry);
+        new JettyConnectionMetrics(prometheusRegistry);
+    }
+
+    @GET
+    @Path("/prometheus")
+    @Operation(summary = "Prometheus Micrometer Metrics", description = "Gets the prometheus micrometer application metrics")
+    public String prometheus(
+        @Parameter(description = "The Accept header to determine the output content type")
+        @HeaderParam(value = Constants.ACCEPT_HEADER) String acceptHeader) {
+        return prometheusRegistry.scrape(TextFormat.chooseContentType(acceptHeader));
+    }
+
+}
diff --git a/catalog-be/src/main/java/org/openecomp/sdc/config/MicrometerSpringConfig.java b/catalog-be/src/main/java/org/openecomp/sdc/config/MicrometerSpringConfig.java
new file mode 100644 (file)
index 0000000..d7e62f7
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * -
+ *  ============LICENSE_START=======================================================
+ *  Copyright (C) 2022 Nordix Foundation.
+ *  ================================================================================
+ *  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
+ *
+ *       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.
+ *
+ *  SPDX-License-Identifier: Apache-2.0
+ *  ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.sdc.config;
+
+import io.micrometer.prometheus.PrometheusConfig;
+import io.micrometer.prometheus.PrometheusMeterRegistry;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class MicrometerSpringConfig {
+
+    @Bean(name = "prometheusRegistry")
+    PrometheusMeterRegistry prometheusRegistry() {
+        return new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
+    }
+
+}
index 403debf..39ac4f8 100644 (file)
@@ -22,6 +22,7 @@
   <bean class="org.openecomp.sdc.be.config.CatalogModelSpringConfig"/>
   <bean class="org.openecomp.sdc.be.components.distribution.engine.config.DistributionEngineSpringConfig"/>
   <bean class="org.openecomp.sdc.config.CatalogBESpringConfig"/>
+  <bean class="org.openecomp.sdc.config.MicrometerSpringConfig"/>
 
 
   <aop:config>