Skip to content
This repository has been archived by the owner on Jun 14, 2023. It is now read-only.

Latest commit

 

History

History
40 lines (30 loc) · 800 Bytes

README.md

File metadata and controls

40 lines (30 loc) · 800 Bytes

Go2Sky with Gorm

Installation

go get -u github.com/SkyAPM/go2sky-plugins/gorm

Usage

import (
	gormPlugin "github.com/SkyAPM/go2sky-plugins/gorm"

	"github.com/SkyAPM/go2sky"
	"github.com/SkyAPM/go2sky/reporter"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

// init reporter
re, err := reporter.NewLogReporter()
defer re.Close()

// init tracer
tracer, err := go2sky.NewTracer("service-name", go2sky.WithReporter(re))
if err != nil {
    log.Fatalf("init tracer error: %v", err)
}

db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})

if err != nil {
  log.Fatalf("open db error: %v \n", err)
}
db.Use(gormPlugin.New(tracer, gormPlugin.WithPeerAddr("127.0.0.1:3306"), gormPlugin.WithSqlDBType(gormPlugin.MYSQL)))

// use with context
dbWithCtx := db.WithContext(ctx)
```(