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 */51publicHttpSenderMetrics(String processName, String recordName) {
52 MetricsContext context = MetricsUtil.getContext(processName);
53 metricsRecord = MetricsUtil.createRecord(context, recordName);
54 metricsRecord.setTag("process", processName);
55 mbean = newHttpSenderActivityMBean(registry, recordName);
56 context.registerUpdater(this);
57 }
585960/**61 * Since this object is a registered updater, this method will be called62 * periodically, e.g. every 5 seconds.63 */64publicvoid doUpdates(MetricsContext unused) {
65synchronized (this) {
66for (MetricsBase m : registry.getMetricsList()) {
67 m.pushMetric(metricsRecord);
68 }
69 }
70 metricsRecord.update();
71 }
7273publicvoid shutdown() {
74if (mbean != null)
75 mbean.shutdown();
76 }
7778 }