Skip to content

Commit

Permalink
refactor: introduce common logging
Browse files Browse the repository at this point in the history
  • Loading branch information
lzap committed Feb 3, 2025
1 parent 629d30a commit 0b1a5de
Show file tree
Hide file tree
Showing 27 changed files with 227 additions and 879 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ __debug*
coverage*
/tools/bin
vendor/
/*.log
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ ubi-container:
ubi-maintenance-container-test: ubi-container
# just check if the container would start
# functional tests are in the target "db-tests"
podman run --rm --tty --entrypoint /app/image-builder-maintenance osbuild/image-builder 2>&1 | grep "Dry run, no state will be changed"
podman run --rm --tty --entrypoint /app/image-builder-maintenance osbuild/image-builder 2>&1 | tee ubi-maintenance-container-test.log | grep "dry run, no state will be changed"

.PHONY: generate-openscap-blueprints
generate-openscap-blueprints:
Expand Down
2 changes: 1 addition & 1 deletion cmd/image-builder-maintenance/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/jackc/pgx/v5"

"github.com/sirupsen/logrus"
"github.com/osbuild/logging/pkg/logrus"
)

const (
Expand Down
31 changes: 23 additions & 8 deletions cmd/image-builder-maintenance/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ package main
import (
"context"
"fmt"
"log/slog"
"os"
"os/signal"
"syscall"
"time"

"github.com/sirupsen/logrus"
"github.com/osbuild/logging/pkg/sinit"
)

func main() {
Expand All @@ -35,8 +36,6 @@ func main() {
ctx, cancelTimeout := context.WithTimeout(ctx, 2*time.Hour)
defer cancelTimeout()

logrus.SetReportCaller(true)

conf := Config{
DryRun: true,
EnableDBMaintenance: false,
Expand All @@ -45,15 +44,30 @@ func main() {

err := LoadConfigFromEnv(&conf)
if err != nil {
logrus.Fatal(err)
panic(err)
}

loggingConfig := sinit.LoggingConfig{
StdoutConfig: sinit.StdoutConfig{
Enabled: true,
Level: "debug",
Format: "text",
},
}

err = sinit.InitializeLogging(ctx, loggingConfig)
if err != nil {
panic(err)
}

slog.InfoContext(ctx, "starting image-builder maintainance")

if conf.DryRun {
logrus.Info("Dry run, no state will be changed")
slog.InfoContext(ctx, "dry run, no state will be changed")
}

if !conf.EnableDBMaintenance {
logrus.Info("🦀🦀🦀 DB maintenance not enabled, skipping 🦀🦀🦀")
slog.InfoContext(ctx, "🦀🦀🦀 DB maintenance not enabled, skipping 🦀🦀🦀")
return
}
dbURL := fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=%s",
Expand All @@ -66,8 +80,9 @@ func main() {
)
err = DBCleanup(ctx, dbURL, conf.DryRun, conf.ComposesRetentionMonths)
if err != nil {
logrus.Fatalf("Error during DBCleanup: %v", err)
slog.ErrorContext(ctx, "error during DBCleanup", "err", err)
os.Exit(1)
}
logrus.Info("🦀🦀🦀 dbqueue cleanup done 🦀🦀🦀")
slog.InfoContext(ctx, "🦀🦀🦀 dbqueue cleanup done 🦀🦀🦀")
close(shutdownSignal)
}
59 changes: 48 additions & 11 deletions cmd/image-builder-migrate-db-tern/image-builder-migrate-db-tern.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ package main
import (
"bufio"
"bytes"
"context"
"fmt"
"log/slog"
"os"
"os/exec"

"github.com/osbuild/image-builder/internal/config"
"github.com/osbuild/image-builder/internal/logger"
"github.com/sirupsen/logrus"
"github.com/osbuild/logging/pkg/sinit"
)

func main() {
ctx := context.Background()
conf := config.ImageBuilderConfig{
ListenAddress: "unused",
LogLevel: "INFO",
Expand All @@ -29,18 +33,51 @@ func main() {
panic(err)
}

err = logger.ConfigLogger(logrus.StandardLogger(), conf.LogLevel)
hostname, err := os.Hostname()
if err != nil {
panic(err)
hostname = "image-builder-unknown"
}

loggingConfig := sinit.LoggingConfig{
StdoutConfig: sinit.StdoutConfig{
Enabled: true,
Level: "warning",
Format: "text",
},
SplunkConfig: sinit.SplunkConfig{
Enabled: conf.SplunkHost != "" && conf.SplunkPort != "" && conf.SplunkToken != "",
Level: conf.LogLevel,
URL: fmt.Sprintf("https://%s:%s/services/collector/event", conf.SplunkHost, conf.SplunkPort),
Token: conf.SplunkToken,
Source: "image-builder",
Hostname: hostname,
},
CloudWatchConfig: sinit.CloudWatchConfig{
Enabled: conf.CwAccessKeyID != "" && conf.CwSecretAccessKey != "" && conf.CwRegion != "",
Level: conf.LogLevel,
AWSRegion: conf.CwRegion,
AWSSecret: conf.CwSecretAccessKey,
AWSKey: conf.CwAccessKeyID,
AWSLogGroup: conf.LogGroup,
AWSLogStream: hostname,
},
SentryConfig: sinit.SentryConfig{
Enabled: conf.GlitchTipDSN != "",
DSN: conf.GlitchTipDSN,
},
}

if conf.CwAccessKeyID != "" {
err = logger.AddCloudWatchHook(logrus.StandardLogger(), conf.CwAccessKeyID, conf.CwSecretAccessKey, conf.CwRegion, conf.LogGroup)
if err != nil {
panic(err)
}
err = sinit.InitializeLogging(ctx, loggingConfig)
if err != nil {
panic(err)
}

slog.InfoContext(ctx, "starting image-builder migration",
"splunk", loggingConfig.SplunkConfig.Enabled,
"cloudwatch", loggingConfig.CloudWatchConfig.Enabled,
"sentry", loggingConfig.SentryConfig.Enabled,
)

// #nosec G204 -- the executable in the config can be trusted
cmd := exec.Command(conf.TernExecutable,
"migrate",
Expand All @@ -55,12 +92,12 @@ func main() {

scanner := bufio.NewScanner(bytes.NewReader(out))
for scanner.Scan() {
logrus.Info(scanner.Text())
slog.InfoContext(ctx, scanner.Text())
}

if err != nil {
panic(err)
}

logrus.Info("DB migration successful")
slog.InfoContext(ctx, "DB migration successful")
}
107 changes: 0 additions & 107 deletions cmd/image-builder/logging.go

This file was deleted.

69 changes: 0 additions & 69 deletions cmd/image-builder/logging_test.go

This file was deleted.

Loading

0 comments on commit 0b1a5de

Please sign in to comment.