001/*
002 * FileContext.java
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020
021package org.apache.hadoop.metrics.file;
022
023import java.io.BufferedOutputStream;
024import java.io.File;
025import java.io.FileWriter;
026import java.io.IOException;
027import java.io.PrintWriter;
028
029import org.apache.hadoop.classification.InterfaceAudience;
030import org.apache.hadoop.classification.InterfaceStability;
031import org.apache.hadoop.metrics.ContextFactory;
032import org.apache.hadoop.metrics.spi.AbstractMetricsContext;
033import org.apache.hadoop.metrics.spi.OutputRecord;
034
035/**
036 * Metrics context for writing metrics to a file.<p/>
037 *
038 * This class is configured by setting ContextFactory attributes which in turn
039 * are usually configured through a properties file.  All the attributes are
040 * prefixed by the contextName. For example, the properties file might contain:
041 * <pre>
042 * myContextName.fileName=/tmp/metrics.log
043 * myContextName.period=5
044 * </pre>
045 * @see org.apache.hadoop.metrics2.sink.FileSink for metrics 2.0.
046 */
047@InterfaceAudience.Public
048@InterfaceStability.Evolving
049@Deprecated
050public class FileContext extends AbstractMetricsContext {
051    
052  /* Configuration attribute names */
053  @InterfaceAudience.Private
054  protected static final String FILE_NAME_PROPERTY = "fileName";
055  @InterfaceAudience.Private
056  protected static final String PERIOD_PROPERTY = "period";
057    
058  private File file = null;              // file for metrics to be written to
059  private PrintWriter writer = null;
060    
061  /** Creates a new instance of FileContext */
062  @InterfaceAudience.Private
063  public FileContext() {}
064    
065  @InterfaceAudience.Private
066  public void init(String contextName, ContextFactory factory) {
067    super.init(contextName, factory);
068        
069    String fileName = getAttribute(FILE_NAME_PROPERTY);
070    if (fileName != null) {
071      file = new File(fileName);
072    }
073        
074    parseAndSetPeriod(PERIOD_PROPERTY);
075  }
076
077  /**
078   * Returns the configured file name, or null.
079   */
080  @InterfaceAudience.Private
081  public String getFileName() {
082    if (file == null) {
083      return null;
084    } else {
085      return file.getName();
086    }
087  }
088    
089  /**
090   * Starts or restarts monitoring, by opening in append-mode, the
091   * file specified by the <code>fileName</code> attribute,
092   * if specified. Otherwise the data will be written to standard
093   * output.
094   */
095  @InterfaceAudience.Private
096  public void startMonitoring()
097    throws IOException 
098  {
099    if (file == null) {
100      writer = new PrintWriter(new BufferedOutputStream(System.out));
101    } else {
102      writer = new PrintWriter(new FileWriter(file, true));
103    }
104    super.startMonitoring();
105  }
106    
107  /**
108   * Stops monitoring, closing the file.
109   * @see #close()
110   */
111  @InterfaceAudience.Private
112  public void stopMonitoring() {
113    super.stopMonitoring();
114        
115    if (writer != null) {
116      writer.close();
117      writer = null;
118    }
119  }
120    
121  /**
122   * Emits a metrics record to a file.
123   */
124  @InterfaceAudience.Private
125  public void emitRecord(String contextName, String recordName, OutputRecord outRec) {
126    writer.print(contextName);
127    writer.print(".");
128    writer.print(recordName);
129    String separator = ": ";
130    for (String tagName : outRec.getTagNames()) {
131      writer.print(separator);
132      separator = ", ";
133      writer.print(tagName);
134      writer.print("=");
135      writer.print(outRec.getTag(tagName));
136    }
137    for (String metricName : outRec.getMetricNames()) {
138      writer.print(separator);
139      separator = ", ";
140      writer.print(metricName);
141      writer.print("=");
142      writer.print(outRec.getMetric(metricName));
143    }
144    writer.println();
145  }
146    
147  /**
148   * Flushes the output writer, forcing updates to disk.
149   */
150  @InterfaceAudience.Private
151  public void flush() {
152    writer.flush();
153  }
154}