Skip to content
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

Merged
merged 9 commits into from Dec 7, 2020
Merged

Conversation

macnibblet
Copy link
Contributor

No description provided.

@apmmachine
Copy link
Collaborator

apmmachine commented Dec 7, 2020

💚 Build Succeeded

the below badges are clickable and redirect to their specific view in the CI or DOCS
Pipeline View Test View Changes Artifacts preview

Expand to view the summary

Build stats

  • Build Cause: Pull request #857 updated

  • Start Time: 2020-12-07T11:00:21.725+0000

  • Duration: 23 min 12 sec

Test stats 🧪

Test Results
Failed 0
Passed 7638
Skipped 188
Total 7826

Copy link
Member

@axw axw left a 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.

@macnibblet
Copy link
Contributor Author

@axw Tests are passing it Jenkins but it's reporting that it failed, not sure whats wrong here.

@@ -0,0 +1,14 @@
module go.elastic.co/apm/module/apmgopg
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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

@axw
Copy link
Member

axw commented Dec 7, 2020

jenkins run the tests please

module/apmgopgv10/doc.go Outdated Show resolved Hide resolved
module/apmgopgv10/doc.go Outdated Show resolved Hide resolved
module/apmgopgv10/hook_test.go Outdated Show resolved Hide resolved
macnibblet and others added 2 commits December 7, 2020 17:43
Co-authored-by: Andrew Wilkins <axwalk@gmail.com>
Co-authored-by: Andrew Wilkins <axwalk@gmail.com>
@codecov-io
Copy link

Codecov Report

Merging #857 (e00d617) into master (d72ef83) will decrease coverage by 0.04%.
The diff coverage is 70.00%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
apmtest/configwatcher.go 100.00% <ø> (ø)
apmtest/discard.go 80.00% <ø> (ø)
apmtest/env.go 100.00% <ø> (ø)
apmtest/httpsuite.go 100.00% <ø> (ø)
apmtest/recorder.go 77.77% <ø> (ø)
apmtest/recordlogger.go 87.50% <ø> (ø)
apmtest/testlogger.go 66.66% <ø> (ø)
apmtest/withtransaction.go 100.00% <ø> (ø)
breakdown.go 95.38% <ø> (ø)
builtin_metrics.go 81.81% <ø> (ø)
... and 102 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b228473...e00d617. Read the comment docs.

Copy link
Member

@axw axw left a 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'

Comment on lines 44 to 50
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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Contributor Author

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.

type queryHook struct{}

// BeforeQuery initiates the span for the database query
func (qh *queryHook) BeforeQuery(evt *pg.QueryEvent) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

sql = []byte(fmt.Sprintf("[go-pg] error: %s", err.Error()))
}

span, _ := apm.StartSpan(evt.DB.Context(), apmsql.QuerySignature(string(sql)), "db.postgresql.query")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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")

Instance: database,
})

evt.Stash[elasticApmSpanKey] = span
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
evt.Stash[elasticApmSpanKey] = span
return ctx, nil

Comment on lines 37 to 38
const elasticApmSpanKey = "go-apm-agent:span"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const elasticApmSpanKey = "go-apm-agent:span"

}

// AfterQuery ends the initiated span from BeforeQuery
func (qh *queryHook) AfterQuery(evt *pg.QueryEvent) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func (qh *queryHook) AfterQuery(evt *pg.QueryEvent) {
func (qh *queryHook) AfterQuery(ctx context.Context, evt *pg.QueryEvent) error {

Comment on lines 89 to 95
span, ok := evt.Stash[elasticApmSpanKey]
if !ok {
return
}
if s, ok := span.(*apm.Span); ok {
s.End()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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")
Copy link
Contributor Author

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.

Copy link
Member

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
Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Member

@axw axw left a 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 axw merged commit 46d8c0a into elastic:master Dec 7, 2020
@macnibblet
Copy link
Contributor Author

@axw Can I bother you for a tag :)?

@axw
Copy link
Member

axw commented Dec 8, 2020

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

@axw
Copy link
Member

axw commented Jan 21, 2021

@macnibblet happy new year! Sorry for the long wait -- we've just created a new release, v1.10.0, with your changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants