4b65ad9453e86310421bf04a9199166e582631ac
[controller.git] /
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.common.actor;
9
10 import akka.actor.AbstractActor;
11 import com.codahale.metrics.MetricRegistry;
12 import com.codahale.metrics.Timer;
13 import org.opendaylight.controller.cluster.reporting.MetricsReporter;
14 import scala.PartialFunction;
15 import scala.runtime.AbstractPartialFunction;
16 import scala.runtime.BoxedUnit;
17
18 /**
19  * Represents behaviour that can be exhibited by actors of type {@link AbstractActor}. This behaviour meters actor's
20  * default behaviour. It captures 2 metrics:
21  * <ul>
22  *     <li>message processing rate of actor's receive block</li>
23  *     <li>message processing rate by message type</li>
24  * </ul>
25  * The information is reported to {@link org.opendaylight.controller.cluster.reporting.MetricsReporter}
26  */
27 public class MeteringBehavior extends AbstractPartialFunction<Object, BoxedUnit> {
28     public static final String DOMAIN = "org.opendaylight.controller.actor.metric";
29
30     private static final String MSG_PROCESSING_RATE = "msg-rate";
31
32     private final MetricRegistry metricRegistry = MetricsReporter.getInstance(DOMAIN).getMetricsRegistry();
33     private final String actorQualifiedName;
34     private final Timer msgProcessingTimer;
35     private final PartialFunction<Object, BoxedUnit> receive;
36
37     private MeteringBehavior(final String actorName, final AbstractActor meteredActor) {
38         actorQualifiedName = meteredActor.getSelf().path().parent().toStringWithoutAddress() + "/" + actorName;
39         msgProcessingTimer = metricRegistry.timer(MetricRegistry.name(actorQualifiedName, MSG_PROCESSING_RATE));
40         receive = meteredActor.createReceive().onMessage();
41     }
42
43     /**
44      * Constructs an instance.
45      *
46      * @param actor whose behaviour needs to be metered
47      */
48     public MeteringBehavior(final AbstractUntypedActorWithMetering actor) {
49         this(actor.getActorNameOverride() != null ? actor.getActorNameOverride() : actor.getSelf().path().name(),
50                 actor);
51     }
52
53     public MeteringBehavior(final AbstractActor actor) {
54         this(actor.getSelf().path().name(), actor);
55     }
56
57     @Override
58     public boolean isDefinedAt(final Object obj) {
59         return receive.isDefinedAt(obj);
60     }
61
62     /**
63      * Uses 2 timers to measure message processing rate. One for overall message processing rate and
64      * another to measure rate by message type. The timers are re-used if they were previously created.
65      *
66      * <p>{@link com.codahale.metrics.MetricRegistry} maintains a reservoir for different timers where
67      * collected timings are kept. It exposes various metrics for each timer based on collected
68      * data. Eg: count of messages, 99, 95, 50... percentiles, max, mean etc.
69      *
70      * <p>These metrics are exposed as JMX bean.
71      *
72      * @see <a href="http://dropwizard.github.io/metrics/manual/core/#timers">
73      *     http://dropwizard.github.io/metrics/manual/core/#timers</a>
74      *
75      * @param message the message to process
76      */
77     @Override
78     public BoxedUnit apply(final Object message) {
79         final String messageType = message.getClass().getSimpleName();
80         final String msgProcessingTimeByMsgType =
81                 MetricRegistry.name(actorQualifiedName, MSG_PROCESSING_RATE, messageType);
82         final Timer msgProcessingTimerByMsgType = metricRegistry.timer(msgProcessingTimeByMsgType);
83
84         //start timers
85         final Timer.Context context = msgProcessingTimer.time();
86         final Timer.Context contextByMsgType = msgProcessingTimerByMsgType.time();
87
88         try {
89             return receive.apply(message);
90         } finally {
91             //stop timers
92             contextByMsgType.stop();
93             context.stop();
94         }
95     }
96 }