Skip to content

Commit 405f4fb

Browse files
committed
feat(cronsetup/request-id) Generate a unique request ID for each cron execution Fixes #1126
1 parent aab7ae1 commit 405f4fb

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

cronsetup/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## To be Released
44

5+
* feat(request_id) Inject `request_id` in context for each cron execution
6+
57
## v1.1.4
68

79
* Various dependencies updates

cronsetup/cronsetup.go

+14-3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import (
55
"fmt"
66

77
"github.com/pkg/errors"
8+
"github.com/sirupsen/logrus"
89
etcdclient "go.etcd.io/etcd/client/v3"
910

1011
etcdcron "github.com/Scalingo/go-etcd-cron"
1112
"github.com/Scalingo/go-utils/logger"
13+
14+
"github.com/gofrs/uuid/v5"
1215
)
1316

1417
type SetupOpts struct {
@@ -29,9 +32,17 @@ func Setup(ctx context.Context, opts SetupOpts) (func(), error) {
2932

3033
funcCtx := func(ctx context.Context, j etcdcron.Job) context.Context {
3134
log := logger.Get(ctx)
32-
log = log.WithField("cron-job", j.Name)
33-
log.Debug("Running cron job")
34-
return logger.ToCtx(ctx, log)
35+
requestID, err := uuid.NewV4()
36+
if err != nil {
37+
log.WithError(err).Error("generate UUID v4")
38+
} else {
39+
ctx = context.WithValue(ctx, "request_id", requestID.String())
40+
}
41+
ctx, _ = logger.WithFieldsToCtx(ctx, logrus.Fields{
42+
"cron-job", j.Name,
43+
"request_id", requestID.String(),
44+
})
45+
return ctx
3546
}
3647

3748
errorHandler := func(ctx context.Context, j etcdcron.Job, err error) {

cronsetup/go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ toolchain go1.23.7
66
require (
77
github.com/Scalingo/go-etcd-cron v1.3.3
88
github.com/Scalingo/go-utils/logger v1.4.0
9+
github.com/gofrs/uuid/v5 v5.3.2
910
github.com/pkg/errors v0.9.1
11+
github.com/sirupsen/logrus v1.9.3
1012
go.etcd.io/etcd/client/v3 v3.5.18
1113
)
1214

@@ -16,7 +18,6 @@ require (
1618
github.com/gogo/protobuf v1.3.2 // indirect
1719
github.com/golang/protobuf v1.5.4 // indirect
1820
github.com/iancoleman/strcase v0.3.0 // indirect
19-
github.com/sirupsen/logrus v1.9.3 // indirect
2021
go.etcd.io/etcd/api/v3 v3.5.18 // indirect
2122
go.etcd.io/etcd/client/pkg/v3 v3.5.18 // indirect
2223
go.uber.org/multierr v1.11.0 // indirect

cronsetup/go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ4
1414
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
1515
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
1616
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
17+
github.com/gofrs/uuid/v5 v5.3.2 h1:2jfO8j3XgSwlz/wHqemAEugfnTlikAYHhnqQ8Xh4fE0=
18+
github.com/gofrs/uuid/v5 v5.3.2/go.mod h1:CDOjlDMVAtN56jqyRUZh58JT31Tiw7/oQyEXZV+9bD8=
1719
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
1820
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
1921
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=

0 commit comments

Comments
 (0)