-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Implement appendCallerID feature in vtgate executor #18635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
corbantek
wants to merge
3
commits into
vitessio:main
Choose a base branch
from
HubSpot:append-caller-id-external
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+201
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package vtgate | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "vitess.io/vitess/go/vt/callerid" | ||
| ) | ||
|
|
||
| // appends the immediateCallerID to a SQL query as a trailing comment. | ||
| // the comment persists through MySQL to the general logs. | ||
| func addCallerIDUserToQuery(ctx context.Context, sql string) string { | ||
| safeCallerID := strings.Split(callerid.ImmediateCallerIDFromContext(ctx).GetUsername(), " ")[0] | ||
| return fmt.Sprintf("%s/* user:%s */", sql, safeCallerID) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| /* | ||
| Copyright 2019 The Vitess Authors. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package vtgate | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "vitess.io/vitess/go/vt/callerid" | ||
| ) | ||
|
|
||
| func TestAddCallerIDUserToQuery(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| ctx context.Context | ||
| sql string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "valid caller ID with simple username", | ||
| ctx: callerid.NewContext(context.Background(), nil, callerid.NewImmediateCallerID("testuser")), | ||
| sql: "SELECT * FROM users", | ||
| expected: "SELECT * FROM users/* user:testuser */", | ||
| }, | ||
| { | ||
| name: "valid caller ID with username containing spaces", | ||
| ctx: callerid.NewContext(context.Background(), nil, callerid.NewImmediateCallerID("test user with spaces")), | ||
| sql: "SELECT * FROM users", | ||
| expected: "SELECT * FROM users/* user:test */", | ||
| }, | ||
| { | ||
| name: "valid caller ID with username containing multiple spaces", | ||
| ctx: callerid.NewContext(context.Background(), nil, callerid.NewImmediateCallerID("first second third")), | ||
| sql: "INSERT INTO table VALUES (1)", | ||
| expected: "INSERT INTO table VALUES (1)/* user:first */", | ||
| }, | ||
| { | ||
| name: "valid caller ID with empty username", | ||
| ctx: callerid.NewContext(context.Background(), nil, callerid.NewImmediateCallerID("")), | ||
| sql: "DELETE FROM table", | ||
| expected: "DELETE FROM table/* user: */", | ||
| }, | ||
| { | ||
| name: "nil caller ID", | ||
| ctx: callerid.NewContext(context.Background(), nil, nil), | ||
| sql: "UPDATE table SET col=1", | ||
| expected: "UPDATE table SET col=1/* user: */", | ||
| }, | ||
| { | ||
| name: "context without caller ID", | ||
| ctx: context.Background(), | ||
| sql: "SHOW TABLES", | ||
| expected: "SHOW TABLES/* user: */", | ||
| }, | ||
| { | ||
| name: "empty SQL query", | ||
| ctx: callerid.NewContext(context.Background(), nil, callerid.NewImmediateCallerID("user")), | ||
| sql: "", | ||
| expected: "/* user:user */", | ||
| }, | ||
| { | ||
| name: "complex SQL query with caller ID", | ||
| ctx: callerid.NewContext(context.Background(), nil, callerid.NewImmediateCallerID("complex_user")), | ||
| sql: "SELECT u.id, u.name FROM users u JOIN orders o ON u.id = o.user_id WHERE u.status = 'active'", | ||
| expected: "SELECT u.id, u.name FROM users u JOIN orders o ON u.id = o.user_id WHERE u.status = 'active'/* user:complex_user */", | ||
| }, | ||
| { | ||
| name: "SQL query with existing comment", | ||
| ctx: callerid.NewContext(context.Background(), nil, callerid.NewImmediateCallerID("user")), | ||
| sql: "SELECT * FROM table /* existing comment */", | ||
| expected: "SELECT * FROM table /* existing comment *//* user:user */", | ||
| }, | ||
| { | ||
| name: "username with leading/trailing spaces", | ||
| ctx: callerid.NewContext(context.Background(), nil, callerid.NewImmediateCallerID(" trimmed ")), | ||
| sql: "SELECT 1", | ||
| expected: "SELECT 1/* user: */", | ||
| }, | ||
| { | ||
| name: "username that is only spaces", | ||
| ctx: callerid.NewContext(context.Background(), nil, callerid.NewImmediateCallerID(" ")), | ||
| sql: "SELECT 1", | ||
| expected: "SELECT 1/* user: */", | ||
| }, | ||
| { | ||
| name: "single character username", | ||
| ctx: callerid.NewContext(context.Background(), nil, callerid.NewImmediateCallerID("x")), | ||
| sql: "SELECT 1", | ||
| expected: "SELECT 1/* user:x */", | ||
| }, | ||
| { | ||
| name: "username with special characters", | ||
| ctx: callerid.NewContext(context.Background(), nil, callerid.NewImmediateCallerID("[email protected]")), | ||
| sql: "SELECT 1", | ||
| expected: "SELECT 1/* user:[email protected] */", | ||
| }, | ||
| { | ||
| name: "username with special characters and spaces", | ||
| ctx: callerid.NewContext(context.Background(), nil, callerid.NewImmediateCallerID("[email protected] admin role")), | ||
| sql: "SELECT 1", | ||
| expected: "SELECT 1/* user:[email protected] */", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| result := addCallerIDUserToQuery(tt.ctx, tt.sql) | ||
| assert.Equal(t, tt.expected, result) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // TestAddCallerIDUserToQuery_EdgeCases tests additional edge cases to ensure 100% coverage | ||
| func TestAddCallerIDUserToQuery_EdgeCases(t *testing.T) { | ||
| t.Run("multiline SQL query", func(t *testing.T) { | ||
| ctx := callerid.NewContext(context.Background(), nil, callerid.NewImmediateCallerID("multiline_user")) | ||
| sql := `SELECT * | ||
| FROM users | ||
| WHERE id = 1` | ||
| expected := `SELECT * | ||
| FROM users | ||
| WHERE id = 1/* user:multiline_user */` | ||
| result := addCallerIDUserToQuery(ctx, sql) | ||
| assert.Equal(t, expected, result) | ||
| }) | ||
|
|
||
| t.Run("SQL with newlines and tabs", func(t *testing.T) { | ||
| ctx := callerid.NewContext(context.Background(), nil, callerid.NewImmediateCallerID("tab_user")) | ||
| sql := "SELECT\t*\nFROM\ttable" | ||
| expected := "SELECT\t*\nFROM\ttable/* user:tab_user */" | ||
| result := addCallerIDUserToQuery(ctx, sql) | ||
| assert.Equal(t, expected, result) | ||
| }) | ||
|
|
||
| t.Run("very long username with spaces", func(t *testing.T) { | ||
| longUsername := "very long username with many spaces and words that should be truncated at first space" | ||
| ctx := callerid.NewContext(context.Background(), nil, callerid.NewImmediateCallerID(longUsername)) | ||
| sql := "SELECT 1" | ||
| expected := "SELECT 1/* user:very */" | ||
| result := addCallerIDUserToQuery(ctx, sql) | ||
| assert.Equal(t, expected, result) | ||
| }) | ||
| } | ||
|
|
||
| // TestAddCallerIDUserToQuery_Consistency tests that the function consistently behaves the same way | ||
| func TestAddCallerIDUserToQuery_Consistency(t *testing.T) { | ||
| ctx := callerid.NewContext(context.Background(), nil, callerid.NewImmediateCallerID("consistent_user")) | ||
| sql := "SELECT * FROM table" | ||
| expected := "SELECT * FROM table/* user:consistent_user */" | ||
|
|
||
| // Call the function multiple times to ensure consistency | ||
| for i := 0; i < 5; i++ { | ||
| result := addCallerIDUserToQuery(ctx, sql) | ||
| assert.Equal(t, expected, result, "Function should return consistent results on call %d", i+1) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can bloat the plan cache and more planner work for the prepared statement queries.
I would suggest we move this logic either in vttablet or add it directly as margin comments when we create the vcursol_impl object.