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.sender.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.MetricsRegistry;
26import org.apache.hadoop.metrics.util.MetricsTimeVaryingInt;
2728publicclassHttpSenderMetricsimplements Updater {
2930public MetricsRegistry registry = new MetricsRegistry();
31private MetricsRecord metricsRecord;
32privateHttpSenderActivityMBean mbean;
333435public MetricsTimeVaryingInt collectorRollover =
36new MetricsTimeVaryingInt("collectorRollover", registry,"number of collector rollovert");
3738public MetricsTimeVaryingInt httpPost =
39new MetricsTimeVaryingInt("httpPost", registry,"number of HTTP post");
4041public MetricsTimeVaryingInt httpException =
42new MetricsTimeVaryingInt("httpException", registry,"number of HTTP Exception");
4344public MetricsTimeVaryingInt httpThrowable =
45new MetricsTimeVaryingInt("httpThrowable", registry,"number of HTTP Throwable exception");
4647public MetricsTimeVaryingInt httpTimeOutException =
48new MetricsTimeVaryingInt("httpTimeOutException", registry,"number of HTTP TimeOutException");
4950/** Creates a new instance of HttpSenderMetrics 51 * @param processName is jvm process name52 * @param recordName is Hadoop metrics data type53 * */54publicHttpSenderMetrics(String processName, String recordName) {
55 MetricsContext context = MetricsUtil.getContext(processName);
56 metricsRecord = MetricsUtil.createRecord(context, recordName);
57 metricsRecord.setTag("process", processName);
58 mbean = newHttpSenderActivityMBean(registry, recordName);
59 context.registerUpdater(this);
60 }
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 }