-
Notifications
You must be signed in to change notification settings - Fork 198
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
Added support for go-pg v10 #857
Conversation
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.
Thanks @macnibblet! LGTM. Can you please run go generate
in the scripts directory? That is needed to update Dockerfile-testing.
@axw Tests are passing it Jenkins but it's reporting that it failed, not sure whats wrong here. |
module/apmgopgv10/go.mod
Outdated
@@ -0,0 +1,14 @@ | |||
module go.elastic.co/apm/module/apmgopg |
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.
module go.elastic.co/apm/module/apmgopg | |
module go.elastic.co/apm/module/apmgopgv10 |
Sorry, missed this one before! This should fix the CI failures.
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.
Ah, my bad I missed it too.
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.
Didn't seam to help
jenkins run the tests please |
Co-authored-by: Andrew Wilkins <[email protected]>
Co-authored-by: Andrew Wilkins <[email protected]>
Codecov Report
@@ Coverage Diff @@
## master #857 +/- ##
==========================================
- Coverage 83.83% 83.78% -0.05%
==========================================
Files 141 141
Lines 7849 7856 +7
==========================================
+ Hits 6580 6582 +2
- Misses 863 867 +4
- Partials 406 407 +1
Continue to review full report at Codecov.
|
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.
Now that the other CI issues are sorted, there's a real issue with the code showing up. The hooks API has changed a bit in v10, and requires some changes to hooks.go
.
If you'd like to iterate locally, you can run the integration tests like:
scripts/docker-compose-testing build go-agent-tests
scripts/docker-compose-testing run -T --rm go-agent-tests bash -c 'cd module/apmgopgv10 && go test -v'
module/apmgopgv10/hook.go
Outdated
qh := &queryHook{} | ||
switch qh := ((interface{})(qh)).(type) { | ||
case pg.QueryHook: | ||
db.AddQueryHook(qh) | ||
return nil | ||
} | ||
return errors.New("cannot instrument pg.DB, does not implement required interface") |
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.
qh := &queryHook{} | |
switch qh := ((interface{})(qh)).(type) { | |
case pg.QueryHook: | |
db.AddQueryHook(qh) | |
return nil | |
} | |
return errors.New("cannot instrument pg.DB, does not implement required interface") | |
db.AddQueryHook(&queryHook{}) | |
return nil |
IIRC, this interface approach existed because the AddQueryHook method didn't exist at the beginning of the v9 cycle. AddQueryHook exists in all versions of v10, so we should just call the method directly -- then we get static type checking back again.
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.
Makes sense, sorry for the screw-up. I was replacing the wrong path locally and still using the old version when I tested it.
module/apmgopgv10/hook.go
Outdated
type queryHook struct{} | ||
|
||
// BeforeQuery initiates the span for the database query | ||
func (qh *queryHook) BeforeQuery(evt *pg.QueryEvent) { |
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.
func (qh *queryHook) BeforeQuery(evt *pg.QueryEvent) { | |
func (qh *queryHook) BeforeQuery(ctx context.Context, evt *pg.QueryEvent) (context.Context, error) { |
Turns out that the QueryHook interface has changed in v10. Instead of using evt.Stash
we should return a context with the span, and extract the span from the context passed into AfterQuery
.
module/apmgopgv10/hook.go
Outdated
sql = []byte(fmt.Sprintf("[go-pg] error: %s", err.Error())) | ||
} | ||
|
||
span, _ := apm.StartSpan(evt.DB.Context(), apmsql.QuerySignature(string(sql)), "db.postgresql.query") |
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.
span, _ := apm.StartSpan(evt.DB.Context(), apmsql.QuerySignature(string(sql)), "db.postgresql.query") | |
span, ctx := apm.StartSpan(ctx, apmsql.QuerySignature(string(sql)), "db.postgresql.query") |
module/apmgopgv10/hook.go
Outdated
Instance: database, | ||
}) | ||
|
||
evt.Stash[elasticApmSpanKey] = span |
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.
evt.Stash[elasticApmSpanKey] = span | |
return ctx, nil |
module/apmgopgv10/hook.go
Outdated
const elasticApmSpanKey = "go-apm-agent:span" | ||
|
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.
const elasticApmSpanKey = "go-apm-agent:span" |
module/apmgopgv10/hook.go
Outdated
} | ||
|
||
// AfterQuery ends the initiated span from BeforeQuery | ||
func (qh *queryHook) AfterQuery(evt *pg.QueryEvent) { |
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.
func (qh *queryHook) AfterQuery(evt *pg.QueryEvent) { | |
func (qh *queryHook) AfterQuery(ctx context.Context, evt *pg.QueryEvent) error { |
module/apmgopgv10/hook.go
Outdated
span, ok := evt.Stash[elasticApmSpanKey] | ||
if !ok { | ||
return | ||
} | ||
if s, ok := span.(*apm.Span); ok { | ||
s.End() | ||
} |
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.
span, ok := evt.Stash[elasticApmSpanKey] | |
if !ok { | |
return | |
} | |
if s, ok := span.(*apm.Span); ok { | |
s.End() | |
} | |
if span := apm.SpanFromContext(ctx); span != nil { | |
span.End() | |
} | |
return nil |
|
||
sql, err := evt.UnformattedQuery() | ||
if err != nil { | ||
return ctx, errors.Wrap(err, "failed to generate sql") |
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.
@axw I'm not sure about this part, but I don't know what would possibly cause it to fail.
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 don't either; this looks fine to me.
func Instrument(db *pg.DB) error { | ||
db.AddQueryHook(&queryHook{}) | ||
|
||
return nil |
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.
Do we want to keep this to have the same API as the previous version?
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 so. This minimises the number of changes required to update the instrumentation when upgrading, and leaves the door open for additional checks later.
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.
Thanks for bearing with me and the CI issues! Looks good.
@axw Can I bother you for a tag :)? |
@macnibblet there's a few issues that I'd like to resolve before creating a new release (https://github.com/elastic/apm-agent-go/milestone/9). That will probably be a few weeks away still. Can you please pin to the latest commit in the mean time? |
@macnibblet happy new year! Sorry for the long wait -- we've just created a new release, v1.10.0, with your changes. |
No description provided.