-
Notifications
You must be signed in to change notification settings - Fork 1
/
http_connect_options.go
98 lines (83 loc) · 2.4 KB
/
http_connect_options.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package connect
import (
"bytes"
"io"
"net/http"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
)
// Transport for tracing HTTP operations.
type Transport struct {
rt http.RoundTripper
}
// Option signature for specifying options, e.g. WithRoundTripper.
type Option func(t *Transport)
// WithRoundTripper specifies the http.RoundTripper to call
// next after this transport. If it is nil (default), the
// transport will use http.DefaultTransport.
func WithRoundTripper(rt http.RoundTripper) Option {
return func(t *Transport) {
t.rt = rt
}
}
// NewTransport specifies a transport that will trace HTTP
// and report back via OpenTracing.
func NewTransport(opts ...Option) *Transport {
t := &Transport{}
for _, o := range opts {
o(t)
}
return t
}
// RoundTrip captures the request and starts an OpenTracing span
// for HTTP operation.
func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
ctx, span := otel.Tracer("HTTP").Start(req.Context(), "HTTPRequest")
defer span.End()
// See General (https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/span-general.md)
// and HTTP (https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md)
attributes := []attribute.KeyValue{
attribute.String("http.url", req.URL.Redacted()),
attribute.String("http.method", req.Method),
attribute.String("http.scheme", req.URL.Scheme),
attribute.String("http.host", req.URL.Hostname()),
attribute.String("http.path", req.URL.Path),
attribute.String("http.user_agent", req.UserAgent()),
}
req = req.WithContext(ctx)
var (
buf []byte
err error
reader io.ReadCloser
)
if req.Body == nil {
goto SetAttribute
}
buf, err = io.ReadAll(req.Body)
if err == nil {
attributes = append(attributes, attribute.String("http.body", string(buf)))
}
reader = io.NopCloser(bytes.NewBuffer(buf))
req.Body = reader
SetAttribute:
span.SetAttributes(
attributes...,
)
var (
resp *http.Response
)
if t.rt != nil {
resp, err = t.rt.RoundTrip(req)
} else {
resp, err = http.DefaultTransport.RoundTrip(req)
}
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
}
if resp != nil {
span.SetAttributes(attribute.Int64("http.status_code", int64(resp.StatusCode)))
}
return resp, err
}