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.MetricsRegistry;
27import org.apache.hadoop.metrics.util.MetricsTimeVaryingInt;
2829publicclassAgentMetricsimplements Updater {
30publicstaticfinalAgentMetrics agentMetrics = newAgentMetrics("chukwaAgent", "metrics");
3132public MetricsRegistry registry = new MetricsRegistry();
33private MetricsRecord metricsRecord;
34privateAgentActivityMBean agentActivityMBean;
3536public MetricsIntValue adaptorCount =
37new MetricsIntValue("adaptorCount", registry,"number of new adaptor");
3839public MetricsTimeVaryingInt addedAdaptor =
40new MetricsTimeVaryingInt("addedAdaptor", registry,"number of added adaptor");
4142public MetricsTimeVaryingInt removedAdaptor =
43new MetricsTimeVaryingInt("removedAdaptor", registry,"number of removed adaptor");
4445/** Creates a new instance of AgentMetrics */46publicAgentMetrics(String processName, String recordName) {
47 MetricsContext context = MetricsUtil.getContext(processName);
48 metricsRecord = MetricsUtil.createRecord(context, recordName);
49 metricsRecord.setTag("process", processName);
50 agentActivityMBean = newAgentActivityMBean(registry, recordName);
51 context.registerUpdater(this);
5253 }
545556/**57 * Since this object is a registered updater, this method will be called58 * periodically, e.g. every 5 seconds.59 */60publicvoid doUpdates(MetricsContext unused) {
61synchronized (this) {
62for (MetricsBase m : registry.getMetricsList()) {
63 m.pushMetric(metricsRecord);
64 }
65 }
66 metricsRecord.update();
67 }
6869publicvoid shutdown() {
70if (agentActivityMBean != null)
71 agentActivityMBean.shutdown();
72 }
7374 }