diff options
author | sourabh_sourabh <sourabh.sourabh@est.tech> | 2024-07-08 17:54:11 +0100 |
---|---|---|
committer | Sourabh Sourabh <sourabh.sourabh@est.tech> | 2024-07-16 11:45:09 +0000 |
commit | 05da8915b0a4454f3f014ea724156645d374d9dc (patch) | |
tree | 9a14a5d004f6cf5f718886c21f7f33dfe1dd3964 /cps-ncmp-service/src/main | |
parent | ac58e919008c4449b389d3681a6f8aa216cb5f8f (diff) |
CPS NCMP: Add state tags to cps.ncmp.lcm.events.publish metrics
- Added cm handle state into micrometer Timer as an extra tags while capturing total time taken to publish a LCM events for publishLcmEvent method.
Issue-ID: CPS-2123
Change-Id: I6357c628496700fee7448dbc381a7e0e62d8b540
Signed-off-by: sourabh_sourabh <sourabh.sourabh@est.tech>
Diffstat (limited to 'cps-ncmp-service/src/main')
-rw-r--r-- | cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsService.java | 55 |
1 files changed, 49 insertions, 6 deletions
diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsService.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsService.java index 10aebfa45d..192667175e 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsService.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/impl/inventory/sync/lcm/LcmEventsService.java @@ -20,13 +20,18 @@ package org.onap.cps.ncmp.impl.inventory.sync.lcm; -import io.micrometer.core.annotation.Timed; +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.Timer; +import java.util.ArrayList; +import java.util.List; import java.util.Map; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.onap.cps.events.EventsPublisher; import org.onap.cps.ncmp.events.lcm.v1.LcmEvent; import org.onap.cps.ncmp.events.lcm.v1.LcmEventHeader; +import org.onap.cps.ncmp.events.lcm.v1.Values; import org.onap.cps.utils.JsonObjectMapper; import org.springframework.beans.factory.annotation.Value; import org.springframework.kafka.KafkaException; @@ -41,8 +46,12 @@ import org.springframework.stereotype.Service; @RequiredArgsConstructor public class LcmEventsService { + private static final Tag TAG_METHOD = Tag.of("method", "publishLcmEvent"); + private static final Tag TAG_CLASS = Tag.of("class", LcmEventsService.class.getName()); + private static final String UNAVAILABLE_CM_HANDLE_STATE = "N/A"; private final EventsPublisher<LcmEvent> eventsPublisher; private final JsonObjectMapper jsonObjectMapper; + private final MeterRegistry meterRegistry; @Value("${app.lcm.events.topic:ncmp-events}") private String topicName; @@ -51,24 +60,58 @@ public class LcmEventsService { private boolean notificationsEnabled; /** - * Publish the LcmEvent with header to the public topic. + * Publishes an LCM event to the dedicated topic with optional notification headers. + * Capture and log KafkaException If an error occurs while publishing the event to Kafka * - * @param cmHandleId Cm Handle Id - * @param lcmEvent Lcm Event - * @param lcmEventHeader Lcm Event Header + * @param cmHandleId Cm Handle Id associated with the LCM event + * @param lcmEvent The LCM event object to be published + * @param lcmEventHeader Optional headers associated with the LCM event */ - @Timed(value = "cps.ncmp.lcm.events.publish", description = "Time taken to publish a LCM event") public void publishLcmEvent(final String cmHandleId, final LcmEvent lcmEvent, final LcmEventHeader lcmEventHeader) { + if (notificationsEnabled) { + final Timer.Sample timerSample = Timer.start(meterRegistry); try { final Map<String, Object> lcmEventHeadersMap = jsonObjectMapper.convertToValueType(lcmEventHeader, Map.class); eventsPublisher.publishEvent(topicName, cmHandleId, lcmEventHeadersMap, lcmEvent); } catch (final KafkaException e) { log.error("Unable to publish message to topic : {} and cause : {}", topicName, e.getMessage()); + } finally { + recordMetrics(lcmEvent, timerSample); } } else { log.debug("Notifications disabled."); } } + + private void recordMetrics(final LcmEvent lcmEvent, final Timer.Sample timerSample) { + final List<Tag> tags = new ArrayList<>(4); + tags.add(TAG_CLASS); + tags.add(TAG_METHOD); + + final String oldCmHandleState = extractCmHandleStateValue(lcmEvent.getEvent().getOldValues()); + tags.add(Tag.of("oldCmHandleState", oldCmHandleState)); + + final String newCmHandleState = extractCmHandleStateValue(lcmEvent.getEvent().getNewValues()); + tags.add(Tag.of("newCmHandleState", newCmHandleState)); + + timerSample.stop(Timer.builder("cps.ncmp.lcm.events.publish") + .description("Time taken to publish a LCM event") + .tags(tags) + .register(meterRegistry)); + } + + /** + * Extracts the CM handle state value from the given Values object. + * If the provided Values object or its CM handle state is null, returns a default value. + * + * @param values The Values object containing CM handle state information. + * @return The CM handle state value as a string, or a default value if null. + */ + private String extractCmHandleStateValue(final Values values) { + return (values != null && values.getCmHandleState() != null) + ? values.getCmHandleState().value() + : UNAVAILABLE_CM_HANDLE_STATE; + } } |