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()
|
|
|
|
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
|
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
|
|
|
// 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
|
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)
|
|
|
|
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
|
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}
|
2021-10-27 18:49:33 +03:00
|
|
|
if execerContext, ok := w.stmt.(driver.ExecerContext); ok {
|
2023-01-05 16:03:21 +03:00
|
|
|
ts := time.Now()
|
2022-12-25 15:35:05 +03:00
|
|
|
res, err := execerContext.ExecContext(nctx, query, args)
|
2023-01-05 16:03:21 +03:00
|
|
|
te := time.Since(ts).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)
|
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())
|
2021-10-27 18:49:33 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-05 16:03:21 +03:00
|
|
|
// nolint:staticcheck
|
2022-12-25 15:35:05 +03:00
|
|
|
res, err := w.Exec(values)
|
|
|
|
if err != nil {
|
|
|
|
span.AddLabels("error", true)
|
2022-12-31 15:50:03 +03:00
|
|
|
span.AddLabels("err", err.Error())
|
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
|
|
|
}
|
2021-10-27 18:49:33 +03:00
|
|
|
if queryerContext, ok := w.stmt.(driver.QueryerContext); ok {
|
|
|
|
rows, err := queryerContext.QueryContext(nctx, query, args)
|
2022-12-25 15:35:05 +03:00
|
|
|
if err != nil {
|
|
|
|
span.AddLabels("error", true)
|
2022-12-31 15:50:03 +03:00
|
|
|
span.AddLabels("err", err.Error())
|
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-05 16:03:21 +03:00
|
|
|
span.AddLabels("error", true)
|
|
|
|
span.AddLabels("err", err.Error())
|
2021-10-27 18:49:33 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-05 16:03:21 +03:00
|
|
|
// nolint:staticcheck
|
2022-12-25 15:35:05 +03:00
|
|
|
rows, err := w.Query(values)
|
|
|
|
if err != nil {
|
|
|
|
span.AddLabels("error", true)
|
2022-12-31 15:50:03 +03:00
|
|
|
span.AddLabels("err", err.Error())
|
2022-12-25 15:35:05 +03:00
|
|
|
}
|
|
|
|
return rows, err
|
2021-10-27 18:49:33 +03:00
|
|
|
}
|