rego: pass along TracingOpts into EvalContext

Signed-off-by: Stephan Renatus <stephan@styra.com>
This commit is contained in:
Stephan Renatus
2025-07-16 13:21:59 +02:00
parent 84778e203d
commit f6a7fca083
2 changed files with 43 additions and 0 deletions
+42
View File
@@ -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{}
}
+1
View File
@@ -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 {