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 46 * @param processName 47 * @param recordName */48publicAgentMetrics(String processName, String recordName) {
49 MetricsContext context = MetricsUtil.getContext(processName);
50 metricsRecord = MetricsUtil.createRecord(context, recordName);
51 metricsRecord.setTag("process", processName);
52 agentActivityMBean = newAgentActivityMBean(registry, recordName);
53 context.registerUpdater(this);
5455 }
565758/**59 * Since this object is a registered updater, this method will be called60 * periodically, e.g. every 5 seconds.61 */62publicvoid doUpdates(MetricsContext unused) {
63synchronized (this) {
64for (MetricsBase m : registry.getMetricsList()) {
65 m.pushMetric(metricsRecord);
66 }
67 }
68 metricsRecord.update();
69 }
7071publicvoid shutdown() {
72if (agentActivityMBean != null)
73 agentActivityMBean.shutdown();
74 }
7576 }