gorm

package module
v0.0.0-...-7ebff8f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2024 License: Apache-2.0, BSD-3-Clause Imports: 9 Imported by: 0

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 Open

func Open(dialector gorm.Dialector, cfg *gorm.Config, opts ...Option) (*gorm.DB, error)

Open opens a new (traced) database connection. The used driver must be formerly registered using (github.com/DataDog/dd-trace-go/v2/contrib/database/sql).Register.

Example
package main

import (
	"log"

	sqltrace "github.com/DataDog/dd-trace-go/v2/contrib/database/sql"
	gormtrace "github.com/DataDog/dd-trace-go/v2/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.WithService("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

type Option interface {
	// contains filtered or unexported methods
}

Option describes options for the Gorm.io integration.

type OptionFn

type OptionFn func(*config)

OptionFn represents options applicable to Open.

func WithAnalytics

func WithAnalytics(on bool) OptionFn

WithAnalytics enables Trace Analytics for all started spans.

func WithAnalyticsRate

func WithAnalyticsRate(rate float64) OptionFn

WithAnalyticsRate sets the sampling rate for Trace Analytics events correlated to started spans.

func WithCustomTag

func WithCustomTag(tag string, tagFn func(db *gorm.DB) interface{}) OptionFn

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

func WithErrorCheck(fn func(err error) bool) OptionFn

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 WithService

func WithService(name string) OptionFn

WithService sets the given service name when registering a driver, or opening a database connection.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL