wy471x opened a new pull request, #7682: URL: https://github.com/apache/shenyu/pull/7682
### Summary The upstream call of an http proxy now continues the trace of the gateway span, so that one trace covers the client, the gateway and the upstream service instead of ending at the gateway. This is the third task of #7566. Make sure that: - [X] You have read the [contribution guidelines](https://shenyu.apache.org/community/contributor-guide). - [X] You submit test cases (unit or integration tests) that back your changes. - [X] Your local test passed `./mvnw clean install -Dmaven.javadoc.skip=true`. Run here for the touched modules and their dependencies: `mvn -pl shenyu-plugin/shenyu-plugin-api,shenyu-plugin/shenyu-plugin-tracing,shenyu-plugin/shenyu-plugin-httpclient -am install` with checkstyle, RAT and tests enabled (BUILD SUCCESS). The full repository build was not run locally, CI covers it. ### Changes: 1. `TraceContextPropagator` and `TraceContextCarrier` (new, `shenyu-plugin/shenyu-plugin-api/.../tracing/TraceContextPropagator.java:52`) — the extension point lives in the plugin contract module, so that a protocol plugin and the tracing implementation do not have to know each other: the http client plugin asks for the propagator registered under `http`, while the tracing module provides it. The module therefore declares the `shenyu-spi` dependency. 2. `W3cTraceContextPropagator` (new, `shenyu-plugin/shenyu-plugin-tracing/.../spi/W3cTraceContextPropagator.java:51`) with its `META-INF/shenyu/org.apache.shenyu.plugin.api.tracing.TraceContextPropagator` registration — injects with the OpenTelemetry W3C propagators, so the upstream call becomes a child of the gateway span, in the trace the client started. It drops the `traceparent` and the `tracestate` of the client before injecting: the headers of the client are copied to the outgoing request, a second trace context would make the upstream pick the wrong parent, and a trace state which does not belong to the context we inject would otherwise stay on the request. The baggage is deliberately forwarded as the client sent it, because it is not trace scoped; `TracingConstants.TRACESTATE` is used for that again. 3. `AbstractHttpClientPlugin.injectTraceContext` (`shenyu-plugin/shenyu-plugin-httpclient/.../AbstractHttpClientPlugin.java:132`) — one injection point shared by both http clients, which hands the carrier of the outgoing request over, so that each client describes its own headers. 4. `WebClientPlugin` (`.../WebClientPlugin.java:91`) and `NettyHttpClientPlugin` (`.../NettyHttpClientPlugin.java:95`) — inject into the headers of the outgoing request. The injection happens on those headers rather than on the request headers which the plugin copies: `new HttpHeaders(request.getHeaders())` wraps the same underlying map instead of copying it, so writing into it throws `UnsupportedOperationException` while the request headers are read only. `HttpHeadersTraceContextCarrier` and `NettyHttpHeadersTraceContextCarrier` (new) cover the two header types. 5. `AbstractHttpClientPlugin.loadTraceContextPropagator` (`.../AbstractHttpClientPlugin.java:152,164`) — tracing is optional for a deployment, so a propagator which cannot be loaded never breaks forwarding: no propagator for the http protocol, which is the normal case without a tracing module, is only logged at debug level, while an installation which cannot be loaded, for example because a dependency of the implementation is missing, is logged at warn level. The second case raises an error while the implementation class is initialized, and it would otherwise fail the initialization of the whole plugin and take every forwarded request with it. ### Test Cases: - `W3cTraceContextPropagatorTest` (6 tests) — the injected context is the gateway span and keeps the trace of the client, the trace state and the baggage are continued, a trace the client did not sample is propagated with its decision, the trace state of the client is cleared when it is not continued, the context of the client stays untouched while tracing is not active, and the propagator is registered for the http protocol. - `AbstractHttpClientPluginTraceContextLoaderTest` (3 tests) — the registered propagator is loaded, and a tracing module which is not installed or which cannot be loaded, including the error raised by a missing dependency, yields no propagator instead of failing the plugin. - `WebClientPluginTraceContextTest` (2 tests) — the upstream receives exactly one traceparent, the injected one, while the other headers of the client are kept, and a request without a client trace context still gets one. - `NettyHttpClientPluginTraceContextTest` (2 tests) — a loopback server receives exactly one traceparent, and a resend does not accumulate the context. - The existing test suites of the three modules are unchanged and pass, including the netty http client tests which cover the read only headers of a request. ### Verification - `mvn -pl shenyu-plugin/shenyu-plugin-api,shenyu-plugin/shenyu-plugin-tracing,shenyu-plugin/shenyu-plugin-httpclient -am install` → BUILD SUCCESS, module tests: plugin-api 18, tracing 64, httpclient 58, no failures. - checkstyle 0 violations and `apache-rat:check` Unapproved 0 for the three modules. - `mvn -pl shenyu-bootstrap -am install -DskipTests` → BUILD SUCCESS, which checks that the plugins still integrate with the gateway build. ### Not covered by this change - The gRPC, Dubbo and Spring Cloud plugins have to hand their own carrier to a propagator registered for their protocol; this change provides the extension point and the http propagator only. - The AI proxy builds its own client (`WebClientAiProxyTransport`) and does not go through the http client plugin, so its outgoing calls do not carry the trace context yet. - OTLP export, sampling configuration and Java agent coexistence (#7612), and the end to end integration test and the documentation (#7617). Fixes #7616 Part of #7566 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
