WIP: rewrite wrapper #53

Merged
vtolstov merged 5 commits from improvements into v3 2023-01-06 23:28:03 +03:00
5 changed files with 235 additions and 32 deletions
Showing only changes of commit b2eb8e6c5c - Show all commits

View File

@ -22,6 +22,7 @@ func namedValueToValue(named []driver.NamedValue) ([]driver.Value, error) {
return dargs, nil return dargs, nil
} }
// namedValueToLabels convert driver arguments to interface{} slice
func namedValueToLabels(named []driver.NamedValue) []interface{} { func namedValueToLabels(named []driver.NamedValue) []interface{} {
largs := make([]interface{}, len(named)*2) largs := make([]interface{}, len(named)*2)
var name string var name string

167
conn.go
View File

@ -4,6 +4,7 @@ import (
"context" "context"
"database/sql/driver" "database/sql/driver"
"fmt" "fmt"
"time"
) )
// wrapperConn defines a wrapper for driver.Conn // wrapperConn defines a wrapper for driver.Conn
@ -14,79 +15,160 @@ type wrapperConn struct {
// Prepare implements driver.Conn Prepare // Prepare implements driver.Conn Prepare
func (w *wrapperConn) Prepare(query string) (driver.Stmt, error) { func (w *wrapperConn) Prepare(query string) (driver.Stmt, error) {
labels := []string{labelMethod, "Prepare", labelQuery, labelUnknown}
ts := time.Now()
stmt, err := w.conn.Prepare(query) stmt, err := w.conn.Prepare(query)
te := time.Since(ts).Seconds()
if err != nil { if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return nil, err return nil, err
} }
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return &wrapperStmt{stmt: stmt, opts: w.opts}, nil return &wrapperStmt{stmt: stmt, opts: w.opts}, nil
} }
// Close implements driver.Conn Close // Close implements driver.Conn Close
func (w *wrapperConn) Close() error { func (w *wrapperConn) Close() error {
return w.conn.Close() labels := []string{labelMethod, "Close"}
ts := time.Now()
err := w.conn.Close()
te := time.Since(ts).Seconds()
if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
} else {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
}
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return err
} }
// Begin implements driver.Conn Begin // Begin implements driver.Conn Begin
func (w *wrapperConn) Begin() (driver.Tx, error) { func (w *wrapperConn) Begin() (driver.Tx, error) {
labels := []string{labelMethod, "Begin"}
ts := time.Now()
// nolint:staticcheck
tx, err := w.conn.Begin() tx, err := w.conn.Begin()
te := time.Since(ts).Seconds()
if err != nil { if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return nil, err return nil, err
} }
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return &wrapperTx{tx: tx, opts: w.opts}, nil return &wrapperTx{tx: tx, opts: w.opts}, nil
} }
// BeginTx implements driver.ConnBeginTx BeginTx // BeginTx implements driver.ConnBeginTx BeginTx
func (w *wrapperConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) { func (w *wrapperConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
nctx, span := w.opts.Tracer.Start(ctx, "BeginTx") nctx, span := w.opts.Tracer.Start(ctx, "BeginTx")
span.AddLabels("op", "BeginTx") span.AddLabels("method", "BeginTx")
if name := getQueryName(ctx); name != "" { name := getQueryName(ctx)
if name != "" {
span.AddLabels("query", name) span.AddLabels("query", name)
} else {
name = labelUnknown
} }
labels := []string{labelMethod, "BeginTx", labelQuery, name}
if connBeginTx, ok := w.conn.(driver.ConnBeginTx); ok { if connBeginTx, ok := w.conn.(driver.ConnBeginTx); ok {
ts := time.Now()
tx, err := connBeginTx.BeginTx(nctx, opts) tx, err := connBeginTx.BeginTx(nctx, opts)
te := time.Since(ts).Seconds()
if err != nil { if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
span.AddLabels("error", true) span.AddLabels("error", true)
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
return nil, err return nil, err
} }
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
return &wrapperTx{tx: tx, opts: w.opts, span: span}, nil return &wrapperTx{tx: tx, opts: w.opts, span: span}, nil
} }
ts := time.Now()
// nolint:staticcheck
tx, err := w.conn.Begin() tx, err := w.conn.Begin()
te := time.Since(ts).Seconds()
if err != nil { if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
span.AddLabels("error", true) span.AddLabels("error", true)
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
} }
return tx, err w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return tx, nil
} }
// PrepareContext implements driver.ConnPrepareContext PrepareContext // PrepareContext implements driver.ConnPrepareContext PrepareContext
func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) { func (w *wrapperConn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
nctx, span := w.opts.Tracer.Start(ctx, "PrepareContext") nctx, span := w.opts.Tracer.Start(ctx, "PrepareContext")
span.AddLabels("op", "PrepareContext") span.AddLabels("method", "PrepareContext")
if name := getQueryName(ctx); name != "" { name := getQueryName(ctx)
if name != "" {
span.AddLabels("query", name) span.AddLabels("query", name)
} else {
name = labelUnknown
} }
labels := []string{labelMethod, "PrepareContext", labelQuery, name}
if connPrepareContext, ok := w.conn.(driver.ConnPrepareContext); ok { if connPrepareContext, ok := w.conn.(driver.ConnPrepareContext); ok {
ts := time.Now()
stmt, err := connPrepareContext.PrepareContext(nctx, query) stmt, err := connPrepareContext.PrepareContext(nctx, query)
te := time.Since(ts).Seconds()
if err != nil { if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
span.AddLabels("error", true) span.AddLabels("error", true)
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
return nil, err return nil, err
} }
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return &wrapperStmt{stmt: stmt, opts: w.opts}, nil return &wrapperStmt{stmt: stmt, opts: w.opts}, nil
} }
ts := time.Now()
stmt, err := w.conn.Prepare(query) stmt, err := w.conn.Prepare(query)
te := time.Since(ts).Seconds()
if err != nil { if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
span.AddLabels("error", true) span.AddLabels("error", true)
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
} }
return stmt, err w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return stmt, nil
} }
// Exec implements driver.Execer Exec // Exec implements driver.Execer Exec
func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, error) { func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, error) {
// nolint:staticcheck
labels := []string{labelMethod, "Exec", labelQuery, labelUnknown}
if execer, ok := w.conn.(driver.Execer); ok { if execer, ok := w.conn.(driver.Execer); ok {
return execer.Exec(query, args) ts := time.Now()
res, err := execer.Exec(query, args)
te := time.Since(ts).Seconds()
if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
} else {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
}
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return res, err
} }
return nil, ErrUnsupported return nil, ErrUnsupported
} }
@ -94,20 +176,32 @@ func (w *wrapperConn) Exec(query string, args []driver.Value) (driver.Result, er
// Exec implements driver.StmtExecContext ExecContext // Exec implements driver.StmtExecContext ExecContext
func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
nctx, span := w.opts.Tracer.Start(ctx, "ExecContext") nctx, span := w.opts.Tracer.Start(ctx, "ExecContext")
span.AddLabels("op", "ExecContext") span.AddLabels("method", "ExecContext")
if name := getQueryName(ctx); name != "" { name := getQueryName(ctx)
if name != "" {
span.AddLabels("query", name) span.AddLabels("query", name)
} else {
name = labelUnknown
} }
defer span.Finish() defer span.Finish()
if len(args) > 0 { if len(args) > 0 {
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args))) span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
} }
labels := []string{labelMethod, "ExecContext", labelQuery, name}
if execerContext, ok := w.conn.(driver.ExecerContext); ok { if execerContext, ok := w.conn.(driver.ExecerContext); ok {
ts := time.Now()
res, err := execerContext.ExecContext(nctx, query, args) res, err := execerContext.ExecContext(nctx, query, args)
te := time.Since(ts).Seconds()
if err != nil { if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
span.AddLabels("error", true) span.AddLabels("error", true)
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
} else {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
} }
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return res, err return res, err
} }
values, err := namedValueToValue(args) values, err := namedValueToValue(args)
@ -116,11 +210,19 @@ func (w *wrapperConn) ExecContext(ctx context.Context, query string, args []driv
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
return nil, err return nil, err
} }
ts := time.Now()
// nolint:staticcheck
res, err := w.Exec(query, values) res, err := w.Exec(query, values)
te := time.Since(ts).Seconds()
if err != nil { if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
span.AddLabels("error", true) span.AddLabels("error", true)
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
} else {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
} }
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return res, err return res, err
} }
@ -129,20 +231,40 @@ func (w *wrapperConn) Ping(ctx context.Context) error {
if pinger, ok := w.conn.(driver.Pinger); ok { if pinger, ok := w.conn.(driver.Pinger); ok {
nctx, span := w.opts.Tracer.Start(ctx, "Ping") nctx, span := w.opts.Tracer.Start(ctx, "Ping")
defer span.Finish() defer span.Finish()
labels := []string{labelMethod, "Ping"}
ts := time.Now()
err := pinger.Ping(nctx) err := pinger.Ping(nctx)
te := time.Since(ts).Seconds()
if err != nil { if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
span.AddLabels("error", true) span.AddLabels("error", true)
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
return err return err
} else {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
} }
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
} }
return ErrUnsupported return ErrUnsupported
} }
// Query implements driver.Queryer Query // Query implements driver.Queryer Query
func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, error) { func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, error) {
// nolint:staticcheck
if queryer, ok := w.conn.(driver.Queryer); ok { if queryer, ok := w.conn.(driver.Queryer); ok {
return queryer.Query(query, args) labels := []string{labelMethod, "Query", labelQuery, labelUnknown}
ts := time.Now()
rows, err := queryer.Query(query, args)
te := time.Since(ts).Seconds()
if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
} else {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
}
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return rows, err
} }
return nil, ErrUnsupported return nil, ErrUnsupported
} }
@ -150,20 +272,31 @@ func (w *wrapperConn) Query(query string, args []driver.Value) (driver.Rows, err
// QueryContext implements Driver.QueryerContext QueryContext // QueryContext implements Driver.QueryerContext QueryContext
func (w *wrapperConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { func (w *wrapperConn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
nctx, span := w.opts.Tracer.Start(ctx, "QueryContext") nctx, span := w.opts.Tracer.Start(ctx, "QueryContext")
span.AddLabels("op", "QueryContext") span.AddLabels("method", "QueryContext")
if name := getQueryName(ctx); name != "" { name := getQueryName(ctx)
if name != "" {
span.AddLabels("query", name) span.AddLabels("query", name)
} else {
name = labelUnknown
} }
defer span.Finish() defer span.Finish()
if len(args) > 0 { if len(args) > 0 {
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args))) span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
} }
labels := []string{labelMethod, "QueryContext", labelQuery, name}
if queryerContext, ok := w.conn.(driver.QueryerContext); ok { if queryerContext, ok := w.conn.(driver.QueryerContext); ok {
ts := time.Now()
rows, err := queryerContext.QueryContext(nctx, query, args) rows, err := queryerContext.QueryContext(nctx, query, args)
te := time.Since(ts).Seconds()
if err != nil { if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
span.AddLabels("error", true) span.AddLabels("error", true)
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
} else {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
} }
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return rows, err return rows, err
} }
values, err := namedValueToValue(args) values, err := namedValueToValue(args)
@ -172,10 +305,18 @@ func (w *wrapperConn) QueryContext(ctx context.Context, query string, args []dri
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
return nil, err return nil, err
} }
ts := time.Now()
// nolint:staticcheck
rows, err := w.Query(query, values) rows, err := w.Query(query, values)
te := time.Since(ts).Seconds()
if err != nil { if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
span.AddLabels("error", true) span.AddLabels("error", true)
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
} else {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
} }
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return rows, err return rows, err
} }

View File

@ -27,10 +27,14 @@ var (
MaxIdletimeClosed = "max_idletime_closed" MaxIdletimeClosed = "max_idletime_closed"
MaxLifetimeClosed = "max_lifetime_closed" MaxLifetimeClosed = "max_lifetime_closed"
// RequestTotal = "request_total" meterRequestTotal = "request_total"
// RequestLatencyMicroseconds = "request_latency_microseconds" meterRequestLatencyMicroseconds = "request_latency_microseconds"
// RequestDurationSeconds = "request_duration_seconds" meterRequestDurationSeconds = "request_duration_seconds"
labelUnknown = "unknown"
labelQuery = "query"
labelMethod = "method"
labelStatus = "status"
labelSuccess = "success" labelSuccess = "success"
labelFailure = "failure" labelFailure = "failure"
labelHost = "db_host" labelHost = "db_host"
@ -44,9 +48,6 @@ type Options struct {
Tracer tracer.Tracer Tracer tracer.Tracer
DatabaseHost string DatabaseHost string
DatabaseName string DatabaseName string
ServiceName string
ServiceVersion string
ServiceID string
MeterMetricPrefix string MeterMetricPrefix string
MeterStatsInterval time.Duration MeterStatsInterval time.Duration
LoggerLevel logger.Level LoggerLevel logger.Level
@ -120,6 +121,13 @@ func Logger(l logger.Logger) Option {
} }
} }
// LoggerLevel passes logger.Level option
func LoggerLevel(lvl logger.Level) Option {
return func(o *Options) {
o.LoggerLevel = lvl
}
}
// Tracer passes tracer.Tracer to wrapper // Tracer passes tracer.Tracer to wrapper
func Tracer(t tracer.Tracer) Option { func Tracer(t tracer.Tracer) Option {
return func(o *Options) { return func(o *Options) {

69
stmt.go
View File

@ -4,6 +4,7 @@ import (
"context" "context"
"database/sql/driver" "database/sql/driver"
"fmt" "fmt"
"time"
) )
// wrapperStmt defines a wrapper for driver.Stmt // wrapperStmt defines a wrapper for driver.Stmt
@ -14,7 +15,18 @@ type wrapperStmt struct {
// Close implements driver.Stmt Close // Close implements driver.Stmt Close
func (w *wrapperStmt) Close() error { func (w *wrapperStmt) Close() error {
return w.stmt.Close() labels := []string{labelMethod, "Close"}
ts := time.Now()
err := w.stmt.Close()
te := time.Since(ts).Seconds()
if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
} else {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
}
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return err
} }
// NumInput implements driver.Stmt NumInput // NumInput implements driver.Stmt NumInput
@ -24,39 +36,76 @@ func (w *wrapperStmt) NumInput() int {
// Exec implements driver.Stmt Exec // Exec implements driver.Stmt Exec
func (w *wrapperStmt) Exec(args []driver.Value) (driver.Result, error) { func (w *wrapperStmt) Exec(args []driver.Value) (driver.Result, error) {
return w.stmt.Exec(args) // nolint:staticcheck
labels := []string{labelMethod, "Exec"}
ts := time.Now()
res, err := w.stmt.Exec(args)
te := time.Since(ts).Seconds()
if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
} else {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
}
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return res, err
} }
// Query implements driver.Stmt Query // Query implements driver.Stmt Query
func (w *wrapperStmt) Query(args []driver.Value) (driver.Rows, error) { func (w *wrapperStmt) Query(args []driver.Value) (driver.Rows, error) {
return w.stmt.Query(args) labels := []string{labelMethod, "Query"}
ts := time.Now()
// nolint:staticcheck
rows, err := w.stmt.Query(args)
te := time.Since(ts).Seconds()
if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
} else {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
}
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return rows, err
} }
// ExecContext implements driver.ExecerContext ExecContext // ExecContext implements driver.ExecerContext ExecContext
func (w *wrapperStmt) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) { func (w *wrapperStmt) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
nctx, span := w.opts.Tracer.Start(ctx, "ExecContext") nctx, span := w.opts.Tracer.Start(ctx, "ExecContext")
span.AddLabels("op", "ExecContext") span.AddLabels("method", "ExecContext")
if name := getQueryName(ctx); name != "" { name := getQueryName(ctx)
if name != "" {
span.AddLabels("query", name) span.AddLabels("query", name)
} else {
name = labelUnknown
} }
defer span.Finish() defer span.Finish()
if len(args) > 0 { if len(args) > 0 {
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args))) span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
} }
labels := []string{labelMethod, "ExecContext", labelQuery, name}
if execerContext, ok := w.stmt.(driver.ExecerContext); ok { if execerContext, ok := w.stmt.(driver.ExecerContext); ok {
ts := time.Now()
res, err := execerContext.ExecContext(nctx, query, args) res, err := execerContext.ExecContext(nctx, query, args)
te := time.Since(ts).Seconds()
if err != nil { if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
span.AddLabels("error", true) span.AddLabels("error", true)
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
} else {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
} }
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
return res, err return res, err
} }
values, err := namedValueToValue(args) values, err := namedValueToValue(args)
if err != nil { if err != nil {
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
span.AddLabels("error", true) span.AddLabels("error", true)
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
return nil, err return nil, err
} }
// nolint:staticcheck
res, err := w.Exec(values) res, err := w.Exec(values)
if err != nil { if err != nil {
span.AddLabels("error", true) span.AddLabels("error", true)
@ -68,9 +117,12 @@ func (w *wrapperStmt) ExecContext(ctx context.Context, query string, args []driv
// QueryContext implements Driver.QueryerContext QueryContext // QueryContext implements Driver.QueryerContext QueryContext
func (w *wrapperStmt) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) { func (w *wrapperStmt) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
nctx, span := w.opts.Tracer.Start(ctx, "QueryContext") nctx, span := w.opts.Tracer.Start(ctx, "QueryContext")
span.AddLabels("op", "QueryContext") span.AddLabels("method", "QueryContext")
if name := getQueryName(ctx); name != "" { name := getQueryName(ctx)
if name != "" {
span.AddLabels("query", name) span.AddLabels("query", name)
} else {
name = labelUnknown
} }
defer span.Finish() defer span.Finish()
if len(args) > 0 { if len(args) > 0 {
@ -85,13 +137,12 @@ func (w *wrapperStmt) QueryContext(ctx context.Context, query string, args []dri
return rows, err return rows, err
} }
values, err := namedValueToValue(args) values, err := namedValueToValue(args)
if err != nil {
if err != nil { if err != nil {
span.AddLabels("error", true) span.AddLabels("error", true)
span.AddLabels("err", err.Error()) span.AddLabels("err", err.Error())
}
return nil, err return nil, err
} }
// nolint:staticcheck
rows, err := w.Query(values) rows, err := w.Query(values)
if err != nil { if err != nil {
span.AddLabels("error", true) span.AddLabels("error", true)

6
tx.go
View File

@ -18,7 +18,8 @@ func (w *wrapperTx) Commit() error {
if w.span != nil { if w.span != nil {
defer w.span.Finish() defer w.span.Finish()
} }
return w.tx.Commit() err := w.tx.Commit()
return err
} }
// Rollback implements driver.Tx Rollback // Rollback implements driver.Tx Rollback
@ -26,5 +27,6 @@ func (w *wrapperTx) Rollback() error {
if w.span != nil { if w.span != nil {
defer w.span.Finish() defer w.span.Finish()
} }
return w.tx.Rollback() err := w.tx.Rollback()
return err
} }