Skip to content

Commit

Permalink
feat: add ensure correlation id functionality (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
lks-mtzn authored Jun 7, 2024
1 parent 84fde2e commit f690ab9
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
4 changes: 2 additions & 2 deletions examples/atreugo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func NewAPI(tracer *tracygo.TracyGo, restyClient *resty.Client) *API {
func (a *API) AtreugoHandler(wg *sync.WaitGroup) func(ctx *atreugo.RequestCtx) error {
return func(ctx *atreugo.RequestCtx) error {
//nolint:forbidigo // intended use
fmt.Printf("HelloWorld: X-Correlation-ID = %s\n", a.tracer.CorrelationIDromContext(ctx)) // Zitronenbaum
fmt.Printf("HelloWorld: X-Correlation-ID = %s\n", a.tracer.CorrelationIDFromContext(ctx)) // Zitronenbaum
//nolint:forbidigo // intended use
fmt.Printf("HelloWorld: X-Request-ID = %s\n", a.tracer.RequestIDFromContext(ctx)) // generated

Expand All @@ -105,7 +105,7 @@ func (a *API) AtreugoHandler(wg *sync.WaitGroup) func(ctx *atreugo.RequestCtx) e
func (a *API) AtreugoHandler2(wg *sync.WaitGroup) func(ctx *atreugo.RequestCtx) error {
return func(ctx *atreugo.RequestCtx) error {
//nolint:forbidigo // intended use
fmt.Printf("HelloWorld2: X-Correlation-ID = %s\n", a.tracer.CorrelationIDromContext(ctx)) // Zitronenbaum
fmt.Printf("HelloWorld2: X-Correlation-ID = %s\n", a.tracer.CorrelationIDFromContext(ctx)) // Zitronenbaum
//nolint:forbidigo // intended use
fmt.Printf("HelloWorld2: X-Request-ID = %s\n", a.tracer.RequestIDFromContext(ctx)) // new generated

Expand Down
4 changes: 2 additions & 2 deletions examples/fiber/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func NewAPI(tracer *tracygo.TracyGo, restyClient *resty.Client) *API {
func (a *API) FiberHandler(wg *sync.WaitGroup) func(ctx *fiber.Ctx) error {
return func(ctx *fiber.Ctx) error {
//nolint:forbidigo // intended use
fmt.Printf("HelloWorld: X-Correlation-ID = %s\n", a.tracer.CorrelationIDromContext(ctx.Context())) // Zitronenbaum
fmt.Printf("HelloWorld: X-Correlation-ID = %s\n", a.tracer.CorrelationIDFromContext(ctx.Context())) // Zitronenbaum
//nolint:forbidigo // intended use
fmt.Printf("HelloWorld: X-Request-ID = %s\n", a.tracer.RequestIDFromContext(ctx.Context())) // generated

Expand All @@ -102,7 +102,7 @@ func (a *API) FiberHandler(wg *sync.WaitGroup) func(ctx *fiber.Ctx) error {
func (a *API) FiberHandler2(wg *sync.WaitGroup) func(ctx *fiber.Ctx) error {
return func(ctx *fiber.Ctx) error {
//nolint:forbidigo // intended use
fmt.Printf("HelloWorld2: X-Correlation-ID = %s\n", a.tracer.CorrelationIDromContext(ctx.Context())) // Zitronenbaum
fmt.Printf("HelloWorld2: X-Correlation-ID = %s\n", a.tracer.CorrelationIDFromContext(ctx.Context())) // Zitronenbaum
//nolint:forbidigo // intended use
fmt.Printf("HelloWorld2: X-Request-ID = %s\n", a.tracer.RequestIDFromContext(ctx.Context())) // new generated

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/Clarilab/tracygo/v2

go 1.18
go 1.22.4

require (
github.com/go-resty/resty/v2 v2.13.1
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down
21 changes: 18 additions & 3 deletions tracy.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package tracygo

import (
"context"

"github.com/google/uuid"
)

const (
Expand Down Expand Up @@ -41,8 +43,8 @@ func (t *TracyGo) RequestIDKey() string {
return t.requestID
}

// FromContext returns the correlation id from the given context, or the an empty string.
func (t *TracyGo) CorrelationIDromContext(ctx context.Context) string {
// CorrelationIDFromContext returns the correlation id from the given context, or an empty string.
func (t *TracyGo) CorrelationIDFromContext(ctx context.Context) string {
if ctx != nil {
if correlationID, ok := ctx.Value(t.correlationID).(string); ok {
return correlationID
Expand All @@ -52,7 +54,20 @@ func (t *TracyGo) CorrelationIDromContext(ctx context.Context) string {
return ""
}

// FromContext returns the correlationID from the given context, or the an empty string.
// EnsureCorrelationID makes sure the given context has a correlation id set.
func (t *TracyGo) EnsureCorrelationID(ctx context.Context) context.Context {
if ctx == nil {
ctx = context.Background()
}

if _, ok := ctx.Value(t.correlationID).(string); ok {
return ctx
}

return t.NewContextWithCorrelationID(ctx, uuid.NewString())
}

// RequestIDFromContext returns the requestID from the given context, or an empty string.
func (t *TracyGo) RequestIDFromContext(ctx context.Context) string {
if ctx != nil {
if requestID, ok := ctx.Value(t.requestID).(string); ok {
Expand Down
6 changes: 3 additions & 3 deletions tracy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func Test_FromContext(t *testing.T) {

ctx := context.WithValue(context.Background(), keyCorrelationID, "Zitronenbaum") //nolint:staticcheck,revive // intended use for testing

id := tracer.CorrelationIDromContext(ctx)
id := tracer.CorrelationIDFromContext(ctx)

if id != "Zitronenbaum" {
t.Errorf("expected 'Zitronenbaum', got '%s'", id)
Expand All @@ -83,7 +83,7 @@ func Test_FromContext(t *testing.T) {
t.Run("correlationID does not exist", func(t *testing.T) {
t.Parallel()

id := tracer.CorrelationIDromContext(context.Background())
id := tracer.CorrelationIDFromContext(context.Background())

if id != "" {
t.Errorf("expected '', got '%s'", id)
Expand Down Expand Up @@ -115,7 +115,7 @@ func Test_FromContext(t *testing.T) {
t.Run("nil context", func(t *testing.T) {
t.Parallel()

id := tracer.CorrelationIDromContext(nil) //nolint:staticcheck // intended use for testing
id := tracer.CorrelationIDFromContext(nil) //nolint:staticcheck // intended use for testing

if id != "" {
t.Errorf("expected '', got '%s'", id)
Expand Down

0 comments on commit f690ab9

Please sign in to comment.