Browse Source

Query Logging styling (#6594)

- Fix Json vs JSON in activequerylogger
- Fix SetQueryLogger always returns nil

Signed-off-by: Julien Pivotto <roidelapluie@inuits.eu>
pull/6596/head
Julien Pivotto 5 years ago committed by Brian Brazil
parent
commit
3885562587
  1. 6
      cmd/prometheus/main.go
  2. 4
      promql/engine.go
  3. 22
      promql/query_logger.go
  4. 4
      promql/query_logger_test.go

6
cmd/prometheus/main.go

@ -423,14 +423,16 @@ func main() {
webHandler.ApplyConfig, webHandler.ApplyConfig,
func(cfg *config.Config) error { func(cfg *config.Config) error {
if cfg.GlobalConfig.QueryLogFile == "" { if cfg.GlobalConfig.QueryLogFile == "" {
return queryEngine.SetQueryLogger(nil) queryEngine.SetQueryLogger(nil)
return nil
} }
l, err := logging.NewJSONFileLogger(cfg.GlobalConfig.QueryLogFile) l, err := logging.NewJSONFileLogger(cfg.GlobalConfig.QueryLogFile)
if err != nil { if err != nil {
return err return err
} }
return queryEngine.SetQueryLogger(l) queryEngine.SetQueryLogger(l)
return nil
}, },
// The Scrape and notifier managers need to reload before the Discovery manager as // The Scrape and notifier managers need to reload before the Discovery manager as
// they need to read the most updated config when receiving the new targets list. // they need to read the most updated config when receiving the new targets list.

4
promql/engine.go

@ -333,7 +333,7 @@ func NewEngine(opts EngineOpts) *Engine {
} }
// SetQueryLogger sets the query logger. // SetQueryLogger sets the query logger.
func (ng *Engine) SetQueryLogger(l QueryLogger) error { func (ng *Engine) SetQueryLogger(l QueryLogger) {
ng.queryLoggerLock.Lock() ng.queryLoggerLock.Lock()
defer ng.queryLoggerLock.Unlock() defer ng.queryLoggerLock.Unlock()
@ -353,8 +353,6 @@ func (ng *Engine) SetQueryLogger(l QueryLogger) error {
} else { } else {
ng.metrics.queryLogEnabled.Set(0) ng.metrics.queryLogEnabled.Set(0)
} }
return nil
} }
// NewInstantQuery returns an evaluation query for the given expression at the given time. // NewInstantQuery returns an evaluation query for the given expression at the given time.

22
promql/query_logger.go

@ -41,8 +41,8 @@ const (
entrySize int = 1000 entrySize int = 1000
) )
func parseBrokenJson(brokenJson []byte) (bool, string) { func parseBrokenJSON(brokenJSON []byte) (bool, string) {
queries := strings.ReplaceAll(string(brokenJson), "\x00", "") queries := strings.ReplaceAll(string(brokenJSON), "\x00", "")
if len(queries) > 0 { if len(queries) > 0 {
queries = queries[:len(queries)-1] + "]" queries = queries[:len(queries)-1] + "]"
} }
@ -63,14 +63,14 @@ func logUnfinishedQueries(filename string, filesize int, logger log.Logger) {
return return
} }
brokenJson := make([]byte, filesize) brokenJSON := make([]byte, filesize)
_, err = fd.Read(brokenJson) _, err = fd.Read(brokenJSON)
if err != nil { if err != nil {
level.Error(logger).Log("msg", "Failed to read query log file", "err", err) level.Error(logger).Log("msg", "Failed to read query log file", "err", err)
return return
} }
queriesExist, queries := parseBrokenJson(brokenJson) queriesExist, queries := parseBrokenJSON(brokenJSON)
if !queriesExist { if !queriesExist {
return return
} }
@ -141,7 +141,7 @@ func trimStringByBytes(str string, size int) string {
return string(bytesStr[:trimIndex]) return string(bytesStr[:trimIndex])
} }
func _newJsonEntry(query string, timestamp int64, logger log.Logger) []byte { func _newJSONEntry(query string, timestamp int64, logger log.Logger) []byte {
entry := Entry{query, timestamp} entry := Entry{query, timestamp}
jsonEntry, err := json.Marshal(entry) jsonEntry, err := json.Marshal(entry)
@ -153,12 +153,12 @@ func _newJsonEntry(query string, timestamp int64, logger log.Logger) []byte {
return jsonEntry return jsonEntry
} }
func newJsonEntry(query string, logger log.Logger) []byte { func newJSONEntry(query string, logger log.Logger) []byte {
timestamp := time.Now().Unix() timestamp := time.Now().Unix()
minEntryJson := _newJsonEntry("", timestamp, logger) minEntryJSON := _newJSONEntry("", timestamp, logger)
query = trimStringByBytes(query, entrySize-(len(minEntryJson)+1)) query = trimStringByBytes(query, entrySize-(len(minEntryJSON)+1))
jsonEntry := _newJsonEntry(query, timestamp, logger) jsonEntry := _newJSONEntry(query, timestamp, logger)
return jsonEntry return jsonEntry
} }
@ -176,7 +176,7 @@ func (tracker ActiveQueryTracker) Delete(insertIndex int) {
func (tracker ActiveQueryTracker) Insert(query string) int { func (tracker ActiveQueryTracker) Insert(query string) int {
i, fileBytes := <-tracker.getNextIndex, tracker.mmapedFile i, fileBytes := <-tracker.getNextIndex, tracker.mmapedFile
entry := newJsonEntry(query, tracker.logger) entry := newJSONEntry(query, tracker.logger)
start, end := i, i+entrySize start, end := i, i+entrySize
copy(fileBytes[start:], entry) copy(fileBytes[start:], entry)

4
promql/query_logger_test.go

@ -133,7 +133,7 @@ func TestMMapFile(t *testing.T) {
} }
} }
func TestParseBrokenJson(t *testing.T) { func TestParseBrokenJSON(t *testing.T) {
for _, tc := range []struct { for _, tc := range []struct {
b []byte b []byte
@ -161,7 +161,7 @@ func TestParseBrokenJson(t *testing.T) {
}, },
} { } {
t.Run("", func(t *testing.T) { t.Run("", func(t *testing.T) {
ok, out := parseBrokenJson(tc.b) ok, out := parseBrokenJSON(tc.b)
if tc.ok != ok { if tc.ok != ok {
t.Fatalf("expected %t, got %t", tc.ok, ok) t.Fatalf("expected %t, got %t", tc.ok, ok)
return return

Loading…
Cancel
Save