Skip to content

Commit

Permalink
feat: Remove WithLogWriter
Browse files Browse the repository at this point in the history
  • Loading branch information
devanbenz committed Jan 13, 2025
1 parent a3577d9 commit 41d03da
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 66 deletions.
38 changes: 0 additions & 38 deletions query/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ import (
"sync/atomic"
"time"

l "github.com/influxdata/influxdb/logger"
"github.com/influxdata/influxdb/models"
"github.com/influxdata/influxql"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

var (
Expand Down Expand Up @@ -291,42 +289,6 @@ func (e *Executor) WithLogger(log *zap.Logger) {
e.TaskManager.Logger = e.Logger
}

func (e *Executor) WithLogWriter(log *zap.Logger, path string, format string) error {
// Don't allow console logger for query log writing
if format == "console" {
return fmt.Errorf("%s is not an allowed format for query logging to file", format)
}

// If the format is empty or auto set to default 'logfmt'
if format == "" || format == "auto" {
format = "logfmt"
}

logFile, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0666)
if err != nil {
return err
}

existingCore := log.Core()
encoder, err := l.NewEncoder(format)
if err != nil {
return err
}

fileCore := zapcore.NewCore(
encoder,
zapcore.Lock(logFile),
zapcore.InfoLevel,
)

newCore := zapcore.NewTee(existingCore, fileCore)
logger := zap.New(newCore)
e.Logger = logger
e.TaskManager.Logger = e.Logger

return nil
}

// ExecuteQuery executes each statement within a query.
func (e *Executor) ExecuteQuery(query *influxql.Query, opt ExecutionOptions, closing chan struct{}) <-chan *Result {
results := make(chan *Result)
Expand Down
28 changes: 0 additions & 28 deletions query/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ package query_test
import (
"errors"
"fmt"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
"os"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -598,31 +595,6 @@ func TestQueryExecutor_InvalidSource(t *testing.T) {
}
}

func TestQueryExecutor_WriteQueryToLog(t *testing.T) {
q, err := influxql.ParseQuery(`SELECT count(value) FROM cpu`)
require.NoError(t, err, "parse query")

testLogger := zaptest.NewLogger(t)
testFile := t.TempDir() + "/test.log"

e := NewQueryExecutor()
err = e.WithLogWriter(testLogger, testFile, "logfmt")
require.NoError(t, err, "error with log writer")

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))

dat, err := os.ReadFile(testFile)
require.NoError(t, err, "error reading file")
require.True(t, strings.Contains(string(dat), `SELECT count(value) FROM cpu`))
}

func discardOutput(results <-chan *query.Result) {
for range results {
// Read all results and discard.
Expand Down

0 comments on commit 41d03da

Please sign in to comment.