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}
22 * This behaviour meters actor's default behaviour. It captures 2 metrics:
24 * <li>message processing rate of actor's receive block</li>
25 * <li>message processing rate by message type</li>
27 * The information is reported to {@link org.opendaylight.controller.cluster.reporting.MetricsReporter}
29 public class MeteringBehavior extends AbstractPartialFunction<Object, BoxedUnit> {
30 public static final String DOMAIN = "org.opendaylight.controller.actor.metric";
32 private static final String MSG_PROCESSING_RATE = "msg-rate";
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;
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();
46 * Constructs an instance.
48 * @param actor whose behaviour needs to be metered
50 public MeteringBehavior(final AbstractUntypedActorWithMetering actor) {
51 this(actor.getActorNameOverride() != null ? actor.getActorNameOverride() : actor.getSelf().path().name(),
55 public MeteringBehavior(final AbstractActor actor) {
56 this(actor.getSelf().path().name(), actor);
60 public boolean isDefinedAt(final Object obj) {
61 return receive.isDefinedAt(obj);
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.
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.
74 * These metrics are exposed as JMX bean.
76 * @see <a href="http://dropwizard.github.io/metrics/manual/core/#timers">
77 * http://dropwizard.github.io/metrics/manual/core/#timers</a>
79 * @param message the message to process
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);
89 final Timer.Context context = msgProcessingTimer.time();
90 final Timer.Context contextByMsgType = msgProcessingTimerByMsgType.time();
93 return receive.apply(message);
96 contextByMsgType.stop();