Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 1 | package org.onlab.onos.metrics.intent.cli; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.util.concurrent.TimeUnit; |
| 5 | |
| 6 | import com.codahale.metrics.Gauge; |
| 7 | import com.codahale.metrics.Meter; |
| 8 | import com.codahale.metrics.json.MetricsModule; |
| 9 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 10 | import com.fasterxml.jackson.databind.JsonNode; |
| 11 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 12 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 13 | import org.apache.karaf.shell.commands.Command; |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 14 | import org.onlab.metrics.EventMetric; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 15 | import org.onlab.onos.cli.AbstractShellCommand; |
| 16 | import org.onlab.onos.metrics.intent.IntentMetricsService; |
| 17 | |
| 18 | /** |
| 19 | * Command to show the intent events metrics. |
| 20 | */ |
| 21 | @Command(scope = "onos", name = "intents-events-metrics", |
| 22 | description = "Lists intent events metrics") |
| 23 | public class IntentEventsMetricsCommand extends AbstractShellCommand { |
| 24 | |
| 25 | private static final String FORMAT_GAUGE = |
| 26 | "Intent %s Event Timestamp (ms from epoch)=%d"; |
| 27 | private static final String FORMAT_METER = |
| 28 | "Intent %s Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f"; |
| 29 | |
| 30 | @Override |
| 31 | protected void execute() { |
| 32 | IntentMetricsService service = get(IntentMetricsService.class); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 33 | |
| 34 | if (outputJson()) { |
| 35 | ObjectMapper mapper = new ObjectMapper() |
| 36 | .registerModule(new MetricsModule(TimeUnit.SECONDS, |
| 37 | TimeUnit.MILLISECONDS, |
| 38 | false)); |
| 39 | ObjectNode result = mapper.createObjectNode(); |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 40 | result = json(mapper, result, "intentSubmitted", |
| 41 | service.intentSubmittedEventMetric()); |
| 42 | result = json(mapper, result, "intentInstalled", |
| 43 | service.intentInstalledEventMetric()); |
| 44 | result = json(mapper, result, "intentWithdrawRequested", |
| 45 | service.intentWithdrawRequestedEventMetric()); |
| 46 | result = json(mapper, result, "intentWithdrawn", |
| 47 | service.intentWithdrawnEventMetric()); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 48 | print("%s", result); |
| 49 | } else { |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 50 | printEventMetric("Submitted", |
| 51 | service.intentSubmittedEventMetric()); |
| 52 | printEventMetric("Installed", |
| 53 | service.intentInstalledEventMetric()); |
| 54 | printEventMetric("Withdraw Requested", |
| 55 | service.intentWithdrawRequestedEventMetric()); |
| 56 | printEventMetric("Withdrawn", |
| 57 | service.intentWithdrawnEventMetric()); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 62 | * Produces JSON node for an Event Metric. |
| 63 | * |
| 64 | * @param mapper the JSON object mapper to use |
| 65 | * @param objectNode the JSON object node to use |
| 66 | * @param propertyPrefix the property prefix to use |
| 67 | * @param eventMetric the Event Metric with the data |
| 68 | * @return JSON object node for the Event Metric |
| 69 | */ |
| 70 | private ObjectNode json(ObjectMapper mapper, ObjectNode objectNode, |
| 71 | String propertyPrefix, EventMetric eventMetric) { |
| 72 | String gaugeName = propertyPrefix + "Timestamp"; |
| 73 | String meterName = propertyPrefix + "Rate"; |
| 74 | Gauge<Long> gauge = eventMetric.lastEventTimestampGauge(); |
| 75 | Meter meter = eventMetric.eventRateMeter(); |
| 76 | |
| 77 | objectNode.put(gaugeName, json(mapper, gauge)); |
| 78 | objectNode.put(meterName, json(mapper, meter)); |
| 79 | return objectNode; |
| 80 | } |
| 81 | |
| 82 | /** |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 83 | * Produces JSON node for an Object. |
| 84 | * |
| 85 | * @param mapper the JSON object mapper to use |
| 86 | * @param object the Object with the data |
| 87 | * @return JSON node for the Object |
| 88 | */ |
| 89 | private JsonNode json(ObjectMapper mapper, Object object) { |
| 90 | // |
| 91 | // NOTE: The API for custom serializers is incomplete, |
| 92 | // hence we have to parse the JSON string to create JsonNode. |
| 93 | // |
| 94 | try { |
| 95 | final String objectJson = mapper.writeValueAsString(object); |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 96 | JsonNode jsonNode = mapper.readTree(objectJson); |
| 97 | return jsonNode; |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 98 | } catch (JsonProcessingException e) { |
| 99 | log.error("Error writing value as JSON string", e); |
| 100 | } catch (IOException e) { |
| 101 | log.error("Error writing value as JSON string", e); |
| 102 | } |
| 103 | return null; |
| 104 | } |
| 105 | |
| 106 | /** |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 107 | * Prints an Event Metric. |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 108 | * |
| 109 | * @param operationStr the string with the intent operation to print |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 110 | * @param eventMetric the Event Metric to print |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 111 | */ |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 112 | private void printEventMetric(String operationStr, |
| 113 | EventMetric eventMetric) { |
| 114 | Gauge<Long> gauge = eventMetric.lastEventTimestampGauge(); |
| 115 | Meter meter = eventMetric.eventRateMeter(); |
| 116 | TimeUnit rateUnit = TimeUnit.SECONDS; |
| 117 | double rateFactor = rateUnit.toSeconds(1); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 118 | |
Pavlin Radoslavov | ccc2e33 | 2014-10-23 13:46:28 -0700 | [diff] [blame] | 119 | // Print the Gauge |
| 120 | print(FORMAT_GAUGE, operationStr, gauge.getValue()); |
| 121 | |
| 122 | // Print the Meter |
| 123 | print(FORMAT_METER, operationStr, meter.getCount(), |
| 124 | meter.getMeanRate() * rateFactor, |
| 125 | meter.getOneMinuteRate() * rateFactor, |
| 126 | meter.getFiveMinuteRate() * rateFactor, |
| 127 | meter.getFifteenMinuteRate() * rateFactor); |
Pavlin Radoslavov | 295b296 | 2014-10-23 01:12:41 -0700 | [diff] [blame] | 128 | } |
| 129 | } |