-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Add print function #3319
Comments
💭 Does this issue supersede #2957? |
Hrrm I'd rather have a "log output" pane in the playground, I think. Including the browser's dev tools as part of the UI doesn't seem right 😄 I suppose it could be collapsed by default, same as the data pane. |
Perhaps you are right :) As long as it's dealt with in some way! |
Hi! Since the goal here is debugging, I thought it might be relevant to share a patch for this basic builtin our team has been using for debugging Rego running on our dev/staging OPA servers: yashtewari@040285a
The style/usage is analogous to the "print statement" style that you've described. Do you think an option to enrich the decision log can be added in a future We're happy to help and contribute! |
@yashtewari welcome back!
That's an interesting idea!
I think this relates to #1514 in that we need a way for values generated during evaluation to be included in the decision log. We'll need to think about this a bit more. |
Yeah, I like the idea of being able to enrich decisions with arbitrary data and I think a |
Thanks a lot @tsandall!! And congratulations for OPA being cooler than ever 😁
You mean in terms of implementation? Would be nice to see what you guys think about how
Ah you're right, doing one thing well makes perfect sense. My thinking was that as a Thanks for your responses! |
Thinking about For the sake of progress:
The main issue I see with p {
a := 1
b := 2
a == b # if the body is evaluated in-order, this statement will FAIL and the next will never be reached
print(a, b)
} If you look at the rule body as an imperative sequence of statements (which is incorrect, but common) then you would expect to see some output because there are no conditional statements. Of course, in Rego, statements in the body of the rule are ANDed together and there is no ordering, so there is no need to execute subsequent statements once a failure is encountered. If we assume that print arguments are treated the same way as other function calls, then rewriting could make this a common occurence: p {
print(input.foo)
} Gets rewritten to: p {
_1 = input.foo
print(_1)
} If To deal with this situation, I propose we make
Given
|
Seems like we're saying we want
Do we need some awareness of print() during body reordering so that the body can be reordered between print() statements but not across print() statements? Often people will want to use print() to know the location in the rule body that succeeded during execution. E.g. what do we do with this?
|
@tsandall I totally agree on the problem of undefined values, and really like the idea of letting |
Example above is one where reordering will happen. We can't compute This is likely a corner-case, as I think more about it, because most people will write rules sensibly so they do not require reordering. Though if there are automated tools generating Rego code this could happen. Conceptually it would be fine if we ignored this case for now; however, if we later want to add restrictions that say reject rules like the one above then it would be a backwards incompatible change. Easier to relax restrictions than to add them. |
Ah, gotcha! Completely missed the dependency there. At least to me it seems fine to just print in the order of evaluation in this case. |
@timothyhinrichs this is interesting and it's not what I had in mind... I think there are two options:
If we go with (1) then p {
input.x == input.y
printf("blah blah blah (z=%v)", input.z) # if input.z is undefined, p is undefined
} Using A) It gives users a quick-and-dirty alternative to inspect values during policy evaluation. Users don't have to care where the B) The implementation is quite simple because (i) arguments can be rewritten the same way as other calls (ii) there's no special reordering requirement for safety (iii) the evaluator just has to evaluate Any thoughts on this alternative option? |
Now I understand the original proposal. Love the idea of being able to ignore print()s semantically for prod. I suspected I was missing something. I'd guess that most users will be surprised that Maybe the higher level bit is that over time we've added features that make reordering the rule body more surprising: |
Fixed by #3868 |
One of the most asked questions on the OPA Slack is how to print values to console. While there are many, often better ways, of debugging Rego policies, the quick feedback loop and the zero ceremony offered by a
print
/println
function is sometimes just the right tool for the job. Since such a function exists in most regular programming languages this is a development/debugging style adopted by many, and naturally they'll want to bring that to OPA and Rego. We should let them!While there is some support for doing this via the
trace
built-in already, using it simply for printing values is kind of a hack, and having to learn all the peculiarities like required flags and output format around it as someone who is just learning Rego isn't a great introduction to the language or its tooling.Some things to consider:
print
vsprintln
,echo
, others? 😄print("test", x, 1, y)
.trace
.print
calls toconsole.log
.Related: #2957
The text was updated successfully, but these errors were encountered: