gormopentracing

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

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

Go to latest
Published: Dec 20, 2021 License: MIT Imports: 7 Imported by: 67

README

gorm-opentracing

Go Report Card go.dev reference

opentracing support for gorm2.

Features
  • Record SQL in span logs.
  • Record Result in span logs.
  • Record Table in span tags.
  • Record Error in span tags and logs.
  • Register Create Query Delete Update Row Raw tracing callbacks.
Get Started

I assume that you already have an opentracing Tracer client started in your project.

import (
        ....
	gormopentracing "gorm.io/plugin/opentracing"
)
func main() {
	var db *gorm.DB
	
	db.Use(gormopentracing.New())
	
	// if you want to use customized tracer instead of opentracing.GlobalTracer() which is default,
	// you can use the option WithTracer(yourTracer)
}

Otherwise, you need to deploy distributed tracing server(jaeger, zipkin for example), then you need to boot tracer client in yours project and set tracer to opentracing.

import (
	"github.com/opentracing/opentracing-go"
	"github.com/uber/jaeger-client-go"
	"github.com/uber/jaeger-client-go/config"
	jaegerlog "github.com/uber/jaeger-client-go/log"
)

func bootTracerBasedJaeger() {
	// jaeger tracer configuration
	cfg := &config.Configuration{
		Sampler: &config.SamplerConfig{
			Type:  jaeger.SamplerTypeConst,
			Param: 1,
		},
		ServiceName: "gormopentracing",
		Reporter: &config.ReporterConfig{
			LogSpans: true,
			//LocalAgentHostPort:  "127.0.0.1:6381",
			BufferFlushInterval: 100 * time.Millisecond,
			CollectorEndpoint:   "http://127.0.0.1:14268/api/traces",
		},
	}

	// jaeger tracer client 
	tracer, _, err := cfg.NewTracer(
		config.Logger(jaegerlog.StdLogger),
		config.ZipkinSharedRPCSpan(true),
	)
	if err != nil {
		log.Printf("failed to use jaeger tracer plugin, got error %v", err)
		os.Exit(1)
	}
	
	// set into opentracing's global tracer, so the plugin would take it as default tracer.
	opentracing.SetGlobalTracer(tracer)
}
Plugin options
// WithLogResult log result into span log, default: disabled.
func WithLogResult(logResult bool)

// WithTracer allows to use customized tracer rather than the global one only.
func WithTracer(tracer opentracing.Tracer)

// WithSqlParameters is a switch to control that whether record parameters in sql or not.  
func WithSqlParameters(logSqlParameters bool)

// WithErrorTagHook allows to customize error tag on opentracing.Span.
func WithErrorTagHook(errorTagHook errorTagHook)
Snapshots

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(opts ...ApplyOption) gorm.Plugin

New constructs a new plugin based opentracing. It supports to trace all operations in gorm, so if you have already traced your servers, now this plugin will perfect your tracing job.

Types

type ApplyOption

type ApplyOption func(o *options)

func WithCreateOpName

func WithCreateOpName(name operationName) ApplyOption

func WithDeleteOpName

func WithDeleteOpName(name operationName) ApplyOption

func WithErrorTagHook

func WithErrorTagHook(errorTagHook errorTagHook) ApplyOption

func WithLogResult

func WithLogResult(logResult bool) ApplyOption

WithLogResult enable opentracingPlugin to log the result of each executed sql.

func WithQueryOpName

func WithQueryOpName(name operationName) ApplyOption

func WithRawOpName

func WithRawOpName(name operationName) ApplyOption

func WithRowOpName

func WithRowOpName(name operationName) ApplyOption

func WithSqlParameters

func WithSqlParameters(logSqlParameters bool) ApplyOption

func WithTracer

func WithTracer(tracer opentracing.Tracer) ApplyOption

WithTracer allows to use customized tracer rather than the global one only.

func WithUpdateOpName

func WithUpdateOpName(name operationName) ApplyOption

Jump to

Keyboard shortcuts

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