View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.metrics.file;
20  
21  import java.io.BufferedOutputStream;
22  import java.io.File;
23  import java.io.FileWriter;
24  import java.io.IOException;
25  import java.io.PrintWriter;
26  import java.text.SimpleDateFormat;
27  import java.util.Date;
28  
29  import org.apache.hadoop.classification.InterfaceAudience;
30  import org.apache.hadoop.metrics.ContextFactory;
31  import org.apache.hadoop.metrics.file.FileContext;
32  import org.apache.hadoop.metrics.spi.OutputRecord;
33  
34  /**
35   * Add timestamp to {@link org.apache.hadoop.metrics.file.FileContext#emitRecord(String, String, OutputRecord)}.
36   */
37  @Deprecated
38  @InterfaceAudience.Private
39  public class TimeStampingFileContext extends FileContext {
40    // Copies bunch of FileContext here because writer and file are private in
41    // superclass.
42    private File file = null;
43    private PrintWriter writer = null;
44    private final SimpleDateFormat sdf;
45  
46    public TimeStampingFileContext() {
47      super();
48      this.sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
49    }
50  
51    @Override
52    public void init(String contextName, ContextFactory factory) {
53      super.init(contextName, factory);
54      String fileName = getAttribute(FILE_NAME_PROPERTY);
55      if (fileName != null) {
56        file = new File(fileName);
57      }
58    }
59  
60    @Override
61    public void startMonitoring() throws IOException {
62      if (file == null) {
63        writer = new PrintWriter(new BufferedOutputStream(System.out));
64      } else {
65        writer = new PrintWriter(new FileWriter(file, true));
66      }
67      super.startMonitoring();
68    }
69  
70    @Override
71    public void stopMonitoring() {
72      super.stopMonitoring();
73      if (writer != null) {
74        writer.close();
75        writer = null;
76      }
77    }
78  
79    private synchronized String iso8601() {
80      return this.sdf.format(new Date());
81    }
82  
83    @Override
84    public void emitRecord(String contextName, String recordName,
85        OutputRecord outRec) {
86      writer.print(iso8601());
87      writer.print(" ");
88      writer.print(contextName);
89      writer.print(".");
90      writer.print(recordName);
91      String separator = ": ";
92      for (String tagName : outRec.getTagNames()) {
93        writer.print(separator);
94        separator = ", ";
95        writer.print(tagName);
96        writer.print("=");
97        writer.print(outRec.getTag(tagName));
98      }
99      for (String metricName : outRec.getMetricNames()) {
100       writer.print(separator);
101       separator = ", ";
102       writer.print(metricName);
103       writer.print("=");
104       writer.print(outRec.getMetric(metricName));
105     }
106     writer.println();
107   }
108 
109   @Override
110   public void flush() {
111     writer.flush();
112   }
113 }