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.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;
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:
22 * <li>message processing rate of actor's receive block</li>
23 * <li>message processing rate by message type</li>
25 * The information is reported to {@link org.opendaylight.controller.cluster.reporting.MetricsReporter}
27 public class MeteringBehavior extends AbstractPartialFunction<Object, BoxedUnit> {
28 public static final String DOMAIN = "org.opendaylight.controller.actor.metric";
30 private static final String MSG_PROCESSING_RATE = "msg-rate";
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;
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();
44 * Constructs an instance.
46 * @param actor whose behaviour needs to be metered
48 public MeteringBehavior(final AbstractUntypedActorWithMetering actor) {
49 this(actor.getActorNameOverride() != null ? actor.getActorNameOverride() : actor.getSelf().path().name(),
53 public MeteringBehavior(final AbstractActor actor) {
54 this(actor.getSelf().path().name(), actor);
58 public boolean isDefinedAt(final Object obj) {
59 return receive.isDefinedAt(obj);
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.
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.
70 * <p>These metrics are exposed as JMX bean.
72 * @see <a href="http://dropwizard.github.io/metrics/manual/core/#timers">
73 * http://dropwizard.github.io/metrics/manual/core/#timers</a>
75 * @param message the message to process
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);
85 final Timer.Context context = msgProcessingTimer.time();
86 final Timer.Context contextByMsgType = msgProcessingTimerByMsgType.time();
89 return receive.apply(message);
92 contextByMsgType.stop();