wy471x opened a new pull request, #7619:
URL: https://github.com/apache/shenyu/pull/7619

   ### Summary
   
   Adds vendor neutral end-to-end tracing for the gateway: a request routed 
through ShenYu now carries one trace which starts at the client, contains the 
gateway span, and is propagated to the upstream service. Tracing is implemented 
as a plugin plus a Spring Boot starter, depends on the OpenTelemetry API only, 
and stays disabled until `shenyu.tracing.enabled=true`.
   
   This is the first 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-tracing,shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-tracing
 -am install` with checkstyle, RAT and tests enabled (BUILD SUCCESS). The full 
repository build was not run locally, CI covers it.
   
   ### Changes:
   
   1. `TracingPlugin` 
(`shenyu-plugin/shenyu-plugin-tracing/.../TracingPlugin.java:77,102`) — new 
plugin which extracts the inbound context, starts a `SERVER` span, and ends it 
once the request completes, fails or is cancelled. The span is created inside 
`Mono.defer`, so it belongs to a subscription and is never ended twice; the 
remaining chain is still assembled eagerly like every other ShenYu plugin.
   2. `TraceContextExtractor` (`.../extract/TraceContextExtractor.java:55,89`) 
— extracts the W3C `traceparent`/`tracestate` and baggage with the propagators 
from the OpenTelemetry API. An absent or malformed context falls back to the 
root context, so the gateway starts a new trace instead of failing the request. 
A repeated `traceparent` is rejected because it is a trace context injection 
vector, while repeated `tracestate`/`baggage` headers are combined as the 
specification requires.
   3. `ReactorTracingContext` 
(`.../context/ReactorTracingContext.java:51,61,75`) — carries the active 
context in the exchange attributes, which every plugin of the request shares 
and which are not bound to a thread, and exposes it from the Reactor context so 
it survives a thread switch. No thread local is used: `ShenyuWebHandler` may 
run the chain on a scheduler thread.
   4. `TracingConstants` (`.../TracingConstants.java`) — instrumentation scope 
name, the span name that is used when no `ShenyuContext` is available yet, the 
context keys and the `service.name` resource attribute.
   5. `PluginEnum.TRACING` (`shenyu-common/.../enums/PluginEnum.java:66`) — new 
entry with order 0, so the span is opened right after `global` and covers all 
later plugins. No row is added to the `plugin` table, the plugin is configured 
locally like `metrics`.
   6. `Constants.SHENYU_AGENT_TRACE_ID` 
(`shenyu-common/.../constant/Constants.java:882`) — the trace id is now written 
through the exchange attribute which the logging plugins already read, which 
fixes the currently dangling contract. 
`GenericLoggingConstant.SHENYU_AGENT_TRACE_ID` 
(`.../GenericLoggingConstant.java:40`) references that constant instead of 
repeating the literal, so the existing public constant and its value are 
unchanged.
   7. `ShenyuConfig.TracingConfig` 
(`shenyu-common/.../config/ShenyuConfig.java:68,157,1972`) — 
`shenyu.tracing.enabled` and `shenyu.tracing.service-name`, following the 
existing `MetricsConfig` pattern.
   8. `TracingPluginConfiguration` 
(`shenyu-spring-boot-starter-plugin-tracing/.../TracingPluginConfiguration.java:60,74,88,99`)
 — owns the OpenTelemetry SDK, gated by 
`@ConditionalOnProperty("shenyu.tracing.enabled")`, mirroring the metrics 
starter. Every bean is resolved by its explicit name, so an application with 
its own OpenTelemetry beans keeps working, and the global OpenTelemetry is 
deliberately not replaced.
   9. `shenyu-bootstrap/pom.xml:190` and `application.yml:310` — the starter is 
on the gateway classpath and tracing is off by default.
   10. `shenyu-dist/shenyu-bootstrap-dist/src/main/release-docs/LICENSE` — 
registers the OpenTelemetry runtime artifacts which the bootstrap distribution 
now contains (the SDK and its modules, plus `opentelemetry-api-incubator`), 
together with the two artifacts that were listed before.
   
   ### Test Cases:
   
   - `TracingPluginTest` (18 tests) — an inbound `traceparent` is continued 
with the correct remote parent, a request without a context or with a malformed 
one starts a new trace, the trace id is exposed through 
`shenyu-agent-trace-id`, a parent which is not sampled is honoured without 
recording a span, the span ends on success, on an error signal, on cancellation 
and never twice, there is exactly one span per subscription, no span is created 
when the chain fails while it is assembled, a no-op tracer keeps forwarding, 
and the context survives a `boundedElastic` thread switch.
   - `TraceContextExtractorTest` (18 tests) — valid, not sampled and reserved 
trace flags, `tracestate` and baggage extraction, repeated `tracestate`/baggage 
combined, repeated `traceparent` rejected, and a table of malformed contexts 
that must not be trusted as a parent.
   - `TracingPluginConfigurationTest` (7 tests) — disabled by default and by an 
explicit `false`, the beans when enabled, the configured service name, spans 
having a usable trace id even though the exporter is added by a follow-up task, 
and an application which already provides its own `Tracer` and 
`SdkTracerProvider` beans.
   
   ### Verification
   
   - `mvn -pl 
shenyu-plugin/shenyu-plugin-tracing,shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-tracing
 -am install` → BUILD SUCCESS, 43 new tests green, checkstyle 0 violations, RAT 
0 unapproved files.
   - `mvn -pl shenyu-bootstrap -am install -DskipTests` → BUILD SUCCESS, which 
checks that the new starter integrates with the gateway build.
   - Not covered by this change: outbound context injection (#7616), span 
status, exception recording and normalized attributes (#7613), OTLP export, 
sampling and Java agent coexistence (#7612), the integration test and 
documentation (#7617). The access log correlation is already active because the 
trace id is written to the exchange attribute the logging plugins read.
   
   Fixes #7615
   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]

Reply via email to