/
driver.go
68 lines (54 loc) · 1.67 KB
/
driver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package analytics
import (
"context"
"database/sql/driver"
"time"
"github.com/Juniper/contrail/pkg/collector"
)
const (
commitOperation = "COMMIT"
sqlApplication = "SQL"
)
// WithCommitLatencyReporting returns database driver wrapper reporting database commit latency.
func WithCommitLatencyReporting(c collector.Collector) func(driver.Driver) driver.Driver {
if c == nil {
return func(d driver.Driver) driver.Driver { return d }
}
return func(d driver.Driver) driver.Driver {
return &latencyReportingDriver{Driver: d, c: c}
}
}
type latencyReportingDriver struct {
driver.Driver
c collector.Collector
}
// Open opens database connection using underlying database driver and returns wrapped connection.
func (l *latencyReportingDriver) Open(name string) (driver.Conn, error) {
conn, err := l.Driver.Open(name)
return &latencyReportingConn{Conn: conn, c: l.c}, err
}
type latencyReportingConn struct {
driver.Conn
c collector.Collector
}
// BeginTx begins transaction using underlying database driver and wraps wrapped transaction.
func (l *latencyReportingConn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error) {
t, err := l.Conn.Begin()
return &latencyReportingTx{Tx: t, ctx: ctx, c: l.c}, err
}
type latencyReportingTx struct {
driver.Tx
ctx context.Context
c collector.Collector
}
// Commit commits transaction using underlying database driver and reports database commit latency.
func (l *latencyReportingTx) Commit() error {
start := time.Now()
if err := l.Tx.Commit(); err != nil {
return err
}
l.c.Send(
VncAPILatencyStatsLog(l.ctx, commitOperation, sqlApplication, int64(time.Since(start)/time.Microsecond)),
)
return nil
}