Skip to content

Commit d64eda3

Browse files
authored
Add network peer address attributes to the span for the okhttp-3.0 instrumentation (#12012)
1 parent 6700efd commit d64eda3

File tree

8 files changed

+61
-37
lines changed

8 files changed

+61
-37
lines changed

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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 6 additions & 4 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 37 additions & 16 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 5 additions & 5 deletions
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

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,9 @@ SpanDataAssert assertClientSpan(
999999
// TODO: Move to test knob rather than always treating as optional
10001000
if (attrs.get(NetworkAttributes.NETWORK_PEER_ADDRESS) != null) {
10011001
assertThat(attrs)
1002-
.containsEntry(NetworkAttributes.NETWORK_PEER_ADDRESS, "127.0.0.1");
1002+
.hasEntrySatisfying(
1003+
NetworkAttributes.NETWORK_PEER_ADDRESS,
1004+
addr -> assertThat(addr).isIn("127.0.0.1", "0:0:0:0:0:0:0:1"));
10031005
}
10041006
if (attrs.get(NetworkAttributes.NETWORK_PEER_PORT) != null) {
10051007
assertThat(attrs)

0 commit comments

Comments
 (0)