-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
feat: add query-log-path
configuration
#25710
Draft
devanbenz
wants to merge
15
commits into
master-1.x
Choose a base branch
from
db/459/query-loggin
base: master-1.x
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.
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
410241e
feat: add query-log-path configuration
devanbenz 9e9c572
feat: flush and close file handlers on error
devanbenz 0e90db3
feat: create function to close query log file
devanbenz 96565b9
feat: remove some white space
devanbenz 4a687e4
feat: Add test for removing file and renaming file
devanbenz 3abf661
feat: Use temp file
devanbenz 144556e
feat: Add some more time for the test. fsnotify may be slower in CI
devanbenz fae0009
feat: working on modifying code -- small refactor to make testing easier
devanbenz 03384a1
feat: checkfmt
devanbenz 9708d3d
feat: working on it
devanbenz dd42868
feat: Refactor to use an interface for log file watching
devanbenz c1610b8
feat: formatting
devanbenz 79bbe80
feat: Modified testing, IO bound testing required me to do a few mute…
devanbenz 89bf0c6
feat: adds a bunch of locks. Need to keep refactoring
devanbenz 93622fc
feat: Use existing config items for logger
devanbenz 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 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 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 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 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 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 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 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 |
---|---|---|
@@ -1,9 +1,15 @@ | ||
package query_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
"os" | ||
"strings" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -595,6 +601,172 @@ func TestQueryExecutor_InvalidSource(t *testing.T) { | |
} | ||
} | ||
|
||
type mockWatcher struct { | ||
ChangeEvents []string | ||
path string | ||
tmpFile *os.File | ||
log *zap.Logger | ||
t *testing.T | ||
Mu sync.Mutex | ||
} | ||
|
||
func newMockWatcher(t *testing.T, path string, tmpFile *os.File) *mockWatcher { | ||
encoderConfig := zap.NewProductionEncoderConfig() | ||
|
||
fileCore := zapcore.NewCore( | ||
zapcore.NewJSONEncoder(encoderConfig), | ||
zapcore.Lock(tmpFile), | ||
zapcore.InfoLevel, | ||
) | ||
|
||
logger := zap.New(fileCore) | ||
return &mockWatcher{ | ||
ChangeEvents: make([]string, 0), | ||
path: path, | ||
tmpFile: tmpFile, | ||
log: logger, | ||
t: t, | ||
Mu: sync.Mutex{}, | ||
} | ||
} | ||
|
||
func (m *mockWatcher) GetLogger() *zap.Logger { | ||
return m.log | ||
} | ||
|
||
func (m *mockWatcher) GetLogPath() string { | ||
return m.path | ||
} | ||
|
||
func (m *mockWatcher) FileChangeCapture() error { | ||
m.Mu.Lock() | ||
defer m.Mu.Unlock() | ||
m.ChangeEvents = append(m.ChangeEvents, "file updated") | ||
return nil | ||
} | ||
|
||
func (m *mockWatcher) Close() { | ||
return | ||
} | ||
|
||
func TestQueryExecutor_WriteQueryToLog(t *testing.T) { | ||
q, err := influxql.ParseQuery(`SELECT count(value) FROM cpu`) | ||
require.NoError(t, err, "parse query") | ||
|
||
f, err := os.CreateTemp("", "query-test.1.log") | ||
require.NoError(t, err, "create temp file") | ||
err = f.Sync() | ||
require.NoError(t, err, "sync temp file") | ||
|
||
e := NewQueryExecutor() | ||
mockWatcher := newMockWatcher(t, f.Name(), f) | ||
e.WithLogWriter(mockWatcher, context.Background()) | ||
|
||
e.StatementExecutor = &StatementExecutor{ | ||
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error { | ||
require.Equal(t, uint64(1), ctx.QueryID, "query ID") | ||
return nil | ||
}, | ||
} | ||
|
||
discardOutput(e.ExecuteQuery(q, query.ExecutionOptions{}, nil)) | ||
err = f.Sync() | ||
require.NoError(t, err, "sync temp file") | ||
|
||
dat, err := os.ReadFile(f.Name()) | ||
cont := strings.Contains(string(dat), "SELECT count(value) FROM cpu") | ||
require.True(t, cont, "expected query output") | ||
err = os.Remove(f.Name()) | ||
require.NoError(t, err, "remove temp file") | ||
} | ||
|
||
// Test to ensure that Watcher creates new file on file rename | ||
func TestQueryExecutor_WriteQueryToLog_WatcherRemoveFile(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It appears that CI doesn't like these tests where on my local machine they pass no problem. I may need to adjust how I'm doing this 🤔 |
||
q, err := influxql.ParseQuery(`SELECT count(value) FROM cpu`) | ||
require.NoError(t, err, "parse query") | ||
|
||
f, err := os.CreateTemp("", "query-test.2.log") | ||
require.NoError(t, err, "create temp file") | ||
err = f.Sync() | ||
require.NoError(t, err, "sync temp file") | ||
|
||
e := NewQueryExecutor() | ||
mockWatcher := newMockWatcher(t, f.Name(), f) | ||
e.WithLogWriter(mockWatcher, context.Background()) | ||
|
||
e.StatementExecutor = &StatementExecutor{ | ||
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error { | ||
require.Equal(t, uint64(1), ctx.QueryID, "query ID") | ||
return nil | ||
}, | ||
} | ||
|
||
discardOutput(e.ExecuteQuery(q, query.ExecutionOptions{}, nil)) | ||
err = f.Sync() | ||
require.NoError(t, err, "sync temp file") | ||
|
||
dat, err := os.ReadFile(f.Name()) | ||
cont := strings.Contains(string(dat), "SELECT count(value) FROM cpu") | ||
require.True(t, cont, "expected query output") | ||
require.Equal(t, 0, len(mockWatcher.ChangeEvents), "expected change events length") | ||
|
||
err = f.Close() | ||
require.NoError(t, err, "close temp file") | ||
// Remove file -- there should be a change event now | ||
err = os.Remove(f.Name()) | ||
require.NoError(t, err, "remove temp file") | ||
// sleep for a few ms because fsnotify needs to pick up event | ||
time.Sleep(100 * time.Millisecond) | ||
mockWatcher.Mu.Lock() | ||
defer mockWatcher.Mu.Unlock() | ||
require.Equal(t, 1, len(mockWatcher.ChangeEvents), "expected change events length") | ||
} | ||
|
||
// Test to ensure that Watcher creates new file on file rename | ||
func TestQueryExecutor_WriteQueryToLog_WatcherChangeFile(t *testing.T) { | ||
q, err := influxql.ParseQuery(`SELECT count(value) FROM cpu`) | ||
require.NoError(t, err, "parse query") | ||
|
||
f, err := os.CreateTemp("", "query-test.3.log") | ||
require.NoError(t, err, "create temp file") | ||
err = f.Sync() | ||
require.NoError(t, err, "sync temp file") | ||
|
||
e := NewQueryExecutor() | ||
mockWatcher := newMockWatcher(t, f.Name(), f) | ||
e.WithLogWriter(mockWatcher, context.Background()) | ||
|
||
e.StatementExecutor = &StatementExecutor{ | ||
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error { | ||
require.Equal(t, uint64(1), ctx.QueryID, "query ID") | ||
return nil | ||
}, | ||
} | ||
|
||
discardOutput(e.ExecuteQuery(q, query.ExecutionOptions{}, nil)) | ||
err = f.Sync() | ||
require.NoError(t, err, "sync temp file") | ||
|
||
dat, err := os.ReadFile(f.Name()) | ||
cont := strings.Contains(string(dat), "SELECT count(value) FROM cpu") | ||
require.True(t, cont, "expected query output") | ||
mockWatcher.Mu.Lock() | ||
require.Equal(t, 0, len(mockWatcher.ChangeEvents), "expected change events length") | ||
mockWatcher.Mu.Unlock() | ||
|
||
// Remove file -- there should be a change event now | ||
err = os.Rename(f.Name(), f.Name()+".foo") | ||
require.NoError(t, err, "rename temp file") | ||
// sleep for a few ms because fsnotify needs to pick up event | ||
time.Sleep(100 * time.Millisecond) | ||
|
||
mockWatcher.Mu.Lock() | ||
require.Equal(t, 1, len(mockWatcher.ChangeEvents), "expected change events length") | ||
mockWatcher.Mu.Unlock() | ||
err = os.Remove(f.Name() + ".foo") | ||
require.NoError(t, err, "remove temp file") | ||
} | ||
|
||
func discardOutput(results <-chan *query.Result) { | ||
for range results { | ||
// Read all results and discard. | ||
|
Oops, something went wrong.
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.
Shouldn't the default be the empty string?