-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use client interceptors added in Ratpack 1.6 (#29)
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
Showing
11 changed files
with
324 additions
and
440 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/ratpack/zipkin/ClientTracingInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
src/main/java/ratpack/zipkin/internal/ClientHttpAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
111 changes: 111 additions & 0 deletions
111
src/main/java/ratpack/zipkin/internal/DefaultClientTracingInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.