http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/TimelineAnnotation.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/TimelineAnnotation.java b/htrace-core/src/main/java/org/htrace/TimelineAnnotation.java deleted file mode 100644 index 265d209..0000000 --- a/htrace-core/src/main/java/org/htrace/TimelineAnnotation.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace; - -public class TimelineAnnotation { - private final long time; - private final String msg; - - public TimelineAnnotation(long time, String msg) { - this.time = time; - this.msg = msg; - } - - public long getTime() { - return time; - } - - public String getMessage() { - return msg; - } - - @Override - public String toString() { - return "@" + time + ": " + msg; - } -}
http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/Trace.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/Trace.java b/htrace-core/src/main/java/org/htrace/Trace.java deleted file mode 100644 index 8b1255b..0000000 --- a/htrace-core/src/main/java/org/htrace/Trace.java +++ /dev/null @@ -1,208 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace; - -import org.htrace.impl.MilliSpan; -import org.htrace.impl.TrueIfTracingSampler; -import org.htrace.wrappers.TraceCallable; -import org.htrace.wrappers.TraceRunnable; - -import java.security.SecureRandom; -import java.util.Random; -import java.util.concurrent.Callable; - -/** - * The primary way to interact with the library. Provides methods to start - * spans, as well as set necessary tracing information. - */ -public class Trace { - private final static Random random = new SecureRandom(); - - /** - * Starts and returns a new span as the child of the current span if the - * default sampler (TrueIfTracingSampler) returns true, otherwise returns the - * NullSpan. - * - * @param description Description of the span to be created. - * @return - */ - public static TraceScope startSpan(String description) { - return startSpan(description, TrueIfTracingSampler.INSTANCE); - } - - /** - * Starts and returns a new span as the child of the parameter 'parent'. This - * will always return a new span, even if tracing wasn't previously enabled for - * this thread. - * - * @param description Description of the span to be created. - * @param parent The parent that should be used to create the child span that is to - * be returned. - * @return - */ - public static TraceScope startSpan(String description, Span parent) { - if (parent == null) return startSpan(description); - return continueSpan(parent.child(description)); - } - - public static TraceScope startSpan(String description, TraceInfo tinfo) { - if (tinfo == null) return continueSpan(null); - Span newSpan = new MilliSpan(description, tinfo.traceId, tinfo.spanId, - random.nextLong(), Tracer.getProcessId()); - return continueSpan(newSpan); - } - - public static <T> TraceScope startSpan(String description, Sampler<T> s) { - return startSpan(description, s, null); - } - - public static TraceScope startSpan(String description, Sampler<TraceInfo> s, TraceInfo tinfo) { - Span span = null; - if (isTracing() || s.next(tinfo)) { - span = new MilliSpan(description, tinfo.traceId, tinfo.spanId, - random.nextLong(), Tracer.getProcessId()); - } - return continueSpan(span); - } - - public static <T> TraceScope startSpan(String description, Sampler<T> s, T info) { - Span span = null; - if (isTracing() || s.next(info)) { - span = Tracer.getInstance().createNew(description); - } - return continueSpan(span); - } - - /** - * Pick up an existing span from another thread. - */ - public static TraceScope continueSpan(Span s) { - // Return an empty TraceScope that does nothing on close - if (s == null) return NullScope.INSTANCE; - return Tracer.getInstance().continueSpan(s); - } - - /** - * Set the processId to be used for all Spans created by this Tracer. - * - * @param processId - * @see Span.java - */ - public static void setProcessId(String processId) { - Tracer.processId = processId; - } - - /** - * Removes the given SpanReceiver from the list of SpanReceivers. - * - * @param rcvr - */ - public static void removeReceiver(SpanReceiver rcvr) { - Tracer.getInstance().removeReceiver(rcvr); - } - - /** - * Adds the given SpanReceiver to the current Tracer instance's list of - * SpanReceivers. - * - * @param rcvr - */ - public static void addReceiver(SpanReceiver rcvr) { - Tracer.getInstance().addReceiver(rcvr); - } - - /** - * Adds a data annotation to the current span if tracing is currently on. - */ - public static void addKVAnnotation(byte[] key, byte[] value) { - Span s = currentSpan(); - if (s != null) { - s.addKVAnnotation(key, value); - } - } - - /** - * Annotate the current span with the given message. - */ - public static void addTimelineAnnotation(String msg) { - Span s = currentSpan(); - if (s != null) { - s.addTimelineAnnotation(msg); - } - } - - /** - * Returns true if the current thread is a part of a trace, false otherwise. - * - * @return - */ - public static boolean isTracing() { - return Tracer.getInstance().isTracing(); - } - - /** - * If we are tracing, return the current span, else null - * - * @return Span representing the current trace, or null if not tracing. - */ - public static Span currentSpan() { - return Tracer.getInstance().currentSpan(); - } - - /** - * Wrap the callable in a TraceCallable, if tracing. - * - * @param callable - * @return The callable provided, wrapped if tracing, 'callable' if not. - */ - public static <V> Callable<V> wrap(Callable<V> callable) { - if (isTracing()) { - return new TraceCallable<V>(Trace.currentSpan(), callable); - } else { - return callable; - } - } - - /** - * Wrap the runnable in a TraceRunnable, if tracing - * - * @param runnable - * @return The runnable provided, wrapped if tracing, 'runnable' if not. - */ - public static Runnable wrap(Runnable runnable) { - if (isTracing()) { - return new TraceRunnable(Trace.currentSpan(), runnable); - } else { - return runnable; - } - } - - /** - * Wrap the runnable in a TraceRunnable, if tracing - * - * @param description name of the span to be created. - * @param runnable - * @return The runnable provided, wrapped if tracing, 'runnable' if not. - */ - public static Runnable wrap(String description, Runnable runnable) { - if (isTracing()) { - return new TraceRunnable(Trace.currentSpan(), runnable, description); - } else { - return runnable; - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/TraceInfo.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/TraceInfo.java b/htrace-core/src/main/java/org/htrace/TraceInfo.java deleted file mode 100644 index 438289d..0000000 --- a/htrace-core/src/main/java/org/htrace/TraceInfo.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace; - - -public class TraceInfo { - public final long traceId; - public final long spanId; - - public TraceInfo(long traceId, long spanId) { - this.traceId = traceId; - this.spanId = spanId; - } - - @Override - public String toString() { - return "TraceInfo(traceId=" + traceId + ", spanId=" + spanId + ")"; - } - - public static TraceInfo fromSpan(Span s) { - if (s == null) return null; - return new TraceInfo(s.getTraceId(), s.getSpanId()); - } -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/TraceScope.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/TraceScope.java b/htrace-core/src/main/java/org/htrace/TraceScope.java deleted file mode 100644 index f001c52..0000000 --- a/htrace-core/src/main/java/org/htrace/TraceScope.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace; - -import java.io.Closeable; - -public class TraceScope implements Closeable { - - /** - * the span for this scope - */ - private final Span span; - - /** - * the span that was "current" before this scope was entered - */ - private final Span savedSpan; - - private boolean detached = false; - - TraceScope(Span span, Span saved) { - this.span = span; - this.savedSpan = saved; - } - - public Span getSpan() { - return span; - } - - /** - * Remove this span as the current thread, but don't stop it yet or - * send it for collection. This is useful if the span object is then - * passed to another thread for use with Trace.continueTrace(). - * - * @return the same Span object - */ - public Span detach() { - detached = true; - - Span cur = Tracer.getInstance().currentSpan(); - if (cur != span) { - Tracer.LOG.debug("Closing trace span " + span + " but " + - cur + " was top-of-stack"); - } else { - Tracer.getInstance().setCurrentSpan(savedSpan); - } - return span; - } - - /** - * Return true when {@link #detach()} has been called. Helpful when debugging - * multiple threads working on a single span. - */ - public boolean isDetached() { - return detached; - } - - @Override - public void close() { - if (span == null) return; - - if (!detached) { - // The span is done - span.stop(); - detach(); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/TraceTree.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/TraceTree.java b/htrace-core/src/main/java/org/htrace/TraceTree.java deleted file mode 100644 index fadcf2f..0000000 --- a/htrace-core/src/main/java/org/htrace/TraceTree.java +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace; - -import com.google.common.collect.HashMultimap; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Multimap; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -import java.util.Collection; - -/** - * Used to create the graph formed by spans. - */ -public class TraceTree { - public static final Log LOG = LogFactory.getLog(Tracer.class); - private Multimap<Long, Span> spansByParentID; - private Collection<Span> spans; - private Multimap<String, Span> spansByPid; - - /** - * Create a new TraceTree - * - * @param spans The collection of spans to use to create this TraceTree. Should - * have at least one root span (span with parentId = - * Span.ROOT_SPAN_ID - */ - public TraceTree(Collection<Span> spans) { - this.spans = ImmutableList.copyOf(spans); - this.spansByParentID = HashMultimap.<Long, Span>create(); - this.spansByPid = HashMultimap.<String, Span>create(); - - for (Span s : this.spans) { - if (s.getProcessId() != null) { - spansByPid.put(s.getProcessId(), s); - } else { - LOG.warn("Encountered span with null processId. This should not happen. Span: " - + s); - } - spansByParentID.put(s.getParentId(), s); - } - } - - /** - * @return The collection of spans given to this TraceTree at construction. - */ - public Collection<Span> getSpans() { - return spans; - } - - /** - * @return A copy of the MultiMap from parent span ID -> children of span with - * that ID. - */ - public Multimap<Long, Span> getSpansByParentIdMap() { - return HashMultimap.<Long, Span>create(spansByParentID); - } - - /** - * @return A collection of the root spans (spans with parent ID = - * Span.ROOT_SPAN_ID) in this tree. - */ - public Collection<Span> getRoots() { - Collection<Span> roots = spansByParentID.get(Span.ROOT_SPAN_ID); - if (roots != null) { - return roots; - } - throw new IllegalStateException( - "TraceTree is not correctly formed - there are no root spans in the collection provided at construction."); - } - - /** - * @return A copy of the Multimap from String process ID -> spans with that - * process ID. If process ID was not set in Trace.java, all spans will - * have empty string process IDs. - */ - public Multimap<String, Span> getSpansByPidMap() { - return HashMultimap.<String, Span>create(spansByPid); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/Tracer.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/Tracer.java b/htrace-core/src/main/java/org/htrace/Tracer.java deleted file mode 100644 index ac31165..0000000 --- a/htrace-core/src/main/java/org/htrace/Tracer.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.htrace.impl.MilliSpan; - -import java.security.SecureRandom; -import java.util.List; -import java.util.Random; -import java.util.concurrent.CopyOnWriteArrayList; - -/** - * A Tracer provides the implementation for collecting and distributing Spans - * within a process. - */ -public class Tracer { - public static final Log LOG = LogFactory.getLog(Tracer.class); - private final static Random random = new SecureRandom(); - private final List<SpanReceiver> receivers = new CopyOnWriteArrayList<SpanReceiver>(); - private static final ThreadLocal<Span> currentSpan = new ThreadLocal<Span>() { - @Override - protected Span initialValue() { - return null; - } - }; - public static final TraceInfo DONT_TRACE = new TraceInfo(-1, -1); - protected static String processId = null; - - /** - * Internal class for defered singleton idiom. - * <p/> - * https://en.wikipedia.org/wiki/Initialization_on_demand_holder_idiom - */ - private static class TracerHolder { - private static final Tracer INSTANCE = new Tracer(); - } - - public static Tracer getInstance() { - return TracerHolder.INSTANCE; - } - - protected Span createNew(String description) { - Span parent = currentSpan.get(); - if (parent == null) { - return new MilliSpan(description, - /* traceId = */ random.nextLong(), - /* parentSpanId = */ Span.ROOT_SPAN_ID, - /* spanId = */ random.nextLong(), - getProcessId()); - } else { - return parent.child(description); - } - } - - protected boolean isTracing() { - return currentSpan.get() != null; - } - - protected Span currentSpan() { - return currentSpan.get(); - } - - public void deliver(Span span) { - for (SpanReceiver receiver : receivers) { - receiver.receiveSpan(span); - } - } - - protected void addReceiver(SpanReceiver receiver) { - receivers.add(receiver); - } - - protected void removeReceiver(SpanReceiver receiver) { - receivers.remove(receiver); - } - - protected Span setCurrentSpan(Span span) { - if (LOG.isTraceEnabled()) { - LOG.trace("setting current span " + span); - } - currentSpan.set(span); - return span; - } - - - public TraceScope continueSpan(Span s) { - Span oldCurrent = currentSpan(); - setCurrentSpan(s); - return new TraceScope(s, oldCurrent); - } - - protected int numReceivers() { - return receivers.size(); - } - - static String getProcessId() { - if (processId == null) { - String cmdLine = System.getProperty("sun.java.command"); - if (cmdLine != null && !cmdLine.isEmpty()) { - String fullClassName = cmdLine.split("\\s+")[0]; - String[] classParts = fullClassName.split("\\."); - cmdLine = classParts[classParts.length - 1]; - } - - processId = (cmdLine == null || cmdLine.isEmpty()) ? "Unknown" : cmdLine; - } - return processId; - } -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/impl/AlwaysSampler.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/impl/AlwaysSampler.java b/htrace-core/src/main/java/org/htrace/impl/AlwaysSampler.java deleted file mode 100644 index c8d53e8..0000000 --- a/htrace-core/src/main/java/org/htrace/impl/AlwaysSampler.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace.impl; - -import org.htrace.Sampler; - -public final class AlwaysSampler implements Sampler<Object> { - - public static final AlwaysSampler INSTANCE = new AlwaysSampler(); - - private AlwaysSampler() { - } - - @Override - public boolean next(Object info) { - return true; - } -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/impl/CountSampler.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/impl/CountSampler.java b/htrace-core/src/main/java/org/htrace/impl/CountSampler.java deleted file mode 100644 index 8afcd8e..0000000 --- a/htrace-core/src/main/java/org/htrace/impl/CountSampler.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace.impl; - -import org.htrace.Sampler; - -import java.util.Random; - -/** - * Sampler that returns true every N calls. - */ -public class CountSampler implements Sampler<Object> { - - final static Random random = new Random(); - - final long frequency; - long count = random.nextLong(); - - public CountSampler(long frequency) { - this.frequency = frequency; - } - - @Override - public boolean next(Object info) { - return (count++ % frequency) == 0; - } -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/impl/LocalFileSpanReceiver.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/impl/LocalFileSpanReceiver.java b/htrace-core/src/main/java/org/htrace/impl/LocalFileSpanReceiver.java deleted file mode 100644 index b05b067..0000000 --- a/htrace-core/src/main/java/org/htrace/impl/LocalFileSpanReceiver.java +++ /dev/null @@ -1,137 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace.impl; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.htrace.HTraceConfiguration; -import org.htrace.Span; -import org.htrace.SpanReceiver; -import org.mortbay.util.ajax.JSON; - -import java.io.BufferedWriter; -import java.io.FileWriter; -import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; - -/** - * Writes the spans it receives to a local file. - * A production LocalFileSpanReceiver should use a real CSV format. - */ -public class LocalFileSpanReceiver implements SpanReceiver { - public static final Log LOG = LogFactory.getLog(LocalFileSpanReceiver.class); - public static final String PATH_KEY = "local-file-span-receiver.path"; - public static final String CAPACITY_KEY = "local-file-span-receiver.capacity"; - // default capacity for the executors blocking queue - public static final int CAPACITY_DEFAULT = 5000; - // default timeout duration when calling executor.awaitTermination() - public static final long EXECUTOR_TERMINATION_TIMEOUT_DURATION_DEFAULT = 60; - private String file; - private FileWriter fwriter; - private BufferedWriter bwriter; - private Map<String, Object> values; - private ExecutorService executor; - private long executorTerminationTimeoutDuration; - - public LocalFileSpanReceiver() { - } - - - @Override - public void configure(HTraceConfiguration conf) { - this.executorTerminationTimeoutDuration = EXECUTOR_TERMINATION_TIMEOUT_DURATION_DEFAULT; - int capacity = conf.getInt(CAPACITY_KEY, CAPACITY_DEFAULT); - this.file = conf.get(PATH_KEY); - if (file == null || file.isEmpty()) { - throw new IllegalArgumentException("must configure " + PATH_KEY); - } - this.executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.SECONDS, - new LinkedBlockingQueue<Runnable>(capacity)); - try { - this.fwriter = new FileWriter(this.file, true); - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } - this.bwriter = new BufferedWriter(fwriter); - this.values = new LinkedHashMap<String, Object>(); - } - - - private class WriteSpanRunnable implements Runnable { - public final Span span; - - public WriteSpanRunnable(Span span) { - this.span = span; - } - - @Override - public void run() { - try { - values.put("TraceID", span.getTraceId()); - values.put("SpanID", span.getSpanId()); - values.put("ParentID", span.getParentId()); - values.put("ProcessID", span.getProcessId()); - values.put("Start", span.getStartTimeMillis()); - values.put("Stop", span.getStopTimeMillis()); - values.put("Description", span.getDescription()); - values.put("KVAnnotations", span.getKVAnnotations()); - values.put("TLAnnotations", span.getTimelineAnnotations()); - bwriter.write(JSON.toString(values)); - bwriter.newLine(); - bwriter.flush(); - values.clear(); - } catch (IOException e) { - LOG.error("Error when writing to file: " + file, e); - } - } - } - - @Override - public void receiveSpan(Span span) { - executor.submit(new WriteSpanRunnable(span)); - } - - @Override - public void close() throws IOException { - executor.shutdown(); - try { - if (!executor.awaitTermination(this.executorTerminationTimeoutDuration, - TimeUnit.SECONDS)) { - LOG.warn("Was not able to process all remaining spans to write upon closing in: " - + this.executorTerminationTimeoutDuration + "s"); - } - } catch (InterruptedException e1) { - LOG.warn("Thread interrupted when terminating executor.", e1); - } - - try { - fwriter.close(); - } catch (IOException e) { - LOG.error("Error closing filewriter for file: " + file, e); - } - try { - bwriter.close(); - } catch (IOException e) { - LOG.error("Error closing bufferedwriter for file: " + file, e); - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/impl/MilliSpan.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/impl/MilliSpan.java b/htrace-core/src/main/java/org/htrace/impl/MilliSpan.java deleted file mode 100644 index 39c9671..0000000 --- a/htrace-core/src/main/java/org/htrace/impl/MilliSpan.java +++ /dev/null @@ -1,161 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace.impl; - -import org.htrace.Span; -import org.htrace.TimelineAnnotation; -import org.htrace.Tracer; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Random; - -/** - * A Span implementation that stores its information in milliseconds since the - * epoch. - */ -public class MilliSpan implements Span { - - private static Random rand = new Random(); - - private long start; - private long stop; - private final String description; - private final long traceId; - private final long parentSpanId; - private final long spanId; - private Map<byte[], byte[]> traceInfo = null; - private final String processId; - private List<TimelineAnnotation> timeline = null; - - @Override - public Span child(String description) { - return new MilliSpan(description, traceId, spanId, rand.nextLong(), processId); - } - - public MilliSpan(String description, long traceId, long parentSpanId, long spanId, String processId) { - this.description = description; - this.traceId = traceId; - this.parentSpanId = parentSpanId; - this.spanId = spanId; - this.start = System.currentTimeMillis(); - this.stop = 0; - this.processId = processId; - } - - @Override - public synchronized void stop() { - if (stop == 0) { - if (start == 0) - throw new IllegalStateException("Span for " + description - + " has not been started"); - stop = System.currentTimeMillis(); - Tracer.getInstance().deliver(this); - } - } - - protected long currentTimeMillis() { - return System.currentTimeMillis(); - } - - @Override - public synchronized boolean isRunning() { - return start != 0 && stop == 0; - } - - @Override - public synchronized long getAccumulatedMillis() { - if (start == 0) - return 0; - if (stop > 0) - return stop - start; - return currentTimeMillis() - start; - } - - @Override - public String toString() { - return String.format("Span{Id:0x%16x,parentId:0x%16x,desc:%s}", spanId, parentSpanId, description); - } - - @Override - public String getDescription() { - return description; - } - - @Override - public long getSpanId() { - return spanId; - } - - @Override - public long getParentId() { - return parentSpanId; - } - - @Override - public long getTraceId() { - return traceId; - } - - @Override - public long getStartTimeMillis() { - return start; - } - - @Override - public long getStopTimeMillis() { - return stop; - } - - @Override - public void addKVAnnotation(byte[] key, byte[] value) { - if (traceInfo == null) - traceInfo = new HashMap<byte[], byte[]>(); - traceInfo.put(key, value); - } - - @Override - public void addTimelineAnnotation(String msg) { - if (timeline == null) { - timeline = new ArrayList<TimelineAnnotation>(); - } - timeline.add(new TimelineAnnotation(System.currentTimeMillis(), msg)); - } - - @Override - public Map<byte[], byte[]> getKVAnnotations() { - if (traceInfo == null) - return Collections.emptyMap(); - return Collections.unmodifiableMap(traceInfo); - } - - @Override - public List<TimelineAnnotation> getTimelineAnnotations() { - if (timeline == null) { - return Collections.emptyList(); - } - return Collections.unmodifiableList(timeline); - } - - @Override - public String getProcessId() { - return processId; - } -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/impl/NeverSampler.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/impl/NeverSampler.java b/htrace-core/src/main/java/org/htrace/impl/NeverSampler.java deleted file mode 100644 index 38b193a..0000000 --- a/htrace-core/src/main/java/org/htrace/impl/NeverSampler.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace.impl; - -import org.htrace.Sampler; - -public final class NeverSampler implements Sampler<Object> { - - public static final NeverSampler INSTANCE = new NeverSampler(); - - private NeverSampler() { - } - - @Override - public boolean next(Object info) { - return false; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/impl/POJOSpanReceiver.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/impl/POJOSpanReceiver.java b/htrace-core/src/main/java/org/htrace/impl/POJOSpanReceiver.java deleted file mode 100644 index 54874e6..0000000 --- a/htrace-core/src/main/java/org/htrace/impl/POJOSpanReceiver.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace.impl; - -import org.htrace.HTraceConfiguration; -import org.htrace.Span; -import org.htrace.SpanReceiver; - -import java.io.IOException; -import java.util.Collection; -import java.util.HashSet; - -/** - * SpanReceiver for testing only that just collects the Span objects it - * receives. The spans it receives can be accessed with getSpans(); - */ -public class POJOSpanReceiver implements SpanReceiver { - private final Collection<Span> spans; - - @Override - public void configure(HTraceConfiguration conf) { - } - - /** - * @return The spans this POJOSpanReceiver has received. - */ - public Collection<Span> getSpans() { - return spans; - } - - public POJOSpanReceiver() { - this.spans = new HashSet<Span>(); - } - - @Override - public void close() throws IOException { - } - - @Override - public void receiveSpan(Span span) { - spans.add(span); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/impl/ProbabilitySampler.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/impl/ProbabilitySampler.java b/htrace-core/src/main/java/org/htrace/impl/ProbabilitySampler.java deleted file mode 100644 index 1ac1f73..0000000 --- a/htrace-core/src/main/java/org/htrace/impl/ProbabilitySampler.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace.impl; - -import org.htrace.Sampler; - -import java.util.Random; - -public class ProbabilitySampler implements Sampler<Object> { - public final double threshold; - private Random random; - - public ProbabilitySampler(double threshold) { - this.threshold = threshold; - random = new Random(); - } - - @Override - public boolean next(Object info) { - return random.nextDouble() < threshold; - } -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/impl/StandardOutSpanReceiver.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/impl/StandardOutSpanReceiver.java b/htrace-core/src/main/java/org/htrace/impl/StandardOutSpanReceiver.java deleted file mode 100644 index f1ebada..0000000 --- a/htrace-core/src/main/java/org/htrace/impl/StandardOutSpanReceiver.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace.impl; - -import org.htrace.HTraceConfiguration; -import org.htrace.Span; -import org.htrace.SpanReceiver; - -import java.io.IOException; - -/** - * Used for testing. Simply prints to standard out any spans it receives. - */ -public class StandardOutSpanReceiver implements SpanReceiver { - - @Override - public void configure(HTraceConfiguration conf) { - } - - @Override - public void receiveSpan(Span span) { - System.out.println(span); - } - - @Override - public void close() throws IOException { - } -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/impl/TrueIfTracingSampler.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/impl/TrueIfTracingSampler.java b/htrace-core/src/main/java/org/htrace/impl/TrueIfTracingSampler.java deleted file mode 100644 index 100650e..0000000 --- a/htrace-core/src/main/java/org/htrace/impl/TrueIfTracingSampler.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace.impl; - -import org.htrace.Sampler; -import org.htrace.Trace; - -/** - * A Sampler that returns true if and only if tracing is on the current thread. - */ -public class TrueIfTracingSampler implements Sampler<Object> { - - public static final TrueIfTracingSampler INSTANCE = new TrueIfTracingSampler(); - - private TrueIfTracingSampler() { - } - - @Override - public boolean next(Object info) { - return Trace.isTracing(); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/wrappers/TraceCallable.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/wrappers/TraceCallable.java b/htrace-core/src/main/java/org/htrace/wrappers/TraceCallable.java deleted file mode 100644 index 5b25893..0000000 --- a/htrace-core/src/main/java/org/htrace/wrappers/TraceCallable.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace.wrappers; - -import org.htrace.Span; -import org.htrace.Trace; -import org.htrace.TraceScope; - -import java.util.concurrent.Callable; - -/** - * Wrap a Callable with a Span that survives a change in threads. - */ -public class TraceCallable<V> implements Callable<V> { - private final Callable<V> impl; - private final Span parent; - private final String description; - - public TraceCallable(Callable<V> impl) { - this(Trace.currentSpan(), impl); - } - - public TraceCallable(Span parent, Callable<V> impl) { - this(parent, impl, null); - } - - public TraceCallable(Span parent, Callable<V> impl, String description) { - this.impl = impl; - this.parent = parent; - this.description = description; - } - - @Override - public V call() throws Exception { - if (parent != null) { - TraceScope chunk = Trace.startSpan(getDescription(), parent); - - try { - return impl.call(); - } finally { - chunk.close(); - } - } else { - return impl.call(); - } - } - - public Callable<V> getImpl() { - return impl; - } - - private String getDescription() { - return this.description == null ? Thread.currentThread().getName() : description; - } -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/wrappers/TraceExecutorService.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/wrappers/TraceExecutorService.java b/htrace-core/src/main/java/org/htrace/wrappers/TraceExecutorService.java deleted file mode 100644 index 95ae88a..0000000 --- a/htrace-core/src/main/java/org/htrace/wrappers/TraceExecutorService.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace.wrappers; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.List; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - - -public class TraceExecutorService implements ExecutorService { - - private final ExecutorService impl; - - public TraceExecutorService(ExecutorService impl) { - this.impl = impl; - } - - @Override - public void execute(Runnable command) { - impl.execute(new TraceRunnable(command)); - } - - @Override - public void shutdown() { - impl.shutdown(); - } - - @Override - public List<Runnable> shutdownNow() { - return impl.shutdownNow(); - } - - @Override - public boolean isShutdown() { - return impl.isShutdown(); - } - - @Override - public boolean isTerminated() { - return impl.isTerminated(); - } - - @Override - public boolean awaitTermination(long timeout, TimeUnit unit) - throws InterruptedException { - return impl.awaitTermination(timeout, unit); - } - - @Override - public <T> Future<T> submit(Callable<T> task) { - return impl.submit(new TraceCallable<T>(task)); - } - - @Override - public <T> Future<T> submit(Runnable task, T result) { - return impl.submit(new TraceRunnable(task), result); - } - - @Override - public Future<?> submit(Runnable task) { - return impl.submit(new TraceRunnable(task)); - } - - private <T> Collection<? extends Callable<T>> wrapCollection( - Collection<? extends Callable<T>> tasks) { - List<Callable<T>> result = new ArrayList<Callable<T>>(); - for (Callable<T> task : tasks) { - result.add(new TraceCallable<T>(task)); - } - return result; - } - - @Override - public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) - throws InterruptedException { - return impl.invokeAll(wrapCollection(tasks)); - } - - @Override - public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, - long timeout, TimeUnit unit) throws InterruptedException { - return impl.invokeAll(wrapCollection(tasks), timeout, unit); - } - - @Override - public <T> T invokeAny(Collection<? extends Callable<T>> tasks) - throws InterruptedException, ExecutionException { - return impl.invokeAny(wrapCollection(tasks)); - } - - @Override - public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, - TimeUnit unit) throws InterruptedException, ExecutionException, - TimeoutException { - return impl.invokeAny(wrapCollection(tasks), timeout, unit); - } - -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/wrappers/TraceProxy.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/wrappers/TraceProxy.java b/htrace-core/src/main/java/org/htrace/wrappers/TraceProxy.java deleted file mode 100644 index 42cdd5a..0000000 --- a/htrace-core/src/main/java/org/htrace/wrappers/TraceProxy.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace.wrappers; - -import org.htrace.Sampler; -import org.htrace.Trace; -import org.htrace.TraceScope; - -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; - -public class TraceProxy { - /** - * Returns an object that will trace all calls to itself. - * - * @param instance - * @return - */ - public static <T> T trace(T instance) { - return trace(instance, Sampler.ALWAYS); - } - - /** - * Returns an object that will trace all calls to itself. - * - * @param <V> - * @param instance - * @param sampler - * @return - */ - @SuppressWarnings("unchecked") - public static <T, V> T trace(final T instance, final Sampler<V> sampler) { - InvocationHandler handler = new InvocationHandler() { - @Override - public Object invoke(Object obj, Method method, Object[] args) - throws Throwable { - if (!sampler.next(null)) { - return method.invoke(instance, args); - } - - TraceScope scope = Trace.startSpan(method.getName(), Sampler.ALWAYS); - try { - return method.invoke(instance, args); - } catch (Throwable ex) { - ex.printStackTrace(); - throw ex; - } finally { - scope.close(); - } - } - }; - return (T) Proxy.newProxyInstance(instance.getClass().getClassLoader(), - instance.getClass().getInterfaces(), handler); - } -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/main/java/org/htrace/wrappers/TraceRunnable.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/main/java/org/htrace/wrappers/TraceRunnable.java b/htrace-core/src/main/java/org/htrace/wrappers/TraceRunnable.java deleted file mode 100644 index 9f7ea1e..0000000 --- a/htrace-core/src/main/java/org/htrace/wrappers/TraceRunnable.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace.wrappers; - -import org.htrace.Span; -import org.htrace.Trace; -import org.htrace.TraceScope; - -/** - * Wrap a Runnable with a Span that survives a change in threads. - */ -public class TraceRunnable implements Runnable { - - private final Span parent; - private final Runnable runnable; - private final String description; - - public TraceRunnable(Runnable runnable) { - this(Trace.currentSpan(), runnable); - } - - public TraceRunnable(Span parent, Runnable runnable) { - this(parent, runnable, null); - } - - public TraceRunnable(Span parent, Runnable runnable, String description) { - this.parent = parent; - this.runnable = runnable; - this.description = description; - } - - @Override - public void run() { - if (parent != null) { - TraceScope chunk = Trace.startSpan(getDescription(), parent); - - try { - runnable.run(); - } finally { - chunk.close(); - } - } else { - runnable.run(); - } - } - - private String getDescription() { - return this.description == null ? Thread.currentThread().getName() : description; - } -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/test/java/org/apache/htrace/TestCountSampler.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/test/java/org/apache/htrace/TestCountSampler.java b/htrace-core/src/test/java/org/apache/htrace/TestCountSampler.java new file mode 100644 index 0000000..41c06a3 --- /dev/null +++ b/htrace-core/src/test/java/org/apache/htrace/TestCountSampler.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.htrace; + +import org.apache.htrace.impl.CountSampler; +import org.junit.Assert; +import org.junit.Test; + +public class TestCountSampler { + + @Test + public void testNext() { + CountSampler half = new CountSampler(2); + CountSampler hundred = new CountSampler(100); + int halfCount = 0; + int hundredCount = 0; + for (int i = 0; i < 200; i++) { + if (half.next(null)) + halfCount++; + if (hundred.next(null)) + hundredCount++; + } + Assert.assertEquals(2, hundredCount); + Assert.assertEquals(100, halfCount); + } +} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/test/java/org/apache/htrace/TestHTrace.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/test/java/org/apache/htrace/TestHTrace.java b/htrace-core/src/test/java/org/apache/htrace/TestHTrace.java new file mode 100644 index 0000000..ebca7fb --- /dev/null +++ b/htrace-core/src/test/java/org/apache/htrace/TestHTrace.java @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.htrace; + +import com.google.common.collect.Multimap; + +import org.apache.htrace.HTraceConfiguration; +import org.apache.htrace.Span; +import org.apache.htrace.SpanReceiver; +import org.apache.htrace.TraceTree; +import org.apache.htrace.impl.LocalFileSpanReceiver; +import org.apache.htrace.impl.POJOSpanReceiver; +import org.apache.htrace.impl.StandardOutSpanReceiver; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; + +public class TestHTrace { + + public static final String SPAN_FILE_FLAG = "spanFile"; + + /** + * Basic system test of HTrace. + * + * @throws Exception + */ + @Test + public void testHtrace() throws Exception { + final int numTraces = 3; + String fileName = System.getProperty(SPAN_FILE_FLAG); + + Collection<SpanReceiver> rcvrs = new HashSet<SpanReceiver>(); + + // writes spans to a file if one is provided to maven with + // -DspanFile="FILENAME", otherwise writes to standard out. + if (fileName != null) { + File f = new File(fileName); + File parent = f.getParentFile(); + if (parent != null && !parent.exists() && !parent.mkdirs()) { + throw new IllegalArgumentException("Couldn't create file: " + + fileName); + } + HashMap<String, String> conf = new HashMap<String, String>(); + conf.put("local-file-span-receiver.path", fileName); + LocalFileSpanReceiver receiver = new LocalFileSpanReceiver(); + receiver.configure(HTraceConfiguration.fromMap(conf)); + rcvrs.add(receiver); + } else { + rcvrs.add(new StandardOutSpanReceiver()); + } + + POJOSpanReceiver psr = new POJOSpanReceiver(); + rcvrs.add(psr); + runTraceCreatorTraces(new TraceCreator(rcvrs)); + + for (SpanReceiver receiver : rcvrs) { + receiver.close(); + } + + Collection<Span> spans = psr.getSpans(); + TraceTree traceTree = new TraceTree(spans); + Collection<Span> roots = traceTree.getRoots(); + Assert.assertEquals(numTraces, roots.size()); + + Map<String, Span> descriptionToRootSpan = new HashMap<String, Span>(); + for (Span root : roots) { + descriptionToRootSpan.put(root.getDescription(), root); + } + + Assert.assertTrue(descriptionToRootSpan.keySet().contains( + TraceCreator.RPC_TRACE_ROOT)); + Assert.assertTrue(descriptionToRootSpan.keySet().contains( + TraceCreator.SIMPLE_TRACE_ROOT)); + Assert.assertTrue(descriptionToRootSpan.keySet().contains( + TraceCreator.THREADED_TRACE_ROOT)); + + Multimap<Long, Span> spansByParentId = traceTree.getSpansByParentIdMap(); + Span rpcTraceRoot = descriptionToRootSpan.get(TraceCreator.RPC_TRACE_ROOT); + Assert.assertEquals(1, spansByParentId.get(rpcTraceRoot.getSpanId()).size()); + + Span rpcTraceChild1 = spansByParentId.get(rpcTraceRoot.getSpanId()) + .iterator().next(); + Assert.assertEquals(1, spansByParentId.get(rpcTraceChild1.getSpanId()).size()); + + Span rpcTraceChild2 = spansByParentId.get(rpcTraceChild1.getSpanId()) + .iterator().next(); + Assert.assertEquals(1, spansByParentId.get(rpcTraceChild2.getSpanId()).size()); + + Span rpcTraceChild3 = spansByParentId.get(rpcTraceChild2.getSpanId()) + .iterator().next(); + Assert.assertEquals(0, spansByParentId.get(rpcTraceChild3.getSpanId()).size()); + } + + private void runTraceCreatorTraces(TraceCreator tc) { + tc.createThreadedTrace(); + tc.createSimpleTrace(); + tc.createSampleRpcTrace(); + } +} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/test/java/org/apache/htrace/TestHTraceConfiguration.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/test/java/org/apache/htrace/TestHTraceConfiguration.java b/htrace-core/src/test/java/org/apache/htrace/TestHTraceConfiguration.java new file mode 100644 index 0000000..440a826 --- /dev/null +++ b/htrace-core/src/test/java/org/apache/htrace/TestHTraceConfiguration.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.htrace; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.htrace.HTraceConfiguration; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class TestHTraceConfiguration { + @Test + public void testGetBoolean() throws Exception { + + Map<String, String> m = new HashMap<String, String>(); + m.put("testTrue", " True"); + m.put("testFalse", "falsE "); + HTraceConfiguration configuration = HTraceConfiguration.fromMap(m); + + // Tests for value being there + assertTrue(configuration.getBoolean("testTrue", false)); + assertFalse(configuration.getBoolean("testFalse", true)); + + // Test for absent + assertTrue(configuration.getBoolean("absent", true)); + assertFalse(configuration.getBoolean("absent", false)); + } + + @Test + public void testGetInt() throws Exception { + Map<String, String> m = new HashMap<String, String>(); + m.put("a", "100"); + m.put("b", "0"); + m.put("c", "-100"); + m.put("d", "5"); + + HTraceConfiguration configuration = HTraceConfiguration.fromMap(m); + assertEquals(100, configuration.getInt("a", -999)); + assertEquals(0, configuration.getInt("b", -999)); + assertEquals(-100, configuration.getInt("c", -999)); + assertEquals(5, configuration.getInt("d", -999)); + assertEquals(-999, configuration.getInt("absent", -999)); + } +} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/test/java/org/apache/htrace/TestSampler.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/test/java/org/apache/htrace/TestSampler.java b/htrace-core/src/test/java/org/apache/htrace/TestSampler.java new file mode 100644 index 0000000..81b965e --- /dev/null +++ b/htrace-core/src/test/java/org/apache/htrace/TestSampler.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.htrace; + +import org.apache.htrace.Sampler; +import org.apache.htrace.Trace; +import org.apache.htrace.TraceInfo; +import org.apache.htrace.TraceScope; +import org.junit.Assert; +import org.junit.Test; + +public class TestSampler { + @Test + public void testParamterizedSampler() { + TestParamSampler sampler = new TestParamSampler(); + TraceScope s = Trace.startSpan("test", sampler, 1); + Assert.assertNotNull(s.getSpan()); + s.close(); + s = Trace.startSpan("test", sampler, -1); + Assert.assertNull(s.getSpan()); + s.close(); + } + + @Test + public void testAlwaysSampler() { + TraceScope cur = Trace.startSpan("test", new TraceInfo(0, 0)); + Assert.assertNotNull(cur); + cur.close(); + } + + private class TestParamSampler implements Sampler<Integer> { + + @Override + public boolean next(Integer info) { + return info > 0; + } + + } +} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/test/java/org/apache/htrace/TraceCreator.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/test/java/org/apache/htrace/TraceCreator.java b/htrace-core/src/test/java/org/apache/htrace/TraceCreator.java new file mode 100644 index 0000000..f577656 --- /dev/null +++ b/htrace-core/src/test/java/org/apache/htrace/TraceCreator.java @@ -0,0 +1,163 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.htrace; + +import java.util.Collection; +import java.util.Random; + +import org.apache.htrace.Sampler; +import org.apache.htrace.SpanReceiver; +import org.apache.htrace.Trace; +import org.apache.htrace.TraceInfo; +import org.apache.htrace.TraceScope; + +/** + * Does some stuff and traces it. + */ +public class TraceCreator { + + public static final String RPC_TRACE_ROOT = "createSampleRpcTrace"; + public static final String THREADED_TRACE_ROOT = "createThreadedTrace"; + public static final String SIMPLE_TRACE_ROOT = "createSimpleTrace"; + + /** + * Takes as input the SpanReceiver that should used as the sink for Spans when + * createDemoTrace() is called. + * + * @param receiver + */ + public TraceCreator(SpanReceiver receiver) { + Trace.addReceiver(receiver); + } + + /** + * Takes as input the SpanReceivers that should used as the sink for Spans + * when createDemoTrace() is called. + * + * @param receivers + */ + public TraceCreator(Collection<SpanReceiver> receivers) { + for (SpanReceiver receiver : receivers) { + Trace.addReceiver(receiver); + } + } + + public void createSampleRpcTrace() { + TraceScope s = Trace.startSpan(RPC_TRACE_ROOT, Sampler.ALWAYS); + try { + pretendRpcSend(); + } finally { + s.close(); + } + } + + public void createSimpleTrace() { + TraceScope s = Trace.startSpan(SIMPLE_TRACE_ROOT, Sampler.ALWAYS); + try { + importantWork1(); + } finally { + s.close(); + } + } + + /** + * Creates the demo trace (will create different traces from call to call). + */ + public void createThreadedTrace() { + TraceScope s = Trace.startSpan(THREADED_TRACE_ROOT, Sampler.ALWAYS); + try { + Random r = new Random(); + int numThreads = r.nextInt(4) + 1; + Thread[] threads = new Thread[numThreads]; + + for (int i = 0; i < numThreads; i++) { + threads[i] = new Thread(Trace.wrap(new MyRunnable())); + } + for (int i = 0; i < numThreads; i++) { + threads[i].start(); + } + for (int i = 0; i < numThreads; i++) { + try { + threads[i].join(); + } catch (InterruptedException e) { + } + } + importantWork1(); + } finally { + s.close(); + } + } + + private void importantWork1() { + TraceScope cur = Trace.startSpan("important work 1"); + try { + Thread.sleep((long) (2000 * Math.random())); + importantWork2(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } finally { + cur.close(); + } + } + + private void importantWork2() { + TraceScope cur = Trace.startSpan("important work 2"); + try { + Thread.sleep((long) (2000 * Math.random())); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } finally { + cur.close(); + } + } + + private class MyRunnable implements Runnable { + @Override + public void run() { + try { + Thread.sleep(750); + Random r = new Random(); + int importantNumber = 100 / r.nextInt(3); + System.out.println("Important number: " + importantNumber); + } catch (InterruptedException ie) { + Thread.currentThread().interrupt(); + } catch (ArithmeticException ae) { + TraceScope c = Trace.startSpan("dealing with arithmetic exception."); + try { + Thread.sleep((long) (3000 * Math.random())); + } catch (InterruptedException ie1) { + Thread.currentThread().interrupt(); + } finally { + c.close(); + } + } + } + } + + public void pretendRpcSend() { + pretendRpcReceiveWithTraceInfo(TraceInfo.fromSpan(Trace.currentSpan())); + } + + public void pretendRpcReceiveWithTraceInfo(TraceInfo traceInfo) { + TraceScope s = Trace.startSpan("received RPC", traceInfo); + try { + importantWork1(); + } finally { + s.close(); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/test/java/org/htrace/TestCountSampler.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/test/java/org/htrace/TestCountSampler.java b/htrace-core/src/test/java/org/htrace/TestCountSampler.java deleted file mode 100644 index 25a0d9e..0000000 --- a/htrace-core/src/test/java/org/htrace/TestCountSampler.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace; - -import org.htrace.impl.CountSampler; -import org.junit.Assert; -import org.junit.Test; - -public class TestCountSampler { - - @Test - public void testNext() { - CountSampler half = new CountSampler(2); - CountSampler hundred = new CountSampler(100); - int halfCount = 0; - int hundredCount = 0; - for (int i = 0; i < 200; i++) { - if (half.next(null)) - halfCount++; - if (hundred.next(null)) - hundredCount++; - } - Assert.assertEquals(2, hundredCount); - Assert.assertEquals(100, halfCount); - } -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/test/java/org/htrace/TestHTrace.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/test/java/org/htrace/TestHTrace.java b/htrace-core/src/test/java/org/htrace/TestHTrace.java deleted file mode 100644 index f613755..0000000 --- a/htrace-core/src/test/java/org/htrace/TestHTrace.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace; - -import com.google.common.collect.Multimap; -import org.htrace.impl.LocalFileSpanReceiver; -import org.htrace.impl.POJOSpanReceiver; -import org.htrace.impl.StandardOutSpanReceiver; -import org.junit.Assert; -import org.junit.Test; - -import java.io.File; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; - -public class TestHTrace { - - public static final String SPAN_FILE_FLAG = "spanFile"; - - /** - * Basic system test of HTrace. - * - * @throws Exception - */ - @Test - public void testHtrace() throws Exception { - final int numTraces = 3; - String fileName = System.getProperty(SPAN_FILE_FLAG); - - Collection<SpanReceiver> rcvrs = new HashSet<SpanReceiver>(); - - // writes spans to a file if one is provided to maven with - // -DspanFile="FILENAME", otherwise writes to standard out. - if (fileName != null) { - File f = new File(fileName); - File parent = f.getParentFile(); - if (parent != null && !parent.exists() && !parent.mkdirs()) { - throw new IllegalArgumentException("Couldn't create file: " - + fileName); - } - HashMap<String, String> conf = new HashMap<String, String>(); - conf.put("local-file-span-receiver.path", fileName); - LocalFileSpanReceiver receiver = new LocalFileSpanReceiver(); - receiver.configure(HTraceConfiguration.fromMap(conf)); - rcvrs.add(receiver); - } else { - rcvrs.add(new StandardOutSpanReceiver()); - } - - POJOSpanReceiver psr = new POJOSpanReceiver(); - rcvrs.add(psr); - runTraceCreatorTraces(new TraceCreator(rcvrs)); - - for (SpanReceiver receiver : rcvrs) { - receiver.close(); - } - - Collection<Span> spans = psr.getSpans(); - TraceTree traceTree = new TraceTree(spans); - Collection<Span> roots = traceTree.getRoots(); - Assert.assertEquals(numTraces, roots.size()); - - Map<String, Span> descriptionToRootSpan = new HashMap<String, Span>(); - for (Span root : roots) { - descriptionToRootSpan.put(root.getDescription(), root); - } - - Assert.assertTrue(descriptionToRootSpan.keySet().contains( - TraceCreator.RPC_TRACE_ROOT)); - Assert.assertTrue(descriptionToRootSpan.keySet().contains( - TraceCreator.SIMPLE_TRACE_ROOT)); - Assert.assertTrue(descriptionToRootSpan.keySet().contains( - TraceCreator.THREADED_TRACE_ROOT)); - - Multimap<Long, Span> spansByParentId = traceTree.getSpansByParentIdMap(); - Span rpcTraceRoot = descriptionToRootSpan.get(TraceCreator.RPC_TRACE_ROOT); - Assert.assertEquals(1, spansByParentId.get(rpcTraceRoot.getSpanId()).size()); - - Span rpcTraceChild1 = spansByParentId.get(rpcTraceRoot.getSpanId()) - .iterator().next(); - Assert.assertEquals(1, spansByParentId.get(rpcTraceChild1.getSpanId()).size()); - - Span rpcTraceChild2 = spansByParentId.get(rpcTraceChild1.getSpanId()) - .iterator().next(); - Assert.assertEquals(1, spansByParentId.get(rpcTraceChild2.getSpanId()).size()); - - Span rpcTraceChild3 = spansByParentId.get(rpcTraceChild2.getSpanId()) - .iterator().next(); - Assert.assertEquals(0, spansByParentId.get(rpcTraceChild3.getSpanId()).size()); - } - - private void runTraceCreatorTraces(TraceCreator tc) { - tc.createThreadedTrace(); - tc.createSimpleTrace(); - tc.createSampleRpcTrace(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/test/java/org/htrace/TestHTraceConfiguration.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/test/java/org/htrace/TestHTraceConfiguration.java b/htrace-core/src/test/java/org/htrace/TestHTraceConfiguration.java deleted file mode 100644 index 5506ddc..0000000 --- a/htrace-core/src/test/java/org/htrace/TestHTraceConfiguration.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.htrace; - -import java.util.HashMap; -import java.util.Map; - -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -public class TestHTraceConfiguration { - @Test - public void testGetBoolean() throws Exception { - - Map<String, String> m = new HashMap<String, String>(); - m.put("testTrue", " True"); - m.put("testFalse", "falsE "); - HTraceConfiguration configuration = HTraceConfiguration.fromMap(m); - - // Tests for value being there - assertTrue(configuration.getBoolean("testTrue", false)); - assertFalse(configuration.getBoolean("testFalse", true)); - - // Test for absent - assertTrue(configuration.getBoolean("absent", true)); - assertFalse(configuration.getBoolean("absent", false)); - } - - @Test - public void testGetInt() throws Exception { - Map<String, String> m = new HashMap<String, String>(); - m.put("a", "100"); - m.put("b", "0"); - m.put("c", "-100"); - m.put("d", "5"); - - HTraceConfiguration configuration = HTraceConfiguration.fromMap(m); - assertEquals(100, configuration.getInt("a", -999)); - assertEquals(0, configuration.getInt("b", -999)); - assertEquals(-100, configuration.getInt("c", -999)); - assertEquals(5, configuration.getInt("d", -999)); - assertEquals(-999, configuration.getInt("absent", -999)); - } -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/test/java/org/htrace/TestSampler.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/test/java/org/htrace/TestSampler.java b/htrace-core/src/test/java/org/htrace/TestSampler.java deleted file mode 100644 index 9bb2e8b..0000000 --- a/htrace-core/src/test/java/org/htrace/TestSampler.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace; - -import org.junit.Assert; -import org.junit.Test; - -public class TestSampler { - @Test - public void testParamterizedSampler() { - TestParamSampler sampler = new TestParamSampler(); - TraceScope s = Trace.startSpan("test", sampler, 1); - Assert.assertNotNull(s.getSpan()); - s.close(); - s = Trace.startSpan("test", sampler, -1); - Assert.assertNull(s.getSpan()); - s.close(); - } - - @Test - public void testAlwaysSampler() { - TraceScope cur = Trace.startSpan("test", new TraceInfo(0, 0)); - Assert.assertNotNull(cur); - cur.close(); - } - - private class TestParamSampler implements Sampler<Integer> { - - @Override - public boolean next(Integer info) { - return info > 0; - } - - } -} http://git-wip-us.apache.org/repos/asf/incubator-htrace/blob/a27cd4da/htrace-core/src/test/java/org/htrace/TraceCreator.java ---------------------------------------------------------------------- diff --git a/htrace-core/src/test/java/org/htrace/TraceCreator.java b/htrace-core/src/test/java/org/htrace/TraceCreator.java deleted file mode 100644 index 7a034b8..0000000 --- a/htrace-core/src/test/java/org/htrace/TraceCreator.java +++ /dev/null @@ -1,157 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.htrace; - -import java.util.Collection; -import java.util.Random; - -/** - * Does some stuff and traces it. - */ -public class TraceCreator { - - public static final String RPC_TRACE_ROOT = "createSampleRpcTrace"; - public static final String THREADED_TRACE_ROOT = "createThreadedTrace"; - public static final String SIMPLE_TRACE_ROOT = "createSimpleTrace"; - - /** - * Takes as input the SpanReceiver that should used as the sink for Spans when - * createDemoTrace() is called. - * - * @param receiver - */ - public TraceCreator(SpanReceiver receiver) { - Trace.addReceiver(receiver); - } - - /** - * Takes as input the SpanReceivers that should used as the sink for Spans - * when createDemoTrace() is called. - * - * @param receivers - */ - public TraceCreator(Collection<SpanReceiver> receivers) { - for (SpanReceiver receiver : receivers) { - Trace.addReceiver(receiver); - } - } - - public void createSampleRpcTrace() { - TraceScope s = Trace.startSpan(RPC_TRACE_ROOT, Sampler.ALWAYS); - try { - pretendRpcSend(); - } finally { - s.close(); - } - } - - public void createSimpleTrace() { - TraceScope s = Trace.startSpan(SIMPLE_TRACE_ROOT, Sampler.ALWAYS); - try { - importantWork1(); - } finally { - s.close(); - } - } - - /** - * Creates the demo trace (will create different traces from call to call). - */ - public void createThreadedTrace() { - TraceScope s = Trace.startSpan(THREADED_TRACE_ROOT, Sampler.ALWAYS); - try { - Random r = new Random(); - int numThreads = r.nextInt(4) + 1; - Thread[] threads = new Thread[numThreads]; - - for (int i = 0; i < numThreads; i++) { - threads[i] = new Thread(Trace.wrap(new MyRunnable())); - } - for (int i = 0; i < numThreads; i++) { - threads[i].start(); - } - for (int i = 0; i < numThreads; i++) { - try { - threads[i].join(); - } catch (InterruptedException e) { - } - } - importantWork1(); - } finally { - s.close(); - } - } - - private void importantWork1() { - TraceScope cur = Trace.startSpan("important work 1"); - try { - Thread.sleep((long) (2000 * Math.random())); - importantWork2(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } finally { - cur.close(); - } - } - - private void importantWork2() { - TraceScope cur = Trace.startSpan("important work 2"); - try { - Thread.sleep((long) (2000 * Math.random())); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } finally { - cur.close(); - } - } - - private class MyRunnable implements Runnable { - @Override - public void run() { - try { - Thread.sleep(750); - Random r = new Random(); - int importantNumber = 100 / r.nextInt(3); - System.out.println("Important number: " + importantNumber); - } catch (InterruptedException ie) { - Thread.currentThread().interrupt(); - } catch (ArithmeticException ae) { - TraceScope c = Trace.startSpan("dealing with arithmetic exception."); - try { - Thread.sleep((long) (3000 * Math.random())); - } catch (InterruptedException ie1) { - Thread.currentThread().interrupt(); - } finally { - c.close(); - } - } - } - } - - public void pretendRpcSend() { - pretendRpcReceiveWithTraceInfo(TraceInfo.fromSpan(Trace.currentSpan())); - } - - public void pretendRpcReceiveWithTraceInfo(TraceInfo traceInfo) { - TraceScope s = Trace.startSpan("received RPC", traceInfo); - try { - importantWork1(); - } finally { - s.close(); - } - } -} \ No newline at end of file
