-
Notifications
You must be signed in to change notification settings - Fork 351
/
tracing.go
46 lines (35 loc) · 1.14 KB
/
tracing.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
package openpolicyagent
import (
"net/http"
opatracing "github.com/open-policy-agent/opa/tracing"
"github.com/opentracing/opentracing-go"
)
func init() {
opatracing.RegisterHTTPTracing(&tracingFactory{})
}
type tracingFactory struct{}
type transport struct {
opa *OpenPolicyAgentInstance
wrapped http.RoundTripper
}
func (*tracingFactory) NewTransport(tr http.RoundTripper, opts opatracing.Options) http.RoundTripper {
return &transport{
opa: opts[0].(*OpenPolicyAgentInstance),
wrapped: tr,
}
}
func (*tracingFactory) NewHandler(f http.Handler, label string, opts opatracing.Options) http.Handler {
return f
}
func (tr *transport) RoundTrip(req *http.Request) (*http.Response, error) {
ctx := req.Context()
parentSpan := opentracing.SpanFromContext(ctx)
if parentSpan != nil {
span := parentSpan.Tracer().StartSpan("http.send", opentracing.ChildOf(parentSpan.Context()))
defer span.Finish()
req = req.WithContext(opentracing.ContextWithSpan(ctx, span))
carrier := opentracing.HTTPHeadersCarrier(req.Header)
span.Tracer().Inject(span.Context(), opentracing.HTTPHeaders, carrier)
}
return tr.wrapped.RoundTrip(req)
}