Skip to content

Commit 903030f

Browse files
authored
Add peer service mapping to example (#1159)
1 parent c39eee8 commit 903030f

5 files changed

Lines changed: 83 additions & 3 deletions

File tree

javaagent-declarative-configuration/README.md

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ This example demonstrates how to use [declarative configuration](https://opentel
44

55
The configuration file is located at [otel-agent-config.yaml](./otel-agent-config.yaml).
66

7-
This Spring Boot application includes two endpoints:
7+
This Spring Boot application includes three endpoints:
88

99
- `/actuator/health` - A health check endpoint (from Spring Boot Actuator) that is configured to be excluded from tracing
1010
- `/api/example` - A simple API endpoint that will be traced normally
11+
- `/api/remote` - An endpoint that makes an outgoing HTTP (client) call, used to demonstrate peer service mapping
1112

1213
## End-to-End Instructions
1314

@@ -37,7 +38,7 @@ curl -L -o opentelemetry-javaagent.jar https://github.com/open-telemetry/opentel
3738

3839
# Run with the OpenTelemetry Java Agent and contrib extension
3940
java -javaagent:opentelemetry-javaagent.jar \
40-
-Dotel.experimental.config.file=$(pwd)/otel-agent-config.yaml \
41+
-Dotel.config.file=$(pwd)/otel-agent-config.yaml \
4142
-jar build/libs/javaagent-declarative-configuration.jar
4243
```
4344

@@ -51,6 +52,9 @@ curl http://localhost:8080/actuator/health
5152

5253
# This endpoint WILL be traced normally
5354
curl http://localhost:8080/api/example
55+
56+
# This endpoint makes an outgoing client call; its client span is tagged with peer.service
57+
curl http://localhost:8080/api/remote
5458
```
5559

5660
### Step 4: Verify Tracing Behavior
@@ -83,3 +87,25 @@ This configuration:
8387
- Excludes health check endpoints (`/actuator.*`) from tracing using the `DROP` action
8488
- Samples all other requests using the `always_on` fallback sampler
8589
- Only applies to `SERVER` span kinds
90+
91+
### Peer service mapping
92+
93+
The `otel-agent-config.yaml` file also demonstrates peer service mapping, which maps the peer address (host name or IP)
94+
of an outgoing client call to a logical service name:
95+
96+
```yaml
97+
instrumentation/development:
98+
java:
99+
common:
100+
service_peer_mapping:
101+
- peer: localhost
102+
service_name: example-backend
103+
```
104+
105+
This configuration:
106+
107+
- Replaces the `otel.instrumentation.common.peer-service-mapping` system property
108+
- Adds a `peer.service` attribute to client spans whose peer address (`server.address`) matches a configured `peer`.
109+
The port is optional — omitting it matches any port.
110+
- In this example, the `/api/remote` endpoint calls back into the application over `localhost`, so its client span is
111+
tagged with `peer.service=example-backend`

javaagent-declarative-configuration/oats/docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ services:
77
environment:
88
OTEL_SERVICE_NAME: "declarative-config-example-app"
99
OTEL_EXPORTER_OTLP_ENDPOINT: http://lgtm:4318
10+
OTEL_CONFIG_FILE: /usr/src/app/otel-agent-config.yaml
1011
ports:
1112
- "8080:8080"
1213
healthcheck:

javaagent-declarative-configuration/oats/oats.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ input:
1010
- path: /api/example
1111
# This endpoint should NOT be traced (excluded by declarative config)
1212
- path: /actuator/health
13+
# This endpoint makes an outgoing client call, exercising the peer service mapping
14+
- path: /api/remote
1315

1416
expected:
1517
traces:
@@ -22,3 +24,8 @@ expected:
2224
- traceql: '{ span.http.route = "/actuator/health" }'
2325
count:
2426
max: 0
27+
# Verify the outgoing client span is tagged with peer.service via the
28+
# instrumentation.general.peer.service_mapping declarative config
29+
- traceql: '{ span.peer.service = "example-backend" }'
30+
count:
31+
min: 1

javaagent-declarative-configuration/otel-agent-config.yaml

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ resource:
1010
# Read resource attributes from the OTEL_RESOURCE_ATTRIBUTES environment variable.
1111
# This aligns well with the OpenTelemetry Operator and other deployment methods.
1212
attributes_list: ${OTEL_RESOURCE_ATTRIBUTES}
13-
detection/development: # /development properties may not be supported in all SDKs
13+
detection/development:
1414
detectors:
1515
- service: # will add "service.instance.id" and "service.name" from the OTEL_SERVICE_NAME env var
1616
- host:
@@ -22,6 +22,34 @@ propagator:
2222
- tracecontext:
2323
- baggage:
2424

25+
instrumentation/development:
26+
java:
27+
common:
28+
# Map peer addresses (host names or IP addresses) of outgoing client calls
29+
# to a logical service name. Matching client spans get a "peer.service"
30+
# attribute set to the configured service_name (port is optional; omitting
31+
# it matches any port). Previously configured via the
32+
# otel.instrumentation.common.peer-service-mapping property.
33+
service_peer_mapping:
34+
- peer: localhost
35+
service_name: example-backend
36+
general:
37+
http:
38+
client:
39+
request_captured_headers: # was otel.instrumentation.http.client.capture-request-headers
40+
- Content-Type
41+
- Accept
42+
response_captured_headers: # was otel.instrumentation.http.client.capture-response-headers
43+
- Content-Type
44+
- Content-Encoding
45+
server:
46+
request_captured_headers: # was otel.instrumentation.http.server.capture-request-headers
47+
- Content-Type
48+
- Accept
49+
response_captured_headers: # was otel.instrumentation.http.server.capture-response-headers
50+
- Content-Type
51+
- Content-Encoding
52+
2553
# Read backend endpoint from the OTEL_EXPORTER_OTLP_ENDPOINT environment variable.
2654
# This aligns well with the OpenTelemetry Operator and other deployment methods.
2755

javaagent-declarative-configuration/src/main/java/io/opentelemetry/examples/fileconfig/ApiController.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,35 @@
55

66
package io.opentelemetry.examples.fileconfig;
77

8+
import org.springframework.beans.factory.annotation.Value;
89
import org.springframework.http.ResponseEntity;
910
import org.springframework.web.bind.annotation.GetMapping;
1011
import org.springframework.web.bind.annotation.RequestMapping;
1112
import org.springframework.web.bind.annotation.RestController;
13+
import org.springframework.web.client.RestClient;
1214

1315
@RestController
1416
@RequestMapping("/api")
1517
public class ApiController {
1618

19+
private final RestClient restClient = RestClient.create();
20+
private final String remoteTargetUrl;
21+
22+
public ApiController(
23+
@Value("${remote.target.url:http://localhost:8080/api/example}") String remoteTargetUrl) {
24+
this.remoteTargetUrl = remoteTargetUrl;
25+
}
26+
1727
@GetMapping("/example")
1828
public ResponseEntity<String> example() {
1929
return ResponseEntity.ok("Hello from OpenTelemetry example API!");
2030
}
31+
32+
// Makes an outgoing HTTP (CLIENT) call so the peer service mapping in
33+
// otel-agent-config.yaml can tag the client span with a "peer.service" attribute.
34+
@GetMapping("/remote")
35+
public ResponseEntity<String> remote() {
36+
String body = restClient.get().uri(remoteTargetUrl).retrieve().body(String.class);
37+
return ResponseEntity.ok("Remote call returned: " + body);
38+
}
2139
}

0 commit comments

Comments
 (0)