mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2026-05-17 16:59:40 +03:00
Compare commits
4 Commits
query-debu
...
feature/lo
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7a4e0a0683 | ||
|
|
74b00630fd | ||
|
|
b9866b74cb | ||
|
|
c013992b83 |
@@ -12,9 +12,10 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/VictoriaMetrics/metrics"
|
||||
|
||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo"
|
||||
"github.com/VictoriaMetrics/VictoriaMetrics/lib/stringsutil"
|
||||
"github.com/VictoriaMetrics/metrics"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -235,6 +236,11 @@ func (lw *logWriter) Write(p []byte) (int, error) {
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
var (
|
||||
mu sync.Mutex
|
||||
filePathCache = sync.Map{}
|
||||
)
|
||||
|
||||
func logMessage(level, msg string, skipframes int) {
|
||||
timestamp := ""
|
||||
if !*disableTimestamps {
|
||||
@@ -246,10 +252,7 @@ func logMessage(level, msg string, skipframes int) {
|
||||
file = "???"
|
||||
line = 0
|
||||
}
|
||||
if n := strings.Index(file, "/VictoriaMetrics/"); n >= 0 {
|
||||
// Strip /VictoriaMetrics/ prefix
|
||||
file = file[n+len("/VictoriaMetrics/"):]
|
||||
}
|
||||
file = simplifyFilePath(file)
|
||||
location := fmt.Sprintf("%s:%d", file, line)
|
||||
|
||||
// rate limit ERROR and WARN log messages with given limit.
|
||||
@@ -318,7 +321,37 @@ func logMessage(level, msg string, skipframes int) {
|
||||
}
|
||||
}
|
||||
|
||||
var mu sync.Mutex
|
||||
func simplifyFilePath(file string) string {
|
||||
// fast path: cache
|
||||
if result, ok := filePathCache.Load(file); ok {
|
||||
return result.(string)
|
||||
}
|
||||
|
||||
// slow path
|
||||
key := file
|
||||
if n := strings.Index(file, "/VictoriaMetrics/"); n >= 0 {
|
||||
file = file[n+len("/VictoriaMetrics/"):]
|
||||
}
|
||||
|
||||
keyword := "/vendor/github.com/VictoriaMetrics/VictoriaMetrics"
|
||||
if vendorIdx := strings.Index(file, keyword); vendorIdx >= 0 {
|
||||
// the path may under vendor folder
|
||||
if slashOffset := strings.Index(file[vendorIdx+len(keyword):], "/"); slashOffset > 0 {
|
||||
// There is no trailing '/' after '/vendor/github.com/VictoriaMetrics/VictoriaMetrics',
|
||||
// so it must contain a Go module version number.
|
||||
//
|
||||
// example:
|
||||
// VictoriaTraces/vendor/github.com/VictoriaMetrics/VictoriaMetrics@v0.0.0-00010101000000-000000000000/1.go
|
||||
// | vendorIdx | len(keyword) | slashOffset | |
|
||||
//
|
||||
// remove Go module version number in |<-slashOffset->|.
|
||||
file = file[:vendorIdx+len(keyword)] + file[vendorIdx+len(keyword)+slashOffset:]
|
||||
}
|
||||
}
|
||||
filePathCache.Store(key, file)
|
||||
|
||||
return file
|
||||
}
|
||||
|
||||
func shouldSkipLog(level string) bool {
|
||||
switch *loggerLevel {
|
||||
|
||||
@@ -23,3 +23,77 @@ func TestFormatLogMessage(t *testing.T) {
|
||||
// Format args exceeding the maxArgLen
|
||||
f("foo: %s, %q, %s", []any{"abcde", fmt.Errorf("foo bar baz"), "xx"}, 4, `foo: a..e, "f..z", xx`)
|
||||
}
|
||||
|
||||
func TestSimplifyFilePath(t *testing.T) {
|
||||
f := func(path, expectedResult string) {
|
||||
t.Helper()
|
||||
result := simplifyFilePath(path)
|
||||
if result != expectedResult {
|
||||
t.Fatalf("unexpected result; got %q, want %q", result, expectedResult)
|
||||
}
|
||||
}
|
||||
|
||||
// log in VictoriaMetrics repo
|
||||
f(
|
||||
`/VictoriaMetrics/VictoriaMetrics/1.go`,
|
||||
"VictoriaMetrics/1.go",
|
||||
)
|
||||
|
||||
// used in other repo
|
||||
f(
|
||||
`/VictoriaMetrics/VictoriaTraces/1.go`,
|
||||
"VictoriaTraces/1.go",
|
||||
)
|
||||
|
||||
// used in other repo as vendor
|
||||
f(
|
||||
`/VictoriaMetrics/VictoriaTraces/vendor/github.com/VictoriaMetrics/VictoriaMetrics/1.go`,
|
||||
"VictoriaTraces/vendor/github.com/VictoriaMetrics/VictoriaMetrics/1.go",
|
||||
)
|
||||
|
||||
// used in other repo as vendor with version num
|
||||
f(
|
||||
`/VictoriaMetrics/VictoriaTraces/vendor/github.com/VictoriaMetrics/VictoriaMetrics@v0.0.0-00010101000000-000000000000/1.go`,
|
||||
"VictoriaTraces/vendor/github.com/VictoriaMetrics/VictoriaMetrics/1.go",
|
||||
)
|
||||
|
||||
// The following tests are for local builds, for which path is the absolute path.
|
||||
f(
|
||||
`/Users/jiekun/repo/github.com/VictoriaMetrics/VictoriaTraces/1.go`,
|
||||
"VictoriaTraces/1.go",
|
||||
)
|
||||
f(
|
||||
`/Users/jiekun/repo/github.com/VictoriaMetrics/VictoriaTraces/vendor/github.com/VictoriaMetrics/VictoriaMetrics/1.go`,
|
||||
"VictoriaTraces/vendor/github.com/VictoriaMetrics/VictoriaMetrics/1.go",
|
||||
)
|
||||
f(
|
||||
`/Users/jiekun/repo/github.com/VictoriaMetrics/VictoriaTraces/vendor/github.com/VictoriaMetrics/VictoriaMetrics@v0.0.0-00010101000000-000000000000/1.go`,
|
||||
"VictoriaTraces/vendor/github.com/VictoriaMetrics/VictoriaMetrics/1.go",
|
||||
)
|
||||
f(
|
||||
`/Users/jiekun/repo/github.com/VictoriaMetrics/VictoriaMetrics-enterprise/1.go`,
|
||||
"VictoriaMetrics-enterprise/1.go",
|
||||
)
|
||||
f(
|
||||
`/Users/jiekun/repo/github.com/VictoriaMetrics/VictoriaTraces-enterprise/1.go`,
|
||||
"VictoriaTraces-enterprise/1.go",
|
||||
)
|
||||
f(
|
||||
`/Users/jiekun/repo/github.com/VictoriaMetrics/VictoriaTraces-enterprise/vendor/github.com/VictoriaMetrics/VictoriaMetrics/1.go`,
|
||||
"VictoriaTraces-enterprise/vendor/github.com/VictoriaMetrics/VictoriaMetrics/1.go",
|
||||
)
|
||||
f(
|
||||
`/Users/jiekun/repo/github.com/VictoriaMetrics/VictoriaTraces-enterprise/vendor/github.com/VictoriaMetrics/VictoriaMetrics@v0.0.0-00010101000000-000000000000/1.go`,
|
||||
"VictoriaTraces-enterprise/vendor/github.com/VictoriaMetrics/VictoriaMetrics/1.go",
|
||||
)
|
||||
|
||||
// special cases that user may rename the repo to whatever they want and does not contain `/VictoriaMetrics/`.
|
||||
f(
|
||||
`/Users/jiekun/repo/github.com/VictoriaTraces/1.go`,
|
||||
"/Users/jiekun/repo/github.com/VictoriaTraces/1.go",
|
||||
)
|
||||
f(
|
||||
`/what_ever_path/1.go`,
|
||||
"/what_ever_path/1.go",
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user