2021-10-27 18:49:33 +03:00
|
|
|
package wrapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql/driver"
|
2022-12-25 15:35:05 +03:00
|
|
|
"fmt"
|
2023-01-05 16:03:21 +03:00
|
|
|
"time"
|
2021-10-27 18:49:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// wrapperStmt defines a wrapper for driver.Stmt
|
|
|
|
type wrapperStmt struct {
|
|
|
|
stmt driver.Stmt
|
|
|
|
opts Options
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements driver.Stmt Close
|
|
|
|
func (w *wrapperStmt) Close() error {
|
2023-01-05 16:03:21 +03:00
|
|
|
labels := []string{labelMethod, "Close"}
|
|
|
|
ts := time.Now()
|
|
|
|
err := w.stmt.Close()
|
2023-01-06 23:24:39 +03:00
|
|
|
td := time.Since(ts)
|
|
|
|
te := td.Seconds()
|
2023-01-05 16:03:21 +03:00
|
|
|
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)
|
2023-01-06 23:24:39 +03:00
|
|
|
|
|
|
|
if w.opts.LoggerEnabled {
|
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Close", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel)
|
|
|
|
}
|
2023-01-05 16:03:21 +03:00
|
|
|
return err
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NumInput implements driver.Stmt NumInput
|
|
|
|
func (w *wrapperStmt) NumInput() int {
|
|
|
|
return w.stmt.NumInput()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exec implements driver.Stmt Exec
|
|
|
|
func (w *wrapperStmt) Exec(args []driver.Value) (driver.Result, error) {
|
2023-01-05 16:03:21 +03:00
|
|
|
labels := []string{labelMethod, "Exec"}
|
|
|
|
ts := time.Now()
|
2023-01-06 23:24:39 +03:00
|
|
|
// nolint:staticcheck
|
2023-01-05 16:03:21 +03:00
|
|
|
res, err := w.stmt.Exec(args)
|
2023-01-06 23:24:39 +03:00
|
|
|
td := time.Since(ts)
|
|
|
|
te := td.Seconds()
|
2023-01-05 16:03:21 +03:00
|
|
|
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)
|
2023-01-06 23:24:39 +03:00
|
|
|
|
|
|
|
if w.opts.LoggerEnabled {
|
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Exec", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel)
|
|
|
|
}
|
2023-01-05 16:03:21 +03:00
|
|
|
return res, err
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Query implements driver.Stmt Query
|
|
|
|
func (w *wrapperStmt) Query(args []driver.Value) (driver.Rows, error) {
|
2023-01-05 16:03:21 +03:00
|
|
|
labels := []string{labelMethod, "Query"}
|
|
|
|
ts := time.Now()
|
|
|
|
// nolint:staticcheck
|
|
|
|
rows, err := w.stmt.Query(args)
|
2023-01-06 23:24:39 +03:00
|
|
|
td := time.Since(ts)
|
|
|
|
te := td.Seconds()
|
2023-01-05 16:03:21 +03:00
|
|
|
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)
|
2023-01-06 23:24:39 +03:00
|
|
|
|
|
|
|
if w.opts.LoggerEnabled {
|
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "Query", labelUnknown, td, err)...).Log(context.TODO(), w.opts.LoggerLevel)
|
|
|
|
}
|
2023-01-05 16:03:21 +03:00
|
|
|
return rows, err
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ExecContext implements driver.ExecerContext ExecContext
|
|
|
|
func (w *wrapperStmt) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
|
2022-12-31 15:50:03 +03:00
|
|
|
nctx, span := w.opts.Tracer.Start(ctx, "ExecContext")
|
2023-01-05 16:03:21 +03:00
|
|
|
span.AddLabels("method", "ExecContext")
|
|
|
|
name := getQueryName(ctx)
|
|
|
|
if name != "" {
|
2022-12-31 15:50:03 +03:00
|
|
|
span.AddLabels("query", name)
|
2023-01-05 16:03:21 +03:00
|
|
|
} else {
|
|
|
|
name = labelUnknown
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
2021-10-27 18:49:33 +03:00
|
|
|
defer span.Finish()
|
2022-12-31 15:50:03 +03:00
|
|
|
if len(args) > 0 {
|
|
|
|
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
2023-01-05 16:03:21 +03:00
|
|
|
labels := []string{labelMethod, "ExecContext", labelQuery, name}
|
2023-01-06 23:24:39 +03:00
|
|
|
if conn, ok := w.stmt.(driver.ExecerContext); ok {
|
2023-01-05 16:03:21 +03:00
|
|
|
ts := time.Now()
|
2023-01-06 23:24:39 +03:00
|
|
|
res, err := conn.ExecContext(nctx, query, args)
|
|
|
|
td := time.Since(ts)
|
|
|
|
te := td.Seconds()
|
2022-12-25 15:35:05 +03:00
|
|
|
if err != nil {
|
2023-01-05 16:03:21 +03:00
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
2022-12-25 15:35:05 +03:00
|
|
|
span.AddLabels("error", true)
|
2022-12-31 15:50:03 +03:00
|
|
|
span.AddLabels("err", err.Error())
|
2023-01-05 16:03:21 +03:00
|
|
|
} else {
|
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelSuccess)...).Inc()
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
2023-01-05 16:03:21 +03:00
|
|
|
w.opts.Meter.Summary(meterRequestLatencyMicroseconds, labels...).Update(te)
|
|
|
|
w.opts.Meter.Histogram(meterRequestDurationSeconds, labels...).Update(te)
|
2023-01-06 23:24:39 +03:00
|
|
|
|
|
|
|
if w.opts.LoggerEnabled {
|
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "ExecContext", name, td, err)...).Log(context.TODO(), w.opts.LoggerLevel)
|
|
|
|
}
|
2022-12-25 15:35:05 +03:00
|
|
|
return res, err
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
values, err := namedValueToValue(args)
|
|
|
|
if err != nil {
|
2023-01-05 16:03:21 +03:00
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
2022-12-25 15:35:05 +03:00
|
|
|
span.AddLabels("error", true)
|
2022-12-31 15:50:03 +03:00
|
|
|
span.AddLabels("err", err.Error())
|
2023-01-06 23:24:39 +03:00
|
|
|
|
|
|
|
if w.opts.LoggerEnabled {
|
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "ExecContext", name, 0, err)...).Log(context.TODO(), w.opts.LoggerLevel)
|
|
|
|
}
|
2021-10-27 18:49:33 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-06 23:24:39 +03:00
|
|
|
ts := time.Now()
|
2023-01-05 16:03:21 +03:00
|
|
|
// nolint:staticcheck
|
2022-12-25 15:35:05 +03:00
|
|
|
res, err := w.Exec(values)
|
2023-01-06 23:24:39 +03:00
|
|
|
td := time.Since(ts)
|
|
|
|
te := td.Seconds()
|
2022-12-25 15:35:05 +03:00
|
|
|
if err != nil {
|
2023-01-06 23:24:39 +03:00
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
2022-12-25 15:35:05 +03:00
|
|
|
span.AddLabels("error", true)
|
2022-12-31 15:50:03 +03:00
|
|
|
span.AddLabels("err", err.Error())
|
2023-01-06 23:24:39 +03:00
|
|
|
} 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)
|
|
|
|
|
|
|
|
if w.opts.LoggerEnabled {
|
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "ExecContext", name, td, err)).Log(context.TODO(), w.opts.LoggerLevel)
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
|
|
|
return res, err
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryContext implements Driver.QueryerContext QueryContext
|
2022-12-25 15:35:05 +03:00
|
|
|
func (w *wrapperStmt) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
|
2022-12-31 15:50:03 +03:00
|
|
|
nctx, span := w.opts.Tracer.Start(ctx, "QueryContext")
|
2023-01-05 16:03:21 +03:00
|
|
|
span.AddLabels("method", "QueryContext")
|
|
|
|
name := getQueryName(ctx)
|
|
|
|
if name != "" {
|
2022-12-31 15:50:03 +03:00
|
|
|
span.AddLabels("query", name)
|
2023-01-05 16:03:21 +03:00
|
|
|
} else {
|
|
|
|
name = labelUnknown
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
2021-10-27 18:49:33 +03:00
|
|
|
defer span.Finish()
|
2022-12-31 15:50:03 +03:00
|
|
|
if len(args) > 0 {
|
|
|
|
span.AddLabels("args", fmt.Sprintf("%v", namedValueToLabels(args)))
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
2023-01-06 23:24:39 +03:00
|
|
|
labels := []string{labelMethod, "QueryContext", labelQuery, name}
|
|
|
|
if conn, ok := w.stmt.(driver.QueryerContext); ok {
|
|
|
|
ts := time.Now()
|
|
|
|
rows, err := conn.QueryContext(nctx, query, args)
|
|
|
|
td := time.Since(ts)
|
|
|
|
te := td.Seconds()
|
2022-12-25 15:35:05 +03:00
|
|
|
if err != nil {
|
2023-01-06 23:24:39 +03:00
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
2022-12-25 15:35:05 +03:00
|
|
|
span.AddLabels("error", true)
|
2022-12-31 15:50:03 +03:00
|
|
|
span.AddLabels("err", err.Error())
|
2023-01-06 23:24:39 +03:00
|
|
|
} 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)
|
|
|
|
|
|
|
|
if w.opts.LoggerEnabled {
|
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "QueryContext", name, td, err)).Log(context.TODO(), w.opts.LoggerLevel)
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
2021-10-27 18:49:33 +03:00
|
|
|
return rows, err
|
|
|
|
}
|
|
|
|
values, err := namedValueToValue(args)
|
|
|
|
if err != nil {
|
2023-01-06 23:24:39 +03:00
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
|
|
|
|
2023-01-05 16:03:21 +03:00
|
|
|
span.AddLabels("error", true)
|
|
|
|
span.AddLabels("err", err.Error())
|
2023-01-06 23:24:39 +03:00
|
|
|
|
|
|
|
if w.opts.LoggerEnabled {
|
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "QueryContext", name, 0, err)...).Log(context.TODO(), w.opts.LoggerLevel)
|
|
|
|
}
|
2021-10-27 18:49:33 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-06 23:24:39 +03:00
|
|
|
ts := time.Now()
|
2023-01-05 16:03:21 +03:00
|
|
|
// nolint:staticcheck
|
2022-12-25 15:35:05 +03:00
|
|
|
rows, err := w.Query(values)
|
2023-01-06 23:24:39 +03:00
|
|
|
td := time.Since(ts)
|
|
|
|
te := td.Seconds()
|
2022-12-25 15:35:05 +03:00
|
|
|
if err != nil {
|
2023-01-06 23:24:39 +03:00
|
|
|
w.opts.Meter.Counter(meterRequestTotal, append(labels, labelStatus, labelFailure)...).Inc()
|
2022-12-25 15:35:05 +03:00
|
|
|
span.AddLabels("error", true)
|
2022-12-31 15:50:03 +03:00
|
|
|
span.AddLabels("err", err.Error())
|
2023-01-06 23:24:39 +03:00
|
|
|
} 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)
|
|
|
|
|
|
|
|
if w.opts.LoggerEnabled {
|
|
|
|
w.opts.Logger.Fields(w.opts.LoggerObserver(context.TODO(), "QueryContext", name, td, err)...).Log(context.TODO(), w.opts.LoggerLevel)
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
|
|
|
return rows, err
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|