Skip to content

Commit

Permalink
add GlobalTracer support
Browse files Browse the repository at this point in the history
Signed-off-by: Sergei Malafeev <sergeymalafeev@gmail.com>
  • Loading branch information
malafeev committed Aug 28, 2018
1 parent ad34396 commit 1cd5306
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -19,6 +19,7 @@ pom.xml
### Server

- Instantiate tracer
- Optionally register tracer with GlobalTracer: `GlobalTracer.register(tracer)`
- Create a `ServerTracingInterceptor`
- Intercept a service

Expand All @@ -33,6 +34,9 @@ import io.opentracing.Tracer;

private void start() throws IOException {
ServerTracingInterceptor tracingInterceptor = new ServerTracingInterceptor(this.tracer);

// If GlobalTracer is used:
// ServerTracingInterceptor tracingInterceptor = new ServerTracingInterceptor();

server = ServerBuilder.forPort(port)
.addService(tracingInterceptor.intercept(someServiceDef))
Expand All @@ -45,6 +49,7 @@ import io.opentracing.Tracer;
### Client

- Instantiate a tracer
- Optionally register tracer with GlobalTracer: `GlobalTracer.register(tracer)`
- Create a `ClientTracingInterceptor`
- Intercept the client channel

Expand All @@ -64,6 +69,9 @@ import io.opentracing.Tracer;
.build();

ClientTracingInterceptor tracingInterceptor = new ClientTracingInterceptor(this.tracer);

// If GlobalTracer is used:
// ClientTracingInterceptor tracingInterceptor = new ClientTracingInterceptor();

blockingStub = GreeterGrpc.newBlockingStub(tracingInterceptor.intercept(channel));
}
Expand Down
10 changes: 9 additions & 1 deletion pom.xml
Expand Up @@ -14,7 +14,9 @@
the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-grpc</artifactId>
Expand Down Expand Up @@ -72,6 +74,12 @@
<version>${opentracing.version}</version>
</dependency>

<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-util</artifactId>
<version>${opentracing.version}</version>
</dependency>

<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-mock</artifactId>
Expand Down
Expand Up @@ -29,6 +29,7 @@
import io.opentracing.Tracer;
import io.opentracing.propagation.Format;
import io.opentracing.propagation.TextMap;
import io.opentracing.util.GlobalTracer;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
Expand All @@ -51,6 +52,13 @@ public class ClientTracingInterceptor implements ClientInterceptor {
private final ActiveSpanContextSource activeSpanContextSource;
private final ClientSpanDecorator clientSpanDecorator;

/**
* Instantiate interceptor using GlobalTracer to get tracer
*/
public ClientTracingInterceptor() {
this(GlobalTracer.get());
}

/**
* @param tracer to use to trace requests
*/
Expand Down Expand Up @@ -270,7 +278,7 @@ private Span createSpanFromParent(SpanContext parentSpanContext, String operatio
*/
public static class Builder {

private Tracer tracer;
private final Tracer tracer;
private OperationNameConstructor operationNameConstructor;
private boolean streaming;
private boolean verbose;
Expand All @@ -279,6 +287,13 @@ public static class Builder {
private ActiveSpanContextSource activeSpanContextSource;
private ClientSpanDecorator clientSpanDecorator;

/**
* Creates a Builder using GlobalTracer to get tracer
*/
public Builder() {
this(GlobalTracer.get());
}

/**
* @param tracer to use for this intercepter
* Creates a Builder with default configuration
Expand Down Expand Up @@ -388,6 +403,7 @@ public enum ClientRequestAttribute {

private static class NoopClientSpanDecorator implements ClientSpanDecorator {
@Override
public void interceptCall(Span span, MethodDescriptor method, CallOptions callOptions) {}
public void interceptCall(Span span, MethodDescriptor method, CallOptions callOptions) {
}
}
}
Expand Up @@ -30,6 +30,7 @@
import io.opentracing.Tracer;
import io.opentracing.propagation.Format;
import io.opentracing.propagation.TextMapExtractAdapter;
import io.opentracing.util.GlobalTracer;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
Expand All @@ -49,6 +50,13 @@ public class ServerTracingInterceptor implements ServerInterceptor {
private final Set<ServerRequestAttribute> tracedAttributes;
private final ServerSpanDecorator serverSpanDecorator;

/**
* Instantiate interceptor using GlobalTracer to get tracer
*/
public ServerTracingInterceptor() {
this(GlobalTracer.get());
}

/**
* @param tracer used to trace requests
*/
Expand Down Expand Up @@ -222,6 +230,13 @@ public static class Builder {
private Set<ServerRequestAttribute> tracedAttributes;
private ServerSpanDecorator serverSpanDecorator;

/**
* Creates a Builder using GlobalTracer to get tracer
*/
public Builder() {
this(GlobalTracer.get());
}

/**
* @param tracer to use for this intercepter
* Creates a Builder with default configuration
Expand Down Expand Up @@ -303,6 +318,7 @@ public enum ServerRequestAttribute {

private static class NoopServerSpanDecorator implements ServerSpanDecorator {
@Override
public void interceptCall(Span span, ServerCall call, Metadata headers) {}
public void interceptCall(Span span, ServerCall call, Metadata headers) {
}
}
}

0 comments on commit 1cd5306

Please sign in to comment.