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