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 52 * @param recordName */53publicHttpSenderMetrics(String processName, String recordName) {
54 MetricsContext context = MetricsUtil.getContext(processName);
55 metricsRecord = MetricsUtil.createRecord(context, recordName);
56 metricsRecord.setTag("process", processName);
57 mbean = newHttpSenderActivityMBean(registry, recordName);
58 context.registerUpdater(this);
59 }
606162/**63 * Since this object is a registered updater, this method will be called64 * periodically, e.g. every 5 seconds.65 */66publicvoid doUpdates(MetricsContext unused) {
67synchronized (this) {
68for (MetricsBase m : registry.getMetricsList()) {
69 m.pushMetric(metricsRecord);
70 }
71 }
72 metricsRecord.update();
73 }
7475publicvoid shutdown() {
76if (mbean != null)
77 mbean.shutdown();
78 }
7980 }