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; |
| 14 | import org.onlab.onos.cli.AbstractShellCommand; |
| 15 | import org.onlab.onos.metrics.intent.IntentMetricsService; |
| 16 | |
| 17 | /** |
| 18 | * Command to show the intent events metrics. |
| 19 | */ |
| 20 | @Command(scope = "onos", name = "intents-events-metrics", |
| 21 | description = "Lists intent events metrics") |
| 22 | public class IntentEventsMetricsCommand extends AbstractShellCommand { |
| 23 | |
| 24 | private static final String FORMAT_GAUGE = |
| 25 | "Intent %s Event Timestamp (ms from epoch)=%d"; |
| 26 | private static final String FORMAT_METER = |
| 27 | "Intent %s Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f"; |
| 28 | |
| 29 | @Override |
| 30 | protected void execute() { |
| 31 | IntentMetricsService service = get(IntentMetricsService.class); |
| 32 | Gauge<Long> gauge; |
| 33 | Meter meter; |
| 34 | |
| 35 | if (outputJson()) { |
| 36 | ObjectMapper mapper = new ObjectMapper() |
| 37 | .registerModule(new MetricsModule(TimeUnit.SECONDS, |
| 38 | TimeUnit.MILLISECONDS, |
| 39 | false)); |
| 40 | ObjectNode result = mapper.createObjectNode(); |
| 41 | // |
| 42 | gauge = service.intentSubmittedTimestampEpochMsGauge(); |
| 43 | result.put("intentSubmittedTimestamp", json(mapper, gauge)); |
| 44 | gauge = service.intentInstalledTimestampEpochMsGauge(); |
| 45 | result.put("intentInstalledTimestamp", json(mapper, gauge)); |
| 46 | gauge = service.intentWithdrawRequestedTimestampEpochMsGauge(); |
| 47 | result.put("intentWithdrawRequestedTimestamp", |
| 48 | json(mapper, gauge)); |
| 49 | gauge = service.intentWithdrawnTimestampEpochMsGauge(); |
| 50 | result.put("intentWithdrawnTimestamp", json(mapper, gauge)); |
| 51 | // |
| 52 | meter = service.intentSubmittedRateMeter(); |
| 53 | result.put("intentSubmittedRate", json(mapper, meter)); |
| 54 | meter = service.intentInstalledRateMeter(); |
| 55 | result.put("intentInstalledRate", json(mapper, meter)); |
| 56 | meter = service.intentWithdrawRequestedRateMeter(); |
| 57 | result.put("intentWithdrawRequestedRate", json(mapper, meter)); |
| 58 | meter = service.intentWithdrawnRateMeter(); |
| 59 | result.put("intentWithdrawnRate", json(mapper, meter)); |
| 60 | // |
| 61 | print("%s", result); |
| 62 | } else { |
| 63 | gauge = service.intentSubmittedTimestampEpochMsGauge(); |
| 64 | printGauge("Submitted", gauge); |
| 65 | gauge = service.intentInstalledTimestampEpochMsGauge(); |
| 66 | printGauge("Installed", gauge); |
| 67 | gauge = service.intentWithdrawRequestedTimestampEpochMsGauge(); |
| 68 | printGauge("Withdraw Requested", gauge); |
| 69 | gauge = service.intentWithdrawnTimestampEpochMsGauge(); |
| 70 | printGauge("Withdrawn", gauge); |
| 71 | // |
| 72 | meter = service.intentSubmittedRateMeter(); |
| 73 | printMeter("Submitted", meter); |
| 74 | meter = service.intentInstalledRateMeter(); |
| 75 | printMeter("Installed", meter); |
| 76 | meter = service.intentWithdrawRequestedRateMeter(); |
| 77 | printMeter("Withdraw Requested", meter); |
| 78 | meter = service.intentWithdrawnRateMeter(); |
| 79 | printMeter("Withdrawn", meter); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Produces JSON node for an Object. |
| 85 | * |
| 86 | * @param mapper the JSON object mapper to use |
| 87 | * @param object the Object with the data |
| 88 | * @return JSON node for the Object |
| 89 | */ |
| 90 | private JsonNode json(ObjectMapper mapper, Object object) { |
| 91 | // |
| 92 | // NOTE: The API for custom serializers is incomplete, |
| 93 | // hence we have to parse the JSON string to create JsonNode. |
| 94 | // |
| 95 | try { |
| 96 | final String objectJson = mapper.writeValueAsString(object); |
| 97 | JsonNode objectNode = mapper.readTree(objectJson); |
| 98 | return objectNode; |
| 99 | } catch (JsonProcessingException e) { |
| 100 | log.error("Error writing value as JSON string", e); |
| 101 | } catch (IOException e) { |
| 102 | log.error("Error writing value as JSON string", e); |
| 103 | } |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Prints a Gauge. |
| 109 | * |
| 110 | * @param operationStr the string with the intent operation to print |
| 111 | * @param gauge the Gauge to print |
| 112 | */ |
| 113 | private void printGauge(String operationStr, Gauge<Long> gauge) { |
| 114 | print(FORMAT_GAUGE, operationStr, gauge.getValue()); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Prints a Meter. |
| 119 | * |
| 120 | * @param operationStr the string with the intent operation to print |
| 121 | * @param meter the Meter to print |
| 122 | */ |
| 123 | private void printMeter(String operationStr, Meter meter) { |
| 124 | TimeUnit rateUnit = TimeUnit.SECONDS; |
| 125 | double rateFactor = rateUnit.toSeconds(1); |
| 126 | print(FORMAT_METER, operationStr, meter.getCount(), |
| 127 | meter.getMeanRate() * rateFactor, |
| 128 | meter.getOneMinuteRate() * rateFactor, |
| 129 | meter.getFiveMinuteRate() * rateFactor, |
| 130 | meter.getFifteenMinuteRate() * rateFactor); |
| 131 | } |
| 132 | } |