apm

package
v0.0.21 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2024 License: MIT Imports: 42 Imported by: 6

Documentation

Index

Constants

View Source
const (
	HeaderBusinessErrorCode = "X-Business-Error-Code"
	HeaderBusinessErrorMsg  = "X-Business-Error-Msg"
)
View Source
const (
	MetricTypeHTTP = "http"
	MetricTypeGRPC = "grpc"

	LibraryTypeMySQL = "mysql"
	LibraryTypeRedis = "redis"
)

Variables

View Source
var DBUtils = &dbUtils{}
View Source
var Logger = &logger{}
View Source
var (
	// MetricsReg is the global metric registry.
	MetricsReg = newCustomMetricRegistry(map[string]string{
		"host": internal.BuildInfo.Hostname(),
		"app":  internal.BuildInfo.AppName(),
	})
)
View Source
var SQLParser = &sqlParser{}

Functions

func GinOtel

func GinOtel(opts ...GinOtelOption) gin.HandlerFunc

GinOtel creates a Gin middleware for tracing, metrics and logging.

func NewAPM

func NewAPM(otelEndpoint string, opts ...ApmOption) (closeFunc func(), err error)

NewAPM creates a new APM component, which is a wrapper of opentelemetry.

func NewGorm

func NewGorm(name, connectURL string) (*gorm.DB, error)

NewGorm returns a new Gorm DB with hooks.

func NewHomes

func NewHomes(autoPProfOpts *AutoPProfOpt, opts ...holmes.Option) (*holmes.Holmes, error)

NewHomes creates a holmes dumper.

func NewMySQL

func NewMySQL(name, connectURL string) (*sql.DB, error)

NewMySQL returns a new MySQL driver with hooks.

func NewRedisV9

func NewRedisV9(name string, opts *redis.Options) (*redis.Client, error)

NewRedisV9 creates a new redis client with tracing. name is the business name of the redis client, it will be used in the span name.

func SetLongTxThreshold

func SetLongTxThreshold(d time.Duration)

SetLongTxThreshold sets the threshold for a long transaction.

func SetSlowSqlThreshold

func SetSlowSqlThreshold(d time.Duration)

SetSlowSqlThreshold sets the threshold for a slow SQL query.

Types

type ApmOption added in v0.0.16

type ApmOption func(b *apmBuilder)

ApmOption is the option for the apm.

func WithGRPCAuthToken added in v0.0.16

func WithGRPCAuthToken(token string) ApmOption

WithGRPCAuthToken sets the grpc auth token for the apm, it is optional.

func WithGrpcHeader added in v0.0.21

func WithGrpcHeader(headers map[string]string) ApmOption

WithGrpcHeader sets the headers for the grpc client to otel exporter, it is optional.

func WithResource added in v0.0.16

func WithResource(res *resource.Resource) ApmOption

WithResource sets the resource for the apm, if not set, a default resource will be created.

func WithSampler added in v0.0.18

func WithSampler(sampler sdktrace.Sampler) ApmOption

WithSampler sets the custom sampler for the apm, it is optional.

type AutoPProfOpt

type AutoPProfOpt struct {
	// EnableCPU enables cpu pprof.
	EnableCPU bool
	// EnableMem enables mem pprof.
	EnableMem bool
	// EnableGoroutine enables goroutine pprof.
	EnableGoroutine bool
}

AutoPProfOpt is the options for auto pprof.

type Conn

type Conn struct {
	driver.Conn
	// contains filtered or unexported fields
}

Conn is a wrapper around the driver.Conn interface. In order to hook into the execution of SQL queries, it should implement the following interfaces: - driver.ExecerContext - driver.QueryerContext - driver.ConnPrepareContext

func (*Conn) BeginTx

func (conn *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error)

BeginTx starts and returns a new transaction. If the context is canceled by the user the sql package will call Tx.Rollback before discarding and closing the connection.

This must check opts.Isolation to determine if there is a set isolation level. If the driver does not support a non-default level and one is set or if there is a non-default isolation level that is not supported, an error must be returned.

This must also check opts.ReadOnly to determine if the read-only value is true to either set the read-only transaction property if supported or return an error if it is not supported.

func (*Conn) ExecContext

func (conn *Conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error)

func (*Conn) PrepareContext

func (conn *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error)

PrepareContext returns a prepared statement, bound to this connection. context is for the preparation of the statement, it must not store the context within the statement itself.

func (*Conn) QueryContext

func (conn *Conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error)

type Driver

type Driver struct {
	driver.Driver
	// contains filtered or unexported fields
}

Driver is a wrapper around the driver.Driver interface.

func (*Driver) Open

func (d *Driver) Open(name string) (driver.Conn, error)

Open returns a new connection to the database. The name is a string in a driver-specific format.

Open may return a cached connection (one previously closed), but doing so is unnecessary; the sql package maintains a pool of idle connections for efficient re-use.

The returned connection is only used by one goroutine at a time.

type DriverTx

type DriverTx struct {
	driver.Tx
	// contains filtered or unexported fields
}

DriverTx is a wrapper around the driver.Tx interface. It should implement the following interfaces: - driver.Tx And the wrapped Conn need to implement driver.ConnBeginTx interface.

func (*DriverTx) Commit

func (dt *DriverTx) Commit() error

func (*DriverTx) Rollback

func (dt *DriverTx) Rollback() error

type GinOtelOption added in v0.0.19

type GinOtelOption func(o *ginOtel)

func WithPanicHook added in v0.0.19

func WithPanicHook(hook func(ctx context.Context, panic any) (stop bool)) GinOtelOption

type GrpcClient

type GrpcClient struct {
	*grpc.ClientConn
}

GrpcClient is a wrapper around grpc.ClientConn that provides tracing, metrics, and logging.

func NewGrpcClient

func NewGrpcClient(addr, server string, opts ...grpc.DialOption) (*GrpcClient, error)

type GrpcServer

type GrpcServer struct {
	*grpc.Server
	// contains filtered or unexported fields
}

GrpcServer is a wrapper of grpc.Server.

func NewGrpcServer

func NewGrpcServer(addr string, opts ...grpc.ServerOption) *GrpcServer

NewGrpcServer creates a new grpc server with the given address.

func NewGrpcServer2 added in v0.0.7

func NewGrpcServer2(listener net.Listener, opts ...grpc.ServerOption) *GrpcServer

NewGrpcServer2 creates a new grpc server with the given listener.

func (*GrpcServer) Start

func (s *GrpcServer) Start()

func (*GrpcServer) Stop

func (s *GrpcServer) Stop()

type HTTPServer

type HTTPServer struct {
	*http.Server
	// contains filtered or unexported fields
}

HTTPServer is a wrapper around http.Server that adds tracing to the server.

func NewHTTPServer

func NewHTTPServer(addr string) *HTTPServer

NewHTTPServer creates a new HTTPServer, it is a wrapper around http.Server that adds tracing and metrics to the server.

func NewHTTPServer2 added in v0.0.7

func NewHTTPServer2(listener net.Listener) *HTTPServer

NewHTTPServer2 creates a new HTTPServer with a given listener, it is a wrapper around http.Server that adds tracing and metrics to the server.

func (*HTTPServer) Close

func (s *HTTPServer) Close()

Close shutdowns the http server.

func (*HTTPServer) Handle

func (s *HTTPServer) Handle(pattern string, handler http.Handler)

Handle registers a new handler for the given pattern.

func (*HTTPServer) HandleFunc

func (s *HTTPServer) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))

HandleFunc registers a new handler function for the given pattern.

func (*HTTPServer) Start

func (s *HTTPServer) Start()

Start starts the http server in a new goroutine.

type Hooks

type Hooks struct {
	// Before is called before the query is executed.
	Before func(ctx context.Context, query string, args ...any) (context.Context, error)
	// After is called after the query is executed.
	After func(ctx context.Context, query string, args ...any) (context.Context, error)
	// OnError is called if the query fails.
	OnError func(ctx context.Context, err error, query string, args ...any) error
}

Hooks is a set of hooks that can be invoked during the execution of a SQL query.

type RedisV6

type RedisV6 struct {
	*redis.Client
	// contains filtered or unexported fields
}

RedisV6 is a wrapper of redis.Client with otel tracing enabled.

func NewRedisV6

func NewRedisV6(name string, opts *redis.Options) (*RedisV6, error)

NewRedisV6 creates a new redis client with otel tracing enabled. name is the business name of the redis client, it will be used in the span name.

func (*RedisV6) WithContext

func (r *RedisV6) WithContext(ctx context.Context) *redis.Client

WithContext wraps client with context and wraps process and process pipeline with otel tracing.

type Stmt

type Stmt struct {
	driver.Stmt
	// contains filtered or unexported fields
}

Stmt is a wrapper around the driver.Stmt interface. In order to hook into the execution of SQL queries after preparation, it should implement the following interfaces: - driver.StmtExecContext - driver.StmtQueryContext

func (*Stmt) ExecContext

func (s *Stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (driver.Result, error)

ExecContext executes a query that doesn't return rows, such as an INSERT or UPDATE.

ExecContext must honor the context timeout and return when it is canceled.

func (*Stmt) QueryContext

func (s *Stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driver.Rows, error)

QueryContext executes a query that may return rows, such as a SELECT.

QueryContext must honor the context timeout and return when it is canceled.

Jump to

Keyboard shortcuts

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