1 | /* |
---|
2 | * FileContext.java |
---|
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 | |
---|
21 | package org.apache.hadoop.metrics.file; |
---|
22 | |
---|
23 | import java.io.BufferedOutputStream; |
---|
24 | import java.io.File; |
---|
25 | import java.io.FileWriter; |
---|
26 | import java.io.IOException; |
---|
27 | import java.io.PrintWriter; |
---|
28 | |
---|
29 | import org.apache.hadoop.metrics.ContextFactory; |
---|
30 | import org.apache.hadoop.metrics.MetricsException; |
---|
31 | import org.apache.hadoop.metrics.spi.AbstractMetricsContext; |
---|
32 | import org.apache.hadoop.metrics.spi.OutputRecord; |
---|
33 | |
---|
34 | /** |
---|
35 | * Metrics context for writing metrics to a file.<p/> |
---|
36 | * |
---|
37 | * This class is configured by setting ContextFactory attributes which in turn |
---|
38 | * are usually configured through a properties file. All the attributes are |
---|
39 | * prefixed by the contextName. For example, the properties file might contain: |
---|
40 | * <pre> |
---|
41 | * myContextName.fileName=/tmp/metrics.log |
---|
42 | * myContextName.period=5 |
---|
43 | * </pre> |
---|
44 | */ |
---|
45 | public class FileContext extends AbstractMetricsContext { |
---|
46 | |
---|
47 | /* Configuration attribute names */ |
---|
48 | protected static final String FILE_NAME_PROPERTY = "fileName"; |
---|
49 | protected static final String PERIOD_PROPERTY = "period"; |
---|
50 | |
---|
51 | private File file = null; // file for metrics to be written to |
---|
52 | private PrintWriter writer = null; |
---|
53 | |
---|
54 | /** Creates a new instance of FileContext */ |
---|
55 | public FileContext() {} |
---|
56 | |
---|
57 | public void init(String contextName, ContextFactory factory) { |
---|
58 | super.init(contextName, factory); |
---|
59 | |
---|
60 | String fileName = getAttribute(FILE_NAME_PROPERTY); |
---|
61 | if (fileName != null) { |
---|
62 | file = new File(fileName); |
---|
63 | } |
---|
64 | |
---|
65 | String periodStr = getAttribute(PERIOD_PROPERTY); |
---|
66 | if (periodStr != null) { |
---|
67 | int period = 0; |
---|
68 | try { |
---|
69 | period = Integer.parseInt(periodStr); |
---|
70 | } catch (NumberFormatException nfe) { |
---|
71 | } |
---|
72 | if (period <= 0) { |
---|
73 | throw new MetricsException("Invalid period: " + periodStr); |
---|
74 | } |
---|
75 | setPeriod(period); |
---|
76 | } |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Returns the configured file name, or null. |
---|
81 | */ |
---|
82 | public String getFileName() { |
---|
83 | if (file == null) { |
---|
84 | return null; |
---|
85 | } else { |
---|
86 | return file.getName(); |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Starts or restarts monitoring, by opening in append-mode, the |
---|
92 | * file specified by the <code>fileName</code> attribute, |
---|
93 | * if specified. Otherwise the data will be written to standard |
---|
94 | * output. |
---|
95 | */ |
---|
96 | public void startMonitoring() |
---|
97 | throws IOException |
---|
98 | { |
---|
99 | 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 | public void stopMonitoring() { |
---|
112 | super.stopMonitoring(); |
---|
113 | |
---|
114 | if (writer != null) { |
---|
115 | writer.close(); |
---|
116 | writer = null; |
---|
117 | } |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * Emits a metrics record to a file. |
---|
122 | */ |
---|
123 | public void emitRecord(String contextName, String recordName, OutputRecord outRec) { |
---|
124 | writer.print(contextName); |
---|
125 | writer.print("."); |
---|
126 | writer.print(recordName); |
---|
127 | String separator = ": "; |
---|
128 | for (String tagName : outRec.getTagNames()) { |
---|
129 | writer.print(separator); |
---|
130 | separator = ", "; |
---|
131 | writer.print(tagName); |
---|
132 | writer.print("="); |
---|
133 | writer.print(outRec.getTag(tagName)); |
---|
134 | } |
---|
135 | for (String metricName : outRec.getMetricNames()) { |
---|
136 | writer.print(separator); |
---|
137 | separator = ", "; |
---|
138 | writer.print(metricName); |
---|
139 | writer.print("="); |
---|
140 | writer.print(outRec.getMetric(metricName)); |
---|
141 | } |
---|
142 | writer.println(); |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * Flushes the output writer, forcing updates to disk. |
---|
147 | */ |
---|
148 | public void flush() { |
---|
149 | writer.flush(); |
---|
150 | } |
---|
151 | } |
---|