apisix icon indicating copy to clipboard operation
apisix copied to clipboard

As a user, I want the opa plugin to propagate trace context (traceparent) to OPA, so that OPA spans are linked to the APISIX request trace

Open czegi90 opened this issue 1 month ago • 3 comments

Description

Description

When the opentelemetry plugin is enabled, the opa plugin's HTTP request to OPA does not carry W3C trace context. The request headers are hard-coded to Content-Type: application/json in apisix/plugins/opa.lua (_M.access). OPA supports continuing an incoming trace from the traceparent HTTP header, but it never receives one. As a result, OPA's spans start a separate trace instead of appearing as children of the APISIX request span.

The incoming traceparent is currently only visible to OPA inside the JSON body (input.request.headers). OPA does not use that for tracing. See open-policy-agent/opa#6905.

Proposed change

Forward the trace context header on the request to OPA, either always or behind a schema option (e.g. propagate_trace_context: true):

headers = {
    ["Content-Type"] = "application/json",
    ["traceparent"] = core.request.header(ctx, "traceparent"),
},

I tested this by overriding the built-in plugin via extra_lua_path on 3.17.0 with OPA 1.11.0 and distributed tracing enabled. The OPA decision log trace_id matches the incoming trace, and in Tempo the OPA span appears as a child of the APISIX span in the same trace, alongside the upstream service span.

Environment

  • APISIX 3.17.0
  • OPA 1.11.0 (distributed tracing via OTLP gRPC)
  • OpenTelemetry Collector, Grafana Tempo

czegi90 avatar Oct 19 '26 05:10 czegi90

Agreed, the request to OPA in apisix/plugins/opa.lua only sends Content-Type, so OPA never sees the trace context. I'd like to work on this: I'll forward the current traceparent header (the one the opentelemetry plugin injects, if enabled) on the OPA request and add a test. Could you assign it to me?

GitNimay avatar Oct 21 '26 03:10 GitNimay

could this be combined with https://github.com/apache/apisix/pull/11629

jjansenvr avatar Oct 30 '26 10:10 jjansenvr

@jjansenvr thanks for pointing it out. #11629 adds request body support (with_body, issue #11387) and is still being worked on by its author, while this change only adds the traceparent header to the OPA request, so I'd keep them as two separate PRs. That way each one can be reviewed and merged on its own.

I've updated #14059 so the two don't get in each other's way: my test file is now t/plugin/opa5.t (#11629 also adds opa4.t) and the docs note moved to a spot #11629 doesn't touch. I checked with git merge-tree, and the two PRs now merge cleanly in either order.

GitNimay avatar Oct 31 '26 04:10 GitNimay