Skip to content

Commit e807639

Browse files
committed
accounts: start adding request ID to context
In preperation for the trace logging we want to implement in an upcoming commit (where logs for requests & responsescan be linked via their request ID), we start adding the request ID to an intercepted account request/response in this commit.
1 parent 04ef762 commit e807639

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

accounts/checkers_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ func TestAccountChecker(t *testing.T) {
112112
func TestAccountCheckers(t *testing.T) {
113113
t.Parallel()
114114

115+
const reqID = uint64(55)
116+
115117
testCases := []struct {
116118
name string
117119
fullURI string
@@ -416,9 +418,8 @@ func TestAccountCheckers(t *testing.T) {
416418
Invoices: make(AccountInvoices),
417419
Payments: make(AccountPayments),
418420
}
419-
ctx := AddToContext(
420-
context.Background(), KeyAccount, acct,
421-
)
421+
ctx := AddAccountToContext(context.Background(), acct)
422+
ctx = AddRequestIDToContext(ctx, reqID)
422423

423424
// Is a setup call required to initialize initial
424425
// conditions?

accounts/context.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,23 @@ var (
1818
// KeyAccount is the key under which we store the account in the request
1919
// context.
2020
KeyAccount = ContextKey{"account"}
21+
22+
// KeyRequestID is the key under which we store the middleware request
23+
// ID.
24+
KeyRequestID = ContextKey{"request_id"}
2125
)
2226

2327
// FromContext tries to extract a value from the given context.
2428
func FromContext(ctx context.Context, key ContextKey) interface{} {
2529
return ctx.Value(key)
2630
}
2731

28-
// AddToContext adds the given value to the context for easy retrieval later on.
29-
func AddToContext(ctx context.Context, key ContextKey,
32+
// AddAccountToContext adds the given value to the context for easy retrieval
33+
// later on.
34+
func AddAccountToContext(ctx context.Context,
3035
value *OffChainBalanceAccount) context.Context {
3136

32-
return context.WithValue(ctx, key, value)
37+
return context.WithValue(ctx, KeyAccount, value)
3338
}
3439

3540
// AccountFromContext attempts to extract an account from the given context.
@@ -46,3 +51,24 @@ func AccountFromContext(ctx context.Context) (*OffChainBalanceAccount, error) {
4651

4752
return acct, nil
4853
}
54+
55+
// AddRequestIDToContext adds the given request ID to the context for easy
56+
// retrieval later on.
57+
func AddRequestIDToContext(ctx context.Context, value uint64) context.Context {
58+
return context.WithValue(ctx, KeyRequestID, value)
59+
}
60+
61+
// RequestIDFromContext attempts to extract a request ID from the given context.
62+
func RequestIDFromContext(ctx context.Context) (uint64, error) {
63+
val := FromContext(ctx, KeyRequestID)
64+
if val == nil {
65+
return 0, fmt.Errorf("no request ID found in context")
66+
}
67+
68+
reqID, ok := val.(uint64)
69+
if !ok {
70+
return 0, fmt.Errorf("invalid request ID value in context")
71+
}
72+
73+
return reqID, nil
74+
}

accounts/interceptor.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,10 @@ func (s *InterceptorService) Intercept(ctx context.Context,
101101
)
102102
}
103103

104-
// We now add the account to the incoming context to give each checker
105-
// access to it if required.
106-
ctxAccount := AddToContext(ctx, KeyAccount, acct)
104+
// We now add the account and request ID to the incoming context to give
105+
// each checker access to them if required.
106+
ctx = AddAccountToContext(ctx, acct)
107+
ctx = AddRequestIDToContext(ctx, req.RequestId)
107108

108109
switch r := req.InterceptType.(type) {
109110
// In the authentication phase we just check that the account hasn't
@@ -120,7 +121,7 @@ func (s *InterceptorService) Intercept(ctx context.Context,
120121
}
121122

122123
return mid.RPCErr(req, s.checkers.checkIncomingRequest(
123-
ctxAccount, r.Request.MethodFullUri, msg,
124+
ctx, r.Request.MethodFullUri, msg,
124125
))
125126

126127
// Parse and possibly manipulate outgoing responses.
@@ -131,7 +132,7 @@ func (s *InterceptorService) Intercept(ctx context.Context,
131132
}
132133

133134
replacement, err := s.checkers.replaceOutgoingResponse(
134-
ctxAccount, r.Response.MethodFullUri, msg,
135+
ctx, r.Response.MethodFullUri, msg,
135136
)
136137
if err != nil {
137138
return mid.RPCErr(req, err)

0 commit comments

Comments
 (0)