-
Notifications
You must be signed in to change notification settings - Fork 867
/
HttpClientTracer.java
187 lines (156 loc) · 6.37 KB
/
HttpClientTracer.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.opentelemetry.instrumentation.api.tracer;
import static io.opentelemetry.context.ContextUtils.withScopedContext;
import static io.opentelemetry.trace.TracingContextUtils.withSpan;
import io.grpc.Context;
import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.context.Scope;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.context.propagation.TextMapPropagator.Setter;
import io.opentelemetry.instrumentation.api.MoreAttributes;
import io.opentelemetry.instrumentation.api.config.Config;
import io.opentelemetry.instrumentation.api.decorator.HttpStatusConverter;
import io.opentelemetry.instrumentation.api.tracer.utils.HttpUrlUtils;
import io.opentelemetry.instrumentation.api.tracer.utils.NetPeerUtils;
import io.opentelemetry.trace.DefaultSpan;
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.Span.Kind;
import io.opentelemetry.trace.Tracer;
import io.opentelemetry.trace.TracingContextUtils;
import io.opentelemetry.trace.attributes.SemanticAttributes;
import java.net.URI;
import java.net.URISyntaxException;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class HttpClientTracer<REQUEST, CARRIER, RESPONSE> extends BaseTracer {
private static final Logger log = LoggerFactory.getLogger(HttpClientTracer.class);
public static final String DEFAULT_SPAN_NAME = "HTTP request";
protected static final String USER_AGENT = "User-Agent";
protected abstract String method(REQUEST request);
@Nullable
protected abstract URI url(REQUEST request) throws URISyntaxException;
protected abstract Integer status(RESPONSE response);
@Nullable
protected abstract String requestHeader(REQUEST request, String name);
@Nullable
protected abstract String responseHeader(RESPONSE response, String name);
protected abstract TextMapPropagator.Setter<CARRIER> getSetter();
protected HttpClientTracer() {
super();
}
protected HttpClientTracer(Tracer tracer) {
super(tracer);
}
public Span startSpan(REQUEST request) {
return startSpan(request, -1);
}
public Span startSpan(REQUEST request, long startTimeNanos) {
return startSpan(request, spanNameForRequest(request), startTimeNanos);
}
public Scope startScope(Span span, CARRIER carrier) {
Context context = withSpan(span, Context.current());
Setter<CARRIER> setter = getSetter();
if (setter == null) {
throw new IllegalStateException(
"getSetter() not defined but calling startScope(), either getSetter must be implemented or the scope should be setup manually");
}
OpenTelemetry.getPropagators().getTextMapPropagator().inject(context, carrier, setter);
context = context.withValue(CONTEXT_CLIENT_SPAN_KEY, span);
return withScopedContext(context);
}
public void end(Span span, RESPONSE response) {
end(span, response, -1);
}
public void end(Span span, RESPONSE response, long endTimeNanos) {
onResponse(span, response);
super.end(span, endTimeNanos);
}
public void endExceptionally(Span span, RESPONSE response, Throwable throwable) {
endExceptionally(span, response, throwable, -1);
}
public void endExceptionally(
Span span, RESPONSE response, Throwable throwable, long endTimeNanos) {
onResponse(span, response);
super.endExceptionally(span, throwable, endTimeNanos);
}
/**
* Returns a new client {@link Span} if there is no client {@link Span} in the current {@link
* Context}, or an invalid {@link Span} otherwise.
*/
private Span startSpan(REQUEST request, String name, long startTimeNanos) {
Context context = Context.current();
Span clientSpan = CONTEXT_CLIENT_SPAN_KEY.get(context);
if (clientSpan != null) {
// We don't want to create two client spans for a given client call, suppress inner spans.
return DefaultSpan.getInvalid();
}
Span current = TracingContextUtils.getSpan(context);
Span.Builder spanBuilder = tracer.spanBuilder(name).setSpanKind(Kind.CLIENT).setParent(current);
if (startTimeNanos > 0) {
spanBuilder.setStartTimestamp(startTimeNanos);
}
Span span = spanBuilder.startSpan();
onRequest(span, request);
return span;
}
protected Span onRequest(Span span, REQUEST request) {
assert span != null;
if (request != null) {
span.setAttribute(SemanticAttributes.HTTP_METHOD.key(), method(request));
String userAgent = requestHeader(request, USER_AGENT);
if (userAgent != null) {
SemanticAttributes.HTTP_USER_AGENT.set(span, userAgent);
}
try {
URI url = url(request);
if (url != null && url.getHost() != null) {
NetPeerUtils.setNetPeer(span, url.getHost(), null);
if (url.getPort() > 0) {
span.setAttribute(SemanticAttributes.NET_PEER_PORT.key(), url.getPort());
}
}
HttpUrlUtils.setHttpUrl(span, url);
if (Config.get().isHttpClientTagQueryString()) {
span.setAttribute(MoreAttributes.HTTP_QUERY, url.getQuery());
span.setAttribute(MoreAttributes.HTTP_FRAGMENT, url.getFragment());
}
} catch (Exception e) {
log.debug("Error tagging url", e);
}
}
return span;
}
protected Span onResponse(Span span, RESPONSE response) {
assert span != null;
if (response != null) {
Integer status = status(response);
if (status != null) {
span.setAttribute(SemanticAttributes.HTTP_STATUS_CODE.key(), status);
span.setStatus(HttpStatusConverter.statusFromHttpStatus(status));
}
}
return span;
}
protected String spanNameForRequest(REQUEST request) {
if (request == null) {
return DEFAULT_SPAN_NAME;
}
String method = method(request);
return method != null ? "HTTP " + method : DEFAULT_SPAN_NAME;
}
}