Skip to content

Commit

Permalink
support postgres for gorm v2 (#833)
Browse files Browse the repository at this point in the history
* support postgres for gorm v2 and adding docs
  • Loading branch information
Deepak Kumar committed Oct 12, 2020
1 parent 5949a1a commit e50bbbd
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 2 deletions.
24 changes: 24 additions & 0 deletions docs/instrumenting.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,30 @@ func main() {
}
----

==== module/apmgormv2
Package apmgormv2 provides a means of instrumenting http://gorm.io[GORM] database operations.

To trace `GORM` operations, import the appropriate `apmgormv2/driver` package (instead of the
`gorm.io/driver` package), use these dialects to `gorm.Open` instead of gorm drivers.

Once you have a `*gorm.DB`, you can call `db.WithContext` to
propagate a context containing a transaction to the operations:

[source,go]
----
import (
"gorm.io/gorm"
postgres "go.elastic.co/apm/module/apmgormv2/driver/postgres"
)
func main() {
db, err := gorm.Open(postgres.Open("dsn"), &gorm.Config{})
...
db = db.WithContext(ctx)
db.Find(...) // creates a "SELECT FROM <foo>" span
}
----

[[builtin-modules-apmgocql]]
==== module/apmgocql
Package apmgocql provides a means of instrumenting https://github.com/gocql/gocql[gocql] so
Expand Down
5 changes: 4 additions & 1 deletion docs/supported-tech.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ operation.
As with `database/sql` support we provide additional support for the
postgres, mysql, and sqlite dialects.

See <<builtin-modules-apmgorm, module/apmgorm>> for more information
We provide different packages for the Gorm v1 and v2 versions:
`module/apmgorm` for Gorm v1.x, and `module/apmgormv2` for Gorm v2.x.

See <<builtin-modules-apmgorm, module/apmgorm>> or <<builtin-modules-apmgorm, module/apmgormv2>> for more information
about GORM instrumentation.

[float]
Expand Down
17 changes: 17 additions & 0 deletions module/apmgormv2/apmgorm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

"go.elastic.co/apm/apmtest"
mysql "go.elastic.co/apm/module/apmgormv2/driver/mysql"
postgres "go.elastic.co/apm/module/apmgormv2/driver/postgres"
sqlite "go.elastic.co/apm/module/apmgormv2/driver/sqlite"
"go.elastic.co/apm/module/apmsql"
)
Expand All @@ -51,6 +52,22 @@ func TestWithContext(t *testing.T) {
)
})

if pgHost := os.Getenv("PGHOST"); pgHost == "" {
t.Logf("PGHOST not specified, skipping")
} else {
t.Run("postgres", func(t *testing.T) {
testWithContext(t,
apmsql.DSNInfo{
Address: pgHost,
Port: 5432,
Database: "test_db",
User: "postgres",
},
postgres.Open("user=postgres password=hunter2 dbname=test_db sslmode=disable"), &gorm.Config{},
)
})
}

if mysqlHost := os.Getenv("MYSQL_HOST"); mysqlHost == "" {
t.Logf("MYSQL_HOST not specified, skipping")
} else {
Expand Down
41 changes: 41 additions & 0 deletions module/apmgormv2/driver/postgres/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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 apmpostgres imports the gorm mysql dialect package,
// and also registers the mysql driver with apmsql.
package apmpostgres

import (
"gorm.io/driver/postgres"
"gorm.io/gorm"

apmpgxv4 "go.elastic.co/apm/module/apmsql/pgxv4"
)

// Open creates a dialect with apmsql
func Open(dsn string) gorm.Dialector {
dialect := &postgres.Dialector{
Config: &postgres.Config{
DriverName: apmpgxv4.DriverName,
DSN: dsn,
},
}

return dialect
}
2 changes: 1 addition & 1 deletion module/apmgormv2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ require (
github.com/stretchr/testify v1.5.1
go.elastic.co/apm v1.8.0
go.elastic.co/apm/module/apmsql v1.8.0
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae // indirect
gorm.io/driver/mysql v1.0.2
gorm.io/driver/postgres v1.0.2
gorm.io/driver/sqlite v1.1.4-0.20200928065301-698e250a3b0d
gorm.io/gorm v1.20.2
)
Expand Down

0 comments on commit e50bbbd

Please sign in to comment.