From 5b37a445916e387d779600e95b78dc4e7ae60a9d Mon Sep 17 00:00:00 2001 From: Soulou Date: Mon, 31 Mar 2025 18:34:52 +0200 Subject: [PATCH 1/2] feat(cronsetup/request-id) Generate a unique request ID for each cron execution Fixes #1126 --- cronsetup/CHANGELOG.md | 2 ++ cronsetup/cronsetup.go | 17 ++++++++++++++--- cronsetup/go.mod | 4 ++-- cronsetup/go.sum | 2 ++ 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/cronsetup/CHANGELOG.md b/cronsetup/CHANGELOG.md index 4238afd3..f99a3724 100644 --- a/cronsetup/CHANGELOG.md +++ b/cronsetup/CHANGELOG.md @@ -2,6 +2,8 @@ ## To be Released +* feat(request_id) Inject `request_id` in context for each cron execution + ## v1.1.4 * Various dependencies updates diff --git a/cronsetup/cronsetup.go b/cronsetup/cronsetup.go index b10076c7..d286a6eb 100644 --- a/cronsetup/cronsetup.go +++ b/cronsetup/cronsetup.go @@ -5,10 +5,13 @@ import ( "fmt" "github.com/pkg/errors" + "github.com/sirupsen/logrus" etcdclient "go.etcd.io/etcd/client/v3" etcdcron "github.com/Scalingo/go-etcd-cron" "github.com/Scalingo/go-utils/logger" + + "github.com/gofrs/uuid/v5" ) type SetupOpts struct { @@ -29,9 +32,17 @@ func Setup(ctx context.Context, opts SetupOpts) (func(), error) { funcCtx := func(ctx context.Context, j etcdcron.Job) context.Context { log := logger.Get(ctx) - log = log.WithField("cron-job", j.Name) - log.Debug("Running cron job") - return logger.ToCtx(ctx, log) + requestID, err := uuid.NewV4() + if err != nil { + log.WithError(err).Error("generate UUID v4") + } else { + ctx = context.WithValue(ctx, "request_id", requestID.String()) + } + ctx, _ = logger.WithFieldsToCtx(ctx, logrus.Fields{ + "cron-job": j.Name, + "request_id": requestID.String(), + }) + return ctx } errorHandler := func(ctx context.Context, j etcdcron.Job, err error) { diff --git a/cronsetup/go.mod b/cronsetup/go.mod index a560d123..b15a9695 100644 --- a/cronsetup/go.mod +++ b/cronsetup/go.mod @@ -1,12 +1,13 @@ module github.com/Scalingo/go-utils/cronsetup go 1.22 -toolchain go1.23.7 require ( github.com/Scalingo/go-etcd-cron v1.3.3 github.com/Scalingo/go-utils/logger v1.4.0 + github.com/gofrs/uuid/v5 v5.3.2 github.com/pkg/errors v0.9.1 + github.com/sirupsen/logrus v1.9.3 go.etcd.io/etcd/client/v3 v3.5.18 ) @@ -16,7 +17,6 @@ require ( github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/protobuf v1.5.4 // indirect github.com/iancoleman/strcase v0.3.0 // indirect - github.com/sirupsen/logrus v1.9.3 // indirect go.etcd.io/etcd/api/v3 v3.5.18 // indirect go.etcd.io/etcd/client/pkg/v3 v3.5.18 // indirect go.uber.org/multierr v1.11.0 // indirect diff --git a/cronsetup/go.sum b/cronsetup/go.sum index e9b90d12..97667fdb 100644 --- a/cronsetup/go.sum +++ b/cronsetup/go.sum @@ -14,6 +14,8 @@ github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ4 github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/gofrs/uuid/v5 v5.3.2 h1:2jfO8j3XgSwlz/wHqemAEugfnTlikAYHhnqQ8Xh4fE0= +github.com/gofrs/uuid/v5 v5.3.2/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= From 4286c1f27c918d56ec746d901fd470886fa39c54 Mon Sep 17 00:00:00 2001 From: Soulou Date: Tue, 1 Apr 2025 08:03:23 +0200 Subject: [PATCH 2/2] Bump go 1.23 --- cronsetup/go.mod | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/cronsetup/go.mod b/cronsetup/go.mod index b15a9695..7d2b92b4 100644 --- a/cronsetup/go.mod +++ b/cronsetup/go.mod @@ -1,6 +1,8 @@ module github.com/Scalingo/go-utils/cronsetup -go 1.22 +go 1.23.0 + +toolchain go1.23.5 require ( github.com/Scalingo/go-etcd-cron v1.3.3