diff options
author | andre.schmid <andre.schmid@est.tech> | 2021-10-13 16:03:43 +0100 |
---|---|---|
committer | Michael Morris <michael.morris@est.tech> | 2022-04-14 09:48:51 +0000 |
commit | cd2747ff0dc6a02c03f2b17ce986d28b3abdf773 (patch) | |
tree | 46043d5a1eb3c65fea9453f9cb154bae78715dc1 | |
parent | 2cebeb91247c9c1a1fcd45a1bfec56e632d04501 (diff) |
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 <andre.schmid@est.tech>
4 files changed, 128 insertions, 0 deletions
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 @@ <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> @@ -170,6 +171,12 @@ </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 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 @@ <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> |