forked from DataDog/dd-trace-go
/
leveldb.go
277 lines (242 loc) · 7.81 KB
/
leveldb.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2019 Datadog, Inc.
// Package leveldb provides functions to trace the syndtr/goleveldb package (https://github.com/syndtr/goleveldb).
package leveldb // import "gopkg.in/DataDog/dd-trace-go.v1/contrib/syndtr/goleveldb/leveldb"
import (
"context"
"math"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/iterator"
"github.com/syndtr/goleveldb/leveldb/opt"
"github.com/syndtr/goleveldb/leveldb/storage"
"github.com/syndtr/goleveldb/leveldb/util"
)
// A DB wraps a leveldb.DB and traces all queries.
type DB struct {
*leveldb.DB
cfg *config
}
// Open calls leveldb.Open and wraps the resulting DB.
func Open(stor storage.Storage, o *opt.Options, opts ...Option) (*DB, error) {
db, err := leveldb.Open(stor, o)
if err != nil {
return nil, err
}
return WrapDB(db, opts...), nil
}
// OpenFile calls leveldb.OpenFile and wraps the resulting DB.
func OpenFile(path string, o *opt.Options, opts ...Option) (*DB, error) {
db, err := leveldb.OpenFile(path, o)
if err != nil {
return nil, err
}
return WrapDB(db, opts...), nil
}
// WrapDB wraps a leveldb.DB so that queries are traced.
func WrapDB(db *leveldb.DB, opts ...Option) *DB {
return &DB{
DB: db,
cfg: newConfig(opts...),
}
}
// WithContext returns a new DB with the context set to ctx.
func (db *DB) WithContext(ctx context.Context) *DB {
newcfg := *db.cfg
newcfg.ctx = ctx
return &DB{
DB: db.DB,
cfg: &newcfg,
}
}
// CompactRange calls DB.CompactRange and traces the result.
func (db *DB) CompactRange(r util.Range) error {
span := startSpan(db.cfg, "CompactRange")
err := db.DB.CompactRange(r)
span.Finish(tracer.WithError(err))
return err
}
// Delete calls DB.Delete and traces the result.
func (db *DB) Delete(key []byte, wo *opt.WriteOptions) error {
span := startSpan(db.cfg, "Delete")
err := db.DB.Delete(key, wo)
span.Finish(tracer.WithError(err))
return err
}
// Get calls DB.Get and traces the result.
func (db *DB) Get(key []byte, ro *opt.ReadOptions) (value []byte, err error) {
span := startSpan(db.cfg, "Get")
value, err = db.DB.Get(key, ro)
span.Finish(tracer.WithError(err))
return value, err
}
// GetSnapshot calls DB.GetSnapshot and returns a wrapped Snapshot.
func (db *DB) GetSnapshot() (*Snapshot, error) {
snap, err := db.DB.GetSnapshot()
if err != nil {
return nil, err
}
return WrapSnapshot(snap, func(cfg *config) {
*cfg = *db.cfg
}), nil
}
// Has calls DB.Has and traces the result.
func (db *DB) Has(key []byte, ro *opt.ReadOptions) (ret bool, err error) {
span := startSpan(db.cfg, "Has")
ret, err = db.DB.Has(key, ro)
span.Finish(tracer.WithError(err))
return ret, err
}
// NewIterator calls DB.NewIterator and returns a wrapped Iterator.
func (db *DB) NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator {
return WrapIterator(db.DB.NewIterator(slice, ro), func(cfg *config) {
*cfg = *db.cfg
})
}
// OpenTransaction calls DB.OpenTransaction and returns a wrapped Transaction.
func (db *DB) OpenTransaction() (*Transaction, error) {
tr, err := db.DB.OpenTransaction()
if err != nil {
return nil, err
}
return WrapTransaction(tr, func(cfg *config) {
*cfg = *db.cfg
}), nil
}
// Put calls DB.Put and traces the result.
func (db *DB) Put(key, value []byte, wo *opt.WriteOptions) error {
span := startSpan(db.cfg, "Put")
err := db.DB.Put(key, value, wo)
span.Finish(tracer.WithError(err))
return err
}
// Write calls DB.Write and traces the result.
func (db *DB) Write(batch *leveldb.Batch, wo *opt.WriteOptions) error {
span := startSpan(db.cfg, "Write")
err := db.DB.Write(batch, wo)
span.Finish(tracer.WithError(err))
return err
}
// A Snapshot wraps a leveldb.Snapshot and traces all queries.
type Snapshot struct {
*leveldb.Snapshot
cfg *config
}
// WrapSnapshot wraps a leveldb.Snapshot so that queries are traced.
func WrapSnapshot(snap *leveldb.Snapshot, opts ...Option) *Snapshot {
return &Snapshot{
Snapshot: snap,
cfg: newConfig(opts...),
}
}
// WithContext returns a new Snapshot with the context set to ctx.
func (snap *Snapshot) WithContext(ctx context.Context) *Snapshot {
newcfg := *snap.cfg
newcfg.ctx = ctx
return &Snapshot{
Snapshot: snap.Snapshot,
cfg: &newcfg,
}
}
// Get calls Snapshot.Get and traces the result.
func (snap *Snapshot) Get(key []byte, ro *opt.ReadOptions) (value []byte, err error) {
span := startSpan(snap.cfg, "Get")
value, err = snap.Snapshot.Get(key, ro)
span.Finish(tracer.WithError(err))
return value, err
}
// Has calls Snapshot.Has and traces the result.
func (snap *Snapshot) Has(key []byte, ro *opt.ReadOptions) (ret bool, err error) {
span := startSpan(snap.cfg, "Has")
ret, err = snap.Snapshot.Has(key, ro)
span.Finish(tracer.WithError(err))
return ret, err
}
// NewIterator calls Snapshot.NewIterator and returns a wrapped Iterator.
func (snap *Snapshot) NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator {
return WrapIterator(snap.Snapshot.NewIterator(slice, ro), func(cfg *config) {
*cfg = *snap.cfg
})
}
// A Transaction wraps a leveldb.Transaction and traces all queries.
type Transaction struct {
*leveldb.Transaction
cfg *config
}
// WrapTransaction wraps a leveldb.Transaction so that queries are traced.
func WrapTransaction(tr *leveldb.Transaction, opts ...Option) *Transaction {
return &Transaction{
Transaction: tr,
cfg: newConfig(opts...),
}
}
// WithContext returns a new Transaction with the context set to ctx.
func (tr *Transaction) WithContext(ctx context.Context) *Transaction {
newcfg := *tr.cfg
newcfg.ctx = ctx
return &Transaction{
Transaction: tr.Transaction,
cfg: &newcfg,
}
}
// Commit calls Transaction.Commit and traces the result.
func (tr *Transaction) Commit() error {
span := startSpan(tr.cfg, "Commit")
err := tr.Transaction.Commit()
span.Finish(tracer.WithError(err))
return err
}
// Get calls Transaction.Get and traces the result.
func (tr *Transaction) Get(key []byte, ro *opt.ReadOptions) ([]byte, error) {
span := startSpan(tr.cfg, "Get")
value, err := tr.Transaction.Get(key, ro)
span.Finish(tracer.WithError(err))
return value, err
}
// Has calls Transaction.Has and traces the result.
func (tr *Transaction) Has(key []byte, ro *opt.ReadOptions) (bool, error) {
span := startSpan(tr.cfg, "Has")
ret, err := tr.Transaction.Has(key, ro)
span.Finish(tracer.WithError(err))
return ret, err
}
// NewIterator calls Transaction.NewIterator and returns a wrapped Iterator.
func (tr *Transaction) NewIterator(slice *util.Range, ro *opt.ReadOptions) iterator.Iterator {
return WrapIterator(tr.Transaction.NewIterator(slice, ro), func(cfg *config) {
*cfg = *tr.cfg
})
}
// An Iterator wraps a leveldb.Iterator and traces until Release is called.
type Iterator struct {
iterator.Iterator
span ddtrace.Span
}
// WrapIterator wraps a leveldb.Iterator so that queries are traced.
func WrapIterator(it iterator.Iterator, opts ...Option) *Iterator {
return &Iterator{
Iterator: it,
span: startSpan(newConfig(opts...), "Iterator"),
}
}
// Release calls Iterator.Release and traces the result.
func (it *Iterator) Release() {
err := it.Error()
it.Iterator.Release()
it.span.Finish(tracer.WithError(err))
}
func startSpan(cfg *config, name string) ddtrace.Span {
opts := []ddtrace.StartSpanOption{
tracer.SpanType(ext.SpanTypeLevelDB),
tracer.ServiceName(cfg.serviceName),
tracer.ResourceName(name),
}
if !math.IsNaN(cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
}
span, _ := tracer.StartSpanFromContext(cfg.ctx, "leveldb.query", opts...)
return span
}