trace: add tests to assert if options are propagated to childs correctly
@@ -22,6 +22,7 @@ import (
"math/rand"
"net/http"
"reflect"
+ "regexp"
"strings"
"sync"
"testing"
@@ -246,6 +247,56 @@ func TestHeader(t *testing.T) {
}
}
+func TestOutgoingReqHeader(t *testing.T) {
+ all, _ := NewLimitedSampler(1, 1<<16) // trace every request
+
+ tests := []struct {
+ desc string
+ traceHeader string
+ samplingPolicy SamplingPolicy
+
+ wantHeaderRe *regexp.Regexp
+ }{
+ {
+ desc: "Parent span without sampling options, client samples all",
+ traceHeader: "0123456789ABCDEF0123456789ABCDEF/1",
+ samplingPolicy: all,
+ wantHeaderRe: regexp.MustCompile("0123456789ABCDEF0123456789ABCDEF/\\d+;o=1"),
+ },
+ {
+ desc: "Parent span without sampling options, without client sampling",
+ traceHeader: "0123456789ABCDEF0123456789ABCDEF/1",
+ samplingPolicy: nil,
+ wantHeaderRe: regexp.MustCompile("0123456789ABCDEF0123456789ABCDEF/\\d+;o=0"),
+ },
+ {
+ desc: "Parent span with o=1, client samples none",
+ traceHeader: "0123456789ABCDEF0123456789ABCDEF/1;o=1",
+ samplingPolicy: nil,
+ wantHeaderRe: regexp.MustCompile("0123456789ABCDEF0123456789ABCDEF/\\d+;o=1"),
+ },
+ {
+ desc: "Parent span with o=0, without client sampling",
+ traceHeader: "0123456789ABCDEF0123456789ABCDEF/1;o=0",
+ samplingPolicy: nil,
+ wantHeaderRe: regexp.MustCompile("0123456789ABCDEF0123456789ABCDEF/\\d+;o=0"),
+ },
+ }
+
+ tc := newTestClient(nil)
+ for _, tt := range tests {
+ tc.SetSamplingPolicy(tt.samplingPolicy)
+ span := tc.SpanFromHeader("/foo", tt.traceHeader)
+
+ req, _ := http.NewRequest("GET", "http://localhost", nil)
+ span.NewRemoteChild(req)
+
+ if got, re := req.Header.Get(httpHeader), tt.wantHeaderRe; !re.MatchString(got) {
+ t.Errorf("%v (parent=%q): got header %q; want in format %q", tt.desc, tt.traceHeader, got, re)
+ }
+ }
+}
+
func TestTrace(t *testing.T) {
t.Parallel()
testTrace(t, false, true)
