diff --git a/examples/atreugo/main.go b/examples/atreugo/main.go index 355b491..029a518 100644 --- a/examples/atreugo/main.go +++ b/examples/atreugo/main.go @@ -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 @@ -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 diff --git a/examples/fiber/main.go b/examples/fiber/main.go index 4920933..65e4243 100644 --- a/examples/fiber/main.go +++ b/examples/fiber/main.go @@ -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 @@ -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 diff --git a/go.mod b/go.mod index ad9e59d..fd64fb8 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index d7e3ffd..4e1e95c 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/tracy.go b/tracy.go index 74c67c6..d24f198 100644 --- a/tracy.go +++ b/tracy.go @@ -4,6 +4,8 @@ package tracygo import ( "context" + + "github.com/google/uuid" ) const ( @@ -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 @@ -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 { diff --git a/tracy_test.go b/tracy_test.go index 0d17c79..7418c10 100644 --- a/tracy_test.go +++ b/tracy_test.go @@ -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) @@ -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) @@ -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)