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 54 * @param processName is jvm process of Agent process55 * @param recordName is mbean record name56 * */57publicChunkQueueMetrics(String processName, String recordName) {
58 MetricsContext context = MetricsUtil.getContext(processName);
59 metricsRecord = MetricsUtil.createRecord(context, recordName);
60 mbean = newChunkQueueActivityMBean(registry, recordName);
61 context.registerUpdater(this);
6263 }
646566/**67 * Since this object is a registered updater, this method will be called68 * periodically, e.g. every 5 seconds.69 */70publicvoid doUpdates(MetricsContext unused) {
71synchronized (this) {
72for (MetricsBase m : registry.getMetricsList()) {
73 m.pushMetric(metricsRecord);
74 }
75 }
76 metricsRecord.update();
77 }
7879publicvoid shutdown() {
80if (mbean != null)
81 mbean.shutdown();
82 }
8384 }