From cd2747ff0dc6a02c03f2b17ce986d28b3abdf773 Mon Sep 17 00:00:00 2001 From: "andre.schmid" Date: Wed, 13 Oct 2021 16:03:43 +0100 Subject: Add application metrics in the catalog backend 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 --- catalog-be/pom.xml | 7 ++ .../sdc/be/servlets/PrometheusMetricsServlet.java | 83 ++++++++++++++++++++++ .../sdc/config/MicrometerSpringConfig.java | 37 ++++++++++ .../src/main/resources/application-context.xml | 1 + 4 files changed, 128 insertions(+) create mode 100644 catalog-be/src/main/java/org/openecomp/sdc/be/servlets/PrometheusMetricsServlet.java create mode 100644 catalog-be/src/main/java/org/openecomp/sdc/config/MicrometerSpringConfig.java (limited to 'catalog-be') diff --git a/catalog-be/pom.xml b/catalog-be/pom.xml index 9df08050d4..2d7449015b 100644 --- a/catalog-be/pom.xml +++ b/catalog-be/pom.xml @@ -19,6 +19,7 @@ 3.2.0 1.5.3 ${project.version} + 1.8.4 @@ -170,6 +171,12 @@ + + io.micrometer + micrometer-registry-prometheus + ${micrometer.version} + + ch.qos.logback logback-classic 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 index 0000000000..a90164a9c4 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/be/servlets/PrometheusMetricsServlet.java @@ -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 index 0000000000..d7e62f7ea6 --- /dev/null +++ b/catalog-be/src/main/java/org/openecomp/sdc/config/MicrometerSpringConfig.java @@ -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); + } + +} diff --git a/catalog-be/src/main/resources/application-context.xml b/catalog-be/src/main/resources/application-context.xml index 403debfb08..39ac4f8892 100644 --- a/catalog-be/src/main/resources/application-context.xml +++ b/catalog-be/src/main/resources/application-context.xml @@ -22,6 +22,7 @@ + -- cgit 1.2.3-korg