gorm

package
v1.63.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: Apache-2.0, BSD-3-Clause, Apache-2.0 Imports: 11 Imported by: 5

Documentation

Overview

Package gorm provides helper functions for tracing the jinzhu/gorm package (https://github.com/jinzhu/gorm).

Deprecated: The underlying github.com/jinzhu/gorm packages has known security vulnerabilities and is no longer under active development. It is highly recommended that you update to the latest version available here as a contrib package "gorm.io/gorm.v1".

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContextFromDB added in v1.25.0

func ContextFromDB(db *gorm.DB) context.Context

ContextFromDB returns any context previously attached to db using WithContext, otherwise returning context.Background.

func Open

func Open(dialect, source string, opts ...Option) (*gorm.DB, error)

Open opens a new (traced) database connection. The used dialect 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/jinzhu/gorm"

	"github.com/jinzhu/gorm"
	"github.com/lib/pq"
)

func main() {
	// Register augments the provided driver with tracing, enabling it to be loaded by gormtrace.Open.
	sqltrace.Register("postgres", &pq.Driver{}, sqltrace.WithServiceName("my-service"))

	// Open the registered driver, allowing all uses of the returned *gorm.DB to be traced.
	db, err := gormtrace.Open("postgres", "postgres://pqgotest:password@localhost/pqgotest?sslmode=disable")
	defer db.Close()
	if err != nil {
		log.Fatal(err)
	}

	user := struct {
		gorm.Model
		Name string
	}{}

	// All calls through gorm.DB are now traced.
	db.Where("name = ?", "jinzhu").First(&user)
}
Output:

func WithCallbacks added in v1.18.0

func WithCallbacks(db *gorm.DB, opts ...Option) *gorm.DB

WithCallbacks registers callbacks to the gorm.DB for tracing. It should be called once, after opening the db. The callbacks are triggered by Create, Update, Delete, Query and RowQuery operations.

func WithContext added in v1.18.0

func WithContext(ctx context.Context, db *gorm.DB) *gorm.DB

WithContext attaches the specified context to the given db. The context will be used as a basis for creating new spans. An example use case is providing a context which contains a span to be used as a parent.

Types

type Option added in v1.18.0

type Option func(*config)

Option represents an option that can be passed to Register, Open or OpenDB.

func WithAnalytics added in v1.18.0

func WithAnalytics(on bool) Option

WithAnalytics enables Trace Analytics for all started spans.

func WithAnalyticsRate added in v1.18.0

func WithAnalyticsRate(rate float64) Option

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

func WithCustomTag added in v1.27.0

func WithCustomTag(tag string, tagFn func(scope *gorm.Scope) interface{}) Option

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.29.0

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

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 with an error

func WithServiceName added in v1.18.0

func WithServiceName(name string) Option

WithServiceName 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