Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions prober/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package prober
import (
"bytes"
"context"
"errors"
"fmt"
"log/slog"
"net/http"
Expand Down Expand Up @@ -181,20 +182,24 @@ func (sl *scrapeLogger) Enabled(ctx context.Context, level slog.Level) bool {
// slog.Handler.
func (sl *scrapeLogger) Handle(ctx context.Context, r slog.Record) error {
level := getSlogLevel(sl.logLevelProber.String())
var errs []error
// Clone record so we can override the level. We hijack log calls to
// the scrapeLogger and override the level from the original log call
// with the level set via the `--log.prober` flag.
rec := r.Clone()
rec.Level = level

// Scrape logger should only write to next, the "real" logger, if next
// is enabled to write at the level the `--log.prober` flag is set to.
if sl.next.Enabled(context.Background(), level) {
errs = append(errs, sl.next.Handler().Handle(ctx, rec))
}

// Collect attributes from record so we can log them directly. We
// hijack log calls to the scrapeLogger and override the level from the
// original log call with the level set via the `--log.prober` flag.
attrs := make([]slog.Attr, r.NumAttrs())
r.Attrs(func(a slog.Attr) bool {
attrs = append(attrs, a)
return true
})

sl.next.LogAttrs(ctx, level, r.Message, attrs...)
sl.bufferLogger.LogAttrs(ctx, level, r.Message, attrs...)
// Always log to the bufferLogger, this is used to retain scrape log
// output for the `debug` URL param.
errs = append(errs, sl.bufferLogger.Handler().Handle(ctx, rec))

return nil
return errors.Join(errs...)
}

// WithAttrs adds the provided attributes to the scrapeLogger's internal logger and
Expand Down