HBASE-23892 SecureTestCluster should allow its subclasses to pass their Class referen...
[hbase.git] / hbase-common / src / main / java / org / apache / hadoop / hbase / trace / TraceUtil.java
blob10665d89826507bbf829d80cbee285a21a2e8790
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.hadoop.hbase.trace;
20 import org.apache.hadoop.conf.Configuration;
21 import org.apache.htrace.core.HTraceConfiguration;
22 import org.apache.htrace.core.Sampler;
23 import org.apache.htrace.core.Span;
24 import org.apache.htrace.core.SpanReceiver;
25 import org.apache.htrace.core.TraceScope;
26 import org.apache.htrace.core.Tracer;
27 import org.apache.yetus.audience.InterfaceAudience;
29 /**
30 * This wrapper class provides functions for accessing htrace 4+ functionality in a simplified way.
32 @InterfaceAudience.Private
33 public final class TraceUtil {
34 private static HTraceConfiguration conf;
35 private static Tracer tracer;
37 private TraceUtil() {
40 public static void initTracer(Configuration c) {
41 if (c != null) {
42 conf = new HBaseHTraceConfiguration(c);
45 if (tracer == null && conf != null) {
46 tracer = new Tracer.Builder("Tracer").conf(conf).build();
50 /**
51 * Wrapper method to create new TraceScope with the given description
52 * @return TraceScope or null when not tracing
54 public static TraceScope createTrace(String description) {
55 return (tracer == null) ? null : tracer.newScope(description);
58 /**
59 * Wrapper method to create new child TraceScope with the given description
60 * and parent scope's spanId
61 * @param span parent span
62 * @return TraceScope or null when not tracing
64 public static TraceScope createTrace(String description, Span span) {
65 if (span == null) {
66 return createTrace(description);
69 return (tracer == null) ? null : tracer.newScope(description, span.getSpanId());
72 /**
73 * Wrapper method to add new sampler to the default tracer
74 * @return true if added, false if it was already added
76 public static boolean addSampler(Sampler sampler) {
77 if (sampler == null) {
78 return false;
81 return (tracer == null) ? false : tracer.addSampler(sampler);
84 /**
85 * Wrapper method to add key-value pair to TraceInfo of actual span
87 public static void addKVAnnotation(String key, String value){
88 Span span = Tracer.getCurrentSpan();
89 if (span != null) {
90 span.addKVAnnotation(key, value);
94 /**
95 * Wrapper method to add receiver to actual tracerpool
96 * @return true if successfull, false if it was already added
98 public static boolean addReceiver(SpanReceiver rcvr) {
99 return (tracer == null) ? false : tracer.getTracerPool().addReceiver(rcvr);
103 * Wrapper method to remove receiver from actual tracerpool
104 * @return true if removed, false if doesn't exist
106 public static boolean removeReceiver(SpanReceiver rcvr) {
107 return (tracer == null) ? false : tracer.getTracerPool().removeReceiver(rcvr);
111 * Wrapper method to add timeline annotiation to current span with given message
113 public static void addTimelineAnnotation(String msg) {
114 Span span = Tracer.getCurrentSpan();
115 if (span != null) {
116 span.addTimelineAnnotation(msg);
121 * Wrap runnable with current tracer and description
122 * @param runnable to wrap
123 * @return wrapped runnable or original runnable when not tracing
125 public static Runnable wrap(Runnable runnable, String description) {
126 return (tracer == null) ? runnable : tracer.wrap(runnable, description);