Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 781d18b | 2014-10-27 10:31:25 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.metrics.intent.cli; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 17 | |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 18 | import java.util.concurrent.TimeUnit; |
| 19 | |
| 20 | import com.codahale.metrics.Gauge; |
| 21 | import com.codahale.metrics.Meter; |
| 22 | import com.codahale.metrics.json.MetricsModule; |
| 23 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 24 | import com.fasterxml.jackson.databind.JsonNode; |
| 25 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 26 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 27 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 28 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 29 | import org.onlab.metrics.EventMetric; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.cli.AbstractShellCommand; |
| 31 | import org.onosproject.metrics.intent.IntentMetricsService; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Command to show the intent events metrics. |
| 35 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 36 | @Service |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 37 | @Command(scope = "onos", name = "intents-events-metrics", |
| 38 | description = "Lists intent events metrics") |
| 39 | public class IntentEventsMetricsCommand extends AbstractShellCommand { |
| 40 | |
| 41 | private static final String FORMAT_GAUGE = |
| 42 | "Intent %s Event Timestamp (ms from epoch)=%d"; |
| 43 | private static final String FORMAT_METER = |
| 44 | "Intent %s Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f"; |
| 45 | |
| 46 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 47 | protected void doExecute() { |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 48 | IntentMetricsService service = get(IntentMetricsService.class); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 49 | |
| 50 | if (outputJson()) { |
| 51 | ObjectMapper mapper = new ObjectMapper() |
| 52 | .registerModule(new MetricsModule(TimeUnit.SECONDS, |
| 53 | TimeUnit.MILLISECONDS, |
| 54 | false)); |
| 55 | ObjectNode result = mapper.createObjectNode(); |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 56 | result = json(mapper, result, "intentSubmitted", |
| 57 | service.intentSubmittedEventMetric()); |
| 58 | result = json(mapper, result, "intentInstalled", |
| 59 | service.intentInstalledEventMetric()); |
Pavlin Radoslavov | 0f8a1e4 | 2015-01-13 11:52:52 -0800 | [diff] [blame] | 60 | result = json(mapper, result, "intentFailed", |
| 61 | service.intentFailedEventMetric()); |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 62 | result = json(mapper, result, "intentWithdrawRequested", |
| 63 | service.intentWithdrawRequestedEventMetric()); |
| 64 | result = json(mapper, result, "intentWithdrawn", |
| 65 | service.intentWithdrawnEventMetric()); |
Pavlin Radoslavov | 166c547 | 2015-03-18 11:47:50 -0700 | [diff] [blame] | 66 | result = json(mapper, result, "intentPurged", |
| 67 | service.intentPurgedEventMetric()); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 68 | print("%s", result); |
| 69 | } else { |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 70 | printEventMetric("Submitted", |
| 71 | service.intentSubmittedEventMetric()); |
| 72 | printEventMetric("Installed", |
| 73 | service.intentInstalledEventMetric()); |
Pavlin Radoslavov | 0f8a1e4 | 2015-01-13 11:52:52 -0800 | [diff] [blame] | 74 | printEventMetric("Failed", |
| 75 | service.intentFailedEventMetric()); |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 76 | printEventMetric("Withdraw Requested", |
| 77 | service.intentWithdrawRequestedEventMetric()); |
| 78 | printEventMetric("Withdrawn", |
| 79 | service.intentWithdrawnEventMetric()); |
Pavlin Radoslavov | 166c547 | 2015-03-18 11:47:50 -0700 | [diff] [blame] | 80 | printEventMetric("Purged", |
| 81 | service.intentPurgedEventMetric()); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | /** |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 86 | * Produces JSON node for an Event Metric. |
| 87 | * |
| 88 | * @param mapper the JSON object mapper to use |
| 89 | * @param objectNode the JSON object node to use |
| 90 | * @param propertyPrefix the property prefix to use |
| 91 | * @param eventMetric the Event Metric with the data |
| 92 | * @return JSON object node for the Event Metric |
| 93 | */ |
| 94 | private ObjectNode json(ObjectMapper mapper, ObjectNode objectNode, |
| 95 | String propertyPrefix, EventMetric eventMetric) { |
| 96 | String gaugeName = propertyPrefix + "Timestamp"; |
| 97 | String meterName = propertyPrefix + "Rate"; |
| 98 | Gauge<Long> gauge = eventMetric.lastEventTimestampGauge(); |
| 99 | Meter meter = eventMetric.eventRateMeter(); |
| 100 | |
Ray Milkey | 9d810f6 | 2015-02-13 11:20:58 -0800 | [diff] [blame] | 101 | objectNode.set(gaugeName, json(mapper, gauge)); |
| 102 | objectNode.set(meterName, json(mapper, meter)); |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 103 | return objectNode; |
| 104 | } |
| 105 | |
| 106 | /** |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 107 | * Produces JSON node for an Object. |
| 108 | * |
| 109 | * @param mapper the JSON object mapper to use |
| 110 | * @param object the Object with the data |
| 111 | * @return JSON node for the Object |
| 112 | */ |
| 113 | private JsonNode json(ObjectMapper mapper, Object object) { |
| 114 | // |
| 115 | // NOTE: The API for custom serializers is incomplete, |
| 116 | // hence we have to parse the JSON string to create JsonNode. |
| 117 | // |
| 118 | try { |
| 119 | final String objectJson = mapper.writeValueAsString(object); |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 120 | JsonNode jsonNode = mapper.readTree(objectJson); |
| 121 | return jsonNode; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 122 | } catch (JsonProcessingException e) { |
| 123 | log.error("Error writing value as JSON string", e); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 124 | } |
| 125 | return null; |
| 126 | } |
| 127 | |
| 128 | /** |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 129 | * Prints an Event Metric. |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 130 | * |
| 131 | * @param operationStr the string with the intent operation to print |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 132 | * @param eventMetric the Event Metric to print |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 133 | */ |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 134 | private void printEventMetric(String operationStr, |
| 135 | EventMetric eventMetric) { |
| 136 | Gauge<Long> gauge = eventMetric.lastEventTimestampGauge(); |
| 137 | Meter meter = eventMetric.eventRateMeter(); |
| 138 | TimeUnit rateUnit = TimeUnit.SECONDS; |
| 139 | double rateFactor = rateUnit.toSeconds(1); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 140 | |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 141 | // Print the Gauge |
| 142 | print(FORMAT_GAUGE, operationStr, gauge.getValue()); |
| 143 | |
| 144 | // Print the Meter |
| 145 | print(FORMAT_METER, operationStr, meter.getCount(), |
| 146 | meter.getMeanRate() * rateFactor, |
| 147 | meter.getOneMinuteRate() * rateFactor, |
| 148 | meter.getFiveMinuteRate() * rateFactor, |
| 149 | meter.getFifteenMinuteRate() * rateFactor); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 150 | } |
| 151 | } |