2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.common.actor;
10 import akka.actor.UntypedActor;
11 import akka.japi.Procedure;
12 import com.codahale.metrics.MetricRegistry;
13 import com.codahale.metrics.Timer;
14 import com.google.common.base.Preconditions;
15 import org.opendaylight.controller.cluster.reporting.MetricsReporter;
18 * Represents behaviour that can be exhibited by actors of type {@link akka.actor.UntypedActor}
20 * This behaviour meters actor's default behaviour. It captures 2 metrics:
22 * <li>message processing rate of actor's receive block</li>
23 * <li>message processing rate by message type</li>
26 * The information is reported to {@link org.opendaylight.controller.cluster.reporting.MetricsReporter}
28 public class MeteringBehavior implements Procedure<Object> {
29 public static final String DOMAIN = "org.opendaylight.controller.actor.metric";
31 private final UntypedActor meteredActor;
33 private final MetricRegistry METRICREGISTRY = MetricsReporter.getInstance(DOMAIN).getMetricsRegistry();
34 private final String MSG_PROCESSING_RATE = "msg-rate";
36 private String actorQualifiedName;
37 private Timer msgProcessingTimer;
41 * @param actor whose behaviour needs to be metered
43 public MeteringBehavior(AbstractUntypedActorWithMetering actor){
44 Preconditions.checkArgument(actor != null, "actor must not be null");
45 this.meteredActor = actor;
47 String actorName = actor.getActorNameOverride() != null ? actor.getActorNameOverride()
48 : actor.getSelf().path().name();
52 public MeteringBehavior(UntypedActor actor){
53 Preconditions.checkArgument(actor != null, "actor must not be null");
54 this.meteredActor = actor;
56 String actorName = actor.getSelf().path().name();
60 private void init(String actorName){
61 actorQualifiedName = new StringBuilder(meteredActor.getSelf().path().parent().toStringWithoutAddress()).
62 append("/").append(actorName).toString();
64 final String msgProcessingTime = MetricRegistry.name(actorQualifiedName, MSG_PROCESSING_RATE);
65 msgProcessingTimer = METRICREGISTRY.timer(msgProcessingTime);
69 * Uses 2 timers to measure message processing rate. One for overall message processing rate and
70 * another to measure rate by message type. The timers are re-used if they were previously created.
72 * {@link com.codahale.metrics.MetricRegistry} maintains a reservoir for different timers where
73 * collected timings are kept. It exposes various metrics for each timer based on collected
74 * data. Eg: count of messages, 99, 95, 50... percentiles, max, mean etc.
76 * These metrics are exposed as JMX bean.
78 * @see <a href="http://dropwizard.github.io/metrics/manual/core/#timers">
79 * http://dropwizard.github.io/metrics/manual/core/#timers</a>
85 public void apply(Object message) throws Exception {
86 final String messageType = message.getClass().getSimpleName();
88 final String msgProcessingTimeByMsgType =
89 MetricRegistry.name(actorQualifiedName, MSG_PROCESSING_RATE, messageType);
91 final Timer msgProcessingTimerByMsgType = METRICREGISTRY.timer(msgProcessingTimeByMsgType);
94 final Timer.Context context = msgProcessingTimer.time();
95 final Timer.Context contextByMsgType = msgProcessingTimerByMsgType.time();
97 meteredActor.onReceive(message);
100 contextByMsgType.stop();