Skip to content

dev: Switch to leveled logging for alerts #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
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
28 changes: 24 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"log/slog"
"net/http"
"os"

Expand All @@ -14,10 +15,29 @@ import (

// It's alive! The application starts here.
func main() {
// Log the date and time (to the second),
// in UTC regardles of local time zone,
// and the file:line (without the full path- we don't have directories.)
log.SetFlags(log.Ldate | log.Ltime | log.LUTC | log.Lshortfile)
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
AddSource: true,
ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
// Leave all nested attributes as-is.
if len(groups) != 0 {
return attr
}

// Rewrite some standard fields to match the logging agent's
// expectations.
//
// https://cloud.google.com/logging/docs/agent/logging/configuration#special-fields
switch attr.Key {
case slog.MessageKey:
attr.Key = "message"
case slog.LevelKey:
attr.Key = "severity"
case slog.SourceKey:
attr.Key = "logging.googleapis.com/sourceLocation"
}
return attr
},
})))

ctx := context.Background()

Expand Down
6 changes: 3 additions & 3 deletions pairing_bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"log/slog"
"math/rand"
"net/http"
"slices"
Expand Down Expand Up @@ -245,9 +246,8 @@ func (pl *PairingLogic) endofbatch(w http.ResponseWriter, r *http.Request) {
profiles, err := pl.recurse.ActiveRecursers(ctx)
if err != nil {
log.Println("Encountered error while getting currently-active Recursers: ", err)
// TODO: https://github.com/recursecenter/pairing-bot/issues/61: Alert here!
// Using a FATAL here so it gets called out in the logs.
log.Fatal("Aborting end-of-batch processing!")
slog.Error("Aborting end-of-batch processing!")
w.WriteHeader(http.StatusInternalServerError)
return
}

Expand Down
Loading