-
Notifications
You must be signed in to change notification settings - Fork 0
/
restry.go
180 lines (156 loc) · 4.38 KB
/
restry.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package resty
import (
"crypto/tls"
"fmt"
"github.com/go-resty/resty/v2"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/propagation"
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
"go.opentelemetry.io/otel/trace"
"io"
"net"
"net/http"
"net/http/httputil"
"time"
)
/**
* go-resty/v2 支持 otel 链路跟踪的版本封装
*/
// TracerTransport 自定义 http-transport
type TracerTransport struct {
*http.Transport
debug bool
tracer trace.Tracer
propagation propagation.TextMapPropagator
}
// ClientOption 自定义属性
type ClientOption struct {
propagators propagation.TextMapPropagator
tracer trace.Tracer
timeout time.Duration
transport *http.Transport
debug bool
}
type Option func(*ClientOption)
func WithTracer(tracer trace.Tracer) Option {
return func(o *ClientOption) {
o.tracer = tracer
}
}
func WithPropagators(propagators propagation.TextMapPropagator) Option {
return func(o *ClientOption) {
o.propagators = propagators
}
}
func WithTimeout(timeout time.Duration) Option {
return func(o *ClientOption) {
o.timeout = timeout
}
}
func WithTransport(transport *http.Transport) Option {
return func(o *ClientOption) {
o.transport = transport
}
}
func WithDebug(debug bool) Option {
return func(o *ClientOption) {
o.debug = debug
}
}
func New(opts ...Option) *resty.Client {
o := &ClientOption{
propagators: propagation.NewCompositeTextMapPropagator(propagation.Baggage{}, propagation.TraceContext{}),
tracer: otel.GetTracerProvider().
Tracer("go-resty", trace.WithInstrumentationVersion("0.1.0")),
timeout: 30 * time.Second,
debug: false,
}
for _, opt := range opts {
opt(o)
}
if o.transport == nil {
o.transport = createTransport(o.timeout)
}
c := resty.New()
c.Debug = o.debug
c.SetTransport(&TracerTransport{
Transport: o.transport,
propagation: o.propagators,
tracer: o.tracer,
debug: o.debug, // debug 状态同时附加到 client 中,用来控制是否记录 body 到链路跟踪
})
return c
}
func createTransport(timeout time.Duration) *http.Transport {
dialer := &net.Dialer{
Timeout: timeout,
KeepAlive: 30 * time.Second,
}
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: dialer.DialContext,
ForceAttemptHTTP2: true,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 30 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
DisableKeepAlives: false,
DisableCompression: false,
MaxIdleConns: 1000,
MaxIdleConnsPerHost: 100,
MaxConnsPerHost: 1000,
}
}
func (r *TracerTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
attrs := make([]attribute.KeyValue, 0)
ctx, span := r.tracer.Start(req.Context(),
fmt.Sprintf("%s %s%s", req.Method, req.Host, req.URL.Path),
trace.WithSpanKind(trace.SpanKindClient))
r.propagation.Inject(ctx, propagation.HeaderCarrier(req.Header))
defer span.End()
attrs = append(attrs, peerAttr(req.RemoteAddr)...)
attrs = append(attrs, semconv.HTTPClientAttributesFromHTTPRequest(req)...)
attrs = append(attrs, semconv.HTTPTargetKey.String(req.URL.Path))
reqBody, err := req.GetBody()
if err == nil && reqBody != nil {
data, _ := io.ReadAll(reqBody)
if len(data) > 0 {
attrs = append(attrs, attribute.String("http.request.body", string(data)))
}
}
req = req.WithContext(ctx)
r.propagation.Inject(ctx, propagation.HeaderCarrier(req.Header))
resp, err = r.Transport.RoundTrip(req)
if err != nil {
span.RecordError(err, trace.WithTimestamp(time.Now()))
return
}
// record response status
if resp.StatusCode >= 400 {
span.SetStatus(codes.Error, resp.Status)
}
if resp.Body != nil {
respDump, _ := httputil.DumpResponse(resp, r.debug)
if len(respDump) > 0 {
attrs = append(attrs, attribute.String("http.response.data", string(respDump)))
}
}
attrs = append(attrs, semconv.HTTPStatusCodeKey.Int(resp.StatusCode))
span.SetAttributes(attrs...)
return resp, nil
}
func peerAttr(addr string) []attribute.KeyValue {
host, port, err := net.SplitHostPort(addr)
if err != nil {
return []attribute.KeyValue(nil)
}
if host == "" {
host = "127.0.0.1"
}
return []attribute.KeyValue{
semconv.NetPeerIPKey.String(host),
semconv.NetPeerPortKey.String(port),
}
}