Documentation ¶
Index ¶
- Constants
- Variables
- func GinOtel(opts ...GinOtelOption) gin.HandlerFunc
- func NewAPM(otelEndpoint string, opts ...ApmOption) (closeFunc func(), err error)
- func NewGorm(name, connectURL string) (*gorm.DB, error)
- func NewHomes(autoPProfOpts *AutoPProfOpt, opts ...holmes.Option) (*holmes.Holmes, error)
- func NewMySQL(name, connectURL string) (*sql.DB, error)
- func NewRedisV9(name string, opts *redis.Options) (*redis.Client, error)
- func SetLongTxThreshold(d time.Duration)
- func SetSlowSqlThreshold(d time.Duration)
- type ApmOption
- type AutoPProfOpt
- type Conn
- func (conn *Conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error)
- func (conn *Conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error)
- func (conn *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error)
- func (conn *Conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error)
- type Driver
- type DriverTx
- type GinOtelOption
- type GrpcClient
- type GrpcServer
- type HTTPServer
- type Hooks
- type RedisV6
- type Stmt
Constants ¶
const ( HeaderBusinessErrorCode = "X-Business-Error-Code" HeaderBusinessErrorMsg = "X-Business-Error-Msg" )
const ( MetricTypeHTTP = "http" MetricTypeGRPC = "grpc" LibraryTypeMySQL = "mysql" LibraryTypeRedis = "redis" )
Variables ¶
var DBUtils = &dbUtils{}
var Logger = &logger{}
var ( // MetricsReg is the global metric registry. MetricsReg = newCustomMetricRegistry(map[string]string{ "host": internal.BuildInfo.Hostname(), "app": internal.BuildInfo.AppName(), }) )
var SQLParser = &sqlParser{}
Functions ¶
func GinOtel ¶
func GinOtel(opts ...GinOtelOption) gin.HandlerFunc
GinOtel creates a Gin middleware for tracing, metrics and logging.
func NewRedisV9 ¶
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 ¶
SetLongTxThreshold sets the threshold for a long transaction.
func SetSlowSqlThreshold ¶
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
WithGRPCAuthToken sets the grpc auth token for the apm, it is optional.
func WithGrpcHeader ¶ added in v0.0.21
WithGrpcHeader sets the headers for the grpc client to otel exporter, it is optional.
func WithResource ¶ added in v0.0.16
WithResource sets the resource for the apm, if not set, a default resource will be created.
func WithSampler ¶ added in v0.0.18
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 ¶
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 ¶
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) PrepareContext ¶
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.
type Driver ¶
Driver is a wrapper around the driver.Driver interface.
func (*Driver) Open ¶
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 ¶
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.
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 ¶
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 ¶
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) 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 ¶
RedisV6 is a wrapper of redis.Client with otel tracing enabled.
func NewRedisV6 ¶
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.
type Stmt ¶
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 ¶
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 ¶
QueryContext executes a query that may return rows, such as a SELECT.
QueryContext must honor the context timeout and return when it is canceled.