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 (
	
	
	
	
	
	

	
	
	
	
)
Handler is an http.Handler wrapper to instrument your HTTP server with OpenCensus. It supports both stats and tracing. Tracing This handler is aware of the incoming request's span, reading it from request headers as configured using the Propagation field. The extracted span can be accessed from the incoming request's context. span := trace.FromContext(r.Context()) The server span will be automatically ended at the end of ServeHTTP.
Propagation defines how traces are propagated. If unspecified, B3 propagation will be used.
Handler is the handler used to handle the incoming request.
StartOptions are applied to the span started by this Handler around each request. StartOptions.SpanKind will always be set to trace.SpanKindServer for spans started by this transport.
GetStartOptions allows to set start options per request. If set, StartOptions is going to be ignored.
IsPublicEndpoint should be set to true for publicly accessible HTTP(S) servers. If true, any trace metadata set on the incoming request will be added as a linked trace instead of being added as a parent of the current trace.
FormatSpanName holds the function to use for generating the span name from the information found in the incoming HTTP Request. By default the name equals the URL Path.
IsHealthEndpoint holds the function to use for determining if the incoming HTTP request should be considered a health check. This is in addition to the private isHealthEndpoint func which may also indicate tracing should be skipped.
	IsHealthEndpoint func(*http.Request) bool
}

func ( *Handler) ( http.ResponseWriter,  *http.Request) {
	var  addedTags
	,  := .startTrace(, )
	defer ()
	,  := .startStats(, )
	defer (&)
	 := .Handler
	if  == nil {
		 = http.DefaultServeMux
	}
	 = .WithContext(context.WithValue(.Context(), addedTagsKey{}, &))
	.ServeHTTP(, )
}

func ( *Handler) ( http.ResponseWriter,  *http.Request) (*http.Request, func()) {
	if .IsHealthEndpoint != nil && .IsHealthEndpoint() || isHealthEndpoint(.URL.Path) {
		return , func() {}
	}
	var  string
	if .FormatSpanName == nil {
		 = spanNameFromURL()
	} else {
		 = .FormatSpanName()
	}
	 := .Context()

	 := .StartOptions
	if .GetStartOptions != nil {
		 = .GetStartOptions()
	}

	var  *trace.Span
	,  := .extractSpanContext()
	if  && !.IsPublicEndpoint {
		,  = trace.StartSpanWithRemoteParent(, , ,
			trace.WithSampler(.Sampler),
			trace.WithSpanKind(trace.SpanKindServer))
	} else {
		,  = trace.StartSpan(, ,
			trace.WithSampler(.Sampler),
			trace.WithSpanKind(trace.SpanKindServer),
		)
		if  {
			.AddLink(trace.Link{
				TraceID:    .TraceID,
				SpanID:     .SpanID,
				Type:       trace.LinkTypeParent,
				Attributes: nil,
			})
		}
	}
	.AddAttributes(requestAttrs()...)
TODO: Handle cases where ContentLength is not set.
	} else if .ContentLength > 0 {
		.AddMessageReceiveEvent(0, /* TODO: messageID */
			.ContentLength, -1)
	}
	return .WithContext(), .End
}

func ( *Handler) ( *http.Request) (trace.SpanContext, bool) {
	if .Propagation == nil {
		return defaultFormat.SpanContextFromRequest()
	}
	return .Propagation.SpanContextFromRequest()
}

func ( *Handler) ( http.ResponseWriter,  *http.Request) (http.ResponseWriter, func( *addedTags)) {
	,  := tag.New(.Context(),
		tag.Upsert(Host, .Host),
		tag.Upsert(Path, .URL.Path),
		tag.Upsert(Method, .Method))
	 := &trackingResponseWriter{
		start:  time.Now(),
		ctx:    ,
		writer: ,
	}
TODO: Handle cases where ContentLength is not set.
Compile time assertion for ResponseWriter interface
Add message event for request bytes sent.
	 := trace.FromContext(.ctx)
	.AddMessageSendEvent(0 /* TODO: messageID */, int64(), -1)
	return , 
}

func ( *trackingResponseWriter) ( int) {
	.writer.WriteHeader()
	.statusCode = 
	.statusLine = http.StatusText(.statusCode)
}
wrappedResponseWriter returns a wrapped version of the original ResponseWriter and only implements the same combination of additional interfaces as the original. This implementation is based on https://github.com/felixge/httpsnoop.
func ( *trackingResponseWriter) () http.ResponseWriter {
	var (
		,  = .writer.(http.Hijacker)
		,  = .writer.(http.CloseNotifier)
		,  = .writer.(http.Pusher)
		,  = .writer.(http.Flusher)
		,  = .writer.(io.ReaderFrom)
	)

	switch {
	case ! && ! && ! && ! && !:
		return struct {
			http.ResponseWriter
		}{}
	case ! && ! && ! && ! && :
		return struct {
			http.ResponseWriter
			io.ReaderFrom
		}{, }
	case ! && ! && ! &&  && !:
		return struct {
			http.ResponseWriter
			http.Flusher
		}{, }
	case ! && ! && ! &&  && :
		return struct {
			http.ResponseWriter
			http.Flusher
			io.ReaderFrom
		}{, , }
	case ! && ! &&  && ! && !:
		return struct {
			http.ResponseWriter
			http.Pusher
		}{, }
	case ! && ! &&  && ! && :
		return struct {
			http.ResponseWriter
			http.Pusher
			io.ReaderFrom
		}{, , }
	case ! && ! &&  &&  && !:
		return struct {
			http.ResponseWriter
			http.Pusher
			http.Flusher
		}{, , }
	case ! && ! &&  &&  && :
		return struct {
			http.ResponseWriter
			http.Pusher
			http.Flusher
			io.ReaderFrom
		}{, , , }
	case ! &&  && ! && ! && !:
		return struct {
			http.ResponseWriter
			http.CloseNotifier
		}{, }
	case ! &&  && ! && ! && :
		return struct {
			http.ResponseWriter
			http.CloseNotifier
			io.ReaderFrom
		}{, , }
	case ! &&  && ! &&  && !:
		return struct {
			http.ResponseWriter
			http.CloseNotifier
			http.Flusher
		}{, , }
	case ! &&  && ! &&  && :
		return struct {
			http.ResponseWriter
			http.CloseNotifier
			http.Flusher
			io.ReaderFrom
		}{, , , }
	case ! &&  &&  && ! && !:
		return struct {
			http.ResponseWriter
			http.CloseNotifier
			http.Pusher
		}{, , }
	case ! &&  &&  && ! && :
		return struct {
			http.ResponseWriter
			http.CloseNotifier
			http.Pusher
			io.ReaderFrom
		}{, , , }
	case ! &&  &&  &&  && !:
		return struct {
			http.ResponseWriter
			http.CloseNotifier
			http.Pusher
			http.Flusher
		}{, , , }
	case ! &&  &&  &&  && :
		return struct {
			http.ResponseWriter
			http.CloseNotifier
			http.Pusher
			http.Flusher
			io.ReaderFrom
		}{, , , , }
	case  && ! && ! && ! && !:
		return struct {
			http.ResponseWriter
			http.Hijacker
		}{, }
	case  && ! && ! && ! && :
		return struct {
			http.ResponseWriter
			http.Hijacker
			io.ReaderFrom
		}{, , }
	case  && ! && ! &&  && !:
		return struct {
			http.ResponseWriter
			http.Hijacker
			http.Flusher
		}{, , }
	case  && ! && ! &&  && :
		return struct {
			http.ResponseWriter
			http.Hijacker
			http.Flusher
			io.ReaderFrom
		}{, , , }
	case  && ! &&  && ! && !:
		return struct {
			http.ResponseWriter
			http.Hijacker
			http.Pusher
		}{, , }
	case  && ! &&  && ! && :
		return struct {
			http.ResponseWriter
			http.Hijacker
			http.Pusher
			io.ReaderFrom
		}{, , , }
	case  && ! &&  &&  && !:
		return struct {
			http.ResponseWriter
			http.Hijacker
			http.Pusher
			http.Flusher
		}{, , , }
	case  && ! &&  &&  && :
		return struct {
			http.ResponseWriter
			http.Hijacker
			http.Pusher
			http.Flusher
			io.ReaderFrom
		}{, , , , }
	case  &&  && ! && ! && !:
		return struct {
			http.ResponseWriter
			http.Hijacker
			http.CloseNotifier
		}{, , }
	case  &&  && ! && ! && :
		return struct {
			http.ResponseWriter
			http.Hijacker
			http.CloseNotifier
			io.ReaderFrom
		}{, , , }
	case  &&  && ! &&  && !:
		return struct {
			http.ResponseWriter
			http.Hijacker
			http.CloseNotifier
			http.Flusher
		}{, , , }
	case  &&  && ! &&  && :
		return struct {
			http.ResponseWriter
			http.Hijacker
			http.CloseNotifier
			http.Flusher
			io.ReaderFrom
		}{, , , , }
	case  &&  &&  && ! && !:
		return struct {
			http.ResponseWriter
			http.Hijacker
			http.CloseNotifier
			http.Pusher
		}{, , , }
	case  &&  &&  && ! && :
		return struct {
			http.ResponseWriter
			http.Hijacker
			http.CloseNotifier
			http.Pusher
			io.ReaderFrom
		}{, , , , }
	case  &&  &&  &&  && !:
		return struct {
			http.ResponseWriter
			http.Hijacker
			http.CloseNotifier
			http.Pusher
			http.Flusher
		}{, , , , }
	case  &&  &&  &&  && :
		return struct {
			http.ResponseWriter
			http.Hijacker
			http.CloseNotifier
			http.Pusher
			http.Flusher
			io.ReaderFrom
		}{, , , , , }
	default:
		return struct {
			http.ResponseWriter
		}{}
	}