Skip to content

Commit

Permalink
Cleanup old tracing. By default generates single-line B3 only. TraceI…
Browse files Browse the repository at this point in the history
…D can be queried.
  • Loading branch information
Som-Som-CC committed May 5, 2023
1 parent e124076 commit 102d489
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 50 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ func errDeadlineOrCancel(err error) bool {
func doSpan(ctx context.Context, req *http.Request) string {
trace := newTraceFromCtx(ctx)
span := trace.span()
if trace.received || log.IsLevelEnabled(log.TraceLevel) {
if trace.received || isTraced {
span.setHeader(req.Header)
}
return span.string()
Expand Down
8 changes: 7 additions & 1 deletion ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Lambda struct {
}

func newLambda(w http.ResponseWriter, r *http.Request, vars map[string]string) *Lambda {
return &Lambda{w: w, r: r, trace: newTrace(r), vars: vars}
return &Lambda{w: w, r: r, trace: newTraceFromHeader(r), vars: vars}
}

// NewRequestCtx adds request related data to r.Context().
Expand Down Expand Up @@ -137,3 +137,9 @@ func (l *Lambda) ResponseHeaderAddAs(header, value string) {
h := l.w.Header()
h[header] = append(h[header], value)
}

// TraceID returns trace ID of Lambda context.
// That trace ID is either received in request or generated when Lambda context is created.
func (l *Lambda) TraceID() string {
return l.trace.traceID()
}
33 changes: 20 additions & 13 deletions trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,48 @@ import (
log "github.com/sirupsen/logrus"
)

var isTraced bool = true

// SetTrace can enable/disable tracing in restful. By default tracing is enabled
func SetTrace(b bool) {
isTraced = b
}

type trace struct {
parent *traceParent
b3 *traceB3
received bool
}

// newTrace creates new trace object. Never returns nil.
func newTrace(r *http.Request) *trace {
// newTraceFromHeader creates new trace object. If no trace data, then create random. Never returns nil.
func newTraceFromHeader(r *http.Request) *trace {
t := trace{parent: newTraceParent(r), b3: newTraceB3(r)}
t.received = t.valid()

if !t.received { // Create fake one. Saved to r (ctx.r), so that any client to be able to find it. Note: Logger may have created one already.
t.parent = newTraceParentFromFake(r)
if !t.valid() {
return newTraceRandom()
}
if !t.received {
return newTraceRandom()
}

return &t
}

func newTraceRandom() *trace {
if log.IsLevelEnabled(log.TraceLevel) {
traceID := randStr32()
return &trace{parent: newTraceParentWithID(traceID), b3: newTraceB3WithID(traceID, true)}
}
return &trace{b3: &traceB3{spanID: randStr16()}}
debug := log.IsLevelEnabled(log.TraceLevel)
traceID := randStr32()
return &trace{b3: newTraceB3WithID(traceID, debug)}
}

// newTraceFromCtx creates new trace object, preferably from context. Never returns nil.
func newTraceFromCtx(ctx context.Context) *trace {
l := L(ctx)
if l == nil || l.trace == nil {
if l == nil {
return newTraceRandom()
}

if !l.trace.valid() {
l.trace = newTraceRandom() // Updates trace in ctx via l.trace pointer.
}

return l.trace
}

Expand Down
28 changes: 7 additions & 21 deletions trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,19 @@ func TestRecvdParent(t *testing.T) {
assert := assert.New(t)
r, _ := http.NewRequest("POST", "", nil)
r.Header.Set("traceparent", "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01")
trace := newTrace(r)
trace := newTraceFromHeader(r)
assert.True(trace.received)
assert.Equal("00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01", trace.string())
span := trace.span().string()
assert.Contains(span, "00-0af7651916cd43dd8448eb211c80319c-")
assert.Equal(55, len(span))
}

func TestFakeParent(t *testing.T) {
assert := assert.New(t)
r, _ := http.NewRequest("POST", "", nil)
r.Header.Set("x-fake-traceparent", "00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01")
trace := newTrace(r)
assert.False(trace.received)
assert.Equal("00-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01", trace.string())
span := trace.span().string()
assert.Contains(span, "00-0af7651916cd43dd8448eb211c80319c-")
assert.Equal(55, len(span))
}

func TestRecvdBadParent(t *testing.T) {
assert := assert.New(t)
r, _ := http.NewRequest("POST", "", nil)
r.Header.Set("traceparent", "FF-0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-01")
trace := newTrace(r)
trace := newTraceFromHeader(r)
assert.False(trace.received)
assert.NotContains(trace.string(), "-0af7651916cd43dd8448eb211c80319c-")
}
Expand All @@ -63,7 +51,7 @@ func TestB3SingleLine(t *testing.T) {
r, _ := http.NewRequest("POST", "", nil)
traceStr := "0af7651916cd43dd8448eb211c80319c-b9c7c989f97918e1-1-deadbeef87654321"
r.Header.Set("b3", traceStr)
trace := newTrace(r)
trace := newTraceFromHeader(r)
assert.True(trace.received)
assert.Contains(trace.string(), "0af7651916cd43dd8448eb211c80319c")
headers := http.Header{}
Expand All @@ -73,11 +61,10 @@ func TestB3SingleLine(t *testing.T) {

func TestTracePropagation(t *testing.T) {
assert := assert.New(t)
log.SetLevel(log.TraceLevel) // That switches on trace generation and propagation

// Server
srvURL := ""
traceid := ""
traceID := ""
prevSpanID := ""
parents := make(map[string]bool)
depth := 0
Expand All @@ -87,17 +74,16 @@ func TestTracePropagation(t *testing.T) {
t := newTraceFromCtx(ctx)
assert.True(t.received)
if depth == 0 {
traceid = t.traceID()
traceID = t.traceID()
prevSpanID = t.spanID()
parents[t.string()] = true
t.b3.sampled = "1"
t.b3.requestID = "req"
t.b3.spanCtx = "ctx"
} else {
assert.Equal(traceid, t.parent.traceID())
assert.Equal(traceID, t.traceID())
assert.Equal(traceID, L(ctx).TraceID())
assert.NotContains(parents, t.string())
assert.Equal(t.parent.traceID(), t.b3.traceID)
assert.Equal(t.parent.spanID(), t.b3.spanID)
assert.Equal(prevSpanID, t.b3.parentSpanID)
assert.Equal("1", t.b3.sampled)
assert.Equal("req", t.b3.requestID)
Expand Down
4 changes: 2 additions & 2 deletions traceb3.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ func newTraceB3(r *http.Request) *traceB3 {
return b3
}

func newTraceB3WithID(traceID string, trace bool) *traceB3 {
func newTraceB3WithID(traceID string, debug bool) *traceB3 {
b3 := traceB3{traceID: traceID, singleLine: true}
if trace {
if debug {
b3.sampled = "d"
}
return &b3
Expand Down
14 changes: 2 additions & 12 deletions traceparent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package restful

import (
"fmt"
"net/http"
"strings"
)
Expand All @@ -15,9 +14,8 @@ See https://www.w3.org/TR/trace-context
*/

const (
headerTraceParent = "traceparent"
headerTraceState = "tracestate"
headerFakeTraceParent = "x-fake-traceparent"
headerTraceParent = "traceparent"
headerTraceState = "tracestate"
)

type traceParent struct {
Expand All @@ -29,14 +27,6 @@ func newTraceParent(r *http.Request) *traceParent { // May return nil.
return newTraceParentFromHeaderValue(r.Header.Get(headerTraceParent), r.Header.Get(headerTraceState))
}

func newTraceParentFromFake(r *http.Request) *traceParent {
return newTraceParentFromHeaderValue(r.Header.Get(headerFakeTraceParent), "") // Our server logger may have faked one already.
}

func newTraceParentWithID(traceID string) *traceParent {
return &traceParent{parent: []string{"00", traceID, fmt.Sprintf("%016x", 0) /*invalid, span resolves that*/, "00"}}
}

func newTraceParentFromHeaderValue(traceparent, tracestate string) *traceParent {
parent := strings.Split(traceparent, "-")
if len(parent) != 4 {
Expand Down

0 comments on commit 102d489

Please sign in to comment.