-
Notifications
You must be signed in to change notification settings - Fork 713
/
ClientRequestInterceptor.java
94 lines (86 loc) · 4.2 KB
/
ClientRequestInterceptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package com.github.kristofa.brave.client;
import org.apache.commons.lang.Validate;
import com.github.kristofa.brave.ClientTracer;
import com.github.kristofa.brave.SpanId;
import com.github.kristofa.brave.client.spanfilter.SpanNameFilter;
/**
* Intercepts a client request and takes care of tracing the request. It will decide if we need to trace current request and
* if so CS annotation will be submitted. You will also have to use {@link ClientResponseInterceptor} which will set CR
* annotation.
* <p/>
* Abstraction on top of {@link ClientTracer}. If you want to integrate a library with brave it is advised to use this
* abstraction instead of ClientTracer directly.
* </p>
* You will have to implement your own ClientRequestAdapter to provide necessary context.
*
* @see ClientRequestAdapter
* @see ClientResponseInterceptor
*/
public class ClientRequestInterceptor {
private final ClientTracer clientTracer;
private static final String REQUEST_ANNOTATION = "request";
private final SpanNameFilter spanNameFilter;
public ClientRequestInterceptor(final ClientTracer clientTracer, final SpanNameFilter spanNameFilter) {
Validate.notNull(clientTracer);
this.clientTracer = clientTracer;
this.spanNameFilter = spanNameFilter;
}
/**
* Handles a client request.
*
* @param clientRequestAdapter Provides context about the request.
* @param serviceNameOverride Nullable name of the service the client request calls. In case it is not specified the name
* will be derived from the URI of the request. It is important the used service name should be same on client
* as on server side.
*/
public void handle(final ClientRequestAdapter clientRequestAdapter, final String serviceNameOverride) {
final String spanName = getSpanName(clientRequestAdapter, serviceNameOverride);
final SpanId newSpanId = clientTracer.startNewSpan(spanName);
ClientRequestHeaders.addTracingHeaders(clientRequestAdapter, newSpanId, spanName);
final String serviceName = getServiceName(clientRequestAdapter, serviceNameOverride);
if (serviceName != null) {
clientTracer.setCurrentClientServiceName(serviceName);
}
clientTracer.submitBinaryAnnotation(REQUEST_ANNOTATION, clientRequestAdapter.getMethod() + " "
+ clientRequestAdapter.getUri());
clientTracer.setClientSent();
}
private String getServiceName(final ClientRequestAdapter clientRequestAdapter, final String serviceNameOverride) {
String serviceName = null;
if (serviceNameOverride != null) {
serviceName = serviceNameOverride;
} else {
final String path = clientRequestAdapter.getUri().getPath();
final String[] split = path.split("/");
if (split.length > 2 && path.startsWith("/")) {
// If path starts with '/', then context is between first two '/'. Left over is path for service.
final int contextPathSeparatorIndex = path.indexOf("/", 1);
serviceName = path.substring(1, contextPathSeparatorIndex);
}
}
return serviceName;
}
private String getSpanName(final ClientRequestAdapter clientRequestAdapter, final String serviceNameOverride) {
String spanName;
final String spanNameFromRequest = clientRequestAdapter.getSpanName();
final String path = clientRequestAdapter.getUri().getPath();
if (spanNameFromRequest != null) {
return spanNameFromRequest;
} else if (serviceNameOverride != null) {
spanName = path;
} else {
final String[] split = path.split("/");
if (split.length > 2 && path.startsWith("/")) {
// If path starts with '/', then context is between first two '/'. Left over is path for service.
final int contextPathSeparatorIndex = path.indexOf("/", 1);
spanName = path.substring(contextPathSeparatorIndex);
} else {
spanName = path;
}
}
if (spanNameFilter != null) {
spanName = spanNameFilter.filterSpanName(spanName);
}
return spanName;
}
}