diff --git a/.palantir/revapi.yml b/.palantir/revapi.yml index 31b8c0f6c..ba4deba9c 100644 --- a/.palantir/revapi.yml +++ b/.palantir/revapi.yml @@ -19,3 +19,8 @@ acceptedBreaks: - code: "java.method.addedToInterface" new: "method java.util.Optional com.palantir.tracing.TraceMetadata::getRequestId()" justification: "immutables are not for extension" + "5.0.0": + com.palantir.tracing:tracing: + - code: "java.class.removed" + old: "class com.palantir.tracing.AsyncTracer" + justification: "Sourcegraph search shows zero usages of this class" diff --git a/changelog/@unreleased/pr-753.v2.yml b/changelog/@unreleased/pr-753.v2.yml new file mode 100644 index 000000000..5e6c7e370 --- /dev/null +++ b/changelog/@unreleased/pr-753.v2.yml @@ -0,0 +1,5 @@ +type: break +break: + description: The deprecated `AsyncTracer` class is deleted. + links: + - https://github.com/palantir/tracing-java/pull/753 diff --git a/tracing/src/main/java/com/palantir/tracing/AsyncTracer.java b/tracing/src/main/java/com/palantir/tracing/AsyncTracer.java deleted file mode 100644 index 2739c4f5c..000000000 --- a/tracing/src/main/java/com/palantir/tracing/AsyncTracer.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. - * - * Licensed 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 com.palantir.tracing; - -import java.util.Optional; - -/** - * Utility for tracking an operation that will be run asynchronously. It tracks the time it spent before - * {@link #withTrace} is called. - * - *
- * {@code
- * AsyncTracer asyncTracer = new AsyncTracer();
- *
- * //...
- *
- * // some time later
- * asyncTracer.withTrace(() -> {
- *     doThings();
- *     System.out.println(Tracer.getTraceId()); // prints trace id at time of construction of async tracer
- *     return null;
- * });
- *
- * }
- * 
- * - * @deprecated prefer {@link DetachedSpan#start}, which gives you much more granular control. - */ -@Deprecated -public final class AsyncTracer { - - private static final String DEFAULT_OPERATION = "async"; - - private final Trace deferredTrace; - private final String operation; - - public AsyncTracer() { - this(Optional.empty()); - } - - public AsyncTracer(String operation) { - this(Optional.of(operation)); - } - - /** - * Create a new deferred tracer, optionally specifying an operation. If no operation is specified, will attempt to - * use the parent span's operation name. - */ - public AsyncTracer(Optional operation) { - this.operation = operation.orElse(DEFAULT_OPERATION); - Tracer.fastStartSpan(this.operation + "-enqueue"); - deferredTrace = Tracer.copyTrace().get(); - Tracer.fastDiscardSpan(); // span will completed in the deferred execution - } - - /** Runs the given callable with the current trace at the time of construction of this {@link AsyncTracer}. */ - public T withTrace(Tracers.ThrowingCallable inner) throws E { - Trace originalTrace = Tracer.getAndClearTrace(); - Tracer.setTrace(deferredTrace); - // Finish the enqueue span - Tracer.fastCompleteSpan(); - Tracer.fastStartSpan(operation + "-run"); - try { - return inner.call(); - } finally { - // Finish the run span - Tracer.fastCompleteSpan(); - Tracer.setTrace(originalTrace); - } - } -} diff --git a/tracing/src/test/java/com/palantir/tracing/AsyncTracerTest.java b/tracing/src/test/java/com/palantir/tracing/AsyncTracerTest.java deleted file mode 100644 index 9feca27b3..000000000 --- a/tracing/src/test/java/com/palantir/tracing/AsyncTracerTest.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. - * - * Licensed 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 com.palantir.tracing; - -import static org.assertj.core.api.Assertions.assertThat; - -import com.palantir.tracing.api.SpanType; -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; -import org.junit.Before; -import org.junit.Test; - -@SuppressWarnings("deprecation") -public class AsyncTracerTest { - - @Before - public void before() { - Tracer.setSampler(AlwaysSampler.INSTANCE); - } - - @Test - public void doesNotLeakEnqueueSpan() { - Tracer.setTrace(Trace.of(true, "defaultTraceId", Optional.empty())); - Trace originalTrace = getTrace(); - AsyncTracer deferredTracer = new AsyncTracer(); - assertThat(originalTrace.top()).isEmpty(); - - deferredTracer.withTrace(() -> { - Trace traceCopy = Tracer.copyTrace().get(); - assertThat(traceCopy.pop()).isPresent().hasValueSatisfying(span -> assertThat(span.getOperation()) - .isEqualTo("async-run")); - return null; - }); - } - - @Test - public void completesBothDeferredSpans() { - Tracer.initTraceWithSpan(Observability.SAMPLE, "defaultTraceId", "defaultSpan", SpanType.LOCAL); - AsyncTracer asyncTracer = new AsyncTracer(); - List observedSpans = new ArrayList<>(); - Tracer.subscribe(AsyncTracerTest.class.getName(), span -> observedSpans.add(span.getOperation())); - - asyncTracer.withTrace(() -> null); - Tracer.unsubscribe(AsyncTracerTest.class.getName()); - assertThat(observedSpans).containsExactly("async-enqueue", "async-run"); - } - - @Test - public void preservesState() { - Tracer.initTraceWithSpan(Observability.UNDECIDED, "defaultTraceId", "foo", SpanType.LOCAL); - Tracer.fastStartSpan("bar"); - Tracer.fastStartSpan("baz"); - Trace originalTrace = getTrace(); - AsyncTracer asyncTracer = new AsyncTracer(); - - asyncTracer.withTrace(() -> { - Trace traceCopy = Tracer.copyTrace().get(); - assertThat(traceCopy.pop()).isPresent().hasValueSatisfying(span -> assertThat(span.getOperation()) - .isEqualTo("async-run")); - assertThat(traceCopy.pop()).isPresent().hasValueSatisfying(span -> assertThat(span.getOperation()) - .isEqualTo("baz")); - assertThat(traceCopy.pop()).isPresent().hasValueSatisfying(span -> assertThat(span.getOperation()) - .isEqualTo("bar")); - assertThat(traceCopy.pop()).isPresent().hasValueSatisfying(span -> assertThat(span.getOperation()) - .isEqualTo("foo")); - return null; - }); - - assertThat(originalTrace.pop()).isPresent().hasValueSatisfying(span -> assertThat(span.getOperation()) - .isEqualTo("baz")); - assertThat(originalTrace.pop()).isPresent().hasValueSatisfying(span -> assertThat(span.getOperation()) - .isEqualTo("bar")); - assertThat(originalTrace.pop()).isPresent().hasValueSatisfying(span -> assertThat(span.getOperation()) - .isEqualTo("foo")); - } - - /** Get reference to the current trace. */ - private Trace getTrace() { - Trace originalTrace = Tracer.getAndClearTrace(); - Tracer.setTrace(originalTrace); - return originalTrace; - } -}