Skip to content

Commit b2bcb6e

Browse files
committed
misc
1 parent 3378ecf commit b2bcb6e

3 files changed

Lines changed: 27 additions & 20 deletions

File tree

internal/scheduling/lib/filter_weigher_pipeline.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func InitNewFilterWeigherPipeline[RequestType FilterWeigherPipelineRequest](
7373
filter = validateFilter(filter)
7474
filter = monitorFilter(filter, filterConfig.Name, pipelineMonitor)
7575
if err := filter.Init(ctx, client, filterConfig); err != nil {
76-
slog.Error("scheduler: failed to initialize filter", "name", filterConfig.Name, "error", err)
76+
slog.Warn("scheduler: failed to initialize filter", "name", filterConfig.Name, "error", err)
7777
filterErrors[filterConfig.Name] = errors.New("failed to initialize filter: " + err.Error())
7878
continue
7979
}
@@ -102,7 +102,7 @@ func InitNewFilterWeigherPipeline[RequestType FilterWeigherPipelineRequest](
102102
weigher = validateWeigher(weigher)
103103
weigher = monitorWeigher(weigher, weigherConfig.Name, pipelineMonitor)
104104
if err := weigher.Init(ctx, client, weigherConfig); err != nil {
105-
slog.Error("scheduler: failed to initialize weigher", "name", weigherConfig.Name, "error", err)
105+
slog.Warn("scheduler: failed to initialize weigher", "name", weigherConfig.Name, "error", err)
106106
weigherErrors[weigherConfig.Name] = errors.New("failed to initialize weigher: " + err.Error())
107107
continue
108108
}

pkg/monitoring/log_metrics.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ package monitoring
66
import (
77
"context"
88
"log/slog"
9+
"path"
910
"runtime"
10-
"strings"
1111
"sync"
1212

1313
"github.com/prometheus/client_golang/prometheus"
@@ -19,10 +19,6 @@ import (
1919
// this map grows to a fixed size and all subsequent lookups are lock-free reads.
2020
var pcFileCache sync.Map // uintptr -> string
2121

22-
// modulePath is the Go module prefix stripped from caller file paths to produce
23-
// relative paths suitable for use as Prometheus label values.
24-
const modulePath = "github.com/cobaltcore-dev/cortex/"
25-
2622
// LogMessagesTotal counts warn and error log messages emitted by both the slog
2723
// and zap loggers. Labels: "level" (warn|error), "file" (relative source path).
2824
var LogMessagesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
@@ -31,13 +27,16 @@ var LogMessagesTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
3127
Help: "Total number of log messages emitted at warn or error level.",
3228
}, []string{"level", "file"})
3329

34-
// trimModulePrefix strips the Go module path prefix so that the file label
35-
// contains only the project-relative path (e.g. "internal/scheduling/...").
36-
func trimModulePrefix(file string) string {
37-
if i := strings.Index(file, modulePath); i >= 0 {
38-
return file[i+len(modulePath):]
30+
// shortFilePath returns "parent_dir/filename.go" from any absolute or
31+
// module-relative path. This is independent of the build environment (no
32+
// -trimpath needed) and keeps Prometheus label cardinality manageable.
33+
func shortFilePath(file string) string {
34+
dir, base := path.Split(file)
35+
parent := path.Base(dir)
36+
if parent == "." || parent == "/" {
37+
return base
3938
}
40-
return file
39+
return parent + "/" + base
4140
}
4241

4342
// --- slog handler wrapper ---
@@ -75,7 +74,7 @@ func (h *MetricsSlogHandler) Handle(ctx context.Context, r slog.Record) error {
7574
frames := runtime.CallersFrames([]uintptr{r.PC})
7675
f, _ := frames.Next()
7776
if f.File != "" {
78-
file = trimModulePrefix(f.File)
77+
file = shortFilePath(f.File)
7978
}
8079
pcFileCache.Store(r.PC, file)
8180
}
@@ -116,7 +115,7 @@ func WrapCoreWithLogMetrics(core zapcore.Core) zapcore.Core {
116115
}
117116
file := "unknown"
118117
if e.Caller.Defined {
119-
file = trimModulePrefix(e.Caller.File)
118+
file = shortFilePath(e.Caller.File)
120119
}
121120
LogMessagesTotal.WithLabelValues(level, file).Inc()
122121
}

pkg/monitoring/log_metrics_test.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,34 @@ import (
1414
"go.uber.org/zap/zapcore"
1515
)
1616

17-
func TestTrimModulePrefix(t *testing.T) {
17+
func TestShortFilePath(t *testing.T) {
1818
tests := []struct {
1919
input, want string
2020
}{
2121
{
2222
input: "github.com/cobaltcore-dev/cortex/internal/scheduling/nova/external_scheduler_api.go",
23-
want: "internal/scheduling/nova/external_scheduler_api.go",
23+
want: "nova/external_scheduler_api.go",
24+
},
25+
{
26+
input: "/workspace/internal/knowledge/extractor/plugins/compute/vrops_hostsystem_contention_long_term.go",
27+
want: "compute/vrops_hostsystem_contention_long_term.go",
2428
},
2529
{
2630
input: "/some/absolute/path/file.go",
27-
want: "/some/absolute/path/file.go",
31+
want: "path/file.go",
32+
},
33+
{
34+
input: "file.go",
35+
want: "file.go",
2836
},
2937
{
3038
input: "",
3139
want: "",
3240
},
3341
}
3442
for _, tt := range tests {
35-
if got := trimModulePrefix(tt.input); got != tt.want {
36-
t.Errorf("trimModulePrefix(%q) = %q, want %q", tt.input, got, tt.want)
43+
if got := shortFilePath(tt.input); got != tt.want {
44+
t.Errorf("shortFilePath(%q) = %q, want %q", tt.input, got, tt.want)
3745
}
3846
}
3947
}

0 commit comments

Comments
 (0)