Skip to content

Conversation

smacker
Copy link
Contributor

@smacker smacker commented Dec 27, 2016

To solve #1281 I came up with this solution...

@jinzhu
Copy link
Member

jinzhu commented Jan 2, 2017

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.

@jinzhu jinzhu closed this Jan 2, 2017
@smacker
Copy link
Contributor Author

smacker commented Jan 2, 2017

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.

@smacker
Copy link
Contributor Author

smacker commented Jan 2, 2017

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

@jinzhu jinzhu reopened this Jan 3, 2017
scope.go Outdated

// trace print sql log
func (scope *Scope) trace(t time.Time) {
scope.Set("gorm:trace-time", t)
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@ipadavic
Copy link

I'm trying to add sql tracing to my app via datadog apm, this PR makes it possible. Any chance this could be merged?

@zardak
Copy link
Contributor

zardak commented Jan 10, 2017

@jinzhu could you take a look at the pr please?

@jinzhu
Copy link
Member

jinzhu commented Jan 15, 2017

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 RawQuery callback, and it will be call in Row, Rows, Count... Refer https://github.com/jinzhu/gorm/blob/master/callback.go#L78

cc @ipadavic @zardak

@jinzhu jinzhu closed this Jan 15, 2017
@smacker
Copy link
Contributor Author

smacker commented Jan 15, 2017

@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.

@jinzhu
Copy link
Member

jinzhu commented Jan 15, 2017

Hi @smacker

Thank you for your report, just fixed that problem. jinzhu@c62e9bc

@smacker
Copy link
Contributor Author

smacker commented Jan 15, 2017

@jinzhu your commit broke the code of anybody who altered the query in row callbacks. (your new callback registered before any existed one). It was the issue that I mentioned in my original question #1281 JFK

@jinzhu
Copy link
Member

jinzhu commented Jan 16, 2017

Yes, you are right, just pushed another commit to fix the behaviour with a warning message jinzhu@7fb9b62

Thank you for the report.

@ipadavic
Copy link

Sorry for commenting on closed PR, but I'm having trouble getting this to work with Raw(). Does "row_query" callback include Raw queries?

@jinzhu
Copy link
Member

jinzhu commented Jan 22, 2017

No, Raw query is really raw, no callback included.

Only Row, Rows, Count, Plack... those methods include the callback.

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.

4 participants