From 9e859ad9a651f6950f8a95debff0703449193118 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Sat, 21 Dec 2024 01:13:48 +0100 Subject: [PATCH] added tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- cmd/windows_exporter/main.go | 6 +++--- internal/collector/mssql/mssql.go | 7 +++++-- internal/collector/process/process_v1.go | 3 ++- internal/collector/process/process_v2.go | 3 ++- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/cmd/windows_exporter/main.go b/cmd/windows_exporter/main.go index 4ef1c019e..d9537a42b 100644 --- a/cmd/windows_exporter/main.go +++ b/cmd/windows_exporter/main.go @@ -193,7 +193,7 @@ func run() int { } } - logger.Debug("Logging has Started") + logger.LogAttrs(ctx, slog.LevelDebug, "logging has Started") if err = setPriorityWindows(logger, os.Getpid(), *processPriority); err != nil { logger.Error("failed to set process priority", @@ -243,7 +243,7 @@ func run() int { mux.HandleFunc("GET /debug/pprof/trace", pprof.Trace) } - logger.InfoContext(ctx, fmt.Sprintf("starting windows_exporter in %s", time.Since(startTime)), + logger.LogAttrs(ctx, slog.LevelInfo, fmt.Sprintf("starting windows_exporter in %s", time.Since(startTime)), slog.String("version", version.Version), slog.String("branch", version.Branch), slog.String("revision", version.GetRevision()), @@ -334,7 +334,7 @@ func setPriorityWindows(logger *slog.Logger, pid int, priority string) error { return nil } - logger.Debug("setting process priority to " + priority) + logger.LogAttrs(context.Background(), slog.LevelDebug, "setting process priority to "+priority) // https://learn.microsoft.com/en-us/windows/win32/procthread/process-security-and-access-rights handle, err := windows.OpenProcess( diff --git a/internal/collector/mssql/mssql.go b/internal/collector/mssql/mssql.go index c5105eceb..1ff8013cf 100644 --- a/internal/collector/mssql/mssql.go +++ b/internal/collector/mssql/mssql.go @@ -16,6 +16,7 @@ package mssql import ( + "context" "errors" "fmt" "log/slog" @@ -373,6 +374,8 @@ func (c *Collector) collect( ) error { errs := make([]error, 0, len(perfDataCollectors)) + ctx := context.Background() + for sqlInstance, perfDataCollector := range perfDataCollectors { begin := time.Now() success := 1.0 @@ -383,11 +386,11 @@ func (c *Collector) collect( errs = append(errs, err) success = 0.0 - c.logger.Debug(fmt.Sprintf("mssql class collector %s for instance %s failed after %s", collector, sqlInstance, duration), + c.logger.LogAttrs(ctx, slog.LevelDebug, fmt.Sprintf("mssql class collector %s for instance %s failed after %s", collector, sqlInstance, duration), slog.Any("err", err), ) } else { - c.logger.Debug(fmt.Sprintf("mssql class collector %s for instance %s succeeded after %s", collector, sqlInstance, duration)) + c.logger.LogAttrs(ctx, slog.LevelDebug, fmt.Sprintf("mssql class collector %s for instance %s succeeded after %s", collector, sqlInstance, duration)) } if collector == "" { diff --git a/internal/collector/process/process_v1.go b/internal/collector/process/process_v1.go index 7ce2a4869..a50308aff 100644 --- a/internal/collector/process/process_v1.go +++ b/internal/collector/process/process_v1.go @@ -16,6 +16,7 @@ package process import ( + "context" "fmt" "log/slog" "runtime/debug" @@ -118,7 +119,7 @@ func (c *Collector) collectWorkerV1() { cmdLine, processOwner, processGroupID, err := c.getProcessInformation(uint32(pid)) if err != nil { - c.logger.Debug("Failed to get process information", + slog.LogAttrs(context.Background(), slog.LevelDebug, "Failed to get process information", slog.Uint64("pid", pid), slog.Any("err", err), ) diff --git a/internal/collector/process/process_v2.go b/internal/collector/process/process_v2.go index 8cb0f1121..2e0d1a887 100644 --- a/internal/collector/process/process_v2.go +++ b/internal/collector/process/process_v2.go @@ -16,6 +16,7 @@ package process import ( + "context" "fmt" "log/slog" "runtime/debug" @@ -118,7 +119,7 @@ func (c *Collector) collectWorkerV2() { cmdLine, processOwner, processGroupID, err := c.getProcessInformation(uint32(pid)) if err != nil { - c.logger.Debug("Failed to get process information", + slog.LogAttrs(context.Background(), slog.LevelDebug, "Failed to get process information", slog.Uint64("pid", pid), slog.Any("err", err), )