@@ -9,14 +9,29 @@ import (
9
9
)
10
10
11
11
// AddRequestScopedClient returns a net/http middleware that, for each incoming request,
12
- // creates an LDScopedClient seeded with a `request`-kind LDContext and stores it in the
12
+ // creates an LDScopedClient seeded with a `request`-kind LDContext populated with useful
13
+ // HTTP request attributes (e.g., method, path, host, userAgent), and stores it in the
13
14
// request's Go context. Downstream handlers can retrieve it via ld.GetScopedClient.
14
15
func AddRequestScopedClient (client * ld.LDClient ) func (next http.Handler ) http.Handler {
15
16
return func (next http.Handler ) http.Handler {
16
17
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
17
18
// Use a UUID to identify the request context key.
18
19
requestKey := uuid .New ().String ()
19
- requestCtx := ldcontext .NewWithKind ("request" , requestKey )
20
+ b := ldcontext .NewBuilder (requestKey ).Kind ("request" ).Anonymous (true )
21
+ b .SetString ("method" , r .Method )
22
+ b .SetString ("host" , r .Host )
23
+ b .SetString ("userAgent" , r .UserAgent ())
24
+ if r .URL != nil {
25
+ b .SetString ("path" , r .URL .Path )
26
+ b .SetString ("scheme" , r .URL .Scheme )
27
+ b .SetString ("query" , r .URL .RawQuery )
28
+ }
29
+ b .SetString ("proto" , r .Proto )
30
+ b .SetString ("remoteAddr" , r .RemoteAddr )
31
+ if rid := r .Header .Get ("X-Request-Id" ); rid != "" {
32
+ b .SetString ("requestId" , rid )
33
+ }
34
+ requestCtx := b .Build ()
20
35
21
36
scoped := ld .NewScopedClient (client , requestCtx )
22
37
ctxWithScoped := ld .GoContextWithScopedClient (r .Context (), scoped )
0 commit comments