Skip to content

Commit

Permalink
remove labelCounter support
Browse files Browse the repository at this point in the history
labelCounter was a hack hook-in designed to match corso's janky metrics
system.  It was never fully utilized, and therefore can be removed.
  • Loading branch information
ryanfkeepers committed Oct 31, 2024
1 parent 1f44bd5 commit 0e9e580
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 236 deletions.
16 changes: 0 additions & 16 deletions clues.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
14 changes: 2 additions & 12 deletions datanode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -80,9 +71,8 @@ func (dn *dataNode) spawnDescendant() *dataNode {
}

return &dataNode{
parent: dn,
labelCounter: dn.labelCounter,
agents: agents,
parent: dn,
agents: agents,
}
}

Expand Down
48 changes: 0 additions & 48 deletions err.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}
}

Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
160 changes: 0 additions & 160 deletions err_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
// ---------------------------------------------------------------------------
Expand Down

0 comments on commit 0e9e580

Please sign in to comment.