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
Changes from all commits
53b3f55
c74d89a
dfee614
c365578
b1939f4
257a0a8
e00d617
a15044b
c331f19
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
// Package apmgopgv10 provides wrappers for tracing go-pg/v10 operations. | ||
package apmgopgv10 // import "go.elastic.co/apm/module/apmgopgv10" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module go.elastic.co/apm/module/apmgopgv10 | ||
|
||
require ( | ||
github.com/go-pg/pg/v10 v10.7.3 | ||
github.com/stretchr/testify v1.6.1 | ||
go.elastic.co/apm v1.9.0 | ||
go.elastic.co/apm/module/apmsql v1.9.0 | ||
) | ||
|
||
replace go.elastic.co/apm => ../.. | ||
|
||
replace go.elastic.co/apm/module/apmsql => ../apmsql | ||
|
||
go 1.14 |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
// +build go1.14 | ||
|
||
package apmgopgv10 // import "go.elastic.co/apm/module/apmgopgv10" | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-pg/pg/v10" | ||
"github.com/pkg/errors" | ||
|
||
"go.elastic.co/apm" | ||
"go.elastic.co/apm/module/apmsql" | ||
"go.elastic.co/apm/stacktrace" | ||
) | ||
|
||
func init() { | ||
stacktrace.RegisterLibraryPackage("github.com/go-pg/pg/v10") | ||
} | ||
|
||
// Instrument modifies db such that operations are hooked and reported as spans | ||
// to Elastic APM if they occur within the context of a captured transaction. | ||
// | ||
// If Instrument cannot instrument db, then an error will be returned. | ||
func Instrument(db *pg.DB) error { | ||
db.AddQueryHook(&queryHook{}) | ||
|
||
return nil | ||
} | ||
|
||
var _ pg.QueryHook = (*queryHook)(nil) | ||
|
||
// queryHook is an implementation of pg.QueryHook that reports queries as spans to Elastic APM. | ||
type queryHook struct{} | ||
|
||
// BeforeQuery initiates the span for the database query | ||
func (qh *queryHook) BeforeQuery(ctx context.Context, evt *pg.QueryEvent) (context.Context, error) { | ||
var ( | ||
database string | ||
user string | ||
) | ||
if db, ok := evt.DB.(*pg.DB); ok { | ||
opts := db.Options() | ||
user = opts.User | ||
database = opts.Database | ||
} | ||
|
||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. I don't either; this looks fine to me. |
||
} | ||
|
||
span, ctx := apm.StartSpan(ctx, apmsql.QuerySignature(string(sql)), "db.postgresql.query") | ||
span.Context.SetDatabase(apm.DatabaseSpanContext{ | ||
Statement: string(sql), | ||
|
||
// Static | ||
Type: "sql", | ||
User: user, | ||
Instance: database, | ||
}) | ||
|
||
return ctx, nil | ||
} | ||
|
||
// AfterQuery ends the initiated span from BeforeQuery | ||
func (qh *queryHook) AfterQuery(ctx context.Context, evt *pg.QueryEvent) error { | ||
if span := apm.SpanFromContext(ctx); span != nil { | ||
span.End() | ||
} | ||
|
||
return nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Licensed to Elasticsearch B.V. under one or more contributor | ||
// license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright | ||
// ownership. Elasticsearch B.V. licenses this file to you under | ||
// the Apache License, Version 2.0 (the "License"); you may | ||
// not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
// +build go1.14 | ||
|
||
package apmgopgv10_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/go-pg/pg/v10" | ||
"github.com/go-pg/pg/v10/orm" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"go.elastic.co/apm/apmtest" | ||
"go.elastic.co/apm/module/apmgopgv10" | ||
) | ||
|
||
type User struct { | ||
TableName struct{} `sql:"users"` | ||
|
||
Id int `json:"id"` | ||
Name string `json:"name"` | ||
} | ||
|
||
func TestWithContext(t *testing.T) { | ||
if os.Getenv("PGHOST") == "" { | ||
t.Skip("PGHOST env not defined, skipping") | ||
return | ||
} | ||
|
||
_, spans, errors := apmtest.WithTransaction(func(ctx context.Context) { | ||
db := pg.Connect(&pg.Options{ | ||
Addr: fmt.Sprintf("%s:5432", os.Getenv("PGHOST")), | ||
User: "postgres", | ||
Password: "hunter2", | ||
Database: "test_db", | ||
}) | ||
err := apmgopgv10.Instrument(db) | ||
require.NoError(t, err) | ||
|
||
_, err = db.Exec("SELECT 1") | ||
require.NoError(t, err) | ||
|
||
db.Model(&User{}).DropTable(&orm.DropTableOptions{}) | ||
db.Model(&User{}).CreateTable(&orm.CreateTableOptions{}) | ||
|
||
defer db.Close() | ||
db = db.WithContext(ctx) | ||
|
||
_, err = db.Model(&User{Id: 1337, Name: "Antoine Hedgecock"}).Insert() | ||
assert.NoError(t, err) | ||
|
||
assert.NoError(t, db.Model(&User{}).Where("id = ?", 1337).Select()) | ||
|
||
_, err = db.Model(&User{Id: 1337, Name: "new name"}).Column("name").WherePK().Update() | ||
assert.NoError(t, err) | ||
}) | ||
|
||
require.NotEmpty(t, spans) | ||
assert.Empty(t, errors) | ||
|
||
spanNames := make([]string, len(spans)) | ||
for i, span := range spans { | ||
spanNames[i] = span.Name | ||
require.NotNil(t, span.Context) | ||
require.NotNil(t, span.Context.Database) | ||
assert.Equal(t, "test_db", span.Context.Database.Instance) | ||
assert.NotEmpty(t, span.Context.Database.Statement) | ||
assert.Equal(t, "sql", span.Context.Database.Type) | ||
assert.Equal(t, "postgres", span.Context.Database.User) | ||
} | ||
|
||
assert.Equal(t, []string{ | ||
"INSERT INTO users", | ||
"SELECT FROM users", | ||
"UPDATE users", | ||
}, spanNames) | ||
} |
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.