001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.metrics2.sink;
020
021import java.io.Closeable;
022import java.io.File;
023import java.io.FileOutputStream;
024import java.io.IOException;
025import java.io.PrintStream;
026
027import org.apache.commons.configuration.SubsetConfiguration;
028import org.apache.hadoop.classification.InterfaceAudience;
029import org.apache.hadoop.classification.InterfaceStability;
030import org.apache.hadoop.metrics2.AbstractMetric;
031import org.apache.hadoop.metrics2.MetricsException;
032import org.apache.hadoop.metrics2.MetricsRecord;
033import org.apache.hadoop.metrics2.MetricsSink;
034import org.apache.hadoop.metrics2.MetricsTag;
035
036/**
037 * A metrics sink that writes to a file
038 */
039@InterfaceAudience.Public
040@InterfaceStability.Evolving
041public class FileSink implements MetricsSink, Closeable {
042  private static final String FILENAME_KEY = "filename";
043  private PrintStream writer;
044
045  @Override
046  public void init(SubsetConfiguration conf) {
047    String filename = conf.getString(FILENAME_KEY);
048    try {
049      writer = filename == null ? System.out
050          : new PrintStream(new FileOutputStream(new File(filename)),
051                            true, "UTF-8");
052    } catch (Exception e) {
053      throw new MetricsException("Error creating "+ filename, e);
054    }
055  }
056
057  @Override
058  public void putMetrics(MetricsRecord record) {
059    writer.print(record.timestamp());
060    writer.print(" ");
061    writer.print(record.context());
062    writer.print(".");
063    writer.print(record.name());
064    String separator = ": ";
065    for (MetricsTag tag : record.tags()) {
066      writer.print(separator);
067      separator = ", ";
068      writer.print(tag.name());
069      writer.print("=");
070      writer.print(tag.value());
071    }
072    for (AbstractMetric metric : record.metrics()) {
073      writer.print(separator);
074      separator = ", ";
075      writer.print(metric.name());
076      writer.print("=");
077      writer.print(metric.value());
078    }
079    writer.println();
080  }
081
082  @Override
083  public void flush() {
084    writer.flush();
085  }
086
087  @Override
088  public void close() throws IOException {
089    writer.close();
090  }
091}