Skip to content

Commit 4d2a8de

Browse files
committed
Add peer.service to Netty connection error spans
- Modified NettyClientInstrumenterFactory.createConnectionInstrumenter() to accept @nullable PeerServiceResolver parameter - Factory creates HttpClientPeerServiceAttributesExtractor internally with the package-private NettyConnectHttpAttributesGetter - Updated Netty 4.0, 4.1, and reactor-netty javaagent singletons to pass AgentCommonConfig.get().getPeerServiceResolver() - This ensures CONNECT spans (created for connection errors) include peer.service attribute when configured via otel.instrumentation.common.peer-service-mapping
1 parent b4469d1 commit 4d2a8de

File tree

9 files changed

+59048
-1
lines changed

9 files changed

+59048
-1
lines changed

NETTY_PEER_SERVICE_TESTING.md

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Enabling peer.service Testing for Netty 4.1 HTTP Client
2+
3+
## Summary
4+
5+
Enabled `peer.service` attribute testing for both Netty 4.1 javaagent and library instrumentation, validating that the configuration-dependent peer service support works correctly.
6+
7+
## Changes Made
8+
9+
### 1. Javaagent Instrumentation
10+
11+
**File**: `instrumentation/netty/netty-4.1/javaagent/build.gradle.kts`
12+
13+
- Added system property to the test task to configure peer service mapping:
14+
15+
```kotlin
16+
test {
17+
systemProperty("otel.instrumentation.common.peer-service-mapping",
18+
"127.0.0.1=test-peer-service,localhost=test-peer-service")
19+
// ... other configuration
20+
}
21+
```
22+
23+
**File**: `instrumentation/netty/netty-4.1/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/Netty41ClientTest.java`
24+
25+
- Added `configure()` method to enable peer service testing:
26+
27+
```java
28+
@Override
29+
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
30+
super.configure(optionsBuilder);
31+
optionsBuilder.setTestPeerService(true);
32+
optionsBuilder.setExpectedPeerServiceName(uri -> "test-peer-service");
33+
}
34+
```
35+
36+
The javaagent instrumentation automatically reads the `otel.instrumentation.common.peer-service-mapping` system property and applies it via the `HttpClientPeerServiceAttributesExtractor`.
37+
38+
### 2. Library Instrumentation
39+
40+
**File**: `instrumentation/netty/netty-4.1/library/src/test/java/io/opentelemetry/instrumentation/netty/v4_1/Netty41ClientTest.java`
41+
42+
- Added `.setPeerService("test-peer-service")` to the `NettyClientTelemetry` builder:
43+
44+
```java
45+
NettyClientTelemetry.builder(testing.getOpenTelemetry())
46+
.setCapturedRequestHeaders(...)
47+
.setCapturedResponseHeaders(...)
48+
.setPeerService("test-peer-service") // <-- Added
49+
.build()
50+
```
51+
52+
- Added `configure()` method to enable peer service testing in the test options
53+
54+
### 3. NettyClientTelemetryBuilder Enhancement
55+
56+
**File**: `instrumentation/netty/netty-4.1/library/src/main/java/io/opentelemetry/instrumentation/netty/v4_1/NettyClientTelemetryBuilder.java`
57+
58+
- Added `setPeerService(String)` method to expose peer service configuration:
59+
60+
```java
61+
@CanIgnoreReturnValue
62+
public NettyClientTelemetryBuilder setPeerService(String peerService) {
63+
builder.setPeerService(peerService);
64+
return this;
65+
}
66+
```
67+
68+
This delegates to the underlying `DefaultHttpClientInstrumenterBuilder.setPeerService()` method.
69+
70+
## Test Behavior
71+
72+
### What Gets Tested
73+
74+
The new `requestWithPeerServiceConfiguration()` test method (from `AbstractHttpClientTest`) will now run for Netty 4.1 and verify:
75+
76+
1. **Javaagent**: The `peer.service` attribute is emitted with value `"test-peer-service"` when requests are made to `localhost` or `127.0.0.1`
77+
2. **Library**: The `peer.service` attribute is always emitted with value `"test-peer-service"` for all requests
78+
79+
### Configuration Differences
80+
81+
- **Javaagent**: Uses system property `otel.instrumentation.common.peer-service-mapping` with host-based mapping
82+
- Mapping format: `host[:port]=service-name`
83+
- Example: `127.0.0.1=test-peer-service,localhost=test-peer-service`
84+
85+
- **Library**: Uses programmatic configuration via builder API
86+
- Direct constant value: `setPeerService("test-peer-service")`
87+
- Applied to all HTTP requests made with that instrumentation instance
88+
89+
## Validation
90+
91+
Both tests will now:
92+
93+
1. Make HTTP requests to the test server
94+
2. Assert that the `peer.service` attribute is present in client spans
95+
3. Verify the attribute value matches `"test-peer-service"`
96+
97+
This validates the resolution of [issue #10361](https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/10361) - the peer.service support exists and works when properly configured.
98+
99+
## Running the Tests
100+
101+
```bash
102+
# Run Netty 4.1 javaagent tests
103+
./gradlew :instrumentation:netty:netty-4.1:javaagent:test
104+
105+
# Run Netty 4.1 library tests
106+
./gradlew :instrumentation:netty:netty-4.1:library:test
107+
108+
# Run specific test class
109+
./gradlew :instrumentation:netty:netty-4.1:javaagent:test --tests Netty41ClientTest
110+
./gradlew :instrumentation:netty:netty-4.1:library:test --tests Netty41ClientTest
111+
```

OKHTTP_PEER_SERVICE_TESTING.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Enabling peer.service Testing for OkHttp 3.0 HTTP Client
2+
3+
## Summary
4+
5+
Enabled `peer.service` attribute testing for both OkHttp 3.0 javaagent and library instrumentation, validating that the configuration-dependent peer service support works correctly.
6+
7+
## Changes Made
8+
9+
### 1. Javaagent Instrumentation
10+
11+
**File**: `instrumentation/okhttp/okhttp-3.0/javaagent/build.gradle.kts`
12+
13+
- Added system property to the test task to configure peer service mapping:
14+
15+
```kotlin
16+
test {
17+
systemProperty("collectMetadata", findProperty("collectMetadata")?.toString() ?: "false")
18+
systemProperty(
19+
"otel.instrumentation.common.peer-service-mapping",
20+
"127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service"
21+
)
22+
}
23+
```
24+
25+
**File**: `instrumentation/okhttp/okhttp-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/okhttp/v3_0/OkHttp3Test.java`
26+
27+
- Added `configure()` method to enable peer service testing:
28+
29+
```java
30+
@Override
31+
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
32+
super.configure(optionsBuilder);
33+
optionsBuilder.setTestPeerService(true);
34+
optionsBuilder.setExpectedPeerServiceName(uri -> "test-peer-service");
35+
}
36+
```
37+
38+
The javaagent instrumentation automatically reads the `otel.instrumentation.common.peer-service-mapping` system property and applies it via the `HttpClientPeerServiceAttributesExtractor`.
39+
40+
### 2. Library Instrumentation
41+
42+
**File**: `instrumentation/okhttp/okhttp-3.0/library/src/test/java/io/opentelemetry/instrumentation/okhttp/v3_0/OkHttp3Test.java`
43+
44+
- Added `.setPeerService("test-peer-service")` to the `OkHttpTelemetry` builder:
45+
46+
```java
47+
OkHttpTelemetry.builder(testing.getOpenTelemetry())
48+
.setCapturedRequestHeaders(...)
49+
.setCapturedResponseHeaders(...)
50+
.setPeerService("test-peer-service") // <-- Added
51+
.build()
52+
```
53+
54+
- Added `configure()` method to enable peer service testing in the test options
55+
56+
### 3. OkHttpTelemetryBuilder Enhancement
57+
58+
**File**: `instrumentation/okhttp/okhttp-3.0/library/src/main/java/io/opentelemetry/instrumentation/okhttp/v3_0/OkHttpTelemetryBuilder.java`
59+
60+
- Added `setPeerService(String)` method to expose peer service configuration:
61+
62+
```java
63+
@CanIgnoreReturnValue
64+
public OkHttpTelemetryBuilder setPeerService(String peerService) {
65+
builder.setPeerService(peerService);
66+
return this;
67+
}
68+
```
69+
70+
This delegates to the underlying `DefaultHttpClientInstrumenterBuilder.setPeerService()` method.
71+
72+
## Test Behavior
73+
74+
### What Gets Tested
75+
76+
The new `requestWithPeerServiceConfiguration()` test method (from `AbstractHttpClientTest`) will now run for OkHttp 3.0 and verify:
77+
78+
1. **Javaagent**: The `peer.service` attribute is emitted with value `"test-peer-service"` when requests are made to `localhost`, `127.0.0.1`, or `192.0.2.1`
79+
2. **Library**: The `peer.service` attribute is always emitted with value `"test-peer-service"` for all requests
80+
81+
### Configuration Differences
82+
83+
- **Javaagent**: Uses system property `otel.instrumentation.common.peer-service-mapping` with host-based mapping
84+
- Mapping format: `host[:port]=service-name`
85+
- Example: `127.0.0.1=test-peer-service,localhost=test-peer-service,192.0.2.1=test-peer-service`
86+
87+
- **Library**: Uses programmatic configuration via builder API
88+
- Direct constant value: `setPeerService("test-peer-service")`
89+
- Applied to all HTTP requests made with that instrumentation instance
90+
91+
## Implementation Pattern
92+
93+
This implementation follows the same pattern established for other HTTP client instrumentations (like Netty):
94+
95+
1. **For javaagent tests**: Configure peer service mapping via system properties in `build.gradle.kts`
96+
2. **For library tests**: Add `setPeerService()` to the telemetry builder
97+
3. **Enable testing**: Override `configure()` method in test classes to set `setTestPeerService(true)`
98+
99+
## Validation
100+
101+
Both tests will now:
102+
103+
1. Make HTTP requests to the test server
104+
2. Assert that the `peer.service` attribute is present in client spans
105+
3. Verify the attribute value matches `"test-peer-service"`
106+
107+
This validates that the peer.service support exists and works when properly configured for OkHttp instrumentation.
108+
109+
## Running the Tests
110+
111+
```bash
112+
# Run javaagent tests
113+
./gradlew :instrumentation:okhttp:okhttp-3.0:javaagent:test
114+
115+
# Run library tests
116+
./gradlew :instrumentation:okhttp:okhttp-3.0:library:test
117+
```

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ public final class ReactorNettySingletons {
5555
? NettyConnectionInstrumentationFlag.ENABLED
5656
: NettyConnectionInstrumentationFlag.DISABLED,
5757
NettyConnectionInstrumentationFlag.DISABLED);
58-
CONNECTION_INSTRUMENTER = instrumenterFactory.createConnectionInstrumenter();
58+
CONNECTION_INSTRUMENTER =
59+
instrumenterFactory.createConnectionInstrumenter(
60+
AgentCommonConfig.get().getPeerServiceResolver());
5961
}
6062

6163
public static Instrumenter<HttpClientRequest, HttpClientResponse> instrumenter() {

0 commit comments

Comments
 (0)