From f6a7fca08319018bd0cd703473c96c19e18bed24 Mon Sep 17 00:00:00 2001 From: Stephan Renatus Date: Wed, 16 Jul 2025 13:21:59 +0200 Subject: [PATCH] rego: pass along TracingOpts into EvalContext Signed-off-by: Stephan Renatus --- v1/rego/plugins_test.go | 42 +++++++++++++++++++++++++++++++++++++++++ v1/rego/rego.go | 1 + 2 files changed, 43 insertions(+) diff --git a/v1/rego/plugins_test.go b/v1/rego/plugins_test.go index a2a3c871e9..6dcb101244 100644 --- a/v1/rego/plugins_test.go +++ b/v1/rego/plugins_test.go @@ -6,6 +6,7 @@ package rego import ( "context" + "fmt" "testing" "github.com/google/go-cmp/cmp" @@ -14,6 +15,7 @@ import ( "github.com/open-policy-agent/opa/v1/ast" "github.com/open-policy-agent/opa/v1/ir" "github.com/open-policy-agent/opa/v1/topdown" + "github.com/open-policy-agent/opa/v1/tracing" "github.com/open-policy-agent/opa/v1/types" ) @@ -172,6 +174,46 @@ func TestPluginPrepareOptions(t *testing.T) { }) } +// Warning(philipc): This test modifies package variables, which means it cannot +// be run safely in parallel with other tests. +func TestDistributedTracingOptsOnEvalContext(t *testing.T) { + tp := testPluginDT{} + RegisterPlugin("rego.target.foo_dt", &tp) + t.Cleanup(resetPlugins) + r := New( + Query("input"), + Target("foo_dt"), + Runtime(ast.StringTerm("runtime")), + DistributedTracingOpts(tracing.NewOptions("hey")), + ) + assertEval(t, r, `[[{"x":"hey"}]]`) +} + +type testPluginDT struct{} + +func (*testPluginDT) IsTarget(t string) bool { + return t == "foo_dt" +} + +func (t *testPluginDT) PrepareForEval(context.Context, *ir.Policy, ...PrepareOption) (TargetPluginEval, error) { + return t, nil +} + +func (*testPluginDT) Eval(_ context.Context, ectx *EvalContext, _ ast.Value) (ast.Value, error) { + if l := len(ectx.TracingOpts()); l != 1 { + return nil, fmt.Errorf("expected ectx.TracingOpts of len 1, got %d", l) + } + return ast.NewSet(ast.NewTerm(ast.NewObject( + [2]*ast.Term{ + ast.StringTerm("^term1"), + ast.ObjectTerm( + [2]*ast.Term{ + ast.StringTerm("x"), + ast.StringTerm(fmt.Sprintf("%v", ectx.TracingOpts()[0])), + }), + }))), nil +} + func resetPlugins() { targetPlugins = map[string]TargetPlugin{} } diff --git a/v1/rego/rego.go b/v1/rego/rego.go index 1e703703ee..b6bc4aa799 100644 --- a/v1/rego/rego.go +++ b/v1/rego/rego.go @@ -449,6 +449,7 @@ func (pq preparedQuery) newEvalContext(ctx context.Context, options []EvalOption printHook: pq.r.printHook, capabilities: pq.r.capabilities, strictBuiltinErrors: pq.r.strictBuiltinErrors, + tracing: pq.r.distributedTracingOpts, } for _, o := range options {