Skip to content

Commit b6ada0e

Browse files
committed
sql: finish root span of the query
Fixes #853 Signed-off-by: Miguel Molina <miguel@erizocosmi.co>
1 parent fb6f70e commit b6ada0e

File tree

4 files changed

+56
-12
lines changed

4 files changed

+56
-12
lines changed

engine_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"testing"
1010
"time"
1111

12+
"github.com/opentracing/opentracing-go"
13+
1214
sqle "github.com/src-d/go-mysql-server"
1315
"github.com/src-d/go-mysql-server/auth"
1416
"github.com/src-d/go-mysql-server/memory"
@@ -3420,6 +3422,32 @@ func TestDeleteFromErrors(t *testing.T) {
34203422
}
34213423
}
34223424

3425+
type mockSpan struct {
3426+
opentracing.Span
3427+
finished bool
3428+
}
3429+
3430+
func (m *mockSpan) Finish() {
3431+
m.finished = true
3432+
}
3433+
3434+
func TestRootSpanFinish(t *testing.T) {
3435+
e := newEngine(t)
3436+
fakeSpan := &mockSpan{Span: opentracing.NoopTracer{}.StartSpan("")}
3437+
ctx := sql.NewContext(
3438+
context.Background(),
3439+
sql.WithRootSpan(fakeSpan),
3440+
)
3441+
3442+
_, iter, err := e.Query(ctx, "SELECT 1")
3443+
require.NoError(t, err)
3444+
3445+
_, err = sql.RowIterToRows(iter)
3446+
require.NoError(t, err)
3447+
3448+
require.True(t, fakeSpan.finished)
3449+
}
3450+
34233451
var generatorQueries = []struct {
34243452
query string
34253453
expected []sql.Row

server/context.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,13 @@ func (s *SessionManager) NewContextWithQuery(
9292
s.mu.Unlock()
9393

9494
context := sql.NewContext(
95-
opentracing.ContextWithSpan(
96-
context.Background(),
97-
s.tracer.StartSpan("query"),
98-
),
95+
context.Background(),
9996
sql.WithSession(sess),
10097
sql.WithTracer(s.tracer),
10198
sql.WithPid(s.nextPid()),
10299
sql.WithQuery(query),
103100
sql.WithMemoryManager(s.memory),
101+
sql.WithRootSpan(s.tracer.StartSpan("query")),
104102
)
105103

106104
return context

sql/analyzer/process.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,10 @@ func trackProcess(ctx *sql.Context, a *Analyzer, n sql.Node) (sql.Node, error) {
8585
return nil, err
8686
}
8787

88-
return plan.NewQueryProcess(node, func() { processList.Done(ctx.Pid()) }), nil
88+
return plan.NewQueryProcess(node, func() {
89+
processList.Done(ctx.Pid())
90+
if span := ctx.RootSpan(); span != nil {
91+
span.Finish()
92+
}
93+
}), nil
8994
}

sql/session.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,10 +211,11 @@ func NewBaseSession() Session {
211211
type Context struct {
212212
context.Context
213213
Session
214-
Memory *MemoryManager
215-
pid uint64
216-
query string
217-
tracer opentracing.Tracer
214+
Memory *MemoryManager
215+
pid uint64
216+
query string
217+
tracer opentracing.Tracer
218+
rootSpan opentracing.Span
218219
}
219220

220221
// ContextOption is a function to configure the context.
@@ -255,6 +256,13 @@ func WithMemoryManager(m *MemoryManager) ContextOption {
255256
}
256257
}
257258

259+
// WithRootSpan sets the root span of the context.
260+
func WithRootSpan(s opentracing.Span) ContextOption {
261+
return func(ctx *Context) {
262+
ctx.rootSpan = s
263+
}
264+
}
265+
258266
// NewContext creates a new query context. Options can be passed to configure
259267
// the context. If some aspect of the context is not configure, the default
260268
// value will be used.
@@ -264,7 +272,7 @@ func NewContext(
264272
ctx context.Context,
265273
opts ...ContextOption,
266274
) *Context {
267-
c := &Context{ctx, NewBaseSession(), nil, 0, "", opentracing.NoopTracer{}}
275+
c := &Context{ctx, NewBaseSession(), nil, 0, "", opentracing.NoopTracer{}, nil}
268276
for _, opt := range opts {
269277
opt(c)
270278
}
@@ -298,12 +306,17 @@ func (c *Context) Span(
298306
span := c.tracer.StartSpan(opName, opts...)
299307
ctx := opentracing.ContextWithSpan(c.Context, span)
300308

301-
return span, &Context{ctx, c.Session, c.Memory, c.Pid(), c.Query(), c.tracer}
309+
return span, &Context{ctx, c.Session, c.Memory, c.Pid(), c.Query(), c.tracer, c.rootSpan}
302310
}
303311

304312
// WithContext returns a new context with the given underlying context.
305313
func (c *Context) WithContext(ctx context.Context) *Context {
306-
return &Context{ctx, c.Session, c.Memory, c.Pid(), c.Query(), c.tracer}
314+
return &Context{ctx, c.Session, c.Memory, c.Pid(), c.Query(), c.tracer, c.rootSpan}
315+
}
316+
317+
// RootSpan returns the root span, if any.
318+
func (c *Context) RootSpan() opentracing.Span {
319+
return c.rootSpan
307320
}
308321

309322
// Error adds an error as warning to the session.

0 commit comments

Comments
 (0)