Categorygithub.com/middleware-labs/go-agent-gorm
repository
0.0.12
Repository: https://github.com/middleware-labs/go-agent-gorm.git
Documentation: pkg.go.dev

# Packages

No description provided by the author

# README

GORM instrumentation

Installation

go get github.com/middleware-labs/go-agent-gorm

Usage

To instrument GORM, you need to install the plugin :

import (
    "gorm.io/gorm"
    "gorm.io/driver/mysql"
     mw_gorm "github.com/middleware-labs/go-agent-gorm/gorm"
     "github.com/middleware-labs/golang-apm/tracker"
)

db, err := gorm.Open(mysql.Open("username:password@tcp(127.0.0.1:3306)/dbname"), &gorm.Config{})
if err != nil {
   panic(err)
}
if err := db.Use(mw_gorm.NewPlugin(mw_gorm.WithDBName("dbname"), mw_gorm.WithAttributes(tracker.String("db.system", "mysql")))); err != nil {
  panic(err)
}

And then use db.WithContext(ctx) to propagate the active span

var num int
if err := db.WithContext(ctx).Raw("SELECT 42").Scan(&num).Error; err != nil {
	panic(err)
}