-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmiddleware.go
68 lines (59 loc) · 2.17 KB
/
middleware.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package spcontext
import (
"context"
"net/http"
"time"
grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware"
"google.golang.org/grpc"
)
// ContextInjector injects the given context into each request.
// Swapping the underlying context.Context for the one in the request.
func ContextInjector(ctx *Context) func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
return func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
next(w, r.WithContext(&Context{
Context: &mergeValuesContext{base: r.Context(), merged: ctx.Context},
fields: ctx.fields,
logger: ctx.logger,
Notifier: ctx.Notifier,
Tracer: ctx.Tracer,
onSpanStartHooks: ctx.onSpanStartHooks,
}))
}
}
// GRPCStreamContextInjector injects the given context into each stream.
// Swapping the underlying context.Context for the one in the request.
func GRPCStreamContextInjector(ctx *Context) grpc.StreamServerInterceptor {
return func(srv interface{}, stream grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
newCtx := &Context{
Context: &mergeValuesContext{base: stream.Context(), merged: ctx.Context},
fields: ctx.fields,
logger: ctx.logger,
Notifier: ctx.Notifier,
Tracer: ctx.Tracer,
onSpanStartHooks: ctx.onSpanStartHooks,
}
wrappedStream := grpc_middleware.WrapServerStream(stream)
wrappedStream.WrappedContext = newCtx
return handler(srv, wrappedStream)
}
}
// mergeValuesContext merges values from two contexts, with other properties being based on the base one.
// Can be removed when this proposal gets implemented: https://github.com/golang/go/issues/36503
type mergeValuesContext struct {
base, merged context.Context
}
func (m *mergeValuesContext) Deadline() (deadline time.Time, ok bool) {
return m.base.Deadline()
}
func (m *mergeValuesContext) Done() <-chan struct{} {
return m.base.Done()
}
func (m *mergeValuesContext) Err() error {
return m.base.Err()
}
func (m *mergeValuesContext) Value(key interface{}) interface{} {
if val := m.base.Value(key); val != nil {
return val
}
return m.merged.Value(key)
}