-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Callback for tracing sql queries #1289
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
Conversation
Hi @smacker Thank you for your PR, sorry just noticed your issue report, I think you could just make it works with set a new logger, so this might be not necessary. Refer: https://github.com/jinzhu/gorm/blob/master/logger.go Thank you. |
No, it won't work with a logger. I have to start newrelic transaction before sql started execution and finish it after. It measures not only time, but call stack, how much cpu it burn and other things. |
please reconsider trace callback. Code instrumentation is very useful. If you don't like callback, can you suggest any other way to do it? For example go-redis provides WrapProcess method: redis/go-redis#352 |
scope.go
Outdated
|
||
// trace print sql log | ||
func (scope *Scope) trace(t time.Time) { | ||
scope.Set("gorm:trace-time", t) |
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.
IMO, this is a sign of hacky approach. The more or less right way would be to introduce lifecycle hooks, like BeforeX
and AfterX
callbacks.
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.
agreed. I'll try to improve it.
I'm trying to add sql tracing to my app via datadog apm, this PR makes it possible. Any chance this could be merged? |
@jinzhu could you take a look at the pr please? |
Hi @smacker Sorry for the delay, just have time to review it and your code in https://github.com/smacker/newrelic-context/blob/master/gorm.go I think for your problem you could resolve it with |
@jinzhu it was my first intention to use raw callback, but you call it before sql.DB methods, there is no way to know when it finish. For Query, Create, Delete and Update you call sql.DB inside callbacks, so I can add my callback before and after real query. But for RawQuery you do sql query inside scope, not callback. I also tried to change Row, Rows, Count, ... methods and move sql related code from scope to inside callbacks, but code becomes quite messy. |
Hi @smacker Thank you for your report, just fixed that problem. jinzhu@c62e9bc |
Yes, you are right, just pushed another commit to fix the behaviour with a warning message jinzhu@7fb9b62 Thank you for the report. |
Sorry for commenting on closed PR, but I'm having trouble getting this to work with Raw(). Does "row_query" callback include Raw queries? |
No, Only |
To solve #1281 I came up with this solution...