/** * Wrap the callable in a TraceCallable, if tracing. * * @return The callable provided, wrapped if tracing, 'callable' if not. */ public <V> Callable<V> wrap(Callable<V> callable, String description) { TraceScope parentScope = threadLocalScope.get(); if (parentScope == null) { return callable; } return new TraceCallable<V>(this, parentScope, callable, description); }
/** * Wrap the callable in a TraceCallable, if tracing. * * @param <V> The subclass of callable. * @param callable The callable to wrap. * @param description A description of the callable, or null if there * is no description. * @return The callable provided, wrapped if tracing, 'callable' if not. */ public <V> Callable<V> wrap(Callable<V> callable, String description) { TraceScope parentScope = threadLocalScope.get(); if (parentScope == null) { return callable; } return new TraceCallable<V>(this, parentScope.getSpanId(), callable, description); }
/** * Wrap the callable in a TraceCallable, if tracing. * * @param <V> The subclass of callable. * @param callable The callable to wrap. * @param description A description of the callable, or null if there * is no description. * @return The callable provided, wrapped if tracing, 'callable' if not. */ public <V> Callable<V> wrap(Callable<V> callable, String description) { TraceScope parentScope = threadLocalScope.get(); if (parentScope == null) { return callable; } return new TraceCallable<V>(this, parentScope.getSpanId(), callable, description); }
@Test public void testWrappingFromSpan() throws Exception { HTraceConfiguration conf = HTraceConfiguration.fromKeyValuePairs("sampler.classes", "AlwaysSampler"); ExecutorService es = Executors.newSingleThreadExecutor(); try (Tracer tracer = new Tracer.Builder("TestTraceExecutor").conf(conf).build()) { SpanId random = SpanId.fromRandom(); try (TraceScope parentScope = tracer.newScope("parent")) { Callable<SpanId> callable = new TraceCallable<SpanId>(tracer, random, new Callable<SpanId>() { @Override public SpanId call() throws Exception { return Tracer.getCurrentSpan().getParents()[0]; } }, "child"); SpanId result = es.submit(callable).get(WAIT_TIME_SECONDS, TimeUnit.SECONDS); assertEquals(random, result); } } finally { es.shutdown(); } }