Skip to content

Commit 2e301b4

Browse files
authored
feat: add support to slog in libs workspace (#1928)
* feat: add support to slog in libs workspace * feat: redirect gorm and sentry logs to use slog
1 parent cfb4dc9 commit 2e301b4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1947
-490
lines changed

backend/bootstrap/main.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"fmt"
66
"html/template"
77
"io/fs"
8-
"log"
98
"log/slog"
109
"net/http"
1110
"os"
@@ -15,7 +14,9 @@ import (
1514

1615
"github.com/diggerhq/digger/backend/config"
1716
"github.com/diggerhq/digger/backend/segment"
17+
"github.com/diggerhq/digger/backend/utils"
1818
pprof_gin "github.com/gin-contrib/pprof"
19+
sloggin "github.com/samber/slog-gin"
1920

2021
"time"
2122

@@ -38,7 +39,8 @@ func setupProfiler(r *gin.Engine) {
3839

3940
// Create profiles directory if it doesn't exist
4041
if err := os.MkdirAll("/tmp/profiles", 0755); err != nil {
41-
log.Fatalf("Failed to create profiles directory: %v", err)
42+
slog.Error("Failed to create profiles directory", "error", err)
43+
panic(err)
4244
}
4345

4446
// Start periodic profiling goroutine
@@ -108,6 +110,7 @@ func Bootstrap(templates embed.FS, diggerController controllers.DiggerController
108110
TracesSampleRate: 0.1,
109111
Release: "api@" + Version,
110112
Debug: true,
113+
DebugWriter: utils.NewSentrySlogWriter(slog.Default().WithGroup("sentry")),
111114
}); err != nil {
112115
slog.Error("Sentry initialization failed", "error", err)
113116
}
@@ -117,6 +120,8 @@ func Bootstrap(templates embed.FS, diggerController controllers.DiggerController
117120

118121
r := gin.Default()
119122

123+
r.Use(sloggin.New(slog.Default().WithGroup("http")))
124+
120125
if _, exists := os.LookupEnv("DIGGER_PPROF_DEBUG_ENABLED"); exists {
121126
setupProfiler(r)
122127
}

backend/go.mod

+7-5
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ replace github.com/ugorji/go => github.com/ugorji/go v1.2.12
1010
require (
1111
ariga.io/atlas-provider-gorm v0.5.0
1212
github.com/bradleyfalzon/ghinstallation/v2 v2.11.0
13-
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc
1413
github.com/dchest/uniuri v1.2.0
1514
github.com/diggerhq/digger/libs v0.4.15
1615
github.com/dominikbraun/graph v0.23.0
@@ -23,8 +22,10 @@ require (
2322
github.com/google/uuid v1.6.0
2423
github.com/ktrysmt/go-bitbucket v0.9.81
2524
github.com/migueleliasweb/go-github-mock v0.0.23
25+
github.com/orandin/slog-gorm v1.4.0
2626
github.com/robfig/cron v1.2.0
2727
github.com/samber/lo v1.39.0
28+
github.com/samber/slog-gin v1.15.0
2829
github.com/segmentio/analytics-go/v3 v3.3.0
2930
github.com/spf13/cast v1.6.0
3031
github.com/spf13/viper v1.19.0
@@ -108,7 +109,7 @@ require (
108109
github.com/bmatcuk/doublestar v1.3.1 // indirect
109110
github.com/bmatcuk/doublestar/v4 v4.6.1 // indirect
110111
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
111-
github.com/bytedance/sonic v1.11.6 // indirect
112+
github.com/bytedance/sonic v1.11.9 // indirect
112113
github.com/bytedance/sonic/loader v0.1.1 // indirect
113114
github.com/caarlos0/env/v11 v11.1.0 // indirect
114115
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
@@ -120,22 +121,23 @@ require (
120121
github.com/cncf/xds/go v0.0.0-20240905190251-b4127c9b8d78 // indirect
121122
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
122123
github.com/creack/pty v1.1.17 // indirect
124+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
123125
github.com/dimchansky/utfbom v1.1.1 // indirect
124126
github.com/dineshba/tf-summarize v0.3.14 // indirect
125127
github.com/envoyproxy/go-control-plane v0.13.0 // indirect
126128
github.com/envoyproxy/protoc-gen-validate v1.1.0 // indirect
127129
github.com/fatih/color v1.15.0 // indirect
128130
github.com/felixge/httpsnoop v1.0.4 // indirect
129131
github.com/fsnotify/fsnotify v1.7.0 // indirect
130-
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
132+
github.com/gabriel-vasile/mimetype v1.4.4 // indirect
131133
github.com/gin-contrib/sse v0.1.0 // indirect
132134
github.com/go-errors/errors v1.4.2 // indirect
133135
github.com/go-ini/ini v1.67.0 // indirect
134136
github.com/go-logr/logr v1.4.2 // indirect
135137
github.com/go-logr/stdr v1.2.2 // indirect
136138
github.com/go-playground/locales v0.14.1 // indirect
137139
github.com/go-playground/universal-translator v0.18.1 // indirect
138-
github.com/go-playground/validator/v10 v10.20.0 // indirect
140+
github.com/go-playground/validator/v10 v10.22.0 // indirect
139141
github.com/go-sql-driver/mysql v1.8.1 // indirect
140142
github.com/gobwas/glob v0.2.3 // indirect
141143
github.com/goccy/go-json v0.10.3 // indirect
@@ -204,7 +206,7 @@ require (
204206
github.com/json-iterator/go v1.1.12 // indirect
205207
github.com/jstemmer/go-junit-report v1.0.0 // indirect
206208
github.com/klauspost/compress v1.17.7 // indirect
207-
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
209+
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
208210
github.com/kylelemons/godebug v1.1.0 // indirect
209211
github.com/leodido/go-urn v1.4.0 // indirect
210212
github.com/lib/pq v1.10.9 // indirect

backend/go.sum

+12-8
Original file line numberDiff line numberDiff line change
@@ -858,8 +858,8 @@ github.com/bradleyfalzon/ghinstallation/v2 v2.11.0/go.mod h1:0LWKQwOHewXO/1acI6T
858858
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
859859
github.com/bytecodealliance/wasmtime-go/v3 v3.0.2 h1:3uZCA/BLTIu+DqCfguByNMJa2HVHpXvjfy0Dy7g6fuA=
860860
github.com/bytecodealliance/wasmtime-go/v3 v3.0.2/go.mod h1:RnUjnIXxEJcL6BgCvNyzCCRzZcxCgsZCi+RNlvYor5Q=
861-
github.com/bytedance/sonic v1.11.6 h1:oUp34TzMlL+OY1OUWxHqsdkgC/Zfc85zGqw9siXjrc0=
862-
github.com/bytedance/sonic v1.11.6/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
861+
github.com/bytedance/sonic v1.11.9 h1:LFHENlIY/SLzDWverzdOvgMztTxcfcF+cqNsz9pK5zg=
862+
github.com/bytedance/sonic v1.11.9/go.mod h1:LysEHSvpvDySVdC2f87zGWf6CIKJcAvqab1ZaiQtds4=
863863
github.com/bytedance/sonic/loader v0.1.1 h1:c+e5Pt1k/cy5wMveRDyk2X4B9hF4g7an8N3zCYjJFNM=
864864
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
865865
github.com/caarlos0/env/v11 v11.1.0 h1:a5qZqieE9ZfzdvbbdhTalRrHT5vu/4V1/ad1Ka6frhI=
@@ -1012,8 +1012,8 @@ github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7z
10121012
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
10131013
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
10141014
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
1015-
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
1016-
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
1015+
github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I=
1016+
github.com/gabriel-vasile/mimetype v1.4.4/go.mod h1:JwLei5XPtWdGiMFB5Pjle1oEeoSeEuJfJE+TtfvdB/s=
10171017
github.com/getsentry/sentry-go v0.28.0 h1:7Rqx9M3ythTKy2J6uZLHmc8Sz9OGgIlseuO1iBX/s0M=
10181018
github.com/getsentry/sentry-go v0.28.0/go.mod h1:1fQZ+7l7eeJ3wYi82q5Hg8GqAPgefRq+FP/QhafYVgg=
10191019
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
@@ -1073,8 +1073,8 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o
10731073
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
10741074
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
10751075
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
1076-
github.com/go-playground/validator/v10 v10.20.0 h1:K9ISHbSaI0lyB2eWMPJo+kOS/FBExVwjEviJTixqxL8=
1077-
github.com/go-playground/validator/v10 v10.20.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
1076+
github.com/go-playground/validator/v10 v10.22.0 h1:k6HsTZ0sTnROkhS//R0O+55JgM8C4Bx7ia+JlgcnOao=
1077+
github.com/go-playground/validator/v10 v10.22.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM=
10781078
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
10791079
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
10801080
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
@@ -1510,8 +1510,8 @@ github.com/klauspost/compress v1.15.11/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrD
15101510
github.com/klauspost/compress v1.17.7 h1:ehO88t2UGzQK66LMdE8tibEd1ErmzZjNEqWkjLAKQQg=
15111511
github.com/klauspost/compress v1.17.7/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
15121512
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
1513-
github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM=
1514-
github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
1513+
github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM=
1514+
github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
15151515
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
15161516
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
15171517
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
@@ -1670,6 +1670,8 @@ github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQ
16701670
github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
16711671
github.com/opencontainers/runc v1.1.9 h1:XR0VIHTGce5eWPkaPesqTBrhW2yAcaraWfsEalNwQLM=
16721672
github.com/opencontainers/runc v1.1.9/go.mod h1:CbUumNnWCuTGFukNXahoo/RFBZvDAgRh/smNYNOhA50=
1673+
github.com/orandin/slog-gorm v1.4.0 h1:FgA8hJufF9/jeNSYoEXmHPPBwET2gwlF3B85JdpsTUU=
1674+
github.com/orandin/slog-gorm v1.4.0/go.mod h1:MoZ51+b7xE9lwGNPYEhxcUtRNrYzjdcKvA8QXQQGEPA=
16731675
github.com/ory/dockertest v3.3.5+incompatible h1:iLLK6SQwIhcbrG783Dghaaa3WPzGc+4Emza6EbVUUGA=
16741676
github.com/ory/dockertest v3.3.5+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnhNrne+V0E6LAcBILJdPs=
16751677
github.com/owenrumney/go-sarif v1.1.1 h1:QNObu6YX1igyFKhdzd7vgzmw7XsWN3/6NMGuDzBgXmE=
@@ -1768,6 +1770,8 @@ github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6g
17681770
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
17691771
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
17701772
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
1773+
github.com/samber/slog-gin v1.15.0 h1:Kqs/ilXd9divtslWjbz5DVptmLlzyntbBiXUAta2SFg=
1774+
github.com/samber/slog-gin v1.15.0/go.mod h1:mPAEinK/g2jPLauuWO11m3Q0Ca7aG4k9XjXjXY8IhMQ=
17711775
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
17721776
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
17731777
github.com/segmentio/analytics-go/v3 v3.3.0 h1:8VOMaVGBW03pdBrj1CMFfY9o/rnjJC+1wyQHlVxjw5o=

backend/models/setup.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"log/slog"
55
"os"
66

7+
slogGorm "github.com/orandin/slog-gorm"
78
"gorm.io/driver/postgres"
89
_ "gorm.io/driver/postgres"
910
"gorm.io/gorm"
10-
"gorm.io/gorm/logger"
1111
)
1212

1313
type Database struct {
@@ -21,7 +21,7 @@ var DB *Database
2121

2222
func ConnectDatabase() {
2323
database, err := gorm.Open(postgres.Open(os.Getenv("DATABASE_URL")), &gorm.Config{
24-
Logger: logger.Default.LogMode(logger.Info),
24+
Logger: slogGorm.New(),
2525
})
2626
if err != nil {
2727
slog.Error("Failed to connect to database", "error", err)

backend/tasks/runs_test.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"fmt"
5-
"log"
65
"os"
76
"strings"
87
"testing"
@@ -36,22 +35,22 @@ func setupSuite(tb testing.TB) (func(tb testing.TB), *models.Database) {
3635
e := os.Remove(dbName)
3736
if e != nil {
3837
if !strings.Contains(e.Error(), "no such file or directory") {
39-
log.Fatal(e)
38+
panic(e)
4039
}
4140
}
4241

4342
// open and create a new database
4443
gdb, err := gorm.Open(sqlite.Open(dbName), &gorm.Config{})
4544
if err != nil {
46-
log.Fatal(err)
45+
panic(err)
4746
}
4847

4948
// migrate tables
5049
err = gdb.AutoMigrate(&models.Policy{}, &models.Organisation{}, &models.Repo{}, &models.Project{}, &models.Token{},
5150
&models.User{}, &models.ProjectRun{}, &models.GithubAppInstallation{}, &models.VCSConnection{}, &models.GithubAppInstallationLink{},
5251
&models.GithubDiggerJobLink{}, &models.DiggerJob{}, &models.DiggerJobParentLink{}, &models.DiggerRun{}, &models.DiggerRunQueueItem{})
5352
if err != nil {
54-
log.Fatal(err)
53+
panic(err)
5554
}
5655

5756
database := &models.Database{GormDB: gdb}
@@ -63,21 +62,21 @@ func setupSuite(tb testing.TB) (func(tb testing.TB), *models.Database) {
6362
orgName := "testOrg"
6463
org, err := database.CreateOrganisation(orgName, externalSource, orgTenantId)
6564
if err != nil {
66-
log.Fatal(err)
65+
panic(err)
6766
}
6867

6968
// create digger repo
7069
repoName := "test repo"
7170
repo, err := database.CreateRepo(repoName, "", "", "", "", org, "")
7271
if err != nil {
73-
log.Fatal(err)
72+
panic(err)
7473
}
7574

7675
// create test project
7776
projectName := "test project"
7877
_, err = database.CreateProject(projectName, org, repo, false, false)
7978
if err != nil {
80-
log.Fatal(err)
79+
panic(err)
8180
}
8281

8382
models.DB = database

backend/utils/github_test.go

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
package utils
22

33
import (
4-
"github.com/stretchr/testify/assert"
5-
"log"
64
"os"
75
"testing"
8-
)
96

10-
func init() {
11-
log.SetOutput(os.Stdout)
12-
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
13-
}
7+
"github.com/stretchr/testify/assert"
8+
)
149

1510
func TestGithubCloneWithInvalidTokenThrowsErr(t *testing.T) {
1611
f := func(d string) error { return nil }

backend/utils/log.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package utils
2+
3+
import (
4+
"bufio"
5+
"bytes"
6+
"log/slog"
7+
"strings"
8+
)
9+
10+
// SentrySlogWriter adapts Sentry's log output to a structured logger.
11+
type SentrySlogWriter struct {
12+
logger *slog.Logger
13+
}
14+
15+
// NewSentrySlogWriter creates a new adapter to redirect Sentry logs to slog.
16+
func NewSentrySlogWriter(logger *slog.Logger) *SentrySlogWriter {
17+
return &SentrySlogWriter{logger: logger}
18+
}
19+
20+
// Write implements io.Writer to process Sentry's logs and send them to slog.
21+
func (s *SentrySlogWriter) Write(p []byte) (n int, err error) {
22+
scanner := bufio.NewScanner(bytes.NewReader(p))
23+
for scanner.Scan() {
24+
line := scanner.Text()
25+
if strings.HasPrefix(line, "[Sentry]") {
26+
parts := strings.SplitN(line, " ", 4)
27+
if len(parts) >= 4 {
28+
s.logger.Debug(parts[3]) // Extract message without prefix and timestamp
29+
} else {
30+
s.logger.Debug(line)
31+
}
32+
} else {
33+
s.logger.Debug(line)
34+
}
35+
}
36+
return len(p), nil
37+
}

cli/pkg/digger/digger_test.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@ package digger
22

33
import (
44
"fmt"
5-
"github.com/diggerhq/digger/libs/ci"
6-
"github.com/diggerhq/digger/libs/execution"
7-
"github.com/diggerhq/digger/libs/iac_utils"
8-
orchestrator "github.com/diggerhq/digger/libs/scheduler"
95
"os"
106
"sort"
117
"strconv"
128
"strings"
139
"testing"
1410
"time"
1511

12+
"github.com/diggerhq/digger/libs/ci"
13+
"github.com/diggerhq/digger/libs/execution"
14+
"github.com/diggerhq/digger/libs/iac_utils"
15+
orchestrator "github.com/diggerhq/digger/libs/scheduler"
16+
1617
"github.com/diggerhq/digger/libs/comment_utils/reporting"
1718
configuration "github.com/diggerhq/digger/libs/digger_config"
1819
"github.com/dominikbraun/graph"
@@ -86,7 +87,13 @@ func (m *MockPRManager) GetApprovals(prNumber int) ([]string, error) {
8687

8788
func (m *MockPRManager) PublishComment(prNumber int, comment string) (*ci.Comment, error) {
8889
m.Commands = append(m.Commands, RunInfo{"PublishComment", strconv.Itoa(prNumber) + " " + comment, time.Now()})
89-
return nil, nil
90+
id := "mock-comment-id"
91+
url := "http://example.com/comment/1"
92+
return &ci.Comment{
93+
Id: id,
94+
Url: url,
95+
Body: &comment,
96+
}, nil
9097
}
9198

9299
func (m *MockPRManager) ListIssues() ([]*ci.Issue, error) {

0 commit comments

Comments
 (0)