1 // Copyright 2018, OpenCensus Authors
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
25 "go.opencensus.io/stats"
26 "go.opencensus.io/tag"
29 // statsTransport is an http.RoundTripper that collects stats for the outgoing requests.
30 type statsTransport struct {
31 base http.RoundTripper
34 // RoundTrip implements http.RoundTripper, delegating to Base and recording stats for the request.
35 func (t statsTransport) RoundTrip(req *http.Request) (*http.Response, error) {
36 ctx, _ := tag.New(req.Context(),
37 tag.Upsert(KeyClientHost, req.Host),
38 tag.Upsert(Host, req.Host),
39 tag.Upsert(KeyClientPath, req.URL.Path),
40 tag.Upsert(Path, req.URL.Path),
41 tag.Upsert(KeyClientMethod, req.Method),
42 tag.Upsert(Method, req.Method))
43 req = req.WithContext(ctx)
49 // TODO: Handle cases where ContentLength is not set.
51 } else if req.ContentLength > 0 {
52 track.reqSize = req.ContentLength
54 stats.Record(ctx, ClientRequestCount.M(1))
57 resp, err := t.base.RoundTrip(req)
60 track.statusCode = http.StatusInternalServerError
63 track.statusCode = resp.StatusCode
64 if req.Method != "HEAD" {
65 track.respContentLength = resp.ContentLength
70 track.body = resp.Body
71 resp.Body = wrappedBody(track, resp.Body)
77 // CancelRequest cancels an in-flight request by closing its connection.
78 func (t statsTransport) CancelRequest(req *http.Request) {
79 type canceler interface {
80 CancelRequest(*http.Request)
82 if cr, ok := t.base.(canceler); ok {
90 respContentLength int64
98 var _ io.ReadCloser = (*tracker)(nil)
100 func (t *tracker) end() {
101 t.endOnce.Do(func() {
102 latencyMs := float64(time.Since(t.start)) / float64(time.Millisecond)
103 respSize := t.respSize
104 if t.respSize == 0 && t.respContentLength > 0 {
105 respSize = t.respContentLength
107 m := []stats.Measurement{
108 ClientSentBytes.M(t.reqSize),
109 ClientReceivedBytes.M(respSize),
110 ClientRoundtripLatency.M(latencyMs),
111 ClientLatency.M(latencyMs),
112 ClientResponseBytes.M(t.respSize),
115 m = append(m, ClientRequestBytes.M(t.reqSize))
118 stats.RecordWithTags(t.ctx, []tag.Mutator{
119 tag.Upsert(StatusCode, strconv.Itoa(t.statusCode)),
120 tag.Upsert(KeyClientStatus, strconv.Itoa(t.statusCode)),
125 func (t *tracker) Read(b []byte) (int, error) {
126 n, err := t.body.Read(b)
127 t.respSize += int64(n)
137 func (t *tracker) Close() error {
138 // Invoking endSpan on Close will help catch the cases
139 // in which a read returned a non-nil error, we set the
140 // span status but didn't end the span.
142 return t.body.Close()