-
-
Notifications
You must be signed in to change notification settings - Fork 468
Expand file tree
/
Copy pathSentrySpanClientHttpRequestInterceptor.java
More file actions
150 lines (132 loc) · 5.61 KB
/
SentrySpanClientHttpRequestInterceptor.java
File metadata and controls
150 lines (132 loc) · 5.61 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package io.sentry.spring7.tracing;
import static io.sentry.TypeCheckHint.SPRING_REQUEST_INTERCEPTOR_REQUEST;
import static io.sentry.TypeCheckHint.SPRING_REQUEST_INTERCEPTOR_REQUEST_BODY;
import static io.sentry.TypeCheckHint.SPRING_REQUEST_INTERCEPTOR_RESPONSE;
import com.jakewharton.nopen.annotation.Open;
import io.sentry.BaggageHeader;
import io.sentry.Breadcrumb;
import io.sentry.Hint;
import io.sentry.IScopes;
import io.sentry.ISpan;
import io.sentry.SpanDataConvention;
import io.sentry.SpanOptions;
import io.sentry.SpanStatus;
import io.sentry.W3CTraceparentHeader;
import io.sentry.util.Objects;
import io.sentry.util.SpanUtils;
import io.sentry.util.TracingUtils;
import io.sentry.util.UrlUtils;
import java.io.IOException;
import java.util.Locale;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;
@Open
public class SentrySpanClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
private static final String TRACE_ORIGIN_REST_TEMPLATE = "auto.http.spring7.resttemplate";
private static final String TRACE_ORIGIN_REST_CLIENT = "auto.http.spring7.restclient";
private final @NotNull IScopes scopes;
private final @NotNull String traceOrigin;
public SentrySpanClientHttpRequestInterceptor(final @NotNull IScopes scopes) {
this(scopes, true);
}
public SentrySpanClientHttpRequestInterceptor(
final @NotNull IScopes scopes, final @NotNull boolean isRestTemplate) {
this.scopes = Objects.requireNonNull(scopes, "Scopes are required");
this.traceOrigin = isRestTemplate ? TRACE_ORIGIN_REST_TEMPLATE : TRACE_ORIGIN_REST_CLIENT;
}
@Override
public @NotNull ClientHttpResponse intercept(
@NotNull HttpRequest request,
@NotNull byte[] body,
@NotNull ClientHttpRequestExecution execution)
throws IOException {
Integer responseStatusCode = null;
ClientHttpResponse response = null;
try {
final ISpan activeSpan = scopes.getSpan();
if (activeSpan == null) {
maybeAddTracingHeaders(request, null);
return execution.execute(request, body);
}
final @NotNull SpanOptions spanOptions = new SpanOptions();
spanOptions.setOrigin(traceOrigin);
final ISpan span = activeSpan.startChild("http.client", null, spanOptions);
final String methodName =
request.getMethod() != null ? request.getMethod().name() : "unknown";
final @NotNull UrlUtils.UrlDetails urlDetails = UrlUtils.parse(request.getURI().toString());
span.setDescription(methodName + " " + urlDetails.getUrlOrFallback());
span.setData(SpanDataConvention.HTTP_METHOD_KEY, methodName.toUpperCase(Locale.ROOT));
urlDetails.applyToSpan(span);
maybeAddTracingHeaders(request, span);
try {
response = execution.execute(request, body);
// handles both success and error responses
span.setData(SpanDataConvention.HTTP_STATUS_CODE_KEY, response.getStatusCode().value());
span.setStatus(SpanStatus.fromHttpStatusCode(response.getStatusCode().value()));
responseStatusCode = response.getStatusCode().value();
return response;
} catch (Throwable e) {
// handles cases like connection errors
span.setThrowable(e);
span.setStatus(SpanStatus.INTERNAL_ERROR);
throw e;
} finally {
span.finish();
}
} finally {
addBreadcrumb(request, body, responseStatusCode, response);
}
}
private void maybeAddTracingHeaders(
final @NotNull HttpRequest request, final @Nullable ISpan span) {
if (isIgnored()) {
return;
}
final @Nullable TracingUtils.TracingHeaders tracingHeaders =
TracingUtils.traceIfAllowed(
scopes,
request.getURI().toString(),
request.getHeaders().get(BaggageHeader.BAGGAGE_HEADER),
span);
if (tracingHeaders != null) {
request
.getHeaders()
.add(
tracingHeaders.getSentryTraceHeader().getName(),
tracingHeaders.getSentryTraceHeader().getValue());
final @Nullable BaggageHeader baggageHeader = tracingHeaders.getBaggageHeader();
if (baggageHeader != null) {
request.getHeaders().set(baggageHeader.getName(), baggageHeader.getValue());
}
final @Nullable W3CTraceparentHeader w3cTraceparentHeader =
tracingHeaders.getW3cTraceparentHeader();
if (w3cTraceparentHeader != null) {
request.getHeaders().add(w3cTraceparentHeader.getName(), w3cTraceparentHeader.getValue());
}
}
}
private boolean isIgnored() {
return SpanUtils.isIgnored(scopes.getOptions().getIgnoredSpanOrigins(), traceOrigin);
}
private void addBreadcrumb(
final @NotNull HttpRequest request,
final @NotNull byte[] body,
final @Nullable Integer responseStatusCode,
final @Nullable ClientHttpResponse response) {
final String methodName = request.getMethod() != null ? request.getMethod().name() : "unknown";
final Breadcrumb breadcrumb =
Breadcrumb.http(request.getURI().toString(), methodName, responseStatusCode);
breadcrumb.setData("request_body_size", body.length);
final Hint hint = new Hint();
hint.set(SPRING_REQUEST_INTERCEPTOR_REQUEST, request);
hint.set(SPRING_REQUEST_INTERCEPTOR_REQUEST_BODY, body);
if (response != null) {
hint.set(SPRING_REQUEST_INTERCEPTOR_RESPONSE, response);
}
scopes.addBreadcrumb(breadcrumb, hint);
}
}