...

Source file src/go.opencensus.io/exporter/stackdriver/propagation/http.go

Documentation: go.opencensus.io/exporter/stackdriver/propagation

     1  // Copyright 2018, OpenCensus Authors
     2  //
     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
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     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.
    14  
    15  // Package propagation implement X-Cloud-Trace-Context header propagation used
    16  // by Google Cloud products.
    17  package propagation // import "go.opencensus.io/exporter/stackdriver/propagation"
    18  
    19  import (
    20  	"encoding/binary"
    21  	"encoding/hex"
    22  	"fmt"
    23  	"net/http"
    24  	"strconv"
    25  	"strings"
    26  
    27  	"go.opencensus.io/trace"
    28  	"go.opencensus.io/trace/propagation"
    29  )
    30  
    31  const (
    32  	httpHeaderMaxSize = 200
    33  	httpHeader        = `X-Cloud-Trace-Context`
    34  )
    35  
    36  var _ propagation.HTTPFormat = (*HTTPFormat)(nil)
    37  
    38  // HTTPFormat implements propagation.HTTPFormat to propagate
    39  // traces in HTTP headers for Google Cloud Platform and Stackdriver Trace.
    40  type HTTPFormat struct{}
    41  
    42  // SpanContextFromRequest extracts a Stackdriver Trace span context from incoming requests.
    43  func (f *HTTPFormat) SpanContextFromRequest(req *http.Request) (sc trace.SpanContext, ok bool) {
    44  	h := req.Header.Get(httpHeader)
    45  	// See https://cloud.google.com/trace/docs/faq for the header HTTPFormat.
    46  	// Return if the header is empty or missing, or if the header is unreasonably
    47  	// large, to avoid making unnecessary copies of a large string.
    48  	if h == "" || len(h) > httpHeaderMaxSize {
    49  		return trace.SpanContext{}, false
    50  	}
    51  
    52  	// Parse the trace id field.
    53  	slash := strings.Index(h, `/`)
    54  	if slash == -1 {
    55  		return trace.SpanContext{}, false
    56  	}
    57  	tid, h := h[:slash], h[slash+1:]
    58  
    59  	buf, err := hex.DecodeString(tid)
    60  	if err != nil {
    61  		return trace.SpanContext{}, false
    62  	}
    63  	copy(sc.TraceID[:], buf)
    64  
    65  	// Parse the span id field.
    66  	spanstr := h
    67  	semicolon := strings.Index(h, `;`)
    68  	if semicolon != -1 {
    69  		spanstr, h = h[:semicolon], h[semicolon+1:]
    70  	}
    71  	sid, err := strconv.ParseUint(spanstr, 10, 64)
    72  	if err != nil {
    73  		return trace.SpanContext{}, false
    74  	}
    75  	binary.BigEndian.PutUint64(sc.SpanID[:], sid)
    76  
    77  	// Parse the options field, options field is optional.
    78  	if !strings.HasPrefix(h, "o=") {
    79  		return sc, true
    80  	}
    81  	o, err := strconv.ParseUint(h[2:], 10, 64)
    82  	if err != nil {
    83  		return trace.SpanContext{}, false
    84  	}
    85  	sc.TraceOptions = trace.TraceOptions(o)
    86  	return sc, true
    87  }
    88  
    89  // SpanContextToRequest modifies the given request to include a Stackdriver Trace header.
    90  func (f *HTTPFormat) SpanContextToRequest(sc trace.SpanContext, req *http.Request) {
    91  	sid := binary.BigEndian.Uint64(sc.SpanID[:])
    92  	header := fmt.Sprintf("%s/%d;o=%d", hex.EncodeToString(sc.TraceID[:]), sid, int64(sc.TraceOptions))
    93  	req.Header.Set(httpHeader, header)
    94  }
    95  

View as plain text