Copyright 2018, OpenCensus 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 ochttp

import (
	
	
	
	
	
	

	
	
)
statsTransport is an http.RoundTripper that collects stats for the outgoing requests.
RoundTrip implements http.RoundTripper, delegating to Base and recording stats for the request.
func ( statsTransport) ( *http.Request) (*http.Response, error) {
	,  := tag.New(.Context(),
		tag.Upsert(KeyClientHost, .Host),
		tag.Upsert(Host, .Host),
		tag.Upsert(KeyClientPath, .URL.Path),
		tag.Upsert(Path, .URL.Path),
		tag.Upsert(KeyClientMethod, .Method),
		tag.Upsert(Method, .Method))
	 = .WithContext()
	 := &tracker{
		start: time.Now(),
		ctx:   ,
	}
TODO: Handle cases where ContentLength is not set.
		.reqSize = -1
	} else if .ContentLength > 0 {
		.reqSize = .ContentLength
	}
	stats.Record(, ClientRequestCount.M(1))
Perform request.
	,  := .base.RoundTrip()

	if  != nil {
		.statusCode = http.StatusInternalServerError
		.end()
	} else {
		.statusCode = .StatusCode
		if .Method != "HEAD" {
			.respContentLength = .ContentLength
		}
		if .Body == nil {
			.end()
		} else {
			.body = .Body
			.Body = wrappedBody(, .Body)
		}
	}
	return , 
}
CancelRequest cancels an in-flight request by closing its connection.
func ( statsTransport) ( *http.Request) {
	type  interface {
		(*http.Request)
	}
	if ,  := .base.();  {
		.()
	}
}

type tracker struct {
	ctx               context.Context
	respSize          int64
	respContentLength int64
	reqSize           int64
	start             time.Time
	body              io.ReadCloser
	statusCode        int
	endOnce           sync.Once
}

var _ io.ReadCloser = (*tracker)(nil)

func ( *tracker) () {
	.endOnce.Do(func() {
		 := float64(time.Since(.start)) / float64(time.Millisecond)
		 := .respSize
		if .respSize == 0 && .respContentLength > 0 {
			 = .respContentLength
		}
		 := []stats.Measurement{
			ClientSentBytes.M(.reqSize),
			ClientReceivedBytes.M(),
			ClientRoundtripLatency.M(),
			ClientLatency.M(),
			ClientResponseBytes.M(.respSize),
		}
		if .reqSize >= 0 {
			 = append(, ClientRequestBytes.M(.reqSize))
		}

		stats.RecordWithTags(.ctx, []tag.Mutator{
			tag.Upsert(StatusCode, strconv.Itoa(.statusCode)),
			tag.Upsert(KeyClientStatus, strconv.Itoa(.statusCode)),
		}, ...)
	})
}

func ( *tracker) ( []byte) (int, error) {
	,  := .body.Read()
	.respSize += int64()
	switch  {
	case nil:
		return , nil
	case io.EOF:
		.end()
	}
	return , 
}

Invoking endSpan on Close will help catch the cases in which a read returned a non-nil error, we set the span status but didn't end the span.
	.end()
	return .body.Close()