blob: 9fa8fe062c8a0f8b4c59cdbcfb7fe9a9a43eb34c [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska781d18b2014-10-27 10:31:25 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.metrics.intent.cli;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070017
18import java.io.IOException;
19import java.util.concurrent.TimeUnit;
20
21import com.codahale.metrics.Gauge;
22import com.codahale.metrics.Meter;
23import com.codahale.metrics.json.MetricsModule;
24import com.fasterxml.jackson.core.JsonProcessingException;
25import com.fasterxml.jackson.databind.JsonNode;
26import com.fasterxml.jackson.databind.ObjectMapper;
27import com.fasterxml.jackson.databind.node.ObjectNode;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070028import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070029import org.apache.karaf.shell.api.action.lifecycle.Service;
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070030import org.onlab.metrics.EventMetric;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.cli.AbstractShellCommand;
32import org.onosproject.metrics.intent.IntentMetricsService;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070033
34/**
35 * Command to show the intent events metrics.
36 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070037@Service
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070038@Command(scope = "onos", name = "intents-events-metrics",
39 description = "Lists intent events metrics")
40public class IntentEventsMetricsCommand extends AbstractShellCommand {
41
42 private static final String FORMAT_GAUGE =
43 "Intent %s Event Timestamp (ms from epoch)=%d";
44 private static final String FORMAT_METER =
45 "Intent %s Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f";
46
47 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070048 protected void doExecute() {
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070049 IntentMetricsService service = get(IntentMetricsService.class);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070050
51 if (outputJson()) {
52 ObjectMapper mapper = new ObjectMapper()
53 .registerModule(new MetricsModule(TimeUnit.SECONDS,
54 TimeUnit.MILLISECONDS,
55 false));
56 ObjectNode result = mapper.createObjectNode();
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070057 result = json(mapper, result, "intentSubmitted",
58 service.intentSubmittedEventMetric());
59 result = json(mapper, result, "intentInstalled",
60 service.intentInstalledEventMetric());
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -080061 result = json(mapper, result, "intentFailed",
62 service.intentFailedEventMetric());
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070063 result = json(mapper, result, "intentWithdrawRequested",
64 service.intentWithdrawRequestedEventMetric());
65 result = json(mapper, result, "intentWithdrawn",
66 service.intentWithdrawnEventMetric());
Pavlin Radoslavov166c5472015-03-18 11:47:50 -070067 result = json(mapper, result, "intentPurged",
68 service.intentPurgedEventMetric());
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070069 print("%s", result);
70 } else {
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070071 printEventMetric("Submitted",
72 service.intentSubmittedEventMetric());
73 printEventMetric("Installed",
74 service.intentInstalledEventMetric());
Pavlin Radoslavov0f8a1e42015-01-13 11:52:52 -080075 printEventMetric("Failed",
76 service.intentFailedEventMetric());
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070077 printEventMetric("Withdraw Requested",
78 service.intentWithdrawRequestedEventMetric());
79 printEventMetric("Withdrawn",
80 service.intentWithdrawnEventMetric());
Pavlin Radoslavov166c5472015-03-18 11:47:50 -070081 printEventMetric("Purged",
82 service.intentPurgedEventMetric());
Pavlin Radoslavov295b2962014-10-23 01:12:41 -070083 }
84 }
85
86 /**
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -070087 * Produces JSON node for an Event Metric.
88 *
89 * @param mapper the JSON object mapper to use
90 * @param objectNode the JSON object node to use
91 * @param propertyPrefix the property prefix to use
92 * @param eventMetric the Event Metric with the data
93 * @return JSON object node for the Event Metric
94 */
95 private ObjectNode json(ObjectMapper mapper, ObjectNode objectNode,
96 String propertyPrefix, EventMetric eventMetric) {
97 String gaugeName = propertyPrefix + "Timestamp";
98 String meterName = propertyPrefix + "Rate";
99 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
100 Meter meter = eventMetric.eventRateMeter();
101
Ray Milkey9d810f62015-02-13 11:20:58 -0800102 objectNode.set(gaugeName, json(mapper, gauge));
103 objectNode.set(meterName, json(mapper, meter));
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700104 return objectNode;
105 }
106
107 /**
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700108 * Produces JSON node for an Object.
109 *
110 * @param mapper the JSON object mapper to use
111 * @param object the Object with the data
112 * @return JSON node for the Object
113 */
114 private JsonNode json(ObjectMapper mapper, Object object) {
115 //
116 // NOTE: The API for custom serializers is incomplete,
117 // hence we have to parse the JSON string to create JsonNode.
118 //
119 try {
120 final String objectJson = mapper.writeValueAsString(object);
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700121 JsonNode jsonNode = mapper.readTree(objectJson);
122 return jsonNode;
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700123 } catch (JsonProcessingException e) {
124 log.error("Error writing value as JSON string", e);
125 } catch (IOException e) {
126 log.error("Error writing value as JSON string", e);
127 }
128 return null;
129 }
130
131 /**
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700132 * Prints an Event Metric.
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700133 *
134 * @param operationStr the string with the intent operation to print
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700135 * @param eventMetric the Event Metric to print
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700136 */
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700137 private void printEventMetric(String operationStr,
138 EventMetric eventMetric) {
139 Gauge<Long> gauge = eventMetric.lastEventTimestampGauge();
140 Meter meter = eventMetric.eventRateMeter();
141 TimeUnit rateUnit = TimeUnit.SECONDS;
142 double rateFactor = rateUnit.toSeconds(1);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700143
Pavlin Radoslavovccc2e332014-10-23 13:46:28 -0700144 // Print the Gauge
145 print(FORMAT_GAUGE, operationStr, gauge.getValue());
146
147 // Print the Meter
148 print(FORMAT_METER, operationStr, meter.getCount(),
149 meter.getMeanRate() * rateFactor,
150 meter.getOneMinuteRate() * rateFactor,
151 meter.getFiveMinuteRate() * rateFactor,
152 meter.getFifteenMinuteRate() * rateFactor);
Pavlin Radoslavov295b2962014-10-23 01:12:41 -0700153 }
154}