Skip to content

Commit

Permalink
Make changes from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
wjam committed Aug 15, 2015
1 parent 30d9223 commit 7fd469a
Show file tree
Hide file tree
Showing 12 changed files with 335 additions and 216 deletions.
9 changes: 3 additions & 6 deletions brave-core-spring/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# brave-impl-spring #
# brave-core-spring #

Latest release available in Maven central:

<dependency>
<groupId>com.github.kristofa</groupId>
<artifactId>brave-impl-spring</artifactId>
<artifactId>brave-core-spring</artifactId>
<version>2.4.1</version>
</dependency>


The brave-impl-spring module has Spring dependency injection configuration classes for the
The brave-core-spring module has Spring dependency injection configuration classes for the
brave-impl api objects (Java based container configuration).

It does not use XML configuration but Java based container configuration using annotations.
Expand Down Expand Up @@ -60,6 +60,3 @@ public class WebConfig extends WebMvcConfigurerAdapter {

}
```

If you use `RestTemplate` to communicate between your services, you can add the `BraveClientHttpRequestInterceptor`
to automatically add the headers to outgoing requests. To add the interceptor, use the the `RestTemplate.setInterceptors` method.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

5 changes: 5 additions & 0 deletions brave-spring-resttemplate-interceptors/README.md
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.
72 changes: 72 additions & 0 deletions brave-spring-resttemplate-interceptors/pom.xml
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>
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;
}
}
Loading

0 comments on commit 7fd469a

Please sign in to comment.