source: proiecte/HadoopJUnit/hadoop-0.20.1/src/core/org/apache/hadoop/metrics/MetricsUtil.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 3.2 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * 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 and
16 * limitations under the License.
17 */
18package org.apache.hadoop.metrics;
19
20import java.net.InetAddress;
21import java.net.UnknownHostException;
22
23import org.apache.commons.logging.Log;
24import org.apache.commons.logging.LogFactory;
25
26/**
27 * Utility class to simplify creation and reporting of hadoop metrics.
28 *
29 * For examples of usage, see NameNodeMetrics.
30 * @see org.apache.hadoop.metrics.MetricsRecord
31 * @see org.apache.hadoop.metrics.MetricsContext
32 * @see org.apache.hadoop.metrics.ContextFactory
33 */
34public class MetricsUtil {
35   
36  public static final Log LOG =
37    LogFactory.getLog(MetricsUtil.class);
38
39  /**
40   * Don't allow creation of a new instance of Metrics
41   */
42  private MetricsUtil() {}
43   
44  public static MetricsContext getContext(String contextName) {
45    return getContext(contextName, contextName);
46  }
47
48  /**
49   * Utility method to return the named context.
50   * If the desired context cannot be created for any reason, the exception
51   * is logged, and a null context is returned.
52   */
53  public static MetricsContext getContext(String refName, String contextName) {
54    MetricsContext metricsContext;
55    try {
56      metricsContext =
57        ContextFactory.getFactory().getContext(refName, contextName);
58      if (!metricsContext.isMonitoring()) {
59        metricsContext.startMonitoring();
60      }
61    } catch (Exception ex) {
62      LOG.error("Unable to create metrics context " + contextName, ex);
63      metricsContext = ContextFactory.getNullContext(contextName);
64    }
65    return metricsContext;
66  }
67
68  /**
69   * Utility method to create and return new metrics record instance within the
70   * given context. This record is tagged with the host name.
71   *
72   * @param context the context
73   * @param recordName name of the record
74   * @return newly created metrics record
75   */
76  public static MetricsRecord createRecord(MetricsContext context, 
77                                           String recordName) 
78  {
79    MetricsRecord metricsRecord = context.createRecord(recordName);
80    metricsRecord.setTag("hostName", getHostName());
81    return metricsRecord;       
82  }
83   
84  /**
85   * Returns the host name.  If the host name is unobtainable, logs the
86   * exception and returns "unknown".
87   */
88  private static String getHostName() {
89    String hostName = null;
90    try {
91      hostName = InetAddress.getLocalHost().getHostName();
92    } 
93    catch (UnknownHostException ex) {
94      LOG.info("Unable to obtain hostName", ex);
95      hostName = "unknown";
96    }
97    return hostName;
98  }
99
100}
Note: See TracBrowser for help on using the repository browser.