diff --git a/go/vt/vtgate/plugin_mysql_server.go b/go/vt/vtgate/plugin_mysql_server.go index b4b9d5726d6..735814bb04b 100644 --- a/go/vt/vtgate/plugin_mysql_server.go +++ b/go/vt/vtgate/plugin_mysql_server.go @@ -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) { diff --git a/go/vt/vtgate/plugin_mysql_server_test.go b/go/vt/vtgate/plugin_mysql_server_test.go index 711b1ff861b..eebec610326 100644 --- a/go/vt/vtgate/plugin_mysql_server_test.go +++ b/go/vt/vtgate/plugin_mysql_server_test.go @@ -17,6 +17,7 @@ limitations under the License. package vtgate import ( + "fmt" "io/ioutil" "os" "path" @@ -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) @@ -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)