Skip to content
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
wants to merge 9 commits into
base: master-1.x
Choose a base branch
from

Conversation

devanbenz
Copy link

@devanbenz devanbenz commented Dec 26, 2024

This PR adds the ability for an admin to define 'query-log-path' as a configuration item field.

A new configuration option is created

[data]
# Setting a logging path will enable query logging
query-log-path = "/var/influx/query.log"

This will enable query logging.

Logged queries example:

{"level":"info","ts":1735248393.084461,"msg":"Executing query","query":"SHOW DATABASES"}
{"level":"info","ts":1735248395.092188,"msg":"Executing query","query":"SHOW DATABASES"}
{"level":"info","ts":1735248398.58039,"msg":"Executing query","query":"SELECT * FROM stress.autogen.m0 LIMIT 20"}

This PR adds the ability for an admin to define 'query-log-path' as a configuration item field.

A new configuration option is created

```toml
[data]
query-log-path = "/var/influx/query.log"
```

This will enable query logging.

Logged queries example:
```
{"level":"info","ts":1735248393.084461,"msg":"Executing query","query":"SHOW DATABASES"}
{"level":"info","ts":1735248395.092188,"msg":"Executing query","query":"SHOW DATABASES"}
{"level":"info","ts":1735248398.58039,"msg":"Executing query","query":"SELECT * FROM stress.autogen.m0 LIMIT 20"}
```
@devanbenz devanbenz marked this pull request as ready for review December 30, 2024 16:56
}

// Test to ensure that watcher creates new file on file rename
func TestQueryExecutor_WriteQueryToLog_WatcherRemoveFile(t *testing.T) {
Copy link
Author

Choose a reason for hiding this comment

The 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 🤔

Copy link
Contributor

@davidby-influx davidby-influx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did not look at the tests

@@ -66,6 +66,7 @@
# Whether queries should be logged before execution. Very useful for troubleshooting, but will
# log any sensitive data contained within a query.
# query-log-enabled = true
# query-log-path = "/var/log/influxdb/query.log"
Copy link
Contributor

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?

func initQueryLogWriter(log *zap.Logger, e *Executor, path string) (*os.File, error) {
logFile, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644)
if err != nil {
e.Logger.Error("failed to open log file", zap.Error(err))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fmt.Errorf to put the file path into the error. It can help to pinpoint the problem

encoderConfig := zap.NewProductionEncoderConfig()

fileCore := zapcore.NewCore(
zapcore.NewJSONEncoder(encoderConfig),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we respect the configuration setting for log format?


file, err = initQueryLogWriter(log, e, path)
if err != nil {
e.Logger.Error("failed to open log file", zap.Error(err))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this error logged in initQueryLogWriter?

closeQueryLogWriter(file.Fd(), e)
file, err = initQueryLogWriter(log, e, path)
if err != nil {
e.Logger.Error("failed to create log file watcher", zap.Error(err))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already logged?

}

func (e *Executor) WithLogWriter(log *zap.Logger, path string) {
var file *os.File
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the closeQueryLogWriter be in a defer call? There's only one open at a time.

defer watcher.Close()

for {
select {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need an exit case here; a context that is cancelled on influxd shutdown, or some similar mechanism that closes and cleans up the watcher and the log.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise we may not get the Sync() and Close() on the file, I believe.

return logFile, nil
}

func closeQueryLogWriter(fd uintptr, e *Executor) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't operate on the fd here. Stick with the higher level libraries, like os.File.Sync and os.File.Close

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I might call our utility function SyncDir on the containing directory to ensure the log files are saved in case of bad crashes.


err = watcher.Add(file.Name())
if err != nil {
e.Logger.Error("failed to watch log file", zap.Error(err))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add file name to error message?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants