Skip to content

feat(cronsetup/request-id) Generate a unique request ID for each cron execution Fixes #1126 #1127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cronsetup/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 14 additions & 3 deletions cronsetup/cronsetup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It shouldn't happen but could you check that there is no RequestID already in the context ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could even if indeed it shouldn't happen

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) {
Expand Down
8 changes: 5 additions & 3 deletions cronsetup/go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
module github.com/Scalingo/go-utils/cronsetup

go 1.22
toolchain go1.23.7
go 1.23.0

toolchain go1.23.5

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
)

Expand All @@ -16,7 +19,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
Expand Down
2 changes: 2 additions & 0 deletions cronsetup/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down