gorm

package
Version: v0.0.0-...-2c0343b Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2021 License: Apache-2.0, BSD-3-Clause, Apache-2.0 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 (gitlab.com/otavio3/go-tracer/contrib/database/sql).Register.

Example
package main

import (
	"log"

	"github.com/jackc/pgx/v4/stdlib"
	sqltrace "gitlab.com/otavio3/go-tracer/contrib/database/sql"
	gormtrace "gitlab.com/otavio3/go-tracer/contrib/gorm.io/gorm.v1"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

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)
	}

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

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

Types

type Option

type Option func(*config)

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

func WithAnalytics

func WithAnalytics(on bool) Option

WithAnalytics enables Trace Analytics for all started spans.

func WithAnalyticsRate

func WithAnalyticsRate(rate float64) Option

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

func WithServiceName

func WithServiceName(name string) Option

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

Source Files

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL