Skip to content

Commit

Permalink
Use client interceptors added in Ratpack 1.6 (#29)
Browse files Browse the repository at this point in the history
Drops the wrapped HttpClient instrumentation and uses HttpClient Interceptors instead.

This changes the trace results slightly based on the way the Ratpack Interceptor is invoked.  In practice this shouldn't be an issue but if we get complaints we can alway introduce an optional library with the wrapped HttpClient implementation down the road.

By using the Ratpack Interceptor we can close out #8 and it should also fix #27 as well.
  • Loading branch information
llinder authored Jul 25, 2019
1 parent d1cde40 commit a6ba895
Show file tree
Hide file tree
Showing 11 changed files with 324 additions and 440 deletions.
27 changes: 27 additions & 0 deletions src/main/java/ratpack/zipkin/ClientTracingInterceptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2016-2019 The OpenZipkin Authors
*
* 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 ratpack.zipkin;

import ratpack.http.client.HttpResponse;
import ratpack.http.client.RequestSpec;

public interface ClientTracingInterceptor {

void request(RequestSpec spec);

void response(HttpResponse response);

void error(Throwable e);

}
31 changes: 24 additions & 7 deletions src/main/java/ratpack/zipkin/ServerTracingModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,17 @@
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.multibindings.Multibinder;
import ratpack.api.Nullable;
import ratpack.guice.ConfigurableModule;
import ratpack.handling.HandlerDecorator;
import ratpack.http.client.HttpClient;
import ratpack.server.ServerConfig;
import ratpack.zipkin.internal.*;
import zipkin2.Endpoint;
import ratpack.util.Exceptions;
import ratpack.zipkin.internal.DefaultClientTracingInterceptor;
import ratpack.zipkin.internal.DefaultServerTracingHandler;
import ratpack.zipkin.internal.RatpackCurrentTraceContext;
import ratpack.zipkin.internal.RatpackHttpServerParser;
import zipkin2.Span;
import zipkin2.reporter.Reporter;
import java.net.InetAddress;

/**
* Module for Zipkin distributed tracing.
Expand All @@ -50,11 +51,27 @@ protected void configure() {
.to(DefaultServerTracingHandler.class)
.in(Singleton.class);

bind(HttpClient.class).annotatedWith(Zipkin.class)
.to(ZipkinHttpClientImpl.class)
bind(ClientTracingInterceptor.class)
.to(DefaultClientTracingInterceptor.class)
.in(Singleton.class);

bind(ZipkinHttpClientImpl.class);
Provider<ClientTracingInterceptor> clientTracingInterceptorProvider =
getProvider(ClientTracingInterceptor.class);

Provider<HttpClient> httpClientProvider = () ->
// getProvider(HttpClient.class).get().copyWith(
Exceptions.uncheck(() -> HttpClient.of((s) -> {
ClientTracingInterceptor ic = clientTracingInterceptorProvider.get();
s.requestIntercept(ic::request);
s.responseIntercept(ic::response);
s.errorIntercept(ic::error);
})
);

bind(HttpClient.class)
.annotatedWith(Zipkin.class)
.toProvider(httpClientProvider)
.in(Singleton.class);

bind(RatpackCurrentTraceContext.TracingPropagationExecInitializer.class)
.in(Singleton.class);
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/ratpack/zipkin/internal/ClientHttpAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2016-2019 The OpenZipkin Authors
*
* 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 ratpack.zipkin.internal;

import brave.http.HttpClientAdapter;
import ratpack.http.client.HttpResponse;
import ratpack.http.client.RequestSpec;

public class ClientHttpAdapter extends HttpClientAdapter<RequestSpec, HttpResponse> {

@Override
public String method(RequestSpec requestSpec) {
return requestSpec.getMethod().getName();
}

@Override
public String url(RequestSpec requestSpec) {
return requestSpec.getUri().toString();
}

@Override
public String requestHeader(RequestSpec requestSpec, String name) {
return requestSpec.getHeaders().get(name);
}

@Override
public Integer statusCode(HttpResponse response) {
return response.getStatusCode();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Copyright 2016-2019 The OpenZipkin Authors
*
* 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 ratpack.zipkin.internal;

import brave.Span;
import brave.http.HttpClientHandler;
import brave.http.HttpTracing;
import brave.propagation.TraceContext;
import ratpack.exec.Execution;
import ratpack.http.MutableHeaders;
import ratpack.http.client.HttpResponse;
import ratpack.http.client.RequestSpec;
import ratpack.zipkin.ClientTracingInterceptor;

import javax.inject.Inject;
import java.util.Optional;
import java.util.function.Supplier;

public class DefaultClientTracingInterceptor implements ClientTracingInterceptor {

private final HttpClientHandler<RequestSpec, HttpResponse> handler;
private final TraceContext.Injector<MutableHeaders> injector;
private final Supplier<Optional<Execution>> registrySupplier;

@Inject
public DefaultClientTracingInterceptor(final HttpTracing httpTracing) {
this(httpTracing, Execution::currentOpt);
}

public DefaultClientTracingInterceptor(final HttpTracing httpTracing, final Supplier<Optional<Execution>> registry) {
this.handler = HttpClientHandler.create(httpTracing, new ClientHttpAdapter());
this.injector = httpTracing.tracing().propagation().injector(MutableHeaders::set);
this.registrySupplier = registry;
}

@Override
public void request(RequestSpec spec) {
registrySupplier.get()
.ifPresent((execution -> {
final Span span = this.handler.handleSend(injector, spec.getHeaders(), spec);
final ClientSpanHolder holder = new ClientSpanHolder(span);
execution.add(holder);
}));
}

@Override
public void response(HttpResponse response) {
registrySupplier.get()
.ifPresent(execution -> {
execution
.maybeGet(ClientSpanHolder.class)
.ifPresent((s) -> {
Iterable<? extends ClientSpanHolder> i = execution.getAll(ClientSpanHolder.class);
execution.remove(ClientSpanHolder.class);
this.handler.handleReceive(response, null, s.span);
// special case code for tests to ensure the shared test execution doesn't clear out
// other client spans that are still in flight.
i.forEach((csh) -> {
if (csh != s) {
execution.add(csh);
}
});
});
});

}

@Override
public void error(Throwable e) {
registrySupplier.get()
.ifPresent(execution -> {
execution
.maybeGet(ClientSpanHolder.class)
.ifPresent((s) -> {
Iterable<? extends ClientSpanHolder> i = execution.getAll(ClientSpanHolder.class);
execution.remove(ClientSpanHolder.class);
this.handler.handleReceive(null, e, s.span);
// special case code for tests to ensure the shared test execution doesn't clear out
// other client spans that are still in flight.
i.forEach((csh) -> {
if (csh != s) {
execution.add(csh);
}
});
});
});
}

public static class ClientSpanHolder {
private Span span;

public ClientSpanHolder(Span span) {
this.span = span;
}

Span getSpan() {
return this.span;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
package ratpack.zipkin.internal;


import brave.Span;
import brave.propagation.CurrentTraceContext;
import brave.propagation.TraceContext;

import java.util.Optional;
import java.util.function.Supplier;
import org.slf4j.MDC;
import ratpack.exec.ExecInitializer;
import ratpack.exec.Execution;
import ratpack.exec.ExecutionRef;
import ratpack.registry.MutableRegistry;

public final class RatpackCurrentTraceContext extends CurrentTraceContext {
Expand Down Expand Up @@ -112,10 +116,18 @@ public static class TracingPropagationExecInitializer implements ExecInitializer

@Override
public void init(Execution execution) {
execution
.maybeParent()
Optional<ExecutionRef> maybeParent = execution.maybeParent();

maybeParent
.flatMap(parent -> parent.maybeGet(TraceContextHolder.class))
.ifPresent(execution::add);

// Copies forward the HTTP Client instrumentation Span.
// This is important since the interceptor execution is forked between
// the request and the response handling.
maybeParent
.flatMap(parent -> parent.maybeGet(DefaultClientTracingInterceptor.ClientSpanHolder.class))
.ifPresent(execution::add);
}
}

Expand Down
Loading

0 comments on commit a6ba895

Please sign in to comment.