1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */18package org.apache.hadoop.chukwa.datacollection.agent.metrics;
1920import org.apache.hadoop.metrics.MetricsContext;
21import org.apache.hadoop.metrics.MetricsRecord;
22import org.apache.hadoop.metrics.MetricsUtil;
23import org.apache.hadoop.metrics.Updater;
24import org.apache.hadoop.metrics.util.MetricsBase;
25import org.apache.hadoop.metrics.util.MetricsIntValue;
26import org.apache.hadoop.metrics.util.MetricsLongValue;
27import org.apache.hadoop.metrics.util.MetricsRegistry;
28import org.apache.hadoop.metrics.util.MetricsTimeVaryingInt;
2930publicclassChunkQueueMetricsimplements Updater {
3132public MetricsRegistry registry = new MetricsRegistry();
33private MetricsRecord metricsRecord;
34privateChunkQueueActivityMBean mbean;
353637public MetricsIntValue queueSize =
38new MetricsIntValue("queueSize", registry,"Queue size");
3940public MetricsLongValue dataSize =
41new MetricsLongValue("dataSize", registry,"Data size");
4243public MetricsTimeVaryingInt addedChunk =
44new MetricsTimeVaryingInt("addedChunk", registry,"number of added chunk");
4546public MetricsTimeVaryingInt removedChunk =
47new MetricsTimeVaryingInt("removedChunk", registry,"number of removed chunk");
4849public MetricsIntValue fullQueue =
50new MetricsIntValue("fullQueue", registry,"Queue is full");
515253/** Creates a new instance of QueueMetrics */54publicChunkQueueMetrics(String processName, String recordName) {
55 MetricsContext context = MetricsUtil.getContext(processName);
56 metricsRecord = MetricsUtil.createRecord(context, recordName);
57 mbean = newChunkQueueActivityMBean(registry, recordName);
58 context.registerUpdater(this);
5960 }
616263/**64 * Since this object is a registered updater, this method will be called65 * periodically, e.g. every 5 seconds.66 */67publicvoid doUpdates(MetricsContext unused) {
68synchronized (this) {
69for (MetricsBase m : registry.getMetricsList()) {
70 m.pushMetric(metricsRecord);
71 }
72 }
73 metricsRecord.update();
74 }
7576publicvoid shutdown() {
77if (mbean != null)
78 mbean.shutdown();
79 }
8081 }