Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename to TracingClientInterceptor and TracingServerInterceptor #45

Merged
merged 2 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pom.xml

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

```java
Expand All @@ -33,10 +33,10 @@ import io.opentracing.Tracer;
private final Tracer tracer;

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

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

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

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

```java
Expand All @@ -68,10 +68,10 @@ import io.opentracing.Tracer;
.usePlaintext(true)
.build();

ClientTracingInterceptor tracingInterceptor = new ClientTracingInterceptor(this.tracer);
TracingClientInterceptor tracingInterceptor = new TracingClientInterceptor(this.tracer);

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

blockingStub = GreeterGrpc.newBlockingStub(tracingInterceptor.intercept(channel));
}
Expand All @@ -81,7 +81,7 @@ import io.opentracing.Tracer;

## Server Tracing

A `ServerTracingInterceptor` uses default settings, which you can override by creating it using a `ServerTracingInterceptor.Builder`.
A `TracingServerInterceptor` uses default settings, which you can override by creating it using a `TracingServerInterceptor.Builder`.

- `withOperationName(OperationNameConstructor constructor)`: Define how the operation name is constructed for all spans created for the intercepted service. Default sets the operation name as the name of the RPC method. More details in the `Operation Name` section.
- `withStreaming()`: Logs to the server span whenever a message is received. *Note:* This package supports streaming but has not been rigorously tested. If you come across any issues, please let us know.
Expand All @@ -91,7 +91,7 @@ A `ServerTracingInterceptor` uses default settings, which you can override by cr
### Example

```java
ServerTracingInterceptor tracingInterceptor = new ServerTracingInterceptor
TracingServerInterceptor tracingInterceptor = new TracingServerInterceptor
.Builder(tracer)
.withStreaming()
.withVerbosity()
Expand All @@ -108,7 +108,7 @@ A `ServerTracingInterceptor` uses default settings, which you can override by cr

## Client Tracing

A `ClientTracingInterceptor` also has default settings, which you can override by creating it using a `ClientTracingInterceptor.Builder`.
A `TracingClientInterceptor` also has default settings, which you can override by creating it using a `TracingClientInterceptor.Builder`.

- `withOperationName(String operationName)`: Define how the operation name is constructed for all spans created for this intercepted client. Default is the name of the RPC method. More details in the `Operation Name` section.
- `withActiveSpanSource(ActiveSpanSource activeSpanSource)`: Define how to extract the current active span, if any. This is needed if you want your client to continue a trace instead of starting a new one. More details in the `Active Span Sources` section.
Expand All @@ -121,7 +121,7 @@ A `ClientTracingInterceptor` also has default settings, which you can override b
```java
import io.opentracing.Span;

ClientTracingInterceptor tracingInterceptor = new ClientTracingInterceptor
TracingClientInterceptor tracingInterceptor = new TracingClientInterceptor
.Builder(tracer)
.withStreaming()
.withVerbosity()
Expand Down Expand Up @@ -199,10 +199,10 @@ Tracer tracer = ...;

The default operation name for any span is the RPC method name (`io.grpc.MethodDescriptor.getFullMethodName()`). However, you may want to add your own prefixes, alter the name, or define a new name. For examples of good operation names, check out the OpenTracing `semantics`.

To alter the operation name, you need to add an implementation of the interface `OperationNameConstructor` to the `ClientTracingInterceptor.Builder` or `ServerTracingInterceptor.Builder`. For example, if you want to add a prefix to the default operation name of your ClientInterceptor, your code would look like this:
To alter the operation name, you need to add an implementation of the interface `OperationNameConstructor` to the `TracingClientInterceptor.Builder` or `TracingServerInterceptor.Builder`. For example, if you want to add a prefix to the default operation name of your ClientInterceptor, your code would look like this:

```java
ClientTracingInterceptor interceptor = ClientTracingInterceptor.Builder ...
TracingClientInterceptor interceptor = TracingClientInterceptor.Builder ...
.withOperationName(new OperationNameConstructor() {
@Override
public <ReqT, RespT> String constructOperationName(MethodDescriptor<ReqT, RespT> method) {
Expand All @@ -215,14 +215,14 @@ To alter the operation name, you need to add an implementation of the interface

## Active Span Sources

If you want your client to continue a trace rather than starting a new one, then you can tell your `ClientTracingInterceptor` how to extract the current active span by building it with your own implementation of the interface `ActiveSpanSource`. This interface has one method, `getActiveSpan`, in which you will define how to access the current active span.
If you want your client to continue a trace rather than starting a new one, then you can tell your `TracingClientInterceptor` how to extract the current active span by building it with your own implementation of the interface `ActiveSpanSource`. This interface has one method, `getActiveSpan`, in which you will define how to access the current active span.

For example, if you're creating the client in an environment that has the active span stored in a global dictionary-style context under `OPENTRACING_SPAN_KEY`, then you could configure your Interceptor as follows:

```java
import io.opentracing.Span;

ClientTracingInterceptor interceptor = new ClientTracingInterceptor
TracingClientInterceptor interceptor = new TracingClientInterceptor
.Builder(tracer)
...
.withActiveSpanSource(new ActiveSpanSource() {
Expand All @@ -247,7 +247,7 @@ Instead of `ActiveSpanSource` it's possible to use `ActiveSpanContextSource` if
```java
import io.opentracing.Span;

ClientTracingInterceptor interceptor = new ClientTracingInterceptor
TracingClientInterceptor interceptor = new TracingClientInterceptor
.Builder(tracer)
...
.withActiveSpanContextSource(new ActiveSpanContextSource() {
Expand All @@ -272,7 +272,7 @@ If you want to add custom tags or logs to the server and client spans, then you
Multiple different decorators may be added to the builder.

```java
ClientTracingInterceptor clientInterceptor = new ClientTracingInterceptor
TracingClientInterceptor clientInterceptor = new TracingClientInterceptor
.Builder(tracer)
...
.withClientSpanDecorator(new ClientSpanDecorator() {
Expand All @@ -291,7 +291,7 @@ ClientTracingInterceptor clientInterceptor = new ClientTracingInterceptor
...
.build();

ServerTracingInterceptor serverInterceptor = new ServerTracingInterceptor
TracingServerInterceptor serverInterceptor = new TracingServerInterceptor
.Builder(tracer)
...
.withServerSpanDecorator(new ServerSpanDecorator() {
Expand All @@ -312,21 +312,21 @@ ServerTracingInterceptor serverInterceptor = new ServerTracingInterceptor
```

## Integrating with Other Interceptors
Although we provide `ServerTracingInterceptor.intercept(service)` and `ClientTracingInterceptor.intercept(channel)` methods, you don't want to use these if you're chaining multiple interceptors. Instead, use the following code (preferably putting the tracing interceptor at the top of the interceptor stack so that it traces the entire request lifecycle, including other interceptors):
Although we provide `TracingServerInterceptor.intercept(service)` and `TracingClientInterceptor.intercept(channel)` methods, you don't want to use these if you're chaining multiple interceptors. Instead, use the following code (preferably putting the tracing interceptor at the top of the interceptor stack so that it traces the entire request lifecycle, including other interceptors):

### Server
```java
server = ServerBuilder.forPort(port)
.addService(ServerInterceptors.intercept(service, someInterceptor,
someOtherInterceptor, serverTracingInterceptor))
someOtherInterceptor, TracingServerInterceptor))
.build()
.start();
```

### Client
```java
blockingStub = GreeterGrpc.newBlockingStub(ClientInterceptors.intercept(channel,
someInterceptor, someOtherInterceptor, clientTracingInterceptor));
someInterceptor, someOtherInterceptor, TracingClientInterceptor));
```

## License
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package io.opentracing.contrib.grpc;

import io.opentracing.SpanContext;

/**
* An interface that defines how to get the current active span context
* An interface that defines how to get the current active span context.
*/
public interface ActiveSpanContextSource {

/**
* ActiveSpanContextSource implementation that always returns null as the active span context
* ActiveSpanContextSource implementation that always returns null as the active span context.
*/
ActiveSpanContextSource NONE = new ActiveSpanContextSource() {
@Override
Expand All @@ -32,7 +33,7 @@ public SpanContext getActiveSpanContext() {

/**
* ActiveSpanContextSource implementation that returns the current span context stored in the GRPC
* context under {@link OpenTracingContextKey}
* context under {@link OpenTracingContextKey}.
*/
ActiveSpanContextSource GRPC_CONTEXT = new ActiveSpanContextSource() {
@Override
Expand All @@ -42,6 +43,8 @@ public SpanContext getActiveSpanContext() {
};

/**
* Retrieves the active {@link SpanContext}.
*
* @return the active span context
*/
SpanContext getActiveSpanContext();
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/io/opentracing/contrib/grpc/ActiveSpanSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package io.opentracing.contrib.grpc;

import io.opentracing.Span;

/**
* An interface that defines how to get the current active span
* An interface that defines how to get the current active span.
*/
public interface ActiveSpanSource {

/**
* ActiveSpanSource implementation that always returns null as the active span
* ActiveSpanSource implementation that always returns null as the active span.
*/
ActiveSpanSource NONE = new ActiveSpanSource() {
@Override
Expand All @@ -32,7 +33,7 @@ public Span getActiveSpan() {

/**
* ActiveSpanSource implementation that returns the current span stored in the GRPC context under
* {@link OpenTracingContextKey}
* {@link OpenTracingContextKey}.
*/
ActiveSpanSource GRPC_CONTEXT = new ActiveSpanSource() {
@Override
Expand All @@ -42,7 +43,9 @@ public Span getActiveSpan() {
};

/**
* Retieves the active {@link Span}.
*
* @return the active span
*/
Span getActiveSpan();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package io.opentracing.contrib.grpc;

import io.grpc.ForwardingClientCallListener;
Expand All @@ -19,15 +20,14 @@
import io.opentracing.Span;

public interface ClientCloseDecorator {

/**
* The method of the implementation is executed inside {@link ForwardingClientCallListener#onClose(Status,
* Metadata)}
* The method of the implementation is executed inside {@link
* ForwardingClientCallListener#onClose}.
*
* @param span The span created by {@link ClientTracingInterceptor}
* @param status The status passed to {@link ForwardingClientCallListener#onClose(Status,
* Metadata)}.
* @param trailers The trailing headers passed to {@link ForwardingClientCallListener#onClose(Status,
* Metadata)}
* @param span The span created by {@link TracingClientInterceptor}
* @param status The status passed to {@link ForwardingClientCallListener#onClose}.
* @param trailers The trailing headers passed to {@link ForwardingClientCallListener#onClose}.
*/
void close(Span span, Status status, Metadata trailers);
}
19 changes: 10 additions & 9 deletions src/main/java/io/opentracing/contrib/grpc/ClientSpanDecorator.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,29 @@
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

package io.opentracing.contrib.grpc;

import io.grpc.CallOptions;
import io.grpc.Channel;
import io.grpc.MethodDescriptor;
import io.grpc.ServerCall;
import io.opentracing.Span;

/**
* An interface for adding custom span tags to the spans created by {@link
* ClientTracingInterceptor}
* TracingClientInterceptor}.
*/
public interface ClientSpanDecorator {

/**
* The method of the implementation is executed inside {@link ClientTracingInterceptor#interceptCall(MethodDescriptor,
* CallOptions, Channel)}
* The method of the implementation is executed inside {@link
* TracingClientInterceptor#interceptCall}.
*
* @param span The span created by {@link ClientTracingInterceptor}
* @param callOptions The {@link ServerCall} parameter of {@link ClientTracingInterceptor#interceptCall(MethodDescriptor,
* CallOptions, Channel)}
* @param method The {@link MethodDescriptor} parameter of {@link ClientTracingInterceptor#interceptCall(MethodDescriptor,
* CallOptions, Channel)}
* @param span The span created by {@link TracingClientInterceptor}
* @param callOptions The {@link ServerCall} parameter of {@link
* TracingClientInterceptor#interceptCall}
* @param method The {@link MethodDescriptor} parameter of {@link
* TracingClientInterceptor#interceptCall}
*/
void interceptCall(Span span, MethodDescriptor method, CallOptions callOptions);
}
Loading