From 0e9e580ebbf77bbd17d1103400783f130295ded1 Mon Sep 17 00:00:00 2001 From: Keepers Date: Thu, 31 Oct 2024 12:46:54 -0600 Subject: [PATCH] remove labelCounter support labelCounter was a hack hook-in designed to match corso's janky metrics system. It was never fully utilized, and therefore can be removed. --- clues.go | 16 ------ datanode.go | 14 +---- err.go | 48 ---------------- err_test.go | 160 ---------------------------------------------------- 4 files changed, 2 insertions(+), 236 deletions(-) diff --git a/clues.go b/clues.go index 2f2eafd..d89abc6 100644 --- a/clues.go +++ b/clues.go @@ -148,19 +148,3 @@ func Relay( // set values, not add. We don't want agents to own a full clues tree. ag.data.setValues(normalize(vs...)) } - -// --------------------------------------------------------------------------- -// error label counter -// --------------------------------------------------------------------------- - -// AddLabelCounter embeds an Adder interface into this context. Any already -// embedded Adder will get replaced. When adding Labels to a clues.Err the -// LabelCounter will use the label as the key for the Add call, and increment -// the count of that label by one. -func AddLabelCounter(ctx context.Context, counter Adder) context.Context { - nc := nodeFromCtx(ctx) - nn := nc.addValues(nil) - nn.labelCounter = counter - - return setNodeInCtx(ctx, nn) -} diff --git a/datanode.go b/datanode.go index 0530630..b15bd23 100644 --- a/datanode.go +++ b/datanode.go @@ -52,15 +52,6 @@ type dataNode struct { // ancestor to the current node to produce the comment history. comment comment - // labelCounter is a func hook that allows a caller to automatically count the - // number of times a label appears. DataNodes themselves have no labels, so - // in this case the presence of a labelCounter will be used to count the labels - // appearing in errors which attach this data node to the error. - // - // Errors will only utilize the first labelCounter they find. The tree is searched - // from leaf to root when looking for populated labelCounters. - labelCounter Adder - // agents act as proxy dataNodes that can relay specific, intentional data // additions. They're namespaced so that additions to the agents don't accidentally // clobber other values in the dataNode. This also allows agents to protect @@ -80,9 +71,8 @@ func (dn *dataNode) spawnDescendant() *dataNode { } return &dataNode{ - parent: dn, - labelCounter: dn.labelCounter, - agents: agents, + parent: dn, + agents: agents, } } diff --git a/err.go b/err.go index 5159974..2db7d80 100644 --- a/err.go +++ b/err.go @@ -309,19 +309,7 @@ func (err *Err) Label(labels ...string) *Err { err.labels = map[string]struct{}{} } - lc := getLabelCounter(err) - els := err.Labels() - for _, label := range labels { - if lc != nil { - _, inPrior := els[label] - _, inCurrent := err.labels[label] - if !inPrior && !inCurrent { - lc.Add(label, 1) - } - } - // don't duplicate counts - err.labels[label] = struct{}{} } @@ -866,7 +854,6 @@ func (err *Err) WithClues(ctx context.Context) *Err { dn := In(ctx) e := err.WithMap(dn.Map()) - e.data.labelCounter = dn.labelCounter return e } @@ -1077,41 +1064,6 @@ func Comment(err error, msg string, vs ...any) *Err { // helpers // ------------------------------------------------------------ -// getLabelCounter retrieves the a labelCounter from the provided -// error. The algorithm works from the current error up the -// hierarchy, looking into each dataNode tree along the way, and -// eagerly takes the first available counter. -func getLabelCounter(e error) Adder { - if e == nil { - return nil - } - - ce, ok := e.(*Err) - if !ok { - return nil - } - - for i := len(ce.stack) - 1; i >= 0; i-- { - lc := getLabelCounter(ce.stack[i]) - if lc != nil { - return lc - } - } - - if ce.e != nil { - lc := getLabelCounter(ce.e) - if lc != nil { - return lc - } - } - - if ce.data != nil && ce.data.labelCounter != nil { - return ce.data.labelCounter - } - - return nil -} - // returns true if the error is nil, or if it is a non-nil interface // containing a nil value. func isNilErrIface(err error) bool { diff --git a/err_test.go b/err_test.go index 9a192d5..fd64fcf 100644 --- a/err_test.go +++ b/err_test.go @@ -1189,166 +1189,6 @@ var labelTable = []struct { }, } -func TestLabelCounter_iterative(t *testing.T) { - for _, test := range labelTable { - t.Run(test.name, func(t *testing.T) { - var ( - lc = labelCounter{} - ctx = clues.AddLabelCounter(context.Background(), lc) - err = clues.NewWC(ctx, "an err") - ) - - for _, l := range test.labels { - err.Label(l) - } - - mustEquals(t, toMSA(test.expect), toMSA(lc), false) - }) - } -} - -func TestLabelCounter_variadic(t *testing.T) { - for _, test := range labelTable { - t.Run(test.name, func(t *testing.T) { - var ( - lc = labelCounter{} - ctx = clues.AddLabelCounter(context.Background(), lc) - err = clues.NewWC(ctx, "an err") - ) - - err.Label(test.labels...) - - mustEquals(t, toMSA(test.expect), toMSA(lc), false) - }) - } -} - -func TestLabelCounter_iterative_stacked(t *testing.T) { - for _, test := range labelTable { - t.Run(test.name, func(t *testing.T) { - var ( - lc = labelCounter{} - ctx = clues.AddLabelCounter(context.Background(), lc) - err = clues.NewWC(ctx, "an err") - ) - - for _, l := range test.labels { - err.Label(l) - } - - err = clues.Stack(err) - - // duplicates on the wrapped error should not get counted - for _, l := range test.labels { - err.Label(l) - } - - mustEquals(t, toMSA(test.expect), toMSA(lc), false) - }) - } -} - -func TestLabelCounter_variadic_stacked(t *testing.T) { - for _, test := range labelTable { - t.Run(test.name, func(t *testing.T) { - var ( - lc = labelCounter{} - ctx = clues.AddLabelCounter(context.Background(), lc) - err = clues.NewWC(ctx, "an err") - ) - - err.Label(test.labels...) - - // duplicates on the wrapped error should not get counted - err = clues.Stack(err).Label(test.labels...) - - mustEquals(t, toMSA(test.expect), toMSA(lc), false) - }) - } -} - -func TestLabelCounter_iterative_wrapped(t *testing.T) { - for _, test := range labelTable { - t.Run(test.name, func(t *testing.T) { - var ( - lc = labelCounter{} - ctx = clues.AddLabelCounter(context.Background(), lc) - err = clues.NewWC(ctx, "an err") - ) - - for _, l := range test.labels { - err.Label(l) - } - - err = clues.Wrap(err, "wrap") - - // duplicates on the wrapped error should not get counted - for _, l := range test.labels { - err.Label(l) - } - - mustEquals(t, toMSA(test.expect), toMSA(lc), false) - }) - } -} - -func TestLabelCounter_variadic_wrapped(t *testing.T) { - for _, test := range labelTable { - t.Run(test.name, func(t *testing.T) { - var ( - lc = labelCounter{} - ctx = clues.AddLabelCounter(context.Background(), lc) - err = clues.NewWC(ctx, "an err") - ) - - err.Label(test.labels...) - - // duplicates on the wrapped error should not get counted - err = clues.Wrap(err, "wrap").Label(test.labels...) - - mustEquals(t, toMSA(test.expect), toMSA(lc), false) - }) - } -} - -func TestLabelCounter_iterative_noCluesInErr(t *testing.T) { - for _, test := range labelTable { - t.Run(test.name, func(t *testing.T) { - var ( - lc = labelCounter{} - ctx = clues.AddLabelCounter(context.Background(), lc) - err = clues.New("an err") - ) - - for _, l := range test.labels { - err.Label(l) - } - - err = err.WithClues(ctx) - - // no labeling before WithClues is called on the error - mustEquals(t, toMSA(lc), msa{}, false) - }) - } -} - -func TestLabelCounter_variadic_noCluesInErr(t *testing.T) { - for _, test := range labelTable { - t.Run("variadic_"+test.name, func(t *testing.T) { - var ( - lc = labelCounter{} - ctx = clues.AddLabelCounter(context.Background(), lc) - err = clues.New("an err") - ) - - err.Label(test.labels...).WithClues(ctx) - - // no labeling before WithClues is called on the error - mustEquals(t, toMSA(lc), msa{}, false) - }) - } -} - // --------------------------------------------------------------------------- // helpers // ---------------------------------------------------------------------------