-
-
Notifications
You must be signed in to change notification settings - Fork 426
/
SentrySpanClientHttpRequestInterceptor.java
54 lines (48 loc) · 1.85 KB
/
SentrySpanClientHttpRequestInterceptor.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
package io.sentry.spring.tracing;
import com.jakewharton.nopen.annotation.Open;
import io.sentry.IHub;
import io.sentry.ISpan;
import io.sentry.SentryTraceHeader;
import io.sentry.SpanStatus;
import io.sentry.util.Objects;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
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 final @NotNull IHub hub;
public SentrySpanClientHttpRequestInterceptor(final @NotNull IHub hub) {
this.hub = Objects.requireNonNull(hub, "hub is required");
}
@Override
public @NotNull ClientHttpResponse intercept(
@NotNull HttpRequest request,
@NotNull byte[] body,
@NotNull ClientHttpRequestExecution execution)
throws IOException {
final ISpan activeSpan = hub.getSpan();
if (activeSpan == null) {
return execution.execute(request, body);
}
final ISpan span = activeSpan.startChild("http.client");
span.setDescription(request.getMethodValue() + " " + request.getURI());
final SentryTraceHeader sentryTraceHeader = span.toSentryTrace();
request.getHeaders().add(sentryTraceHeader.getName(), sentryTraceHeader.getValue());
try {
final ClientHttpResponse response = execution.execute(request, body);
// handles both success and error responses
span.setStatus(SpanStatus.fromHttpStatusCode(response.getRawStatusCode()));
return response;
} catch (Exception e) {
// handles cases like connection errors
span.setThrowable(e);
span.setStatus(SpanStatus.INTERNAL_ERROR);
throw e;
} finally {
span.finish();
}
}
}