Skip to content

Commit

Permalink
remove autogenerated trace id on Add (#63)
Browse files Browse the repository at this point in the history
In preparation for otel compliance, this removes the clues_trace_id
autogeneration on every Add call. In a follow-up PR it will be replaced
by AddSpan.
  • Loading branch information
ryanfkeepers authored Nov 5, 2024
1 parent 9db2431 commit b39b934
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 57 deletions.
54 changes: 27 additions & 27 deletions clues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func mustEquals[K comparable, V any](
) {
e, g := toMSS(expect), toMSS(got)

if hasCluesTrace && len(g) > 0 {
if _, ok := g["clues_trace"]; !ok {
if len(g) > 0 {
if _, ok := g["clues_trace"]; hasCluesTrace && !ok {
t.Errorf(
"expected map to contain key [clues_trace]\ngot: %+v",
g)
Expand Down Expand Up @@ -163,7 +163,7 @@ func assert(
eS, eSns sa,
) {
vs := clues.In(ctx)
mustEquals(t, eM, vs.Map(), true)
mustEquals(t, eM, vs.Map(), false)
eS.equals(t, vs.Slice())
}

Expand All @@ -175,7 +175,7 @@ func assertMSA(
eS, eSns sa,
) {
vs := clues.In(ctx)
mustEquals(t, eM, toMSA(vs.Map()), true)
mustEquals(t, eM, toMSA(vs.Map()), false)
eS.equals(t, vs.Slice())
}

Expand All @@ -197,12 +197,12 @@ func TestAdd(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
ctx := context.WithValue(context.Background(), testCtx{}, "instance")
check := msa{}
mustEquals(t, check, clues.In(ctx).Map(), true)
mustEquals(t, check, clues.In(ctx).Map(), false)

for _, kv := range test.kvs {
ctx = clues.Add(ctx, kv[0], kv[1])
check[kv[0]] = kv[1]
mustEquals(t, check, clues.In(ctx).Map(), true)
mustEquals(t, check, clues.In(ctx).Map(), false)
}

assert(
Expand All @@ -229,14 +229,14 @@ func TestAddMap(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
ctx := context.WithValue(context.Background(), testCtx{}, "instance")
check := msa{}
mustEquals(t, check, clues.In(ctx).Map(), true)
mustEquals(t, check, clues.In(ctx).Map(), false)

for _, m := range test.ms {
ctx = clues.AddMap(ctx, m)
for k, v := range m {
check[k] = v
}
mustEquals(t, check, clues.In(ctx).Map(), true)
mustEquals(t, check, clues.In(ctx).Map(), false)
}

assert(
Expand All @@ -261,7 +261,7 @@ func TestAddTrace(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
ctx := context.WithValue(context.Background(), testCtx{}, "instance")
check := msa{}
mustEquals(t, check, clues.In(ctx).Map(), true)
mustEquals(t, check, clues.In(ctx).Map(), false)

ctx = clues.AddTrace(ctx, "")

Expand Down Expand Up @@ -292,7 +292,7 @@ func TestAddTraceName(t *testing.T) {
for _, test := range table {
t.Run(test.name, func(t *testing.T) {
ctx := context.WithValue(context.Background(), testCtx{}, "instance")
mustEquals(t, msa{}, clues.In(ctx).Map(), true)
mustEquals(t, msa{}, clues.In(ctx).Map(), false)

for _, name := range test.names {
ctx = clues.AddTraceWith(ctx, name, test.kvs...)
Expand All @@ -319,7 +319,7 @@ func TestImmutableCtx(t *testing.T) {
pre = clues.In(testCtx)
preMap = pre.Map()
)
mustEquals(t, check, preMap, true)
mustEquals(t, check, preMap, false)

ctx2 := clues.Add(testCtx, "k", "v")
if _, ok := preMap["k"]; ok {
Expand All @@ -343,11 +343,11 @@ func TestImmutableCtx(t *testing.T) {
lr = clues.Add(l, "beaux", "regard")
)

mustEquals(t, msa{}, clues.In(ctx).Map(), true)
mustEquals(t, msa{"foo": "bar"}, clues.In(l).Map(), true)
mustEquals(t, msa{"baz": "qux"}, clues.In(r).Map(), true)
mustEquals(t, msa{"foo": "bar", "fnords": "smarf"}, clues.In(ll).Map(), true)
mustEquals(t, msa{"foo": "bar", "beaux": "regard"}, clues.In(lr).Map(), true)
mustEquals(t, msa{}, clues.In(ctx).Map(), false)
mustEquals(t, msa{"foo": "bar"}, clues.In(l).Map(), false)
mustEquals(t, msa{"baz": "qux"}, clues.In(r).Map(), false)
mustEquals(t, msa{"foo": "bar", "fnords": "smarf"}, clues.In(ll).Map(), false)
mustEquals(t, msa{"foo": "bar", "beaux": "regard"}, clues.In(lr).Map(), false)
}

var _ cecrets.Concealer = &safe{}
Expand Down Expand Up @@ -428,12 +428,12 @@ func TestAdd_concealed(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
ctx := context.WithValue(context.Background(), testCtx{}, "instance")
check := msa{}
mustEquals(t, check, toMSA(clues.In(ctx).Map()), true)
mustEquals(t, check, toMSA(clues.In(ctx).Map()), false)

for _, cs := range test.concealers {
ctx = clues.Add(ctx, cs...)
check[concealed(cs[0])] = concealed(cs[1])
mustEquals(t, check, toMSA(clues.In(ctx).Map()), true)
mustEquals(t, check, toMSA(clues.In(ctx).Map()), false)
}

assertMSA(
Expand Down Expand Up @@ -577,15 +577,15 @@ func TestAddAgent(t *testing.T) {

mapEquals(t, ctx, msa{
"one": 1,
}, true)
}, false)

ctxWithWit := clues.AddAgent(ctx, "wit")
clues.Relay(ctx, "wit", "zero", 0)
clues.Relay(ctxWithWit, "wit", "two", 2)

mapEquals(t, ctx, msa{
"one": 1,
}, true)
}, false)

mapEquals(t, ctxWithWit, msa{
"one": 1,
Expand All @@ -594,14 +594,14 @@ func TestAddAgent(t *testing.T) {
"two": 2,
},
},
}, true)
}, false)

ctxWithTim := clues.AddAgent(ctxWithWit, "tim")
clues.Relay(ctxWithTim, "tim", "three", 3)

mapEquals(t, ctx, msa{
"one": 1,
}, true)
}, false)

mapEquals(t, ctxWithTim, msa{
"one": 1,
Expand All @@ -613,14 +613,14 @@ func TestAddAgent(t *testing.T) {
"three": 3,
},
},
}, true)
}, false)

ctxWithBob := clues.AddAgent(ctx, "bob")
clues.Relay(ctxWithBob, "bob", "four", 4)

mapEquals(t, ctx, msa{
"one": 1,
}, true)
}, false)

// should not have changed since its first usage
mapEquals(t, ctxWithWit, msa{
Expand All @@ -630,7 +630,7 @@ func TestAddAgent(t *testing.T) {
"two": 2,
},
},
}, true)
}, false)

// should not have changed since its first usage
mapEquals(t, ctxWithTim, msa{
Expand All @@ -643,7 +643,7 @@ func TestAddAgent(t *testing.T) {
"three": 3,
},
},
}, true)
}, false)

mapEquals(t, ctxWithBob, msa{
"one": 1,
Expand All @@ -652,5 +652,5 @@ func TestAddAgent(t *testing.T) {
"four": 4,
},
},
}, true)
}, false)
}
1 change: 0 additions & 1 deletion datanode.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ func (dn *dataNode) addValues(m map[string]any) *dataNode {
}

spawn := dn.spawnDescendant()
spawn.id = makeNodeID()
spawn.setValues(m)

return spawn
Expand Down
33 changes: 14 additions & 19 deletions err_fmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1734,11 +1734,10 @@ func TestComment(t *testing.T) {

func TestErrCore_String(t *testing.T) {
table := []struct {
name string
core *clues.ErrCore
expectS string
expectVPlus string
expectCluesTrace bool
name string
core *clues.ErrCore
expectS string
expectVPlus string
}{
{
name: "nil",
Expand All @@ -1753,47 +1752,43 @@ func TestErrCore_String(t *testing.T) {
With("key", "value").
Label("label").
Core(),
expectS: `{"message", [label], {key:value}}`,
expectVPlus: `{msg:"message", labels:[label], values:{key:value}, comments:[]}`,
expectCluesTrace: true,
expectS: `{"message", [label], {key:value}}`,
expectVPlus: `{msg:"message", labels:[label], values:{key:value}, comments:[]}`,
},
{
name: "message only",
core: clues.
New("message").
Core(),
expectS: `{"message"}`,
expectVPlus: `{msg:"message", labels:[], values:{}, comments:[]}`,
expectCluesTrace: false,
expectS: `{"message"}`,
expectVPlus: `{msg:"message", labels:[], values:{}, comments:[]}`,
},
{
name: "labels only",
core: clues.
New("").
Label("label").
Core(),
expectS: `{[label]}`,
expectVPlus: `{msg:"", labels:[label], values:{}, comments:[]}`,
expectCluesTrace: false,
expectS: `{[label]}`,
expectVPlus: `{msg:"", labels:[label], values:{}, comments:[]}`,
},
{
name: "values only",
core: clues.
New("").
With("key", "value").
Core(),
expectS: `{{key:value}}`,
expectVPlus: `{msg:"", labels:[], values:{key:value}, comments:[]}`,
expectCluesTrace: true,
expectS: `{{key:value}}`,
expectVPlus: `{msg:"", labels:[], values:{key:value}, comments:[]}`,
},
}
for _, test := range table {
t.Run(test.name, func(t *testing.T) {
tc := test.core
if tc != nil {
if _, ok := tc.Values["clues_trace"]; ok != test.expectCluesTrace {
if _, ok := tc.Values["clues_trace"]; ok {
t.Errorf(
"expected core values to contain key [clues_trace]\ngot: %+v",
"did not expect core values to contain key [clues_trace]\ngot: %+v",
tc.Values)
}
delete(tc.Values, "clues_trace")
Expand Down
20 changes: 10 additions & 10 deletions err_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,8 @@ func TestWith(t *testing.T) {
for _, kv := range test.with {
err = err.With(kv...)
}
mustEquals(t, test.expect, clues.InErr(err).Map(), true)
mustEquals(t, test.expect, err.Values().Map(), true)
mustEquals(t, test.expect, clues.InErr(err).Map(), false)
mustEquals(t, test.expect, err.Values().Map(), false)
})
}
}
Expand Down Expand Up @@ -332,8 +332,8 @@ func TestWithMap(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
err := clues.WithMap(test.initial, test.kv)
err = err.WithMap(test.with)
mustEquals(t, test.expect, clues.InErr(err).Map(), true)
mustEquals(t, test.expect, err.Values().Map(), true)
mustEquals(t, test.expect, clues.InErr(err).Map(), false)
mustEquals(t, test.expect, err.Values().Map(), false)
})
}
}
Expand Down Expand Up @@ -375,8 +375,8 @@ func TestWithClues(t *testing.T) {
tctx := clues.AddMap(ctx, test.kv)
err := clues.WithClues(test.initial, tctx)
err = err.WithMap(test.with)
mustEquals(t, test.expect, clues.InErr(err).Map(), true)
mustEquals(t, test.expect, err.Values().Map(), true)
mustEquals(t, test.expect, clues.InErr(err).Map(), false)
mustEquals(t, test.expect, err.Values().Map(), false)
})
}
}
Expand Down Expand Up @@ -426,7 +426,7 @@ func TestValuePriority(t *testing.T) {
}
for _, test := range table {
t.Run(test.name, func(t *testing.T) {
mustEquals(t, test.expect, clues.InErr(test.err).Map(), true)
mustEquals(t, test.expect, clues.InErr(test.err).Map(), false)
})
}
}
Expand Down Expand Up @@ -681,7 +681,7 @@ func TestErrValues_stacks(t *testing.T) {
for _, test := range table {
t.Run(test.name, func(t *testing.T) {
vs := clues.InErr(test.err)
mustEquals(t, test.expect, vs.Map(), true)
mustEquals(t, test.expect, vs.Map(), false)
})
}
}
Expand All @@ -690,7 +690,7 @@ func TestImmutableErrors(t *testing.T) {
err := clues.New("an error").With("k", "v")
check := msa{"k": "v"}
pre := clues.InErr(err)
mustEquals(t, check, pre.Map(), true)
mustEquals(t, check, pre.Map(), false)

err2 := err.With("k2", "v2")
if _, ok := pre.Map()["k2"]; ok {
Expand Down Expand Up @@ -1073,7 +1073,7 @@ func TestToCore(t *testing.T) {
t.Errorf("expected Msg [%v], got [%v]", test.expectMsg, c.Msg)
}
mustEquals(t, test.expectLabels, toMSA(c.Labels), false)
mustEquals(t, test.expectValues, toMSA(c.Values), true)
mustEquals(t, test.expectValues, toMSA(c.Values), false)
})
}
}
Expand Down

0 comments on commit b39b934

Please sign in to comment.