Skip to content
This repository was archived by the owner on Jan 28, 2021. It is now read-only.

Commit 5077ae6

Browse files
authored
Merge pull request #470 from erizocosmico/trace-timing
sql: add timing info to span traces
2 parents 380ccef + 3e24d6a commit 5077ae6

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

sql/session.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func (s *BaseSession) GetAll() map[string]TypedValue {
8383
// ID implements the Session interface.
8484
func (s *BaseSession) ID() uint32 { return s.id }
8585

86+
// TypedValue is a value along with its type.
8687
type TypedValue struct {
8788
Typ Type
8889
Value interface{}
@@ -192,21 +193,38 @@ func (c *Context) WithContext(ctx context.Context) *Context {
192193

193194
// NewSpanIter creates a RowIter executed in the given span.
194195
func NewSpanIter(span opentracing.Span, iter RowIter) RowIter {
195-
return &spanIter{span, iter, 0, false}
196+
return &spanIter{
197+
span: span,
198+
iter: iter,
199+
}
196200
}
197201

198202
type spanIter struct {
199203
span opentracing.Span
200204
iter RowIter
201205
count int
206+
max time.Duration
207+
min time.Duration
208+
total time.Duration
202209
done bool
203210
}
204211

205-
func (i *spanIter) Next() (Row, error) {
206-
if i.done {
207-
return nil, io.EOF
212+
func (i *spanIter) updateTimings(start time.Time) {
213+
elapsed := time.Since(start)
214+
if i.max < elapsed {
215+
i.max = elapsed
208216
}
209217

218+
if i.min > elapsed || i.min == 0 {
219+
i.min = elapsed
220+
}
221+
222+
i.total += elapsed
223+
}
224+
225+
func (i *spanIter) Next() (Row, error) {
226+
start := time.Now()
227+
210228
row, err := i.iter.Next()
211229
if err == io.EOF {
212230
i.finish()
@@ -219,15 +237,27 @@ func (i *spanIter) Next() (Row, error) {
219237
}
220238

221239
i.count++
240+
i.updateTimings(start)
222241
return row, nil
223242
}
224243

225244
func (i *spanIter) finish() {
245+
var avg time.Duration
246+
if i.count > 0 {
247+
avg = i.total / time.Duration(i.count)
248+
}
249+
226250
i.span.FinishWithOptions(opentracing.FinishOptions{
227251
LogRecords: []opentracing.LogRecord{
228252
{
229253
Timestamp: time.Now(),
230-
Fields: []log.Field{log.Int("rows", i.count)},
254+
Fields: []log.Field{
255+
log.Int("rows", i.count),
256+
log.String("total_time", i.total.String()),
257+
log.String("max_time", i.max.String()),
258+
log.String("min_time", i.min.String()),
259+
log.String("avg_time", avg.String()),
260+
},
231261
},
232262
},
233263
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy