File: transport.go

package info (click to toggle)
golang-github-openzipkin-zipkin-go 0.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 712 kB
  • sloc: makefile: 23
file content (244 lines) | stat: -rw-r--r-- 7,122 bytes parent folder | download
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2022 The OpenZipkin Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package http

import (
	"bytes"
	"io"
	"io/ioutil"
	"log"
	"net/http"
	"net/http/httptrace"
	"os"
	"strconv"

	"github.com/openzipkin/zipkin-go"
	"github.com/openzipkin/zipkin-go/model"
	"github.com/openzipkin/zipkin-go/propagation/b3"
)

// ErrHandler allows instrumentations to decide how to tag errors
// based on the response status code >399 and the error from the
// Transport.RoundTrip
type ErrHandler func(sp zipkin.Span, err error, statusCode int)

func defaultErrHandler(sp zipkin.Span, err error, statusCode int) {
	if err != nil {
		zipkin.TagError.Set(sp, err.Error())
		return
	}

	statusCodeVal := strconv.FormatInt(int64(statusCode), 10)
	zipkin.TagError.Set(sp, statusCodeVal)
}

// ErrResponseReader allows instrumentations to read the error body
// and decide to obtain information to it and add it to the span i.e.
// tag the span with a more meaningful error code or with error details.
type ErrResponseReader func(sp zipkin.Span, body io.Reader)

type transport struct {
	tracer            *zipkin.Tracer
	rt                http.RoundTripper
	httpTrace         bool
	defaultTags       map[string]string
	errHandler        ErrHandler
	errResponseReader *ErrResponseReader
	logger            *log.Logger
	requestSampler    RequestSamplerFunc
	remoteEndpoint    *model.Endpoint
}

// TransportOption allows one to configure optional transport configuration.
type TransportOption func(*transport)

// RoundTripper adds the Transport RoundTripper to wrap.
func RoundTripper(rt http.RoundTripper) TransportOption {
	return func(t *transport) {
		if rt != nil {
			t.rt = rt
		}
	}
}

// TransportTags adds default Tags to inject into transport spans.
func TransportTags(tags map[string]string) TransportOption {
	return func(t *transport) {
		t.defaultTags = tags
	}
}

// TransportTrace allows one to enable Go's net/http/httptrace.
func TransportTrace(enable bool) TransportOption {
	return func(t *transport) {
		t.httpTrace = enable
	}
}

// TransportErrHandler allows to pass a custom error handler for the round trip
func TransportErrHandler(h ErrHandler) TransportOption {
	return func(t *transport) {
		t.errHandler = h
	}
}

// TransportErrResponseReader allows to pass a custom ErrResponseReader
func TransportErrResponseReader(r ErrResponseReader) TransportOption {
	return func(t *transport) {
		t.errResponseReader = &r
	}
}

// TransportRemoteEndpoint will set the remote endpoint for all spans.
func TransportRemoteEndpoint(remoteEndpoint *model.Endpoint) TransportOption {
	return func(c *transport) {
		c.remoteEndpoint = remoteEndpoint
	}
}

// TransportLogger allows to plug a logger into the transport
func TransportLogger(l *log.Logger) TransportOption {
	return func(t *transport) {
		t.logger = l
	}
}

// TransportRequestSampler allows one to set the sampling decision based on
// the details found in the http.Request. It has preference over the existing
// sampling decision contained in the context. The function returns a *bool,
// if returning nil, sampling decision is not being changed whereas returning
// something else than nil is being used as sampling decision.
func TransportRequestSampler(sampleFunc RequestSamplerFunc) TransportOption {
	return func(t *transport) {
		t.requestSampler = sampleFunc
	}
}

// NewTransport returns a new Zipkin instrumented http RoundTripper which can be
// used with a standard library http Client.
func NewTransport(tracer *zipkin.Tracer, options ...TransportOption) (http.RoundTripper, error) {
	if tracer == nil {
		return nil, ErrValidTracerRequired
	}

	t := &transport{
		tracer:     tracer,
		rt:         http.DefaultTransport,
		httpTrace:  false,
		errHandler: defaultErrHandler,
		logger:     log.New(os.Stderr, "", log.LstdFlags),
	}

	for _, option := range options {
		option(t)
	}

	return t, nil
}

// RoundTrip satisfies the RoundTripper interface.
func (t *transport) RoundTrip(req *http.Request) (res *http.Response, err error) {
	sp, _ := t.tracer.StartSpanFromContext(
		req.Context(), req.URL.Scheme+"/"+req.Method, zipkin.Kind(model.Client), zipkin.RemoteEndpoint(t.remoteEndpoint),
	)

	// inject registered headers from span context into the outgoing HTTP request headers
	if sp.Context().Baggage != nil {
		sp.Context().Baggage.Iterate(func(key string, values []string) {
			for _, val := range values {
				req.Header.Add(key, val)
			}
		})
	}

	if zipkin.IsNoop(sp) {
		// While the span is not being recorded, we still want to propagate the context.
		_ = b3.InjectHTTP(req)(sp.Context())
		return t.rt.RoundTrip(req)
	}

	for k, v := range t.defaultTags {
		sp.Tag(k, v)
	}

	if t.httpTrace {
		sptr := spanTrace{
			Span: sp,
		}
		sptr.c = &httptrace.ClientTrace{
			GetConn:              sptr.getConn,
			GotConn:              sptr.gotConn,
			PutIdleConn:          sptr.putIdleConn,
			GotFirstResponseByte: sptr.gotFirstResponseByte,
			Got100Continue:       sptr.got100Continue,
			DNSStart:             sptr.dnsStart,
			DNSDone:              sptr.dnsDone,
			ConnectStart:         sptr.connectStart,
			ConnectDone:          sptr.connectDone,
			TLSHandshakeStart:    sptr.tlsHandshakeStart,
			TLSHandshakeDone:     sptr.tlsHandshakeDone,
			WroteHeaders:         sptr.wroteHeaders,
			Wait100Continue:      sptr.wait100Continue,
			WroteRequest:         sptr.wroteRequest,
		}

		req = req.WithContext(
			httptrace.WithClientTrace(req.Context(), sptr.c),
		)
	}

	zipkin.TagHTTPMethod.Set(sp, req.Method)
	zipkin.TagHTTPPath.Set(sp, req.URL.Path)

	spCtx := sp.Context()
	if t.requestSampler != nil {
		if shouldSample := t.requestSampler(req); shouldSample != nil {
			spCtx.Sampled = shouldSample
		}
	}

	_ = b3.InjectHTTP(req)(spCtx)

	res, err = t.rt.RoundTrip(req)
	if err != nil {
		t.errHandler(sp, err, 0)
		sp.Finish()
		return nil, err
	}

	if res.ContentLength > 0 {
		zipkin.TagHTTPResponseSize.Set(sp, strconv.FormatInt(res.ContentLength, 10))
	}
	if res.StatusCode < 200 || res.StatusCode > 299 {
		statusCode := strconv.FormatInt(int64(res.StatusCode), 10)
		zipkin.TagHTTPStatusCode.Set(sp, statusCode)
		if res.StatusCode > 399 {
			t.errHandler(sp, nil, res.StatusCode)

			if t.errResponseReader != nil {
				sBody, err := ioutil.ReadAll(res.Body)
				if err == nil {
					res.Body.Close()
					(*t.errResponseReader)(sp, ioutil.NopCloser(bytes.NewBuffer(sBody)))
					res.Body = ioutil.NopCloser(bytes.NewBuffer(sBody))
				} else {
					t.logger.Printf("failed to read the response body in the ErrResponseReader: %v", err)
				}
			}
		}
	}
	sp.Finish()
	return res, err
}