View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.trace;
19  import java.io.IOException;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  import org.apache.hadoop.conf.Configurable;
24  import org.apache.hadoop.conf.Configuration;
25  import org.cloudera.htrace.Span;
26  import org.cloudera.htrace.SpanReceiver;
27  import org.cloudera.htrace.Trace;
28  import org.cloudera.htrace.impl.LocalFileSpanReceiver;
29  
30  /**
31   * Wraps the LocalFileSpanReceiver provided in
32   * org.cloudera.htrace.impl.LocalFileSpanReceiver to read the file name
33   * destination for spans from hbase-site.xml.
34   * 
35   * The file path should be added as a property with name
36   * "hbase.trace.spanreceiver.localfilespanreceiver.filename".
37   */
38  public class HBaseLocalFileSpanReceiver implements SpanReceiver, Configurable {
39    public static final Log LOG = LogFactory
40        .getLog(HBaseLocalFileSpanReceiver.class);
41    public static final String FILE_NAME_CONF_KEY = "hbase.trace.spanreceiver.localfilespanreceiver.filename";
42    private Configuration conf;
43    private LocalFileSpanReceiver rcvr;
44  
45    @Override
46    public Configuration getConf() {
47      return conf;
48    }
49  
50    @Override
51    public void setConf(Configuration arg0) {
52      this.conf = arg0;
53      // replace rcvr if it was already created
54      if (rcvr != null) {
55        try {
56          rcvr.close();
57        } catch (IOException e) {
58          LOG.warn("Error closing LocalFileSpanReceiver.", e);
59        }
60      }
61      try {
62        rcvr = new LocalFileSpanReceiver(conf.get(FILE_NAME_CONF_KEY));
63      } catch (IOException e) {
64        Trace.removeReceiver(this);
65        rcvr = null;
66        LOG.warn(
67            "Unable to initialize LocalFileSpanReceiver, removing owner (HBaseLocalFileSpanReceiver) from receiver list.",
68            e);
69      }
70    }
71  
72    @Override
73    public void close() throws IOException {
74      try{
75        if (rcvr != null) {
76          rcvr.close();
77        }
78      } finally {
79        rcvr = null;
80      }
81    }
82  
83    @Override
84    public void receiveSpan(Span span) {
85      if (rcvr != null) {
86        rcvr.receiveSpan(span);
87      }
88    }
89  }