-
Notifications
You must be signed in to change notification settings - Fork 550
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 per-context debugging configuration #597
Conversation
As an api user, I think debug and the debug writer should be handled separately, both for setting and retrieving them. I see how it is convenient in your code the way you implemented the templating, but I think having them separate should still be fairly simple. For naming, this would be my vote for function signatures. I feel like
Thanks for tackling this! |
Unfortunately, DebugWriter is already the name of the global variable that goes with DebugMode. I had been going for two sets like you have above, but realized I couldn't name things consistently and merged them. WithDebug is certainly a better name, I just took the suggested one. Having two is probably fine, but would mean double context value lookups, and my thought was that they'd be looked up together in every case. |
I've split things apart and renamed as suggested. |
Why not create a wrapping database driver for debugging purposes? |
That would work for some cases, but it's definitely not as flexible. Doing it via the context means logging can be done per-query, rather than being forced to log everything that goes through the database connection. It's easy for me to throw in a Doing a driver wrap like that can definitely make some sense if you're using OpenTracing/OpenCensus (and I actually do that already, just not in testing), but for quick debugging, it's pretty heavy handed. For me personally, I have a custom scripting language for my project that my units test run so I don't have to write as much code. I have a directive |
My main issue with this feature would be the performance implication of doing a full context value lookup on every query, one that is almost certainly going to fail every time for "normal" scenarios (as debugging won't be disabled). But, sqlboiler already has context values like |
@zikaeroh I know that you're using both of the values internally together, but I don't think users are going to necessarily want to set both of them together. We would use the debug setting per query, but probably not the writer. There may also be use cases where people want to set the writer but not debug settings. In the scope of the roundtrip query, I'm doubtful that context lookups are going to have a meaningful impact on total latency. Like you said, if you see issues because you have a deep context tree, there's a reasonable workaround for it. |
@derekperkins I think you might have missed it, but I did update the PR to split things apart as you suggested, after I picked a name that didn't conflict for the writer. 😃 |
Thanks, sorry I missed it! |
Make sense, thanks for taking the time to clarify. Perhaps a rationale on the commit and adding a note in the README explaining the use case and why it's per-query would definitly help. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that if we have a debug.go
file, the DebugMode and DebugWriter declarations should also belong in it so the debugging pieces are not strewn about.
A note on context lookups that have been brought up: This is fine, if people are okay with leaving context on (there's a way to turn it off) then they're prepared for context to be used. Obviously we should be cautious and only add what's necessary, but as this is a feature we wanted we'll do the lookups where necessary.
Originally the proposal did not cover a DebugWriter. Is there a use case where this is useful? Would you ever actually log two statements to different writers? What are the costs to adding this feature? Have we considered it?
This "request changes" review is mainly about moving the debugging declarations into the appropriate flie, though I do expect there to be some reasoning or thought behind why we're adding the ctx DebugWriter given that was not previously agreed upon.
Thanks for your work so far @zikaeroh it's looking good.
Thanks for taking a look. I'll make those changes later tonight.
It'd be incredibly useful for me. I use a "real" database in my unit tests, spawned using Since all of the tests are running in the same process, if I want to use SQLBoiler's debugging features I have to go out of my way to avoid breaking things. I need to first disable test parallelism, then if I want the output to actually go to the If I can set the DebugWriter once for the test, everything else sorts itself out, because I can just do In terms of "costs", I don't see this as being any more costly than the original feature request of letting |
While we're discussing logging, I have been thinking separately about proposing a change away from a writer to a context logging func.
By having the context passed back to my logging function, I can use all my context vars, request/trace ids, etc for log correlation, which is lost when using the writer. |
@zikaeroh Understood, thanks for the reply. Seems like it's a well thought out use case to me and because there's no lookup until you actually enter the DebugMode if statement I don't think it matters if it's there or not (given that debugging doesn't need to be fast). @derekperkins It's possible in v4 we can update the logging mechanism in it's entirety. There's an issue to stop splitting writes that I'd like to fix finally. That has always been a bad idea. It's possible we can send context through as part of the logging interface. It does belong in a separate issue though, feel free to make one. |
…go into a new file
The debugging globals have been moved to |
FWIW this will probably need more testing before it gets merged. As I said, I've had some trouble trying to run all of them as it seems like they require all of the different databases running locally. For example, I'm testing it on my own project and am getting:
So some of these don't run with the same template contexts... |
@zikaeroh It's untrue that you need all databases to run them. They don't really run with different contexts, that error you're getting seems to be that you're inside a control structure (like a loop or something) in the template. In which case you should be using |
I'm feeling pretty dumb about testing this.
Since I don't have any of these databases installed or running. And
Which to me implies that I'm supposed to be running this somewhere other than the I have at least been able to generate the models for my project (so psql), and point it to my fork, and things appear to work, but that doesn't feel all too comprehensive. This looks great:
|
I ensured that the normal tests passed for this. |
Thanks for the PR :) |
Fixes #569.
This is my attempt at adding per-context debugging as per #569 in a fully backwards-compatible manner. In addition to the boolean, I also added control over the writer used for the debugging, which I think makes sense to go with overriding the other global. This is especially useful for testing, where a writer that goes to
testing.T.Logf
can be used (for example). I use something like:(Even though it's hacky due to the intermediary
io.Writer
...)The naming of
andContextDebug
IsDebug
WithDebug
,IsDebug
,WithDebugWriter
, andDebugWriterFrom
are certainly not set in stone and I'd happily improve them.I had to modify templates in order to make this change and tried to follow the instructions to update generated files as best I could, but I'm having trouble testing things to make sure this works. I'm also unsure about the performance penalty of checking for a context key on each query, but I doubt that it's significant compared to the query itself.