forked from nhatthm/otelsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
row.go
205 lines (158 loc) · 5.43 KB
/
row.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package otelsql
import (
"context"
"database/sql/driver"
"errors"
"io"
"reflect"
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
xattr "go.nhat.io/otelsql/attribute"
)
const (
traceMethodRowsNext = "rows_next"
traceMethodRowsClose = "rows_close"
)
var _ driver.Rows = (*rows)(nil)
// withRowsColumnTypeScanType is the same as the driver.RowsColumnTypeScanType interface except it omits the driver.Rows embedded interface.
// If the original driver.Rows implementation wrapped by ocsql supports RowsColumnTypeScanType we enable the original method implementation in the returned
// driver.Rows from wrapRows by doing a composition with ocRows.
type withRowsColumnTypeScanType interface {
ColumnTypeScanType(index int) reflect.Type
}
type rowsNextFunc func(dest []driver.Value) (err error)
type rowsCloseFunc func() error
type rowsColumnFunc func() []string
type rows struct {
hasNextResultSetFunc func() bool
nextResultSetFunc func() error
columnTypeDatabaseTypeNameFunc func(index int) string
columnTypeLengthFunc func(index int) (length int64, ok bool)
columnTypeNullableFunc func(index int) (nullable, ok bool)
columnTypePrecisionScaleFunc func(index int) (precision, scale int64, ok bool)
columnsFunc rowsColumnFunc
closeFunc rowsCloseFunc
nextFunc rowsNextFunc
}
func (r rows) HasNextResultSet() bool {
return r.hasNextResultSetFunc()
}
func (r rows) NextResultSet() error {
return r.nextResultSetFunc()
}
func (r rows) ColumnTypeDatabaseTypeName(index int) string {
return r.columnTypeDatabaseTypeNameFunc(index)
}
func (r rows) ColumnTypeLength(index int) (length int64, ok bool) {
return r.columnTypeLengthFunc(index)
}
func (r rows) ColumnTypeNullable(index int) (nullable, ok bool) {
return r.columnTypeNullableFunc(index)
}
func (r rows) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool) {
return r.columnTypePrecisionScaleFunc(index)
}
func (r rows) Columns() []string {
return r.columnsFunc()
}
func (r rows) Close() error {
return r.closeFunc()
}
func (r rows) Next(dest []driver.Value) (err error) {
return r.nextFunc(dest)
}
// nolint: cyclop
func wrapRows(ctx context.Context, parent driver.Rows, t methodTracer, traceRowsNext bool, traceRowsClose bool) driver.Rows {
if !traceRowsNext && !traceRowsClose {
return parent
}
ctx = trace.ContextWithSpanContext(context.Background(), trace.SpanContextFromContext(ctx))
r := rows{
columnsFunc: parent.Columns,
closeFunc: parent.Close,
nextFunc: parent.Next,
hasNextResultSetFunc: func() bool { return false },
nextResultSetFunc: func() error { return io.EOF },
columnTypeDatabaseTypeNameFunc: func(_ int) string { return "" },
columnTypeLengthFunc: func(_ int) (int64, bool) { return 0, false },
columnTypeNullableFunc: func(_ int) (bool, bool) { return false, false },
columnTypePrecisionScaleFunc: func(_ int) (int64, int64, bool) { return 0, 0, false },
}
if traceRowsClose {
successCount, successTotalTime, nextFunc := rowsNextCount(r.nextFunc)
r.nextFunc, r.closeFunc = nextFunc, rowsCloseTrace(ctx, t, successCount, successTotalTime, parent.Close)
}
if traceRowsNext {
r.nextFunc = rowsNextTrace(ctx, t, r.nextFunc)
}
if v, ok := parent.(driver.RowsNextResultSet); ok {
r.hasNextResultSetFunc = v.HasNextResultSet
r.nextResultSetFunc = v.NextResultSet
}
if v, ok := parent.(driver.RowsColumnTypeDatabaseTypeName); ok {
r.columnTypeDatabaseTypeNameFunc = v.ColumnTypeDatabaseTypeName
}
if v, ok := parent.(driver.RowsColumnTypeLength); ok {
r.columnTypeLengthFunc = v.ColumnTypeLength
}
if v, ok := parent.(driver.RowsColumnTypeNullable); ok {
r.columnTypeNullableFunc = v.ColumnTypeNullable
}
if v, ok := parent.(driver.RowsColumnTypePrecisionScale); ok {
r.columnTypePrecisionScaleFunc = v.ColumnTypePrecisionScale
}
if ts, ok := parent.(withRowsColumnTypeScanType); ok {
return struct {
rows
withRowsColumnTypeScanType
}{r, ts}
}
return r
}
func rowsNextTrace(ctx context.Context, t methodTracer, f rowsNextFunc) rowsNextFunc {
return func(dest []driver.Value) (err error) {
_, end := t.MustTrace(ctx, traceMethodRowsNext)
defer func() {
if errors.Is(err, io.EOF) {
end(nil)
} else {
end(err)
}
}()
return f(dest)
}
}
func rowsNextCount(f rowsNextFunc) (*int64, *time.Duration, rowsNextFunc) {
var (
successCount int64
successTotalTime time.Duration
)
return &successCount, &successTotalTime,
func(values []driver.Value) (err error) {
startTime := time.Now()
if err = f(values); err == nil {
successCount++
successTotalTime += time.Since(startTime)
}
return
}
}
func rowsCloseTrace(ctx context.Context, t methodTracer, count *int64, totalTime *time.Duration, f rowsCloseFunc) rowsCloseFunc {
return func() (err error) {
_, end := t.MustTrace(ctx, traceMethodRowsClose)
defer func() {
end(err, rowsCloseAttributes(*count, *totalTime)...)
}()
return f()
}
}
func rowsCloseAttributes(count int64, totalTime time.Duration) []attribute.KeyValue {
attrs := make([]attribute.KeyValue, 0, 2)
attrs = append(attrs, dbSQLRowsNextSuccessCount.Int64(count))
if count < 1 {
return attrs
}
attrs = append(attrs, xattr.KeyValueDuration(dbSQLRowsNextLatencyAvg, time.Duration(int64(totalTime)/count)))
return attrs
}