Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

Commit

Permalink
demonstrate how an HTTP propagator might defer to another
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Sigelman committed Feb 12, 2016
1 parent 8e4bb0a commit 7deaa63
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
55 changes: 55 additions & 0 deletions spanpropagator.go → propagation.go
Expand Up @@ -2,8 +2,10 @@ package standardtracer

import (
"bytes"
"encoding/base64"
"encoding/binary"
"fmt"
"net/http"
"strconv"
"strings"
"time"
Expand All @@ -17,6 +19,9 @@ type splitTextPropagator struct {
type splitBinaryPropagator struct {
tracer *tracerImpl
}
type goHTTPPropagator struct {
splitBinaryPropagator
}

const (
fieldNameTraceID = "traceid"
Expand Down Expand Up @@ -216,3 +221,53 @@ func (p splitBinaryPropagator) JoinTrace(
opentracing.Tags{},
), nil
}

const (
tracerStateHeaderName = "Tracer-State"
traceAttrsHeaderName = "Trace-Attributes"
)

func (p goHTTPPropagator) InjectSpan(
sp opentracing.Span,
carrier interface{},
) error {
// Defer to SplitBinary for the real work.
splitBinaryCarrier := opentracing.NewSplitBinaryCarrier()
if err := p.splitBinaryPropagator.InjectSpan(sp, splitBinaryCarrier); err != nil {
return err
}

// Encode into the HTTP header as two base64 strings.
header := carrier.(http.Header)
header.Add(tracerStateHeaderName, base64.StdEncoding.EncodeToString(
splitBinaryCarrier.TracerState))
header.Add(traceAttrsHeaderName, base64.StdEncoding.EncodeToString(
splitBinaryCarrier.TraceAttributes))

return nil
}

func (p goHTTPPropagator) JoinTrace(
operationName string,
carrier interface{},
) (opentracing.Span, error) {
// Decode the two base64-encoded data blobs from the HTTP header.
header := carrier.(http.Header)
tracerStateBinary, err := base64.StdEncoding.DecodeString(
header.Get(tracerStateHeaderName))
if err != nil {
return nil, err
}
traceAttrsBinary, err := base64.StdEncoding.DecodeString(
header.Get(traceAttrsHeaderName))
if err != nil {
return nil, err
}

// Defer to SplitBinary for the real work.
splitBinaryCarrier := &opentracing.SplitBinaryCarrier{
TracerState: tracerStateBinary,
TraceAttributes: traceAttrsBinary,
}
return p.splitBinaryPropagator.JoinTrace(operationName, splitBinaryCarrier)
}
File renamed without changes.
4 changes: 4 additions & 0 deletions tracer.go
Expand Up @@ -14,6 +14,7 @@ func New(recorder SpanRecorder) opentracing.Tracer {
}
rval.textPropagator = splitTextPropagator{rval}
rval.binaryPropagator = splitBinaryPropagator{rval}
rval.goHTTPPropagator = goHTTPPropagator{rval.binaryPropagator}
return rval
}

Expand All @@ -22,6 +23,7 @@ type tracerImpl struct {
recorder SpanRecorder
textPropagator splitTextPropagator
binaryPropagator splitBinaryPropagator
goHTTPPropagator goHTTPPropagator
}

func (t *tracerImpl) StartSpan(
Expand Down Expand Up @@ -91,6 +93,7 @@ func (t *tracerImpl) Extractor(format interface{}) opentracing.Extractor {
case opentracing.SplitBinary:
return t.binaryPropagator
case opentracing.GoHTTPHeader:
return t.goHTTPPropagator
}
return nil
}
Expand All @@ -102,6 +105,7 @@ func (t *tracerImpl) Injector(format interface{}) opentracing.Injector {
case opentracing.SplitBinary:
return t.binaryPropagator
case opentracing.GoHTTPHeader:
return t.goHTTPPropagator
}
return nil
}

0 comments on commit 7deaa63

Please sign in to comment.