-
Notifications
You must be signed in to change notification settings - Fork 713
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
335 additions
and
216 deletions.
There are no files selected for viewing
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
75 changes: 0 additions & 75 deletions
75
...ing/src/main/java/com/github/kristofa/brave/client/BraveClientHttpRequestInterceptor.java
This file was deleted.
Oops, something went wrong.
40 changes: 0 additions & 40 deletions
40
brave-core-spring/src/main/java/com/github/kristofa/brave/client/SpringRequestAdapter.java
This file was deleted.
Oops, something went wrong.
27 changes: 0 additions & 27 deletions
27
brave-core-spring/src/main/java/com/github/kristofa/brave/client/SpringResponseAdapter.java
This file was deleted.
Oops, something went wrong.
68 changes: 0 additions & 68 deletions
68
...src/test/java/com/github/kristofa/brave/client/BraveClientHttpRequestInterceptorTest.java
This file was deleted.
Oops, something went wrong.
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,5 @@ | ||
# brave-spring-resttemplate-interceptors # | ||
|
||
|
||
Interceptor to be used with the Spring `RestTemplate` class to record the client requests. | ||
To use it, add the interceptor with the `RestTemplate.setInterceptors` method. |
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,72 @@ | ||
<?xml version="1.0"?> | ||
<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> | ||
<parent> | ||
<groupId>com.github.kristofa</groupId> | ||
<artifactId>brave</artifactId> | ||
<version>3.0.0-alpha-2-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>brave-spring-resttemplate-interceptors</artifactId> | ||
<packaging>jar</packaging> | ||
<description> | ||
Spring RestTemplate interceptor implementation. | ||
</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.github.kristofa</groupId> | ||
<artifactId>brave-http</artifactId> | ||
<version>3.0.0-alpha-2-SNAPSHOT</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-webmvc</artifactId> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.commons</groupId> | ||
<artifactId>commons-lang3</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework</groupId> | ||
<artifactId>spring-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-eclipse-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-source-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-javadoc-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-deploy-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-failsafe-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.codehaus.mojo</groupId> | ||
<artifactId>animal-sniffer-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
78 changes: 78 additions & 0 deletions
78
...tors/src/main/java/com/github/krisofa/brave/client/BraveClientHttpRequestInterceptor.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,78 @@ | ||
package com.github.krisofa.brave.client; | ||
|
||
import com.github.kristofa.brave.ClientRequestInterceptor; | ||
import com.github.kristofa.brave.ClientResponseInterceptor; | ||
import com.github.kristofa.brave.NoAnnotationsClientResponseAdapter; | ||
import com.github.kristofa.brave.http.HttpClientRequestAdapter; | ||
import com.github.kristofa.brave.http.HttpClientResponseAdapter; | ||
import com.github.kristofa.brave.http.ServiceNameProvider; | ||
import com.github.kristofa.brave.http.SpanNameProvider; | ||
import org.springframework.http.HttpRequest; | ||
import org.springframework.http.client.ClientHttpRequestExecution; | ||
import org.springframework.http.client.ClientHttpRequestInterceptor; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Spring {@link org.springframework.web.client.RestTemplate RestTemplate} {@link ClientHttpRequestInterceptor} that adds brave/zipkin annotations to outgoing client request and | ||
* logs the response. | ||
* <p/> | ||
* We assume the first part of the URI is the context path. The context name will be used as service name in endpoint. | ||
* Remaining part of path will be used as span name unless X-B3-SpanName http header is set. For example, if we have URI: | ||
* <p/> | ||
* <code>/service/path/a/b</code> | ||
* <p/> | ||
* The service name will be 'service'. The span name will be '/path/a/b'. | ||
* <p/> | ||
* For the response, it inspects the state. If the response indicates an error it submits error code and failure annotation. Finally it submits the client received annotation. | ||
*/ | ||
public class BraveClientHttpRequestInterceptor implements ClientHttpRequestInterceptor { | ||
|
||
private final ClientRequestInterceptor requestInterceptor; | ||
private final ClientResponseInterceptor responseInterceptor; | ||
private final ServiceNameProvider serviceNameProvider; | ||
private final SpanNameProvider spanNameProvider; | ||
|
||
/** | ||
* Creates a new instance. | ||
* | ||
* @param serviceNameProvider Provides service name. | ||
* @param spanNameProvider Provides span name. | ||
* @param requestInterceptor Client request interceptor. | ||
* @param responseInterceptor Client response interceptor. | ||
*/ | ||
public BraveClientHttpRequestInterceptor(final ClientRequestInterceptor requestInterceptor, final ClientResponseInterceptor responseInterceptor, | ||
final ServiceNameProvider serviceNameProvider, final SpanNameProvider spanNameProvider) { | ||
this.requestInterceptor = requestInterceptor; | ||
this.responseInterceptor = responseInterceptor; | ||
this.serviceNameProvider = serviceNameProvider; | ||
this.spanNameProvider = spanNameProvider; | ||
} | ||
|
||
@Override | ||
public ClientHttpResponse intercept(final HttpRequest request, final byte[] body, final ClientHttpRequestExecution execution) throws IOException { | ||
|
||
requestInterceptor.handle(new HttpClientRequestAdapter(new SpringHttpClientRequest(request), serviceNameProvider, spanNameProvider)); | ||
|
||
final ClientHttpResponse response; | ||
|
||
try { | ||
response = execution.execute(request, body); | ||
} catch (RuntimeException | IOException up) { | ||
// Something went serious wrong communicating with the server; let the exception blow up | ||
responseInterceptor.handle(NoAnnotationsClientResponseAdapter.getInstance()); | ||
|
||
throw up; | ||
} | ||
|
||
try { | ||
responseInterceptor.handle(new HttpClientResponseAdapter(new SpringHttpResponse(response.getRawStatusCode()))); | ||
} catch (RuntimeException | IOException up) { | ||
// Ignore the failure of not being able to get the status code from the response; let the calling code find out themselves | ||
responseInterceptor.handle(NoAnnotationsClientResponseAdapter.getInstance()); | ||
} | ||
|
||
return response; | ||
} | ||
} |
Oops, something went wrong.