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 is jvm name of agent process47 * @param recordName is mbean record name48 **/49publicAgentMetrics(String processName, String recordName) {
50 MetricsContext context = MetricsUtil.getContext(processName);
51 metricsRecord = MetricsUtil.createRecord(context, recordName);
52 metricsRecord.setTag("process", processName);
53 agentActivityMBean = newAgentActivityMBean(registry, recordName);
54 context.registerUpdater(this);
5556 }
575859/**60 * Since this object is a registered updater, this method will be called61 * periodically, e.g. every 5 seconds.62 */63publicvoid doUpdates(MetricsContext unused) {
64synchronized (this) {
65for (MetricsBase m : registry.getMetricsList()) {
66 m.pushMetric(metricsRecord);
67 }
68 }
69 metricsRecord.update();
70 }
7172publicvoid shutdown() {
73if (agentActivityMBean != null)
74 agentActivityMBean.shutdown();
75 }
7677 }