Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk: Allow use of a query tracer #5447

Merged
merged 3 commits into from
Dec 7, 2022
Merged

sdk: Allow use of a query tracer #5447

merged 3 commits into from
Dec 7, 2022

Conversation

charlieegan3
Copy link
Contributor

This PR exposes functionality to the callers of opa.Decision & opa.Partial allowing a tracer implementing topdown.QueryTracer to be passed.

Related to #5176

Signed-off-by: Charlie Egan charlieegan3@users.noreply.github.com

@netlify
Copy link

netlify bot commented Dec 6, 2022

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit a738dfcc5539c4b015c8a1e7c4577edf2acac1c0
🔍 Latest deploy log https://app.netlify.com/sites/openpolicyagent/deploys/638f6b3cc3d8b10007925374
😎 Deploy Preview https://deploy-preview-5447--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@@ -501,6 +507,7 @@ func evaluate(ctx context.Context, args evalArgs) (interface{}, ast.Value, map[s
rego.EvalMetrics(args.m),
rego.EvalInterQueryBuiltinCache(args.interQueryCache),
rego.EvalNDBuiltinCache(args.ndbcache),
rego.EvalQueryTracer(args.tracer),
Copy link
Contributor Author

@charlieegan3 charlieegan3 Dec 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had tried to set this on the rego built above but newEvalContext clears the queryTracers:

https://github.com/open-policy-agent/opa/blob/a738dfcc5539c4b015c8a1e7c4577edf2acac1c0/rego/rego.go#L325
https://github.com/charlieegan3/opa/blob/a738dfcc5539c4b015c8a1e7c4577edf2acac1c0/rego/rego.go#L325

I think that this must be correct, but confirmation appreciated :)

sdk/opa_test.go Outdated
}`,
allow {
erroring_function(1)
}`,
Copy link
Contributor Author

@charlieegan3 charlieegan3 Dec 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some adjustments to match rest of file indentation, missed in #5438

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I'd have gone the other way, removing the spacing before package example. But it's fine either way, and I don't believe we've got a convention.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄 me too, but I just wanted to match what was already there in the rest of the file. I can unindent them all since that does seem to be more correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

815d5b75 updates the rego examples in the sdk tests to use this convention.

sdk/opa_test.go Outdated
t.Errorf("expected Enter event, got %v", events[1].Op)
}
if events[2].Op != "Note" {
t.Errorf("expected Enter event, got %v", events[2].Op)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe that I could make these tests look nicer with testify assert. I see we have this elsewhere, but it's not used here in this file. Wdyt? I could reformat the other tests here for consistency too if you'd like.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So historically, we've refrained from using anything else but pure golang for our tests; maybe with a bit of reflect.DeepEqual when it's convenient.

Any use of testify you find should be limited to "manually vendored" (i.e. copied) third-party code.

A pattern I personally like is this

if exp, act := "Note", events[2].Op; exp != act {
    t.Errorf("event 2, expected %v, got %v", exp, act)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I can switch it to look more like that where appropriate.

Any use of testify you find should be limited to "manually vendored"

Ahh, I can see that now! Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

b7270d26 alters the new tests to this format.

srenatus
srenatus previously approved these changes Dec 7, 2022
Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Some comments on the tests, but since our tests are a hotchpotch of different styles, it's OK to keep them as-is.

I'm trying to remember the rationale for not using a testing lib; but I think it's mostly been us trying to keep it as simple as possible. 🤔

sdk/opa_test.go Outdated
}`,
allow {
erroring_function(1)
}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I'd have gone the other way, removing the spacing before package example. But it's fine either way, and I don't believe we've got a convention.

sdk/opa_test.go Outdated
t.Errorf("expected Enter event, got %v", events[1].Op)
}
if events[2].Op != "Note" {
t.Errorf("expected Enter event, got %v", events[2].Op)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So historically, we've refrained from using anything else but pure golang for our tests; maybe with a bit of reflect.DeepEqual when it's convenient.

Any use of testify you find should be limited to "manually vendored" (i.e. copied) third-party code.

A pattern I personally like is this

if exp, act := "Note", events[2].Op; exp != act {
    t.Errorf("event 2, expected %v, got %v", exp, act)
}

@netlify
Copy link

netlify bot commented Dec 7, 2022

Deploy Preview for openpolicyagent ready!

Name Link
🔨 Latest commit 5ca27d4
🔍 Latest deploy log https://app.netlify.com/sites/openpolicyagent/deploys/63909fab78328e00088d7418
😎 Deploy Preview https://deploy-preview-5447--openpolicyagent.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

srenatus
srenatus previously approved these changes Dec 7, 2022
Copy link
Contributor

@srenatus srenatus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

This PR exposes functionality to the callers of opa.Decision &
opa.Partial allowing a tracer implementing topdown.QueryTracer to be
passed.

Signed-off-by: Charlie Egan <charlieegan3@users.noreply.github.com>
This is intended to make them easier to read and formatted more
consistently.

Signed-off-by: Charlie Egan <charlieegan3@users.noreply.github.com>
Refactored tests based on comment here:
#5447 (comment)

Signed-off-by: Charlie Egan <charlieegan3@users.noreply.github.com>
@charlieegan3 charlieegan3 merged commit 4669f9f into open-policy-agent:main Dec 7, 2022
@charlieegan3 charlieegan3 deleted the tracer-sdk branch December 7, 2022 14:35
charlieegan3 added a commit that referenced this pull request Mar 16, 2023
We have testify in our go.mod. This sometimes misleads contributors (including myself!) to think that we can use testify in test code. Since testify is a common testing package, many go developers default to using it.

I'm not sure if we want to merge this as we might rather leave these vendored packages untouched, but it was 10 minutes of work and I was interested to see if we can avoid issues like this in future:

* #5753 (comment)
* #5447 (comment)
* #3952 (review)

Signed-off-by: Charlie Egan <charlie@styra.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants