View Javadoc

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