Skip to content

Commit

Permalink
Migrate from remoting3 to tracing-java (#100)
Browse files Browse the repository at this point in the history
* Remove remoting3 dependency and pick up rebranded tracing-java change.
* Split tracing-api & tracing implementation dependency
* Add tracing sanity check for legacy remoting3
Log error and fallback to remoting3 tracing if the detected remoting3 tracer does not delegate to java-tracing properly.
* Use reflection for remoting3 tracing fallback
* Extract Tracer interface, java-tracing and reflective implementations
* TracingInvocationEventHandler factory
  • Loading branch information
qinfchen authored and schlosna committed Oct 17, 2018
1 parent 348870e commit e8872e1
Show file tree
Hide file tree
Showing 12 changed files with 442 additions and 21 deletions.
1 change: 1 addition & 0 deletions tritium-jmh/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {

jmh project(':tritium-lib')
jmh 'ch.qos.logback:logback-classic'
jmh 'com.palantir.remoting3:tracing'
jmh 'org.slf4j:slf4j-simple'

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

package com.palantir.tritium.microbenchmarks;

import com.palantir.remoting3.tracing.AsyncSlf4jSpanObserver;
import com.palantir.remoting3.tracing.Tracer;
import com.palantir.tracing.AsyncSlf4jSpanObserver;
import com.palantir.tracing.Tracer;
import com.palantir.tritium.metrics.MetricRegistries;
import com.palantir.tritium.proxy.Instrumentation;
import com.palantir.tritium.tracing.RemotingCompatibleTracingInvocationEventHandler;
import com.palantir.tritium.tracing.TracingInvocationEventHandler;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -59,6 +60,7 @@ public class ProxyBenchmark {
private Service instrumentedWithMetrics;
private Service instrumentedWithEverything;
private Service instrumentedWithTracing;
private Service instrumentedWithRemoting;

private ExecutorService executor;

Expand All @@ -78,17 +80,20 @@ public void before() {
.withMetrics(MetricRegistries.createWithHdrHistogramReservoirs())
.build();

TracingInvocationEventHandler tracingInvocationEventHandler = new TracingInvocationEventHandler("jmh");
executor = Executors.newSingleThreadExecutor();
Tracer.subscribe("slf4j", AsyncSlf4jSpanObserver.of("test", executor));
instrumentedWithTracing = Instrumentation.builder(Service.class, raw)
.withHandler(tracingInvocationEventHandler)
.withHandler(TracingInvocationEventHandler.create("jmh"))
.build();

instrumentedWithRemoting = Instrumentation.builder(Service.class, raw)
.withHandler(new RemotingCompatibleTracingInvocationEventHandler("jmh", Remoting3Tracer.INSTANCE))
.build();

instrumentedWithEverything = Instrumentation.builder(Service.class, raw)
.withMetrics(MetricRegistries.createWithHdrHistogramReservoirs())
.withPerformanceTraceLogging()
.withHandler(tracingInvocationEventHandler)
.withHandler(TracingInvocationEventHandler.create("jmh"))
.build();
}

Expand All @@ -107,17 +112,17 @@ public String raw() {
return raw.echo("test");
}

@Benchmark
// @Benchmark
public String instrumentedWithoutHandlers() {
return instrumentedWithoutHandlers.echo("test");
}

@Benchmark
// @Benchmark
public String instrumentedWithPerformanceLogging() {
return instrumentedWithPerformanceLogging.echo("test");
}

@Benchmark
// @Benchmark
public String instrumentedWithMetrics() {
return instrumentedWithMetrics.echo("test");
}
Expand All @@ -128,6 +133,11 @@ public String instrumentedWithTracing() {
}

@Benchmark
public String instrumentedWithRemoting() {
return instrumentedWithRemoting.echo("test");
}

// @Benchmark
public String instrumentedWithEverything() {
return instrumentedWithEverything.echo("test");
}
Expand Down Expand Up @@ -155,4 +165,18 @@ public static void main(String[] args) throws Exception {
new Runner(options).run();
}

public enum Remoting3Tracer implements com.palantir.tritium.tracing.Tracer {
INSTANCE;

@Override
public void startSpan(String operationName) {
com.palantir.remoting3.tracing.Tracer.startSpan(operationName);
}

@Override
public void completeSpan() {
com.palantir.remoting3.tracing.Tracer.fastCompleteSpan();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static <T, U extends T> T instrument(Class<T> serviceInterface, U delegat
return Instrumentation.builder(serviceInterface, delegate)
.withMetrics(metricRegistry)
.withPerformanceTraceLogging()
.withHandler(new TracingInvocationEventHandler(serviceInterface.getName()))
.withHandler(TracingInvocationEventHandler.create(serviceInterface.getName()))
.build();
}
}
2 changes: 1 addition & 1 deletion tritium-tracing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ dependencies {

api project(':tritium-api')
api project(':tritium-core')
api 'com.palantir.remoting3:tracing'

implementation 'com.palantir.safe-logging:safe-logging'
implementation 'com.palantir.tracing:tracing'
implementation 'org.slf4j:slf4j-api'

testImplementation project(':tritium-test')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* (c) Copyright 2018 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.tritium.tracing;

enum JavaTracingTracer implements Tracer {
INSTANCE;

@Override
public void startSpan(String operationName) {
com.palantir.tracing.Tracer.startSpan(operationName);
}

@Override
public void completeSpan() {
com.palantir.tracing.Tracer.fastCompleteSpan();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* (c) Copyright 2018 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.tritium.tracing;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Throwables;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

final class ReflectiveTracer implements Tracer {

private final Method startSpanMethod;
private final Method completeSpanMethod;

ReflectiveTracer(Method startSpanMethod, Method completeSpanMethod) {
this.startSpanMethod = checkStartMethod(startSpanMethod);
this.completeSpanMethod = checkCompleteMethod(completeSpanMethod);
}

@Override
public void startSpan(String operationName) {
try {
startSpanMethod.invoke(null, operationName);
} catch (ReflectiveOperationException e) {
throw throwUnchecked(e);
}
}

@Override
public void completeSpan() {
try {
completeSpanMethod.invoke(null);
} catch (ReflectiveOperationException e) {
throw throwUnchecked(e);
}
}

private static Method checkStartMethod(Method method) {
checkNotNull(method, "method");
checkArgument(Modifier.isPublic(method.getModifiers()), "method must be public: %s", method);
checkArgument(Modifier.isStatic(method.getModifiers()), "method must be static: %s", method);
if (method.getParameterCount() != 1 || !String.class.equals(method.getParameterTypes()[0])) {
throw new IllegalArgumentException(String.format("startSpan method should take 1 String argument, was %s",
paramsToClassNames(method)));
}
return method;
}

private static Method checkCompleteMethod(Method method) {
checkNotNull(method, "method");
checkArgument(Modifier.isPublic(method.getModifiers()), "method must be public: %s", method);
checkArgument(Modifier.isStatic(method.getModifiers()), "method must be static: %s", method);
checkArgument(method.getParameterCount() == 0,
"completeSpan method should take 0 arguments, was %s", paramsToClassNames(method));
return method;
}

private static List<String> paramsToClassNames(Method method) {
return Arrays.stream(method.getParameterTypes())
.map(Class::getName)
.collect(Collectors.toList());
}

private static RuntimeException throwUnchecked(ReflectiveOperationException reflectionException) {
Throwable cause = reflectionException.getCause() != null ? reflectionException.getCause() : reflectionException;
Throwables.throwIfUnchecked(cause);
throw new RuntimeException(cause);
}

}
Loading

0 comments on commit e8872e1

Please sign in to comment.