Skip to content

Commit 4589148

Browse files
committed
Add network peer address attributes to the span for the okhttp-3.0 instrumentation
1 parent 23a110e commit 4589148

File tree

9 files changed

+77
-38
lines changed

9 files changed

+77
-38
lines changed

instrumentation/okhttp/okhttp-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/okhttp/v3_0/OkHttp3Singletons.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
import io.opentelemetry.instrumentation.okhttp.v3_0.internal.TracingInterceptor;
1616
import io.opentelemetry.javaagent.bootstrap.internal.JavaagentHttpClientInstrumenters;
1717
import okhttp3.Interceptor;
18-
import okhttp3.Request;
1918
import okhttp3.Response;
2019

2120
/** Holder of singleton interceptors for adding to instrumented clients. */
2221
public final class OkHttp3Singletons {
2322

24-
private static final Instrumenter<Request, Response> INSTRUMENTER =
23+
private static final Instrumenter<Interceptor.Chain, Response> INSTRUMENTER =
2524
JavaagentHttpClientInstrumenters.create(
2625
OkHttpClientInstrumenterBuilderFactory.create(GlobalOpenTelemetry.get()));
2726

instrumentation/okhttp/okhttp-3.0/library/src/main/java/io/opentelemetry/instrumentation/okhttp/v3_0/OkHttpTelemetry.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import okhttp3.Callback;
1515
import okhttp3.Interceptor;
1616
import okhttp3.OkHttpClient;
17-
import okhttp3.Request;
1817
import okhttp3.Response;
1918

2019
/** Entrypoint for instrumenting OkHttp clients. */
@@ -32,10 +31,11 @@ public static OkHttpTelemetryBuilder builder(OpenTelemetry openTelemetry) {
3231
return new OkHttpTelemetryBuilder(openTelemetry);
3332
}
3433

35-
private final Instrumenter<Request, Response> instrumenter;
34+
private final Instrumenter<Interceptor.Chain, Response> instrumenter;
3635
private final ContextPropagators propagators;
3736

38-
OkHttpTelemetry(Instrumenter<Request, Response> instrumenter, ContextPropagators propagators) {
37+
OkHttpTelemetry(
38+
Instrumenter<Interceptor.Chain, Response> instrumenter, ContextPropagators propagators) {
3939
this.instrumenter = instrumenter;
4040
this.propagators = propagators;
4141
}

instrumentation/okhttp/okhttp-3.0/library/src/main/java/io/opentelemetry/instrumentation/okhttp/v3_0/OkHttpTelemetryBuilder.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
import java.util.List;
1616
import java.util.Set;
1717
import java.util.function.Function;
18-
import okhttp3.Request;
18+
import okhttp3.Interceptor;
1919
import okhttp3.Response;
2020

2121
/** A builder of {@link OkHttpTelemetry}. */
2222
public final class OkHttpTelemetryBuilder {
2323

24-
private final DefaultHttpClientInstrumenterBuilder<Request, Response> builder;
24+
private final DefaultHttpClientInstrumenterBuilder<Interceptor.Chain, Response> builder;
2525

2626
OkHttpTelemetryBuilder(OpenTelemetry openTelemetry) {
2727
builder = OkHttpClientInstrumenterBuilderFactory.create(openTelemetry);
@@ -33,7 +33,7 @@ public final class OkHttpTelemetryBuilder {
3333
*/
3434
@CanIgnoreReturnValue
3535
public OkHttpTelemetryBuilder addAttributeExtractor(
36-
AttributesExtractor<? super Request, ? super Response> attributesExtractor) {
36+
AttributesExtractor<? super Interceptor.Chain, ? super Response> attributesExtractor) {
3737
builder.addAttributeExtractor(attributesExtractor);
3838
return this;
3939
}
@@ -95,7 +95,9 @@ public OkHttpTelemetryBuilder setEmitExperimentalHttpClientMetrics(
9595
/** Sets custom {@link SpanNameExtractor} via transform function. */
9696
@CanIgnoreReturnValue
9797
public OkHttpTelemetryBuilder setSpanNameExtractor(
98-
Function<SpanNameExtractor<? super Request>, ? extends SpanNameExtractor<? super Request>>
98+
Function<
99+
SpanNameExtractor<? super Interceptor.Chain>,
100+
? extends SpanNameExtractor<? super Interceptor.Chain>>
99101
spanNameExtractorTransformer) {
100102
builder.setSpanNameExtractor(spanNameExtractorTransformer);
101103
return this;

instrumentation/okhttp/okhttp-3.0/library/src/main/java/io/opentelemetry/instrumentation/okhttp/v3_0/internal/ConnectionErrorSpanInterceptor.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@
2121
*/
2222
public final class ConnectionErrorSpanInterceptor implements Interceptor {
2323

24-
private final Instrumenter<Request, Response> instrumenter;
24+
private final Instrumenter<Chain, Response> instrumenter;
2525

26-
public ConnectionErrorSpanInterceptor(Instrumenter<Request, Response> instrumenter) {
26+
public ConnectionErrorSpanInterceptor(Instrumenter<Chain, Response> instrumenter) {
2727
this.instrumenter = instrumenter;
2828
}
2929

@@ -43,9 +43,9 @@ public Response intercept(Chain chain) throws IOException {
4343
} finally {
4444
// only create a span when there wasn't any HTTP request
4545
if (HttpClientRequestResendCount.get(parentContext) == 0) {
46-
if (instrumenter.shouldStart(parentContext, request)) {
46+
if (instrumenter.shouldStart(parentContext, chain)) {
4747
InstrumenterUtil.startAndEnd(
48-
instrumenter, parentContext, request, response, error, startTime, Instant.now());
48+
instrumenter, parentContext, chain, response, error, startTime, Instant.now());
4949
}
5050
}
5151
}

instrumentation/okhttp/okhttp-3.0/library/src/main/java/io/opentelemetry/instrumentation/okhttp/v3_0/internal/OkHttpAttributesGetter.java

+37-16
Original file line numberDiff line numberDiff line change
@@ -6,47 +6,52 @@
66
package io.opentelemetry.instrumentation.okhttp.v3_0.internal;
77

88
import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesGetter;
9+
import java.net.InetSocketAddress;
10+
import java.net.SocketAddress;
911
import java.util.List;
1012
import javax.annotation.Nullable;
11-
import okhttp3.Request;
13+
import okhttp3.Connection;
14+
import okhttp3.Interceptor;
1215
import okhttp3.Response;
1316

1417
/**
1518
* This class is internal and is hence not for public use. Its APIs are unstable and can change at
1619
* any time.
1720
*/
18-
public enum OkHttpAttributesGetter implements HttpClientAttributesGetter<Request, Response> {
21+
public enum OkHttpAttributesGetter
22+
implements HttpClientAttributesGetter<Interceptor.Chain, Response> {
1923
INSTANCE;
2024

2125
@Override
22-
public String getHttpRequestMethod(Request request) {
23-
return request.method();
26+
public String getHttpRequestMethod(Interceptor.Chain chain) {
27+
return chain.request().method();
2428
}
2529

2630
@Override
27-
public String getUrlFull(Request request) {
28-
return request.url().toString();
31+
public String getUrlFull(Interceptor.Chain chain) {
32+
return chain.request().url().toString();
2933
}
3034

3135
@Override
32-
public List<String> getHttpRequestHeader(Request request, String name) {
33-
return request.headers(name);
36+
public List<String> getHttpRequestHeader(Interceptor.Chain chain, String name) {
37+
return chain.request().headers(name);
3438
}
3539

3640
@Override
3741
public Integer getHttpResponseStatusCode(
38-
Request request, Response response, @Nullable Throwable error) {
42+
Interceptor.Chain chain, Response response, @Nullable Throwable error) {
3943
return response.code();
4044
}
4145

4246
@Override
43-
public List<String> getHttpResponseHeader(Request request, Response response, String name) {
47+
public List<String> getHttpResponseHeader(
48+
Interceptor.Chain chain, Response response, String name) {
4449
return response.headers(name);
4550
}
4651

4752
@Nullable
4853
@Override
49-
public String getNetworkProtocolName(Request request, @Nullable Response response) {
54+
public String getNetworkProtocolName(Interceptor.Chain chain, @Nullable Response response) {
5055
if (response == null) {
5156
return null;
5257
}
@@ -67,7 +72,7 @@ public String getNetworkProtocolName(Request request, @Nullable Response respons
6772

6873
@Nullable
6974
@Override
70-
public String getNetworkProtocolVersion(Request request, @Nullable Response response) {
75+
public String getNetworkProtocolVersion(Interceptor.Chain chain, @Nullable Response response) {
7176
if (response == null) {
7277
return null;
7378
}
@@ -90,12 +95,28 @@ public String getNetworkProtocolVersion(Request request, @Nullable Response resp
9095

9196
@Override
9297
@Nullable
93-
public String getServerAddress(Request request) {
94-
return request.url().host();
98+
public String getServerAddress(Interceptor.Chain chain) {
99+
return chain.request().url().host();
95100
}
96101

97102
@Override
98-
public Integer getServerPort(Request request) {
99-
return request.url().port();
103+
public Integer getServerPort(Interceptor.Chain chain) {
104+
return chain.request().url().port();
105+
}
106+
107+
@Nullable
108+
@Override
109+
public InetSocketAddress getNetworkPeerInetSocketAddress(
110+
Interceptor.Chain chain, @Nullable Response response) {
111+
Connection connection = chain.connection();
112+
if (connection == null) {
113+
return null;
114+
}
115+
SocketAddress socketAddress = connection.socket().getRemoteSocketAddress();
116+
if (socketAddress instanceof InetSocketAddress) {
117+
return (InetSocketAddress) socketAddress;
118+
} else {
119+
return null;
120+
}
100121
}
101122
}

instrumentation/okhttp/okhttp-3.0/library/src/main/java/io/opentelemetry/instrumentation/okhttp/v3_0/internal/OkHttpClientInstrumenterBuilderFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import io.opentelemetry.api.OpenTelemetry;
99
import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpClientInstrumenterBuilder;
10-
import okhttp3.Request;
10+
import okhttp3.Interceptor;
1111
import okhttp3.Response;
1212

1313
/**
@@ -19,7 +19,7 @@ public class OkHttpClientInstrumenterBuilderFactory {
1919

2020
private OkHttpClientInstrumenterBuilderFactory() {}
2121

22-
public static DefaultHttpClientInstrumenterBuilder<Request, Response> create(
22+
public static DefaultHttpClientInstrumenterBuilder<Interceptor.Chain, Response> create(
2323
OpenTelemetry openTelemetry) {
2424
return new DefaultHttpClientInstrumenterBuilder<>(
2525
INSTRUMENTATION_NAME, openTelemetry, OkHttpAttributesGetter.INSTANCE);

instrumentation/okhttp/okhttp-3.0/library/src/main/java/io/opentelemetry/instrumentation/okhttp/v3_0/internal/TracingInterceptor.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020
*/
2121
public final class TracingInterceptor implements Interceptor {
2222

23-
private final Instrumenter<Request, Response> instrumenter;
23+
private final Instrumenter<Chain, Response> instrumenter;
2424
private final ContextPropagators propagators;
2525

2626
public TracingInterceptor(
27-
Instrumenter<Request, Response> instrumenter, ContextPropagators propagators) {
27+
Instrumenter<Chain, Response> instrumenter, ContextPropagators propagators) {
2828
this.instrumenter = instrumenter;
2929
this.propagators = propagators;
3030
}
@@ -34,11 +34,11 @@ public Response intercept(Chain chain) throws IOException {
3434
Request request = chain.request();
3535
Context parentContext = Context.current();
3636

37-
if (!instrumenter.shouldStart(parentContext, request)) {
37+
if (!instrumenter.shouldStart(parentContext, chain)) {
3838
return chain.proceed(chain.request());
3939
}
4040

41-
Context context = instrumenter.start(parentContext, request);
41+
Context context = instrumenter.start(parentContext, chain);
4242
request = injectContextToRequest(request, context);
4343

4444
Response response = null;
@@ -50,7 +50,7 @@ public Response intercept(Chain chain) throws IOException {
5050
error = e;
5151
throw e;
5252
} finally {
53-
instrumenter.end(context, request, response, error);
53+
instrumenter.end(context, chain, response, error);
5454
}
5555
}
5656

instrumentation/okhttp/okhttp-3.0/testing/src/main/java/io/opentelemetry/instrumentation/okhttp/v3_0/AbstractOkHttp3Test.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest;
1313
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult;
1414
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
15+
import io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions;
1516
import io.opentelemetry.semconv.NetworkAttributes;
1617
import java.io.IOException;
1718
import java.net.URI;
@@ -180,7 +181,16 @@ public void onResponse(Call call, Response response) {
180181
trace -> {
181182
trace.hasSpansSatisfyingExactly(
182183
span -> span.hasName("parent").hasKind(SpanKind.INTERNAL).hasNoParent(),
183-
span -> span.hasName("GET").hasKind(SpanKind.CLIENT).hasParent(trace.getSpan(0)),
184+
span ->
185+
span.hasName("GET")
186+
.hasKind(SpanKind.CLIENT)
187+
.hasParent(trace.getSpan(0))
188+
.hasAttribute(
189+
OpenTelemetryAssertions.satisfies(
190+
NetworkAttributes.NETWORK_PEER_ADDRESS,
191+
val -> val.isIn("127.0.0.1", "0:0:0:0:0:0:0:1")))
192+
.hasAttribute(
193+
NetworkAttributes.NETWORK_PEER_PORT, (long) serverAddress().getPort()),
184194
span ->
185195
span.hasName("test-http-server")
186196
.hasKind(SpanKind.SERVER)

testing-common/src/main/java/io/opentelemetry/instrumentation/testing/junit/http/AbstractHttpClientTest.java

+8-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import io.opentelemetry.semconv.ServerAttributes;
3131
import io.opentelemetry.semconv.UrlAttributes;
3232
import io.opentelemetry.semconv.UserAgentAttributes;
33+
import java.net.InetSocketAddress;
3334
import java.net.URI;
3435
import java.time.Duration;
3536
import java.util.ArrayList;
@@ -87,6 +88,10 @@ void setupOptions() {
8788
options = builder.build();
8889
}
8990

91+
protected InetSocketAddress serverAddress() {
92+
return server.httpSocketAddress();
93+
}
94+
9095
/**
9196
* Override this method to configure the {@link HttpClientTestOptions} for the tested HTTP client.
9297
*/
@@ -999,7 +1004,9 @@ SpanDataAssert assertClientSpan(
9991004
// TODO: Move to test knob rather than always treating as optional
10001005
if (attrs.get(NetworkAttributes.NETWORK_PEER_ADDRESS) != null) {
10011006
assertThat(attrs)
1002-
.containsEntry(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1");
1007+
.hasEntrySatisfying(
1008+
NetworkAttributes.NETWORK_PEER_ADDRESS,
1009+
addr -> assertThat(addr).isIn("127.0.0.1", "0:0:0:0:0:0:0:1"));
10031010
}
10041011
if (attrs.get(NetworkAttributes.NETWORK_PEER_PORT) != null) {
10051012
assertThat(attrs)

0 commit comments

Comments
 (0)