Skip to content

Commit

Permalink
a bit more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
muir committed Oct 4, 2022
1 parent 6c4a78a commit a8bbcbd
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 38 deletions.
5 changes: 5 additions & 0 deletions trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,15 @@ func TestTraceZero(t *testing.T) {

func TestTracePartial(t *testing.T) {
var trace trace.Trace
assert.True(t, trace.TraceID().IsZero(), "initial trace is zero")
trace.SpanID().SetRandom()
t.Log("trace is", trace.String())
assert.NotEqual(t, "0000000000000000", trace.SpanID().String())
assert.Equal(t, "00-00000000000000000000000000000000-"+trace.SpanID().String()+"-00", trace.String())
assert.Equal(t, trace.SpanID().String(), trace.GetSpanID().String())
assert.Len(t, trace.String(), 55)
assert.True(t, trace.TraceID().IsZero(), "final trace is zero")
assert.False(t, trace.SpanID().IsZero(), "final span is zero")
}

func TestTraceRandom(t *testing.T) {
Expand All @@ -43,6 +47,7 @@ func TestTraceRandom(t *testing.T) {
trace.SpanID().SetRandom()
trace.TraceID().SetRandom()
trace.Flags().SetRandom()
t.Log("trace is", trace.String())
assert.NotEqual(t, "00", trace.Version().String())
assert.NotEqual(t, "00000000000000000000000000000000", trace.TraceID().String())
assert.NotEqual(t, "0000000000000000", trace.SpanID().String())
Expand Down
33 changes: 17 additions & 16 deletions xopjson/jsonlogger.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 17 additions & 16 deletions xopjson/jsonlogger.zzzgo
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ func (logger *Logger) ID() string { return logger.id.String() }
func (logger *Logger) Buffered() bool { return logger.writer.Buffered() }
func (logger *Logger) ReferencesKept() bool { return false }

func (logger *Logger) Request(_ context.Context, ts time.Time, trace trace.Bundle, name string) xopbase.Request {
func (logger *Logger) Request(_ context.Context, ts time.Time, bundle trace.Bundle, name string) xopbase.Request {
request := &request{
span: span{
logger: logger,
writer: logger.writer.Request(trace),
trace: trace,
writer: logger.writer.Request(bundle),
bundle: bundle,
name: name,
startTime: ts,
endTime: ts.UnixNano(),
Expand Down Expand Up @@ -93,20 +93,20 @@ func (logger *Logger) Request(_ context.Context, ts time.Time, trace trace.Bundl

func (s *span) addRequestStartData(rq *builder) {
rq.AddSafeKey("trace.id")
rq.AddSafeString(s.trace.Trace.TraceID().String())
rq.AddSafeString(s.bundle.Trace.TraceID().String())
rq.AddSafeKey("span.id")
rq.AddSafeString(s.trace.Trace.SpanID().String())
if !s.trace.ParentTrace.TraceID().IsZero() {
rq.AddSafeString(s.bundle.Trace.SpanID().String())
if !s.bundle.ParentTrace.TraceID().IsZero() {
rq.AddSafeKey("trace.parent")
rq.AddSafeString(s.trace.ParentTrace.String())
rq.AddSafeString(s.bundle.ParentTrace.String())
}
if !s.trace.State.IsZero() {
if !s.bundle.State.IsZero() {
rq.AddSafeKey("trace.state")
rq.AddSafeString(s.trace.State.String())
rq.AddSafeString(s.bundle.State.String())
}
if !s.trace.Baggage.IsZero() {
if !s.bundle.Baggage.IsZero() {
rq.AddSafeKey("trace.baggage")
rq.AddSafeString(s.trace.Baggage.String())
rq.AddSafeString(s.bundle.Baggage.String())
}
rq.AddSafeKey("span.name")
rq.AddString(s.name)
Expand Down Expand Up @@ -222,11 +222,11 @@ func (r *request) Final() {

func (r *request) SetErrorReporter(reporter func(error)) { r.errorFunc = reporter }

func (s *span) Span(_ context.Context, ts time.Time, trace trace.Bundle, name string, spanSequenceCode string) xopbase.Span {
func (s *span) Span(_ context.Context, ts time.Time, bundle trace.Bundle, name string, spanSequenceCode string) xopbase.Span {
n := &span{
logger: s.logger,
writer: s.writer,
trace: trace,
bundle: bundle,
name: name,
request: s.request,
startTime: ts,
Expand All @@ -239,7 +239,7 @@ func (s *span) Span(_ context.Context, ts time.Time, trace trace.Bundle, name st
if s.logger.spanStarts {
rq := s.builder()
rq.AppendBytes([]byte(`{"type":"span","span.ver":0,"span.id":`))
rq.AddSafeString(trace.Trace.SpanID().String())
rq.AddSafeString(bundle.Trace.SpanID().String())
n.spanStartData(rq)
rq.AppendBytes([]byte{'}', '\n'})
n.serializationCount++
Expand All @@ -260,6 +260,7 @@ func (s *span) Span(_ context.Context, ts time.Time, trace trace.Bundle, name st
func (s *span) spanStartData(rq *builder) {
rq.stringKV("span.name", s.name)
rq.Time("ts", s.startTime)
rq.stringKV("span.parent_span", s.bundle.ParentTrace.SpanID().String())
}

func (s *span) setSpanIDPrefill() {
Expand All @@ -273,11 +274,11 @@ func (s *span) setSpanIDPrefill() {
func (s *span) identifySpan(b *xoputil.JBuilder) {
if s.logger.tagOption&SpanIDTagOption != 0 {
b.AddSafeKey("span.id")
b.AddSafeString(s.trace.Trace.SpanID().String())
b.AddSafeString(s.bundle.Trace.SpanID().String())
}
if s.logger.tagOption&TraceIDTagOption != 0 {
b.AddSafeKey("trace.id")
b.AddSafeString(s.trace.Trace.TraceID().String())
b.AddSafeString(s.bundle.Trace.TraceID().String())
}
if s.logger.tagOption&TraceNumberTagOption != 0 {
b.AddSafeKey("trace.num")
Expand Down
11 changes: 9 additions & 2 deletions xopjson/jsonlogger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,17 @@ func TestParameters(t *testing.T) {
if tc.settings != nil {
settings = tc.settings
}
log := xop.NewSeed(
seed := xop.NewSeed(
xop.WithBase(jlog),
xop.WithSettings(settings),
).Copy(xop.WithBase(tlog)).Request(t.Name())
).Copy(xop.WithBase(tlog))

if len(mc.SeedMods) != 0 {
t.Logf("Applying %d extra seed mods", len(mc.SeedMods))
seed = seed.Copy(mc.SeedMods...)
}

log := seed.Request(t.Name())

mc.Do(t, log, tlog)

Expand Down
2 changes: 1 addition & 1 deletion xopjson/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type request struct {
type span struct {
endTime int64
writer xopbytes.BytesRequest
trace trace.Bundle
bundle trace.Bundle
logger *Logger
name string
request *request
Expand Down
16 changes: 13 additions & 3 deletions xopotel/otel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ func TestSpanLog(t *testing.T) {

tlog := xoptest.New(t)
ctx, span := tracer.Start(ctx, mc.Name)
log := xopotel.SpanLog(ctx, mc.Name).Span().Seed(xop.WithBase(tlog)).SubSpan("adding-tlog")
seed := xopotel.SpanLog(ctx, mc.Name).Span().Seed(xop.WithBase(tlog))
if len(mc.SeedMods) != 0 {
t.Logf("Applying %d extra seed mods", len(mc.SeedMods))
seed = seed.Copy(mc.SeedMods...)
}
log := seed.SubSpan("adding-tlog")
mc.Do(t, log, tlog)

span.End()
Expand Down Expand Up @@ -122,10 +127,15 @@ func TestBaseLogger(t *testing.T) {
tracer := tracerProvider.Tracer("")

tlog := xoptest.New(t)
log := xop.NewSeed(
seed := xop.NewSeed(
xop.WithBase(tlog),
xopotel.BaseLogger(ctx, tracer, true),
).Request(t.Name())
)
if len(mc.SeedMods) != 0 {
t.Logf("Applying %d extra seed mods", len(mc.SeedMods))
seed = seed.Copy(mc.SeedMods...)
}
log := seed.Request(t.Name())
mc.Do(t, log, tlog)

cancel()
Expand Down
27 changes: 27 additions & 0 deletions xoptest/xoptestutil/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/xoplog/xop-go"
"github.com/xoplog/xop-go/trace"
"github.com/xoplog/xop-go/xopconst"
"github.com/xoplog/xop-go/xopnum"
"github.com/xoplog/xop-go/xoptest"
Expand All @@ -20,6 +21,7 @@ var MessageCases = []struct {
ExtraFlushes int
Do func(t *testing.T, log *xop.Log, tlog *xoptest.TestLogger)
SkipOTEL bool
SeedMods []xop.SeedModifier
}{
{
Name: "one span",
Expand Down Expand Up @@ -473,6 +475,31 @@ var MessageCases = []struct {
log.Done()
},
},
{
Name: "simulate inbound propagation",
SeedMods: []xop.SeedModifier{
xop.WithBundle(func() trace.Bundle {
var bundle trace.Bundle
bundle.ParentTrace.Flags().SetString("01")
bundle.ParentTrace.TraceID().SetString("a60a3cc0123a043fee48839c9d52a645")
bundle.ParentTrace.SpanID().SetString("c63f9d81e2285f34")
bundle.Trace = bundle.ParentTrace
bundle.Trace.SpanID().SetRandom()
bundle.State.SetString("congo=t61rcWkgMzE")
bundle.Baggage.SetString("userId=alice,serverNode=DF%2028,isProduction=false")
return bundle
}()),
},
Do: func(t *testing.T, log *xop.Log, tlog *xoptest.TestLogger) {
assert.Equal(t, "00-a60a3cc0123a043fee48839c9d52a645-c63f9d81e2285f34-01", log.Span().Bundle().ParentTrace.String(), "trace parent")
assert.Equal(t, "a60a3cc0123a043fee48839c9d52a645", log.Span().Bundle().Trace.GetTraceID().String(), "trace trace")
assert.NotEqual(t, "c63f9d81e2285f34", log.Span().Bundle().Trace.GetSpanID().String(), "trace trace")
assert.Equal(t, "congo=t61rcWkgMzE", log.Span().Bundle().State.String(), "trace state")
assert.Equal(t, "userId=alice,serverNode=DF%2028,isProduction=false", log.Span().Bundle().Baggage.String())
MicroNap()
log.Done()
},
},
}

type stringCounter struct {
Expand Down

0 comments on commit a8bbcbd

Please sign in to comment.