Documentation ¶
Overview ¶
Package gorm provides helper functions for tracing the gorm.io/gorm package (https://github.com/go-gorm/gorm).
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewTracePlugin ¶ added in v1.67.0
NewTracePlugin returns a new gorm.Plugin that enhances the underlying *gorm.DB with tracing.
Example ¶
ExampleNewTracePlugin illustrates how to trace gorm using the gorm.Plugin api.
package main import ( "errors" "log" sqltrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql" gormtrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/gorm.io/gorm.v1" "github.com/jackc/pgx/v5/stdlib" "gorm.io/driver/postgres" "gorm.io/gorm" ) type User struct { gorm.Model Name string } func main() { // Register augments the provided driver with tracing, enabling it to be loaded by gorm.Open and the gormtrace.TracePlugin. sqltrace.Register("pgx", &stdlib.Driver{}, sqltrace.WithServiceName("my-service")) sqlDb, err := sqltrace.Open("pgx", "postgres://pqgotest:password@localhost/pqgotest?sslmode=disable") if err != nil { log.Fatal(err) } db, err := gorm.Open(postgres.New(postgres.Config{Conn: sqlDb}), &gorm.Config{}) if err != nil { log.Fatal(err) } var user User errCheck := gormtrace.WithErrorCheck(func(err error) bool { return !errors.Is(err, gorm.ErrRecordNotFound) }) if err := db.Use(gormtrace.NewTracePlugin(errCheck)); err != nil { log.Fatal(err) } // All calls through gorm.DB are now traced. db.Where("name = ?", "jinzhu").First(&user) }
Output:
func Open ¶
Open opens a new (traced) database connection. The used driver must be formerly registered using (gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql).Register.
Example ¶
package main import ( "log" sqltrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/database/sql" gormtrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/gorm.io/gorm.v1" "github.com/jackc/pgx/v5/stdlib" "gorm.io/driver/postgres" "gorm.io/gorm" ) type User struct { gorm.Model Name string } func main() { // Register augments the provided driver with tracing, enabling it to be loaded by gormtrace.Open. sqltrace.Register("pgx", &stdlib.Driver{}, sqltrace.WithServiceName("my-service")) sqlDb, err := sqltrace.Open("pgx", "postgres://pqgotest:password@localhost/pqgotest?sslmode=disable") if err != nil { log.Fatal(err) } db, err := gormtrace.Open(postgres.New(postgres.Config{Conn: sqlDb}), &gorm.Config{}) if err != nil { log.Fatal(err) } var user User // All calls through gorm.DB are now traced. db.Where("name = ?", "jinzhu").First(&user) }
Output:
Types ¶
type Option ¶
Option represents an option that can be passed to Register, Open or OpenDB.
func WithAnalytics ¶
WithAnalytics enables Trace Analytics for all started spans.
func WithAnalyticsRate ¶
WithAnalyticsRate sets the sampling rate for Trace Analytics events correlated to started spans.
func WithCustomTag ¶ added in v1.42.0
WithCustomTag will cause the given tagFn to be evaluated after executing a query and attach the result to the span tagged by the key.
func WithErrorCheck ¶ added in v1.34.0
WithErrorCheck specifies a function fn which determines whether the passed error should be marked as an error. The fn is called whenever a gorm operation finishes
func WithServiceName ¶
WithServiceName sets the given service name when registering a driver, or opening a database connection.