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 (
	
	
	

	
	
	
)
TODO(jbd): Add godoc examples.
Attributes recorded on the span for the requests. Only trace exporters will need them.
TODO(jbd): Add message events for request and response size.
RoundTrip creates a trace.Span and inserts it into the outgoing request's headers. The created span can follow a parent span, if a parent is presented in the request's context.
TODO(jbd): Discuss whether we want to prefix outgoing requests with Sent.
	,  := trace.StartSpan(.Context(), ,
		trace.WithSampler(.startOptions.Sampler),
		trace.WithSpanKind(trace.SpanKindClient))

	if .newClientTrace != nil {
		 = .WithContext(httptrace.WithClientTrace(, .newClientTrace(, )))
	} else {
		 = .WithContext()
	}

SpanContextToRequest will modify its Request argument, which is contrary to the contract for http.RoundTripper, so we need to pass it a copy of the Request. However, the Request struct itself was already copied by the WithContext calls above and so we just need to copy the header.
		 := make(http.Header)
		for ,  := range .Header {
			[] = 
		}
		.Header = 
		.format.SpanContextToRequest(.SpanContext(), )
	}

	.AddAttributes(requestAttrs()...)
	,  := .base.RoundTrip()
	if  != nil {
		.SetStatus(trace.Status{Code: trace.StatusCodeUnknown, Message: .Error()})
		.End()
		return , 
	}

	.AddAttributes(responseAttrs()...)
	.SetStatus(TraceStatus(.StatusCode, .Status))
span.End() will be invoked after a read from resp.Body returns io.EOF or when resp.Body.Close() is invoked.
	 := &bodyTracker{rc: .Body, span: }
	.Body = wrappedBody(, .Body)
	return , 
}
bodyTracker wraps a response.Body and invokes trace.EndSpan on encountering io.EOF on reading the body of the original response.
type bodyTracker struct {
	rc   io.ReadCloser
	span *trace.Span
}

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

func ( *bodyTracker) ( []byte) (int, error) {
	,  := .rc.Read()

	switch  {
	case nil:
		return , nil
	case io.EOF:
		.span.End()
For all other errors, set the span status
Code 2 is the error code for Internal server error.
			Code:    2,
			Message: .Error(),
		})
	}
	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.
	.span.End()
	return .rc.Close()
}
CancelRequest cancels an in-flight request by closing its connection.
func ( *traceTransport) ( *http.Request) {
	type  interface {
		(*http.Request)
	}
	if ,  := .base.();  {
		.()
	}
}

func ( *http.Request) string {
	return .URL.Path
}

func ( *http.Request) []trace.Attribute {
	 := .UserAgent()

	 := make([]trace.Attribute, 0, 5)
	 = append(,
		trace.StringAttribute(PathAttribute, .URL.Path),
		trace.StringAttribute(URLAttribute, .URL.String()),
		trace.StringAttribute(HostAttribute, .Host),
		trace.StringAttribute(MethodAttribute, .Method),
	)

	if  != "" {
		 = append(, trace.StringAttribute(UserAgentAttribute, ))
	}

	return 
}

func ( *http.Response) []trace.Attribute {
	return []trace.Attribute{
		trace.Int64Attribute(StatusCodeAttribute, int64(.StatusCode)),
	}
}
TraceStatus is a utility to convert the HTTP status code to a trace.Status that represents the outcome as closely as possible.
func ( int,  string) trace.Status {
	var  int32
	if  < 200 ||  >= 400 {
		 = trace.StatusCodeUnknown
	}
	switch  {
	case 499:
		 = trace.StatusCodeCancelled
	case http.StatusBadRequest:
		 = trace.StatusCodeInvalidArgument
	case http.StatusUnprocessableEntity:
		 = trace.StatusCodeInvalidArgument
	case http.StatusGatewayTimeout:
		 = trace.StatusCodeDeadlineExceeded
	case http.StatusNotFound:
		 = trace.StatusCodeNotFound
	case http.StatusForbidden:
		 = trace.StatusCodePermissionDenied
	case http.StatusUnauthorized: // 401 is actually unauthenticated.
		 = trace.StatusCodeUnauthenticated
	case http.StatusTooManyRequests:
		 = trace.StatusCodeResourceExhausted
	case http.StatusNotImplemented:
		 = trace.StatusCodeUnimplemented
	case http.StatusServiceUnavailable:
		 = trace.StatusCodeUnavailable
	case http.StatusOK:
		 = trace.StatusCodeOK
	case http.StatusConflict:
		 = trace.StatusCodeAlreadyExists
	}

	return trace.Status{Code: , Message: codeToStr[]}
}

var codeToStr = map[int32]string{
	trace.StatusCodeOK:                 `OK`,
	trace.StatusCodeCancelled:          `CANCELLED`,
	trace.StatusCodeUnknown:            `UNKNOWN`,
	trace.StatusCodeInvalidArgument:    `INVALID_ARGUMENT`,
	trace.StatusCodeDeadlineExceeded:   `DEADLINE_EXCEEDED`,
	trace.StatusCodeNotFound:           `NOT_FOUND`,
	trace.StatusCodeAlreadyExists:      `ALREADY_EXISTS`,
	trace.StatusCodePermissionDenied:   `PERMISSION_DENIED`,
	trace.StatusCodeResourceExhausted:  `RESOURCE_EXHAUSTED`,
	trace.StatusCodeFailedPrecondition: `FAILED_PRECONDITION`,
	trace.StatusCodeAborted:            `ABORTED`,
	trace.StatusCodeOutOfRange:         `OUT_OF_RANGE`,
	trace.StatusCodeUnimplemented:      `UNIMPLEMENTED`,
	trace.StatusCodeInternal:           `INTERNAL`,
	trace.StatusCodeUnavailable:        `UNAVAILABLE`,
	trace.StatusCodeDataLoss:           `DATA_LOSS`,
	trace.StatusCodeUnauthenticated:    `UNAUTHENTICATED`,
}

Health checking is pretty frequent and traces collected for health endpoints can be extremely noisy and expensive. Disable canonical health checking endpoints like /healthz and /_ah/health for now.
	if  == "/healthz" ||  == "/_ah/health" {
		return true
	}
	return false