Copyright 2018 Google LLC. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
+build go1.8
Package propagation implements X-Cloud-Trace-Context header propagation used by Google Cloud products.
package propagation

import (
	
	
	
	
	
	

	
	
)

const (
	httpHeaderMaxSize = 200
	httpHeader        = `X-Cloud-Trace-Context`
)

var _ propagation.HTTPFormat = (*HTTPFormat)(nil)
HTTPFormat implements propagation.HTTPFormat to propagate traces in HTTP headers for Google Cloud Platform and Stackdriver Trace.
type HTTPFormat struct{}
SpanContextFromRequest extracts a Stackdriver Trace span context from incoming requests.
See https://cloud.google.com/trace/docs/faq for the header HTTPFormat. Return if the header is empty or missing, or if the header is unreasonably large, to avoid making unnecessary copies of a large string.
	if  == "" || len() > httpHeaderMaxSize {
		return trace.SpanContext{}, false
	}
Parse the trace id field.
	 := strings.Index(, `/`)
	if  == -1 {
		return trace.SpanContext{}, false
	}
	,  := [:], [+1:]

	,  := hex.DecodeString()
	if  != nil {
		return trace.SpanContext{}, false
	}
	copy(.TraceID[:], )
Parse the span id field.
	 := 
	 := strings.Index(, `;`)
	if  != -1 {
		,  = [:], [+1:]
	}
	,  := strconv.ParseUint(, 10, 64)
	if  != nil {
		return trace.SpanContext{}, false
	}
	binary.BigEndian.PutUint64(.SpanID[:], )
Parse the options field, options field is optional.
	if !strings.HasPrefix(, "o=") {
		return , true
	}
	,  := strconv.ParseUint([2:], 10, 64)
	if  != nil {
		return trace.SpanContext{}, false
	}
	.TraceOptions = trace.TraceOptions()
	return , true
}
SpanContextToRequest modifies the given request to include a Stackdriver Trace header.
func ( *HTTPFormat) ( trace.SpanContext,  *http.Request) {
	 := binary.BigEndian.Uint64(.SpanID[:])
	 := fmt.Sprintf("%s/%d;o=%d", hex.EncodeToString(.TraceID[:]), , int64(.TraceOptions))
	.Header.Set(httpHeader, )