Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions go/vt/vtgate/plugin_mysql_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,20 +147,25 @@ func startSpanTestable(ctx context.Context, query, label string,
newSpanFromString func(context.Context, string, string) (trace.Span, context.Context, error)) (trace.Span, context.Context, error) {
_, comments := sqlparser.SplitMarginComments(query)
match := r.FindStringSubmatch(comments.Leading)
span, ctx := getSpan(ctx, match, newSpan, label, newSpanFromString)

trace.AnnotateSQL(span, query)

return span, ctx, nil
}

func getSpan(ctx context.Context, match []string, newSpan func(context.Context, string) (trace.Span, context.Context), label string, newSpanFromString func(context.Context, string, string) (trace.Span, context.Context, error)) (trace.Span, context.Context) {
var span trace.Span
if len(match) == 0 {
span, ctx = newSpan(ctx, label)
} else {
if len(match) != 0 {
var err error
span, ctx, err = newSpanFromString(ctx, match[1], label)
if err != nil {
return nil, nil, err
if err == nil {
return span, ctx
}
log.Warningf("Unable to parse VT_SPAN_CONTEXT: %s", err.Error())
}

trace.AnnotateSQL(span, query)

return span, ctx, nil
span, ctx = newSpan(ctx, label)
return span, ctx
}

func startSpan(ctx context.Context, query, label string) (trace.Span, context.Context, error) {
Expand Down
19 changes: 19 additions & 0 deletions go/vt/vtgate/plugin_mysql_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package vtgate

import (
"fmt"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -170,6 +171,12 @@ func newFromStringFail(t *testing.T) func(ctx context.Context, parentSpan string
}
}

func newFromStringError(t *testing.T) func(ctx context.Context, parentSpan string, label string) (trace.Span, context.Context, error) {
return func(ctx context.Context, parentSpan string, label string) (trace.Span, context.Context, error) {
return trace.NoopSpan{}, context.Background(), fmt.Errorf("")
}
}

func newFromStringExpect(t *testing.T, expected string) func(ctx context.Context, parentSpan string, label string) (trace.Span, context.Context, error) {
return func(ctx context.Context, parentSpan string, label string) (trace.Span, context.Context, error) {
assert.Equal(t, expected, parentSpan)
Expand Down Expand Up @@ -206,6 +213,18 @@ func TestSpanContextPassedInEvenAroundOtherComments(t *testing.T) {
assert.NoError(t, err)
}

func TestSpanContextNotParsable(t *testing.T) {
hasRun := false
_, _, err := startSpanTestable(context.Background(), "/*VT_SPAN_CONTEXT=123*/SQL QUERY", "someLabel",
func(c context.Context, s string) (trace.Span, context.Context) {
hasRun = true
return trace.NoopSpan{}, context.Background()
},
newFromStringError(t))
assert.NoError(t, err)
assert.True(t, hasRun, "Should have continued execution despite failure to parse VT_SPAN_CONTEXT")
}

func newTestAuthServerStatic() *mysql.AuthServerStatic {
jsonConfig := "{\"user1\":{\"Password\":\"password1\", \"UserData\":\"userData1\", \"SourceHost\":\"localhost\"}}"
return mysql.NewAuthServerStatic("", jsonConfig, 0)
Expand Down