From 0d61c432b2604f0459dea95bbd328c279b04fbef Mon Sep 17 00:00:00 2001 From: emaclee Date: Wed, 11 Dec 2024 14:58:25 +0000 Subject: Add gauge metric for NCMP "cmhandle states" Issue-ID: CPS-2456 Change-Id: I1aebcc68dfdc9c48c230c74376742d67b05c0615 Signed-off-by: emaclee --- .../java/org/onap/cps/config/MicroMeterConfig.java | 89 +++++++++++++++++++++- cps-application/src/main/resources/application.yml | 4 +- .../onap/cps/config/MicroMeterConfigSpec.groovy | 28 ++++++- 3 files changed, 112 insertions(+), 9 deletions(-) (limited to 'cps-application') diff --git a/cps-application/src/main/java/org/onap/cps/config/MicroMeterConfig.java b/cps-application/src/main/java/org/onap/cps/config/MicroMeterConfig.java index 22194f3ad8..39ed6ef5a7 100644 --- a/cps-application/src/main/java/org/onap/cps/config/MicroMeterConfig.java +++ b/cps-application/src/main/java/org/onap/cps/config/MicroMeterConfig.java @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2023 Nordix Foundation. + * Copyright (C) 2023-2025 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,17 +20,100 @@ package org.onap.cps.config; +import com.hazelcast.map.IMap; import io.micrometer.core.aop.TimedAspect; +import io.micrometer.core.instrument.Gauge; import io.micrometer.core.instrument.MeterRegistry; +import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration +@RequiredArgsConstructor public class MicroMeterConfig { + private static final String TAG = "state"; + private static final String CMHANDLE_STATE_GAUGE = "cmHandlesByState"; + final IMap cmHandlesByState; + + @Bean + public TimedAspect timedAspect(final MeterRegistry meterRegistry) { + return new TimedAspect(meterRegistry); + } + + /** + * Register gauge metric for cm handles with state 'advised'. + * + * @param meterRegistry meter registry + * @return cm handle state gauge + */ + @Bean + public Gauge advisedCmHandles(final MeterRegistry meterRegistry) { + return Gauge.builder(CMHANDLE_STATE_GAUGE, cmHandlesByState, + value -> cmHandlesByState.get("advisedCmHandlesCount")) + .tag(TAG, "ADVISED") + .description("Current number of cmhandles in advised state") + .register(meterRegistry); + } + + /** + * Register gauge metric for cm handles with state 'ready'. + * + * @param meterRegistry meter registry + * @return cm handle state gauge + */ + @Bean + public Gauge readyCmHandles(final MeterRegistry meterRegistry) { + return Gauge.builder(CMHANDLE_STATE_GAUGE, cmHandlesByState, + value -> cmHandlesByState.get("readyCmHandlesCount")) + .tag(TAG, "READY") + .description("Current number of cmhandles in ready state") + .register(meterRegistry); + } + + /** + * Register gauge metric for cm handles with state 'locked'. + * + * @param meterRegistry meter registry + * @return cm handle state gauge + */ + @Bean + public Gauge lockedCmHandles(final MeterRegistry meterRegistry) { + return Gauge.builder(CMHANDLE_STATE_GAUGE, cmHandlesByState, + value -> cmHandlesByState.get("lockedCmHandlesCount")) + .tag(TAG, "LOCKED") + .description("Current number of cmhandles in locked state") + .register(meterRegistry); + } + + /** + * Register gauge metric for cm handles with state 'deleting'. + * + * @param meterRegistry meter registry + * @return cm handle state gauge + */ + @Bean + public Gauge deletingCmHandles(final MeterRegistry meterRegistry) { + return Gauge.builder(CMHANDLE_STATE_GAUGE, cmHandlesByState, + value -> cmHandlesByState.get("deletingCmHandlesCount")) + .tag(TAG, "DELETING") + .description("Current number of cmhandles in deleting state") + .register(meterRegistry); + } + + /** + * Register gauge metric for cm handles with state 'deleted'. + * + * @param meterRegistry meter registry + * @return cm handle state gauge + */ @Bean - public TimedAspect timedAspect(final MeterRegistry registry) { - return new TimedAspect(registry); + public Gauge deletedCmHandles(final MeterRegistry meterRegistry) { + return Gauge.builder(CMHANDLE_STATE_GAUGE, cmHandlesByState, + value -> cmHandlesByState.get("deletedCmHandlesCount")) + .tag(TAG, "DELETED") + .description("Current number of cmhandles in deleted state") + .register(meterRegistry); } } diff --git a/cps-application/src/main/resources/application.yml b/cps-application/src/main/resources/application.yml index d7e39f7fae..573db1fb10 100644 --- a/cps-application/src/main/resources/application.yml +++ b/cps-application/src/main/resources/application.yml @@ -1,8 +1,8 @@ # ============LICENSE_START======================================================= # Copyright (C) 2021 Pantheon.tech # Modifications Copyright (C) 2021-2022 Bell Canada -# Modifications Copyright (C) 2021-2024 Nordix Foundation # Modifications Copyright (C) 2024 TechMahindra Ltd +# Modifications Copyright (C) 2021-2025 Nordix Foundation # ================================================================================ # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -172,7 +172,7 @@ management: endpoints: web: exposure: - include: info,health,loggers,prometheus + include: info,health,loggers,prometheus,metrics endpoint: health: show-details: always diff --git a/cps-application/src/test/groovy/org/onap/cps/config/MicroMeterConfigSpec.groovy b/cps-application/src/test/groovy/org/onap/cps/config/MicroMeterConfigSpec.groovy index 61bc2cf027..67ca64624a 100644 --- a/cps-application/src/test/groovy/org/onap/cps/config/MicroMeterConfigSpec.groovy +++ b/cps-application/src/test/groovy/org/onap/cps/config/MicroMeterConfigSpec.groovy @@ -1,6 +1,6 @@ /* * ============LICENSE_START======================================================= - * Copyright (C) 2023 Nordix Foundation. + * Copyright (C) 2023-2025 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,16 +20,36 @@ package org.onap.cps.config +import com.hazelcast.map.IMap import io.micrometer.core.instrument.simple.SimpleMeterRegistry import spock.lang.Specification class MicroMeterConfigSpec extends Specification { - def objectUnderTest = new MicroMeterConfig() + def cmHandlesByState = Mock(IMap) + def objectUnderTest = new MicroMeterConfig(cmHandlesByState) + def simpleMeterRegistry = new SimpleMeterRegistry() - def 'Creating a tined aspect.'() { + def 'Creating a timed aspect.'() { expect: ' a timed aspect can be created' - assert objectUnderTest.timedAspect(new SimpleMeterRegistry()) != null + assert objectUnderTest.timedAspect(simpleMeterRegistry) != null + } + + def 'Creating gauges for cm handle states.'() { + given: 'cache returns value for each state' + cmHandlesByState.get(_) >> 1 + when: 'gauges for each state are created' + objectUnderTest.advisedCmHandles(simpleMeterRegistry) + objectUnderTest.readyCmHandles(simpleMeterRegistry) + objectUnderTest.lockedCmHandles(simpleMeterRegistry) + objectUnderTest.deletingCmHandles(simpleMeterRegistry) + objectUnderTest.deletedCmHandles(simpleMeterRegistry) + then: 'each state has the correct value when queried' + def states = ["ADVISED", "READY", "LOCKED", "DELETING", "DELETED"] + states.each { state -> + def gaugeValue = simpleMeterRegistry.get("cmHandlesByState").tag("state",state).gauge().value() + assert gaugeValue == 1 + } } } -- cgit