Skip to content

Commit c7617dc

Browse files
authored
Rework reactor netty context tracking (#9286)
1 parent 6ba2f49 commit c7617dc

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

instrumentation-api-semconv/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST
113113
if (SemconvStability.emitOldHttpSemconv()) {
114114
internalSet(attributes, SemanticAttributes.HTTP_URL, fullUrl);
115115
}
116+
117+
int resendCount = resendCountIncrementer.applyAsInt(parentContext);
118+
if (resendCount > 0) {
119+
attributes.put(SemanticAttributes.HTTP_RESEND_COUNT, resendCount);
120+
}
116121
}
117122

118123
@Override
@@ -127,11 +132,6 @@ public void onEnd(
127132
internalNetExtractor.onEnd(attributes, request, response);
128133
internalNetworkExtractor.onEnd(attributes, request, response);
129134
internalServerExtractor.onEnd(attributes, request, response);
130-
131-
int resendCount = resendCountIncrementer.applyAsInt(context);
132-
if (resendCount > 0) {
133-
attributes.put(SemanticAttributes.HTTP_RESEND_COUNT, resendCount);
134-
}
135135
}
136136

137137
/**

instrumentation-api-semconv/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ void normal() {
148148
AttributeKey.stringArrayKey("http.request.header.custom_request_header"),
149149
asList("123", "456")),
150150
entry(SemanticAttributes.NET_PEER_NAME, "github.com"),
151-
entry(SemanticAttributes.NET_PEER_PORT, 123L));
151+
entry(SemanticAttributes.NET_PEER_PORT, 123L),
152+
entry(SemanticAttributes.HTTP_RESEND_COUNT, 2L));
152153

153154
AttributesBuilder endAttributes = Attributes.builder();
154155
extractor.onEnd(endAttributes, Context.root(), request, response, null);
@@ -157,7 +158,6 @@ void normal() {
157158
entry(SemanticAttributes.HTTP_REQUEST_CONTENT_LENGTH, 10L),
158159
entry(SemanticAttributes.HTTP_STATUS_CODE, 202L),
159160
entry(SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH, 20L),
160-
entry(SemanticAttributes.HTTP_RESEND_COUNT, 2L),
161161
entry(
162162
AttributeKey.stringArrayKey("http.response.header.custom_response_header"),
163163
asList("654", "321")),

instrumentation-api-semconv/src/testBothHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractorBothSemconvTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ void normal() {
145145
entry(SemanticAttributes.NET_PEER_NAME, "github.com"),
146146
entry(SemanticAttributes.NET_PEER_PORT, 123L),
147147
entry(SemanticAttributes.SERVER_ADDRESS, "github.com"),
148-
entry(SemanticAttributes.SERVER_PORT, 123L));
148+
entry(SemanticAttributes.SERVER_PORT, 123L),
149+
entry(SemanticAttributes.HTTP_RESEND_COUNT, 2L));
149150

150151
AttributesBuilder endAttributes = Attributes.builder();
151152
extractor.onEnd(endAttributes, Context.root(), request, response, null);
@@ -157,7 +158,6 @@ void normal() {
157158
entry(SemanticAttributes.HTTP_RESPONSE_STATUS_CODE, 202L),
158159
entry(SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH, 20L),
159160
entry(SemanticAttributes.HTTP_RESPONSE_BODY_SIZE, 20L),
160-
entry(SemanticAttributes.HTTP_RESEND_COUNT, 2L),
161161
entry(
162162
AttributeKey.stringArrayKey("http.response.header.custom_response_header"),
163163
asList("654", "321")),

instrumentation-api-semconv/src/testStableHttpSemconv/java/io/opentelemetry/instrumentation/api/instrumenter/http/HttpClientAttributesExtractorStableSemconvTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ void normal() {
152152
AttributeKey.stringArrayKey("http.request.header.custom_request_header"),
153153
asList("123", "456")),
154154
entry(SemanticAttributes.SERVER_ADDRESS, "github.com"),
155-
entry(SemanticAttributes.SERVER_PORT, 123L));
155+
entry(SemanticAttributes.SERVER_PORT, 123L),
156+
entry(SemanticAttributes.HTTP_RESEND_COUNT, 2L));
156157

157158
AttributesBuilder endAttributes = Attributes.builder();
158159
extractor.onEnd(endAttributes, Context.root(), request, response, null);
@@ -161,7 +162,6 @@ void normal() {
161162
entry(SemanticAttributes.HTTP_REQUEST_BODY_SIZE, 10L),
162163
entry(SemanticAttributes.HTTP_RESPONSE_STATUS_CODE, 202L),
163164
entry(SemanticAttributes.HTTP_RESPONSE_BODY_SIZE, 20L),
164-
entry(SemanticAttributes.HTTP_RESEND_COUNT, 2L),
165165
entry(
166166
AttributeKey.stringArrayKey("http.response.header.custom_response_header"),
167167
asList("654", "321")),

instrumentation/reactor/reactor-netty/reactor-netty-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v1_0/InstrumentationContexts.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientResendCount;
1212
import io.opentelemetry.instrumentation.api.internal.InstrumenterUtil;
1313
import io.opentelemetry.instrumentation.api.internal.Timer;
14+
import io.opentelemetry.instrumentation.api.util.VirtualField;
1415
import java.util.Queue;
1516
import java.util.concurrent.LinkedBlockingQueue;
1617
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
@@ -19,6 +20,8 @@
1920
import reactor.netty.http.client.HttpClientResponse;
2021

2122
final class InstrumentationContexts {
23+
private static final VirtualField<HttpClientRequest, Context> requestContextVirtualField =
24+
VirtualField.find(HttpClientRequest.class, Context.class);
2225

2326
private static final AtomicReferenceFieldUpdater<InstrumentationContexts, Context>
2427
parentContextUpdater =
@@ -56,18 +59,27 @@ Context startClientSpan(HttpClientRequest request) {
5659
Context context = null;
5760
if (instrumenter().shouldStart(parentContext, request)) {
5861
context = instrumenter().start(parentContext, request);
62+
requestContextVirtualField.set(request, context);
5963
clientContexts.offer(new RequestAndContext(request, context));
6064
}
6165
return context;
6266
}
6367

64-
// we are synchronizing here to ensure that spans are ended in the oder they are read from the
65-
// queue
66-
synchronized void endClientSpan(
67-
@Nullable HttpClientResponse response, @Nullable Throwable error) {
68+
void endClientSpan(@Nullable HttpClientResponse response, @Nullable Throwable error) {
69+
HttpClientRequest request = null;
70+
Context context = null;
6871
RequestAndContext requestAndContext = clientContexts.poll();
69-
if (requestAndContext != null) {
70-
instrumenter().end(requestAndContext.context, requestAndContext.request, response, error);
72+
if (response instanceof HttpClientRequest) {
73+
request = (HttpClientRequest) response;
74+
context = requestContextVirtualField.get(request);
75+
} else if (requestAndContext != null) {
76+
// this branch is taken when there was an error (e.g. timeout) and response was null
77+
request = requestAndContext.request;
78+
context = requestAndContext.context;
79+
}
80+
81+
if (request != null && context != null) {
82+
instrumenter().end(context, request, response, error);
7183
}
7284
}
7385

0 commit comments

Comments
 (0)