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