From 213067a02ac328145f93054848545eddeaf725bd Mon Sep 17 00:00:00 2001 From: Mike Drob Date: Fri, 15 Apr 2016 11:52:58 -0500 Subject: [PATCH] HTRACE-355 Set wrapped thread span description from child thread name --- .../java/org/apache/htrace/core/TraceCallable.java | 10 +- .../java/org/apache/htrace/core/TraceRunnable.java | 10 +- .../org/apache/htrace/core/TestTraceExecutor.java | 107 +++++++++++++++++++++ 3 files changed, 117 insertions(+), 10 deletions(-) create mode 100644 htrace-core4/src/test/java/org/apache/htrace/core/TestTraceExecutor.java diff --git a/htrace-core4/src/main/java/org/apache/htrace/core/TraceCallable.java b/htrace-core4/src/main/java/org/apache/htrace/core/TraceCallable.java index f5434e1..9cf478d 100644 --- a/htrace-core4/src/main/java/org/apache/htrace/core/TraceCallable.java +++ b/htrace-core4/src/main/java/org/apache/htrace/core/TraceCallable.java @@ -32,15 +32,15 @@ public class TraceCallable implements Callable { this.tracer = tracer; this.impl = impl; this.parent = parent; - if (description == null) { - this.description = Thread.currentThread().getName(); - } else { - this.description = description; - } + this.description = description; } @Override public V call() throws Exception { + String description = this.description; + if (description == null) { + description = Thread.currentThread().getName(); + } try (TraceScope chunk = tracer.newScope(description, parent.getSpan().getSpanId())) { return impl.call(); } diff --git a/htrace-core4/src/main/java/org/apache/htrace/core/TraceRunnable.java b/htrace-core4/src/main/java/org/apache/htrace/core/TraceRunnable.java index abf530f..f2db5c2 100644 --- a/htrace-core4/src/main/java/org/apache/htrace/core/TraceRunnable.java +++ b/htrace-core4/src/main/java/org/apache/htrace/core/TraceRunnable.java @@ -30,15 +30,15 @@ public class TraceRunnable implements Runnable { this.tracer = tracer; this.parent = parent; this.runnable = runnable; - if (description == null) { - this.description = Thread.currentThread().getName(); - } else { - this.description = description; - } + this.description = description; } @Override public void run() { + String description = this.description; + if (description == null) { + description = Thread.currentThread().getName(); + } try (TraceScope chunk = tracer.newScope(description, parent.getSpan().getSpanId())) { runnable.run(); } diff --git a/htrace-core4/src/test/java/org/apache/htrace/core/TestTraceExecutor.java b/htrace-core4/src/test/java/org/apache/htrace/core/TestTraceExecutor.java new file mode 100644 index 0000000..7629cc3 --- /dev/null +++ b/htrace-core4/src/test/java/org/apache/htrace/core/TestTraceExecutor.java @@ -0,0 +1,107 @@ +/* + * 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.core; + +import static org.junit.Assert.assertEquals; + +import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class TestTraceExecutor { + + Tracer tracer; + + @Before + public void setup() { + tracer = new Tracer.Builder("TestTraceExecutor").conf(HTraceConfiguration.fromKeyValuePairs("sampler.classes", "AlwaysSampler")).build(); + } + + @After + public void teardown() { + tracer.close(); + } + + @Test + public void testExecutorWithNullScope() throws InterruptedException { + ExecutorService es = Executors.newSingleThreadExecutor(new NamingThreadFactory("child")); + es = tracer.newTraceExecutorService(es, null); + + final AtomicReference description = new AtomicReference(""); + + final CountDownLatch runnableLatch = new CountDownLatch(1); + try (TraceScope scope = tracer.newScope("parent")) { + es.submit(new Runnable() { + @Override + public void run() { + description.set(Tracer.getCurrentSpan().getDescription()); + runnableLatch.countDown(); + } + }); + } + + runnableLatch.await(1, TimeUnit.SECONDS); + assertEquals("TraceRunnable did not set description", "child-thread-1", description.get()); + description.set(""); + + final CountDownLatch callableLatch = new CountDownLatch(1); + try (TraceScope scope = tracer.newScope("parent")) { + es.submit(new Callable() { + @Override + public Void call() throws Exception { + description.set(Tracer.getCurrentSpan().getDescription()); + callableLatch.countDown(); + return null; + } + }); + } + + callableLatch.await(1, TimeUnit.SECONDS); + assertEquals("TraceCallable did not set description", "child-thread-1", description.get()); + + es.shutdown(); + } + + /* + * Inspired by org.apache.solr.util.DefaultSolrThreadFactory + */ + static class NamingThreadFactory implements ThreadFactory { + private final ThreadGroup group; + private final AtomicInteger threadNumber = new AtomicInteger(1); + private final String prefix; + + public NamingThreadFactory(String namePrefix) { + SecurityManager s = System.getSecurityManager(); + group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup(); + prefix = namePrefix + "-thread-"; + } + + @Override + public Thread newThread(Runnable r) { + return new Thread(group, r, prefix + threadNumber.getAndIncrement(), 0); + } + } +} -- 1.9.1