Pavlin Radoslavov

Added a new module: ONOS intent metrics application

It can be used as an Intent-related event and event metrics collector.
It can be loaded by one of the following two (new) features:
     onos-app-metrics, onos-app-metrics-intent

After loading the module, it subscribes for intent-related events
and keeps the following state:
 (a) The last 100 events
 (b) The timestamp of the last event (ms after epoch) as observed by this
     module for each event type: SUBMITTED, INSTALLED, WITHDRAWN.
     The missing event type is the equivalent of "Withdraw Requested"
 (c) The rate of each intent event type: count, median rate, average rate
      over the last 1, 5 or 15 minutes

The following CLI commands are added:
 * onos:intents-events
   Shows the last 100 intent events
 * onos:intents-events-metrics
   Shows the timestamp of the last event (ms after epoch) as observed by
   this module for each event type, and the rate of the topology
   events (for each event type): see (b) and (c) above

Change-Id: I9f23e9086bbd433b8f24283539abdeb97e199e2e
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.onlab.onos</groupId>
<artifactId>onos-app-metrics</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>onos-app-metrics-intent</artifactId>
<packaging>bundle</packaging>
<description>ONOS intent metrics application</description>
<dependencies>
<dependency>
<groupId>org.onlab.onos</groupId>
<artifactId>onos-cli</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.karaf.shell</groupId>
<artifactId>org.apache.karaf.shell.console</artifactId>
</dependency>
</dependencies>
</project>
package org.onlab.onos.metrics.intent;
import static org.slf4j.LoggerFactory.getLogger;
import java.util.LinkedList;
import java.util.List;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Meter;
import com.google.common.collect.ImmutableList;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.Service;
import org.onlab.metrics.MetricsComponent;
import org.onlab.metrics.MetricsFeature;
import org.onlab.metrics.MetricsService;
import org.onlab.onos.net.intent.IntentEvent;
import org.onlab.onos.net.intent.IntentListener;
import org.onlab.onos.net.intent.IntentService;
import org.slf4j.Logger;
/**
* ONOS Intent Metrics Application that collects intent-related metrics.
*/
@Component(immediate = true)
@Service
public class IntentMetrics implements IntentMetricsService,
IntentListener {
private static final Logger log = getLogger(IntentMetrics.class);
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected IntentService intentService;
private LinkedList<IntentEvent> lastEvents = new LinkedList<>();
private static final int LAST_EVENTS_MAX_N = 100;
//
// Metrics
//
@Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
protected MetricsService metricsService;
//
private static final String COMPONENT_NAME = "Intent";
private static final String FEATURE_SUBMITTED_NAME = "Submitted";
private static final String FEATURE_INSTALLED_NAME = "Installed";
private static final String FEATURE_WITHDRAW_REQUESTED_NAME =
"WithdrawRequested";
private static final String FEATURE_WITHDRAWN_NAME = "Withdrawn";
private static final String GAUGE_TIMESTAMP_NAME = "Timestamp.EpochMs";
private static final String METER_RATE_NAME = "Rate";
//
private MetricsComponent metricsComponent;
private MetricsFeature metricsFeatureSubmitted;
private MetricsFeature metricsFeatureInstalled;
private MetricsFeature metricsFeatureWithdrawRequested;
private MetricsFeature metricsFeatureWithdrawn;
//
// Timestamps:
// - Intent Submitted API operation (ms from the Epoch)
// - Intent Installed operation completion (ms from the Epoch)
// - Intent Withdraw Requested API operation (ms from the Epoch)
// - Intent Withdrawn operation completion (ms from the Epoch)
//
private volatile long intentSubmittedTimestampEpochMs = 0;
private volatile long intentInstalledTimestampEpochMs = 0;
private volatile long intentWithdrawRequestedTimestampEpochMs = 0;
private volatile long intentWithdrawnTimestampEpochMs = 0;
//
private Gauge<Long> intentSubmittedTimestampEpochMsGauge;
private Gauge<Long> intentInstalledTimestampEpochMsGauge;
private Gauge<Long> intentWithdrawRequestedTimestampEpochMsGauge;
private Gauge<Long> intentWithdrawnTimestampEpochMsGauge;
//
// Rate meters:
// - Rate of the Submitted Intent API operations
// - Rate of the Installed Intent operations
// - Rate of the Withdrawn Requested Intent API operations
// - Rate of the Withdrawn Intent operations
//
private Meter intentSubmittedRateMeter;
private Meter intentInstalledRateMeter;
private Meter intentWithdrawRequestedRateMeter;
private Meter intentWithdrawnRateMeter;
@Activate
protected void activate() {
clear();
registerMetrics();
intentService.addListener(this);
log.info("ONOS Intent Metrics started.");
}
@Deactivate
public void deactivate() {
intentService.removeListener(this);
removeMetrics();
clear();
log.info("ONOS Intent Metrics stopped.");
}
@Override
public List<IntentEvent> getEvents() {
synchronized (lastEvents) {
return ImmutableList.<IntentEvent>copyOf(lastEvents);
}
}
@Override
public Gauge<Long> intentSubmittedTimestampEpochMsGauge() {
return intentSubmittedTimestampEpochMsGauge;
}
@Override
public Gauge<Long> intentInstalledTimestampEpochMsGauge() {
return intentInstalledTimestampEpochMsGauge;
}
@Override
public Gauge<Long> intentWithdrawRequestedTimestampEpochMsGauge() {
return intentWithdrawRequestedTimestampEpochMsGauge;
}
@Override
public Gauge<Long> intentWithdrawnTimestampEpochMsGauge() {
return intentWithdrawnTimestampEpochMsGauge;
}
@Override
public Meter intentSubmittedRateMeter() {
return intentSubmittedRateMeter;
}
@Override
public Meter intentInstalledRateMeter() {
return intentInstalledRateMeter;
}
@Override
public Meter intentWithdrawRequestedRateMeter() {
return intentWithdrawRequestedRateMeter;
}
@Override
public Meter intentWithdrawnRateMeter() {
return intentWithdrawnRateMeter;
}
@Override
public void event(IntentEvent event) {
synchronized (lastEvents) {
//
// TODO: The processing below is incomplete: we don't have
// an event equivalent of "Withdraw Requested"
//
switch (event.type()) {
case SUBMITTED:
intentSubmittedTimestampEpochMs = System.currentTimeMillis();
intentSubmittedRateMeter.mark(1);
break;
case INSTALLED:
intentInstalledTimestampEpochMs = System.currentTimeMillis();
intentInstalledRateMeter.mark(1);
break;
case FAILED:
// TODO: Just ignore?
break;
/*
case WITHDRAW_REQUESTED:
intentWithdrawRequestedTimestampEpochMs =
System.currentTimeMillis();
intentWithdrawRequestedRateMeter.mark(1);
break;
*/
case WITHDRAWN:
intentWithdrawnTimestampEpochMs = System.currentTimeMillis();
intentWithdrawnRateMeter.mark(1);
break;
default:
break;
}
//
// Keep only the last N events, where N = LAST_EVENTS_MAX_N
//
while (lastEvents.size() >= LAST_EVENTS_MAX_N) {
lastEvents.remove();
}
lastEvents.add(event);
}
log.debug("Intent Event: time = {} type = {} event = {}",
event.time(), event.type(), event);
}
/**
* Clears the internal state.
*/
private void clear() {
synchronized (lastEvents) {
intentSubmittedTimestampEpochMs = 0;
intentInstalledTimestampEpochMs = 0;
intentWithdrawRequestedTimestampEpochMs = 0;
intentWithdrawnTimestampEpochMs = 0;
lastEvents.clear();
}
}
/**
* Registers the metrics.
*/
private void registerMetrics() {
metricsComponent = metricsService.registerComponent(COMPONENT_NAME);
//
metricsFeatureSubmitted =
metricsComponent.registerFeature(FEATURE_SUBMITTED_NAME);
metricsFeatureInstalled =
metricsComponent.registerFeature(FEATURE_INSTALLED_NAME);
metricsFeatureWithdrawRequested =
metricsComponent.registerFeature(FEATURE_WITHDRAW_REQUESTED_NAME);
metricsFeatureWithdrawn =
metricsComponent.registerFeature(FEATURE_WITHDRAWN_NAME);
//
intentSubmittedTimestampEpochMsGauge =
metricsService.registerMetric(metricsComponent,
metricsFeatureSubmitted,
GAUGE_TIMESTAMP_NAME,
new Gauge<Long>() {
@Override
public Long getValue() {
return intentSubmittedTimestampEpochMs;
}
});
//
intentInstalledTimestampEpochMsGauge =
metricsService.registerMetric(metricsComponent,
metricsFeatureInstalled,
GAUGE_TIMESTAMP_NAME,
new Gauge<Long>() {
@Override
public Long getValue() {
return intentInstalledTimestampEpochMs;
}
});
//
intentWithdrawRequestedTimestampEpochMsGauge =
metricsService.registerMetric(metricsComponent,
metricsFeatureWithdrawRequested,
GAUGE_TIMESTAMP_NAME,
new Gauge<Long>() {
@Override
public Long getValue() {
return intentWithdrawRequestedTimestampEpochMs;
}
});
//
intentWithdrawnTimestampEpochMsGauge =
metricsService.registerMetric(metricsComponent,
metricsFeatureWithdrawn,
GAUGE_TIMESTAMP_NAME,
new Gauge<Long>() {
@Override
public Long getValue() {
return intentWithdrawnTimestampEpochMs;
}
});
//
intentSubmittedRateMeter =
metricsService.createMeter(metricsComponent,
metricsFeatureSubmitted,
METER_RATE_NAME);
//
intentInstalledRateMeter =
metricsService.createMeter(metricsComponent,
metricsFeatureInstalled,
METER_RATE_NAME);
//
intentWithdrawRequestedRateMeter =
metricsService.createMeter(metricsComponent,
metricsFeatureWithdrawRequested,
METER_RATE_NAME);
//
intentWithdrawnRateMeter =
metricsService.createMeter(metricsComponent,
metricsFeatureWithdrawn,
METER_RATE_NAME);
}
/**
* Removes the metrics.
*/
private void removeMetrics() {
metricsService.removeMetric(metricsComponent,
metricsFeatureSubmitted,
GAUGE_TIMESTAMP_NAME);
metricsService.removeMetric(metricsComponent,
metricsFeatureInstalled,
GAUGE_TIMESTAMP_NAME);
metricsService.removeMetric(metricsComponent,
metricsFeatureWithdrawRequested,
GAUGE_TIMESTAMP_NAME);
metricsService.removeMetric(metricsComponent,
metricsFeatureWithdrawn,
GAUGE_TIMESTAMP_NAME);
metricsService.removeMetric(metricsComponent,
metricsFeatureSubmitted,
METER_RATE_NAME);
metricsService.removeMetric(metricsComponent,
metricsFeatureInstalled,
METER_RATE_NAME);
metricsService.removeMetric(metricsComponent,
metricsFeatureWithdrawRequested,
METER_RATE_NAME);
metricsService.removeMetric(metricsComponent,
metricsFeatureWithdrawn,
METER_RATE_NAME);
}
}
package org.onlab.onos.metrics.intent;
import java.util.List;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Meter;
import org.onlab.onos.net.intent.IntentEvent;
/**
* Service interface exported by IntentMetrics.
*/
public interface IntentMetricsService {
/**
* Gets the last saved intent events.
*
* @return the last saved intent events.
*/
public List<IntentEvent> getEvents();
/**
* Gets the Metrics' Gauge for the intent SUBMITTED event timestamp
* (ms from the epoch).
*
* @return the Metrics' Gauge for the intent SUBMITTED event timestamp
* (ms from the epoch)
*/
public Gauge<Long> intentSubmittedTimestampEpochMsGauge();
/**
* Gets the Metrics' Gauge for the intent INSTALLED event timestamp
* (ms from the epoch).
*
* @return the Metrics' Gauge for the intent INSTALLED event timestamp
* (ms from the epoch)
*/
public Gauge<Long> intentInstalledTimestampEpochMsGauge();
/**
* Gets the Metrics' Gauge for the intent WITHDRAW_REQUESTED event
* timestamp (ms from the epoch).
*
* TODO: This intent event is not implemented yet.
*
* @return the Metrics' Gauge for the intent WITHDRAW_REQUESTED event
* timestamp (ms from the epoch)
*/
public Gauge<Long> intentWithdrawRequestedTimestampEpochMsGauge();
/**
* Gets the Metrics' Gauge for the intent WITHDRAWN event timestamp
* (ms from the epoch).
*
* @return the Metrics' Gauge for the intent WITHDRAWN event timestamp
* (ms from the epoch)
*/
public Gauge<Long> intentWithdrawnTimestampEpochMsGauge();
/**
* Gets the Metrics' Meter for the submitted intents event rate.
*
* @return the Metrics' Meter for the submitted intents event rate
*/
public Meter intentSubmittedRateMeter();
/**
* Gets the Metrics' Meter for the installed intents event rate.
*
* @return the Metrics' Meter for the installed intent event rate
*/
public Meter intentInstalledRateMeter();
/**
* Gets the Metrics' Meter for the withdraw requested intents event rate.
*
* @return the Metrics' Meter for the withdraw requested intents event rate
*/
public Meter intentWithdrawRequestedRateMeter();
/**
* Gets the Metrics' Meter for the withdraw completed intents event rate.
*
* @return the Metrics' Meter for the withdraw completed intents event rate
*/
public Meter intentWithdrawnRateMeter();
}
package org.onlab.onos.metrics.intent.cli;
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.karaf.shell.commands.Command;
import org.onlab.onos.cli.AbstractShellCommand;
import org.onlab.onos.metrics.intent.IntentMetricsService;
import org.onlab.onos.net.intent.IntentEvent;
/**
* Command to show the list of last intent events.
*/
@Command(scope = "onos", name = "intents-events",
description = "Lists the last intent events")
public class IntentEventsListCommand extends AbstractShellCommand {
private static final String FORMAT_EVENT = "Event=%s";
@Override
protected void execute() {
IntentMetricsService service = get(IntentMetricsService.class);
if (outputJson()) {
print("%s", json(service.getEvents()));
} else {
for (IntentEvent event : service.getEvents()) {
print(FORMAT_EVENT, event);
print(""); // Extra empty line for clarity
}
}
}
/**
* Produces a JSON array of intent events.
*
* @param intentEvents the intent events with the data
* @return JSON array with the intent events
*/
private JsonNode json(List<IntentEvent> intentEvents) {
ObjectMapper mapper = new ObjectMapper();
ArrayNode result = mapper.createArrayNode();
for (IntentEvent event : intentEvents) {
result.add(json(mapper, event));
}
return result;
}
/**
* Produces JSON object for a intent event.
*
* @param mapper the JSON object mapper to use
* @param intentEvent the intent event with the data
* @return JSON object for the intent event
*/
private ObjectNode json(ObjectMapper mapper, IntentEvent intentEvent) {
ObjectNode result = mapper.createObjectNode();
result.put("time", intentEvent.time())
.put("type", intentEvent.type().toString())
.put("event", intentEvent.toString());
return result;
}
}
package org.onlab.onos.metrics.intent.cli;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import com.codahale.metrics.Gauge;
import com.codahale.metrics.Meter;
import com.codahale.metrics.json.MetricsModule;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.karaf.shell.commands.Command;
import org.onlab.onos.cli.AbstractShellCommand;
import org.onlab.onos.metrics.intent.IntentMetricsService;
/**
* Command to show the intent events metrics.
*/
@Command(scope = "onos", name = "intents-events-metrics",
description = "Lists intent events metrics")
public class IntentEventsMetricsCommand extends AbstractShellCommand {
private static final String FORMAT_GAUGE =
"Intent %s Event Timestamp (ms from epoch)=%d";
private static final String FORMAT_METER =
"Intent %s Events count=%d rate(events/sec) mean=%f m1=%f m5=%f m15=%f";
@Override
protected void execute() {
IntentMetricsService service = get(IntentMetricsService.class);
Gauge<Long> gauge;
Meter meter;
if (outputJson()) {
ObjectMapper mapper = new ObjectMapper()
.registerModule(new MetricsModule(TimeUnit.SECONDS,
TimeUnit.MILLISECONDS,
false));
ObjectNode result = mapper.createObjectNode();
//
gauge = service.intentSubmittedTimestampEpochMsGauge();
result.put("intentSubmittedTimestamp", json(mapper, gauge));
gauge = service.intentInstalledTimestampEpochMsGauge();
result.put("intentInstalledTimestamp", json(mapper, gauge));
gauge = service.intentWithdrawRequestedTimestampEpochMsGauge();
result.put("intentWithdrawRequestedTimestamp",
json(mapper, gauge));
gauge = service.intentWithdrawnTimestampEpochMsGauge();
result.put("intentWithdrawnTimestamp", json(mapper, gauge));
//
meter = service.intentSubmittedRateMeter();
result.put("intentSubmittedRate", json(mapper, meter));
meter = service.intentInstalledRateMeter();
result.put("intentInstalledRate", json(mapper, meter));
meter = service.intentWithdrawRequestedRateMeter();
result.put("intentWithdrawRequestedRate", json(mapper, meter));
meter = service.intentWithdrawnRateMeter();
result.put("intentWithdrawnRate", json(mapper, meter));
//
print("%s", result);
} else {
gauge = service.intentSubmittedTimestampEpochMsGauge();
printGauge("Submitted", gauge);
gauge = service.intentInstalledTimestampEpochMsGauge();
printGauge("Installed", gauge);
gauge = service.intentWithdrawRequestedTimestampEpochMsGauge();
printGauge("Withdraw Requested", gauge);
gauge = service.intentWithdrawnTimestampEpochMsGauge();
printGauge("Withdrawn", gauge);
//
meter = service.intentSubmittedRateMeter();
printMeter("Submitted", meter);
meter = service.intentInstalledRateMeter();
printMeter("Installed", meter);
meter = service.intentWithdrawRequestedRateMeter();
printMeter("Withdraw Requested", meter);
meter = service.intentWithdrawnRateMeter();
printMeter("Withdrawn", meter);
}
}
/**
* Produces JSON node for an Object.
*
* @param mapper the JSON object mapper to use
* @param object the Object with the data
* @return JSON node for the Object
*/
private JsonNode json(ObjectMapper mapper, Object object) {
//
// NOTE: The API for custom serializers is incomplete,
// hence we have to parse the JSON string to create JsonNode.
//
try {
final String objectJson = mapper.writeValueAsString(object);
JsonNode objectNode = mapper.readTree(objectJson);
return objectNode;
} catch (JsonProcessingException e) {
log.error("Error writing value as JSON string", e);
} catch (IOException e) {
log.error("Error writing value as JSON string", e);
}
return null;
}
/**
* Prints a Gauge.
*
* @param operationStr the string with the intent operation to print
* @param gauge the Gauge to print
*/
private void printGauge(String operationStr, Gauge<Long> gauge) {
print(FORMAT_GAUGE, operationStr, gauge.getValue());
}
/**
* Prints a Meter.
*
* @param operationStr the string with the intent operation to print
* @param meter the Meter to print
*/
private void printMeter(String operationStr, Meter meter) {
TimeUnit rateUnit = TimeUnit.SECONDS;
double rateFactor = rateUnit.toSeconds(1);
print(FORMAT_METER, operationStr, meter.getCount(),
meter.getMeanRate() * rateFactor,
meter.getOneMinuteRate() * rateFactor,
meter.getFiveMinuteRate() * rateFactor,
meter.getFifteenMinuteRate() * rateFactor);
}
}
/**
* ONOS Intent Metrics Application that collects intent-related metrics.
*/
package org.onlab.onos.metrics.intent;
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
<command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
<command>
<action class="org.onlab.onos.metrics.intent.cli.IntentEventsListCommand"/>
</command>
<command>
<action class="org.onlab.onos.metrics.intent.cli.IntentEventsMetricsCommand"/>
</command>
</command-bundle>
</blueprint>
......@@ -17,6 +17,7 @@
<description>ONOS metrics applications</description>
<modules>
<module>intent</module>
<module>topology</module>
</modules>
......
......@@ -199,9 +199,16 @@
<feature name="onos-app-metrics" version="1.0.0"
description="ONOS metrics applications">
<feature>onos-app-metrics-intent</feature>
<feature>onos-app-metrics-topology</feature>
</feature>
<feature name="onos-app-metrics-intent" version="1.0.0"
description="ONOS intent metrics application">
<feature>onos-api</feature>
<bundle>mvn:org.onlab.onos/onos-app-metrics-intent/1.0.0-SNAPSHOT</bundle>
</feature>
<feature name="onos-app-metrics-topology" version="1.0.0"
description="ONOS topology metrics application">
<feature>onos-api</feature>
......