Skip to content

Commit 92358f1

Browse files
authored
Feat/breardon2011/logging extended (#2100)
* shared logging wrapper
1 parent a6b42ee commit 92358f1

File tree

8 files changed

+382
-49
lines changed

8 files changed

+382
-49
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.idea/
2+
.vscode/
23
**/.env
34
**/.env*
45
.DS_Store

backend/bootstrap/main.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313
"runtime/pprof"
1414

1515
"github.com/diggerhq/digger/backend/config"
16+
"github.com/diggerhq/digger/backend/logging"
1617
"github.com/diggerhq/digger/backend/segment"
1718
"github.com/diggerhq/digger/backend/utils"
1819
pprof_gin "github.com/gin-contrib/pprof"
19-
sloggin "github.com/samber/slog-gin"
2020

2121
"time"
2222

@@ -98,7 +98,7 @@ func cleanupOldProfiles(dir string, keep int) {
9898

9999
func Bootstrap(templates embed.FS, diggerController controllers.DiggerController) *gin.Engine {
100100
defer segment.CloseClient()
101-
initLogging()
101+
logging.Init()
102102
cfg := config.DiggerConfig
103103

104104
if err := sentry.Init(sentry.ClientOptions{
@@ -120,7 +120,7 @@ func Bootstrap(templates embed.FS, diggerController controllers.DiggerController
120120

121121
r := gin.Default()
122122

123-
r.Use(sloggin.New(slog.Default().WithGroup("http")))
123+
r.Use(logging.Middleware())
124124

125125
if _, exists := os.LookupEnv("DIGGER_PPROF_DEBUG_ENABLED"); exists {
126126
setupProfiler(r)
@@ -256,24 +256,3 @@ func Bootstrap(templates embed.FS, diggerController controllers.DiggerController
256256

257257
return r
258258
}
259-
260-
func initLogging() {
261-
logLevel := os.Getenv("DIGGER_LOG_LEVEL")
262-
var level slog.Leveler
263-
264-
if logLevel == "DEBUG" {
265-
level = slog.LevelDebug
266-
} else if logLevel == "WARN" {
267-
level = slog.LevelWarn
268-
} else if logLevel == "ERROR" {
269-
level = slog.LevelError
270-
} else {
271-
level = slog.LevelInfo
272-
}
273-
274-
handler := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
275-
Level: level,
276-
})
277-
logger := slog.New(handler)
278-
slog.SetDefault(logger)
279-
}

backend/controllers/cache.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package controllers
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"github.com/diggerhq/digger/libs/digger_config/terragrunt/tac"
@@ -14,6 +15,7 @@ import (
1415
"path"
1516
"strings"
1617

18+
"github.com/diggerhq/digger/backend/logging"
1719
"github.com/diggerhq/digger/backend/models"
1820
"github.com/diggerhq/digger/backend/utils"
1921
dg_configuration "github.com/diggerhq/digger/libs/digger_config"
@@ -74,7 +76,8 @@ func (d DiggerController) UpdateRepoCache(c *gin.Context) {
7476
var newAtlantisConfig *tac.AtlantisConfig
7577

7678
// update the cache here, do it async for immediate response
77-
go func() {
79+
go func(ctx context.Context) {
80+
defer logging.InheritRequestLogger(ctx)()
7881
err = git_utils.CloneGitRepoAndDoAction(cloneUrl, branch, "", *token, "", func(dir string) error {
7982
diggerYmlBytes, err := os.ReadFile(path.Join(dir, "digger.yml"))
8083
diggerYmlStr = string(diggerYmlBytes)
@@ -96,7 +99,7 @@ func (d DiggerController) UpdateRepoCache(c *gin.Context) {
9699
return
97100
}
98101
slog.Info("Successfully updated repo cache", "repoFullName", repoFullName, "orgId", orgId)
99-
}()
102+
}(c.Request.Context())
100103

101104
c.String(http.StatusOK, "successfully submitted cache for processing, check backend logs for progress")
102105
}

backend/controllers/github.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/diggerhq/digger/backend/ci_backends"
2626
config2 "github.com/diggerhq/digger/backend/config"
2727
"github.com/diggerhq/digger/backend/locking"
28+
"github.com/diggerhq/digger/backend/logging"
2829
"github.com/diggerhq/digger/backend/middleware"
2930
"github.com/diggerhq/digger/backend/models"
3031
"github.com/diggerhq/digger/backend/segment"
@@ -109,7 +110,10 @@ func (d DiggerController) GithubAppWebHook(c *gin.Context) {
109110
"repo", *event.Repo.FullName,
110111
)
111112

112-
go handlePushEvent(gh, event, appId64)
113+
go func(ctx context.Context) {
114+
defer logging.InheritRequestLogger(ctx)()
115+
handlePushEvent(ctx, gh, event, appId64)
116+
}(c.Request.Context())
113117

114118
case *github.IssueCommentEvent:
115119
slog.Info("Processing IssueCommentEvent",
@@ -123,7 +127,10 @@ func (d DiggerController) GithubAppWebHook(c *gin.Context) {
123127
c.String(http.StatusOK, "OK")
124128
return
125129
}
126-
go handleIssueCommentEvent(gh, event, d.CiBackendProvider, appId64, d.GithubWebhookPostIssueCommentHooks)
130+
go func(ctx context.Context) {
131+
defer logging.InheritRequestLogger(ctx)()
132+
handleIssueCommentEvent(gh, event, d.CiBackendProvider, appId64, d.GithubWebhookPostIssueCommentHooks)
133+
}(c.Request.Context())
127134

128135
case *github.PullRequestEvent:
129136
slog.Info("Processing PullRequestEvent",
@@ -134,7 +141,10 @@ func (d DiggerController) GithubAppWebHook(c *gin.Context) {
134141
)
135142

136143
// run it as a goroutine to avoid timeouts
137-
go handlePullRequestEvent(gh, event, d.CiBackendProvider, appId64)
144+
go func(ctx context.Context) {
145+
defer logging.InheritRequestLogger(ctx)()
146+
handlePullRequestEvent(gh, event, d.CiBackendProvider, appId64)
147+
}(c.Request.Context())
138148

139149
default:
140150
slog.Debug("Unhandled event type", "eventType", reflect.TypeOf(event))
@@ -397,7 +407,7 @@ func handleInstallationDeletedEvent(installation *github.InstallationEvent, appI
397407
return nil
398408
}
399409

400-
func handlePushEvent(gh utils.GithubClientProvider, payload *github.PushEvent, appId int64) error {
410+
func handlePushEvent(ctx context.Context, gh utils.GithubClientProvider, payload *github.PushEvent, appId int64) error {
401411
slog.Debug("Handling push event", "appId", appId, "payload", payload)
402412
defer func() {
403413
if r := recover(); r != nil {
@@ -431,11 +441,12 @@ func handlePushEvent(gh utils.GithubClientProvider, payload *github.PushEvent, a
431441

432442
repoCacheEnabled := os.Getenv("DIGGER_CONFIG_REPO_CACHE_ENABLED")
433443
if repoCacheEnabled == "1" && strings.HasSuffix(ref, defaultBranch) {
434-
go func() {
444+
go func(ctx context.Context) {
445+
defer logging.InheritRequestLogger(ctx)()
435446
if err := sendProcessCacheRequest(repoFullName, defaultBranch, installationId); err != nil {
436447
slog.Error("Failed to process cache request", "error", err, "repoFullName", repoFullName)
437448
}
438-
}()
449+
}(ctx)
439450
}
440451

441452
return nil

backend/controllers/projects.go

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package controllers
22

33
import (
4+
"context"
45
"encoding/json"
56
"errors"
67
"fmt"
@@ -12,6 +13,7 @@ import (
1213
"strings"
1314
"time"
1415

16+
"github.com/diggerhq/digger/backend/logging"
1517
"github.com/diggerhq/digger/backend/middleware"
1618
"github.com/diggerhq/digger/backend/models"
1719
"github.com/diggerhq/digger/backend/services"
@@ -623,7 +625,6 @@ type SetJobStatusRequest struct {
623625
WorkflowUrl string `json:"workflow_url,omitempty"`
624626
}
625627

626-
627628
func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
628629
jobId := c.Param("jobId")
629630
orgId, exists := c.Get(middleware.ORGANISATION_ID_KEY)
@@ -679,7 +680,10 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
679680
slog.Info("Job status updated to created", "jobId", jobId)
680681

681682
// Update PR comment with real-time status
682-
go utils.UpdatePRComment(d.GithubClientProvider, jobId, job, "created")
683+
go func(ctx context.Context) {
684+
defer logging.InheritRequestLogger(ctx)()
685+
utils.UpdatePRComment(d.GithubClientProvider, jobId, job, "created")
686+
}(c.Request.Context())
683687

684688
case "triggered":
685689
job.Status = orchestrator_scheduler.DiggerJobTriggered
@@ -697,7 +701,10 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
697701
slog.Info("Job status updated to triggered", "jobId", jobId)
698702

699703
// Update PR comment with real-time status
700-
go utils.UpdatePRComment(d.GithubClientProvider, jobId, job, "triggered")
704+
go func(ctx context.Context) {
705+
defer logging.InheritRequestLogger(ctx)()
706+
utils.UpdatePRComment(d.GithubClientProvider, jobId, job, "triggered")
707+
}(c.Request.Context())
701708

702709
case "started":
703710
job.Status = orchestrator_scheduler.DiggerJobStarted
@@ -719,7 +726,10 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
719726
slog.Info("Job status updated to started", "jobId", jobId)
720727

721728
// Update PR comment with real-time status
722-
go utils.UpdatePRComment(d.GithubClientProvider, jobId, job, "started")
729+
go func(ctx context.Context) {
730+
defer logging.InheritRequestLogger(ctx)()
731+
utils.UpdatePRComment(d.GithubClientProvider, jobId, job, "started")
732+
}(c.Request.Context())
723733

724734
case "succeeded":
725735
job.Status = orchestrator_scheduler.DiggerJobSucceeded
@@ -770,7 +780,7 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
770780
"batchId", batchId,
771781
)
772782

773-
go func() {
783+
go func(ctx context.Context) {
774784
defer func() {
775785
if r := recover(); r != nil {
776786
stack := string(debug.Stack())
@@ -783,8 +793,9 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
783793
)
784794
}
785795
}()
796+
defer logging.InheritRequestLogger(ctx)()
786797

787-
slog.Debug("Starting post-success job processing", "jobId", jobId)
798+
slog.Debug("Starting post-success job processing", "job_id", jobId)
788799

789800
ghClientProvider := d.GithubClientProvider
790801
installationLink, err := models.DB.GetGithubInstallationLinkForOrg(orgId)
@@ -871,15 +882,18 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
871882
}
872883

873884
slog.Debug("Successfully processed job completion", "jobId", jobId)
874-
}()
885+
}(c.Request.Context())
875886

876887
// store digger job summary
877888
if request.JobSummary != nil {
878889
models.DB.UpdateDiggerJobSummary(job.DiggerJobID, request.JobSummary.ResourcesCreated, request.JobSummary.ResourcesUpdated, request.JobSummary.ResourcesDeleted)
879890
}
880891

881892
// Update PR comment with real-time status for succeeded job
882-
go utils.UpdatePRComment(d.GithubClientProvider, jobId, job, "succeeded")
893+
go func(ctx context.Context) {
894+
defer logging.InheritRequestLogger(ctx)()
895+
utils.UpdatePRComment(d.GithubClientProvider, jobId, job, "succeeded")
896+
}(c.Request.Context())
883897

884898
case "failed":
885899
job.Status = orchestrator_scheduler.DiggerJobFailed
@@ -901,7 +915,10 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
901915
)
902916

903917
// Update PR comment with real-time status for failed job
904-
go utils.UpdatePRComment(d.GithubClientProvider, jobId, job, "failed")
918+
go func(ctx context.Context) {
919+
defer logging.InheritRequestLogger(ctx)()
920+
utils.UpdatePRComment(d.GithubClientProvider, jobId, job, "failed")
921+
}(c.Request.Context())
905922

906923
default:
907924
slog.Warn("Unexpected job status received",
@@ -1010,12 +1027,6 @@ func (d DiggerController) SetJobStatusForProject(c *gin.Context) {
10101027
c.JSON(http.StatusOK, res)
10111028
}
10121029

1013-
1014-
1015-
1016-
1017-
1018-
10191030
func updateWorkflowUrlForJob(githubClientProvider utils.GithubClientProvider, job *models.DiggerJob) error {
10201031
if job == nil {
10211032
return fmt.Errorf("job is nil")

0 commit comments

Comments
 (0)