otelxorm

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: May 16, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

README

otelxorm

otelxorm is a hook for xorm that enables OpenTelemetry tracing for database operations.

Installation

To install otelxorm, use go get:

go get github.com/jenbonzhang/otelxorm

Usage

To use otelxorm, first create an instance of xorm.Engine:

engine, err := xorm.NewEngine(dbType, dsn)
if err != nil {
    logrus.Errorf("Database connection failed err: %v. Database name: %s", err, name)
    panic(err)
}

Then, add the otelxorm hook to the engine:

engine.AddHook(otelxorm.Hook(
    otelxorm.WithDBName(name),       // Set the database name
    otelxorm.WithFormatSQLReplace(), // Set the method for replacing parameters in formatted SQL statements
))

This will enable tracing for all database operations performed by the engine.

Configuration

otelxorm provides several options for configuration:

  • WithDBName(name string): Sets the name of the database being traced.
  • WithFormatSQL(formatSQL func(sql string, args []interface{}) string) Option : Sets the method for formatted SQL statements. By default, otelxorm uses otelxorm.defaultFormatSQL to format SQL statements and parameters.
  • WithFormatSQLReplace() this is use args to replace the sql parameters with $d in the sql statement.

You can define your own implementation of the formatSQL method and pass it to the WithFormatSQL option when adding the otelxorm hook to the engine. This will override the default implementation used by otelxorm. Here's an example:

func myFormatSQL(sql string, args []interface{}) string {
    // Custom implementation for formatting SQL statements and parameters
    // ...
    return formattedQuery
}

// ...

engine.AddHook(otelxorm.Hook(
    otelxorm.WithDBName(name),
    otelxorm.WithFormatSQL(myFormatSQL),
))

This will enable tracing for all database operations performed by the engine using your custom implementation of the formatSQL method.

Contributing

We welcome contributions! Please see our contributing guidelines for more information.

License

otelxorm is licensed under the Apache 2.0 License. See LICENSE for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Hook

func Hook(opts ...Option) contexts.Hook

func SemVersion

func SemVersion() string

SemVersion is the semantic version to be supplied to tracer/meter creation.

func Version

func Version() string

Version is the current release version of the xorm instrumentation.

func WrapEngine

func WrapEngine(e *xorm.Engine, opts ...Option)

func WrapEngineGroup

func WrapEngineGroup(eg *xorm.EngineGroup, opts ...Option)

Types

type OpenTelemetryHook

type OpenTelemetryHook struct {
	// contains filtered or unexported fields
}

func (*OpenTelemetryHook) AfterProcess

func (h *OpenTelemetryHook) AfterProcess(c *contexts.ContextHook) error

func (*OpenTelemetryHook) BeforeProcess

func (h *OpenTelemetryHook) BeforeProcess(c *contexts.ContextHook) (context.Context, error)

type Option

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

Option specifies instrumentation configuration options.

func WithDBName

func WithDBName(name string) Option

WithDBName configures a db.name attribute.

func WithDBSystem

func WithDBSystem(system string) Option

WithDBSystem configures a db.system attribute. You should prefer using WithAttributes and semconv, for example, `otelsql.WithAttributes(semconv.DBSystemSqlite)`.

func WithFormatSQL

func WithFormatSQL(formatSQL func(sql string, args []interface{}) string) Option

func WithFormatSQLReplace

func WithFormatSQLReplace() Option

func WithTracerProvider

func WithTracerProvider(provider trace.TracerProvider) Option

WithTracerProvider with tracer provider.

Jump to

Keyboard shortcuts

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