-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add unique ID per request (#386)
- Loading branch information
Showing
11 changed files
with
196 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package controllers_test | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/google/uuid" | ||
"github.com/numary/ledger/pkg" | ||
"github.com/numary/ledger/pkg/api" | ||
"github.com/numary/ledger/pkg/api/controllers" | ||
"github.com/numary/ledger/pkg/api/internal" | ||
"github.com/numary/ledger/pkg/core" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/fx" | ||
) | ||
|
||
func TestContext(t *testing.T) { | ||
internal.RunTest(t, fx.Invoke(func(lc fx.Lifecycle, api *api.API) { | ||
lc.Append(fx.Hook{ | ||
OnStart: func(ctx context.Context) error { | ||
t.Run("GET/stats", func(t *testing.T) { | ||
rsp := internal.GetStats(api) | ||
_, err := uuid.Parse(rsp.Header().Get(string(pkg.KeyContextID))) | ||
require.NoError(t, err) | ||
}) | ||
t.Run("GET/log", func(t *testing.T) { | ||
rsp := internal.GetLogs(api, url.Values{}) | ||
_, err := uuid.Parse(rsp.Header().Get(string(pkg.KeyContextID))) | ||
require.NoError(t, err) | ||
}) | ||
t.Run("GET/accounts", func(t *testing.T) { | ||
rsp := internal.GetAccounts(api, url.Values{}) | ||
_, err := uuid.Parse(rsp.Header().Get(string(pkg.KeyContextID))) | ||
require.NoError(t, err) | ||
}) | ||
t.Run("GET/transactions", func(t *testing.T) { | ||
rsp := internal.GetTransactions(api, url.Values{}) | ||
_, err := uuid.Parse(rsp.Header().Get(string(pkg.KeyContextID))) | ||
require.NoError(t, err) | ||
}) | ||
t.Run("POST/transactions", func(t *testing.T) { | ||
rsp := internal.PostTransaction(t, api, controllers.PostTransaction{}, true) | ||
_, err := uuid.Parse(rsp.Header().Get(string(pkg.KeyContextID))) | ||
require.NoError(t, err) | ||
}) | ||
t.Run("POST/transactions/batch", func(t *testing.T) { | ||
rsp := internal.PostTransactionBatch(t, api, core.Transactions{}) | ||
_, err := uuid.Parse(rsp.Header().Get(string(pkg.KeyContextID))) | ||
require.NoError(t, err) | ||
}) | ||
t.Run("GET/balances", func(t *testing.T) { | ||
rsp := internal.GetBalances(api, url.Values{}) | ||
_, err := uuid.Parse(rsp.Header().Get(string(pkg.KeyContextID))) | ||
require.NoError(t, err) | ||
}) | ||
|
||
return nil | ||
}, | ||
}) | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package pkg | ||
|
||
type ContextKeyIDType string | ||
|
||
var KeyContextID ContextKeyIDType = "contextID" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package contextlogger | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/formancehq/go-libs/sharedlogging" | ||
"github.com/numary/ledger/pkg" | ||
) | ||
|
||
var _ sharedlogging.Logger = &ContextLogger{} | ||
|
||
type ContextLogger struct { | ||
ctx context.Context | ||
underlyingLogger sharedlogging.Logger | ||
} | ||
|
||
func New(ctx context.Context, logger sharedlogging.Logger) *ContextLogger { | ||
return &ContextLogger{ | ||
ctx: ctx, | ||
underlyingLogger: logger, | ||
} | ||
} | ||
|
||
func (c ContextLogger) Debugf(format string, args ...any) { | ||
id := c.ctx.Value(pkg.KeyContextID) | ||
c.underlyingLogger. | ||
WithFields(map[string]any{string(pkg.KeyContextID): id}). | ||
Debugf(format, args...) | ||
} | ||
|
||
func (c ContextLogger) Infof(format string, args ...any) { | ||
id := c.ctx.Value(pkg.KeyContextID) | ||
c.underlyingLogger. | ||
WithFields(map[string]any{string(pkg.KeyContextID): id}). | ||
Infof(format, args...) | ||
} | ||
|
||
func (c ContextLogger) Errorf(format string, args ...any) { | ||
id := c.ctx.Value(pkg.KeyContextID) | ||
c.underlyingLogger. | ||
WithFields(map[string]any{string(pkg.KeyContextID): id}). | ||
Errorf(format, args...) | ||
} | ||
|
||
func (c ContextLogger) Debug(args ...any) { | ||
c.underlyingLogger.Debug(args...) | ||
} | ||
|
||
func (c ContextLogger) Info(args ...any) { | ||
c.underlyingLogger.Info(args...) | ||
} | ||
|
||
func (c ContextLogger) Error(args ...any) { | ||
c.underlyingLogger.Error(args...) | ||
} | ||
|
||
func (c ContextLogger) WithFields(m map[string]any) sharedlogging.Logger { | ||
m[string(pkg.KeyContextID)] = c.ctx.Value(pkg.KeyContextID) | ||
return &ContextLogger{ | ||
ctx: c.ctx, | ||
underlyingLogger: c.underlyingLogger.WithFields(m), | ||
} | ||
} | ||
|
||
func (c ContextLogger) WithContext(ctx context.Context) sharedlogging.Logger { | ||
return &ContextLogger{ | ||
ctx: ctx, | ||
underlyingLogger: c.underlyingLogger, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters