Skip to content

Commit

Permalink
Improvement: Add the ability to trim keys no longer in use from the k…
Browse files Browse the repository at this point in the history
…eyedErrorHealthCheckSource (#44)
  • Loading branch information
k-simons authored Aug 11, 2021
1 parent b5f5ff8 commit 8d7dc33
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-44.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: improvement
improvement:
description: |-
Adds PurgeKeys to window.KeyedErrorSubmitter interface, allowing a caller to remove entries from a keyed window health check.
links:
- https://github.com/palantir/witchcraft-go-health/pull/44
13 changes: 13 additions & 0 deletions sources/store/keyed_error_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type KeyedErrorSubmitter interface {
// Submit stores non-nil errors by the provided key in a map; keys of submitted nil errors are
// deleted from the map.
Submit(key string, err error)
// PurgeKeys iterates through all currently tracked keys, calling fn on each.
// If fn returns true, the key will be removed from the healthcheck.
PurgeKeys(fn func(key string) bool)
}

// KeyedErrorHealthCheckSource tracks errors by key to compute health status. Only entries with non-nil
Expand Down Expand Up @@ -100,3 +103,13 @@ func (k *keyedErrorHealthCheckSource) HealthStatus(ctx context.Context) health.H
},
}
}

func (k *keyedErrorHealthCheckSource) PurgeKeys(fn func(key string) bool) {
k.lock.Lock()
defer k.lock.Unlock()
for keyName := range k.keyedErrors {
if fn(keyName) {
delete(k.keyedErrors, keyName)
}
}
}
49 changes: 49 additions & 0 deletions sources/store/keyed_error_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,52 @@ func TestKeyedMessengerHealthStateHealthy(t *testing.T) {
},
}, keyedErrorSource.HealthStatus(context.Background()))
}

func TestKeyedMessengerHealthStateErrorAndPreserve(t *testing.T) {
keyedErrorSource := NewKeyedErrorHealthCheckSource("TEST", testMessage)
keyedErrorSource.Submit("1", werror.Error("error message 1"))
keyedErrorSource.Submit("2", werror.Error("error message 2"))
keyedErrorSource.Submit("3", nil)
assert.Equal(t, health.HealthStatus{
Checks: map[health.CheckType]health.HealthCheckResult{
"TEST": {
Message: &testMessage,
Params: map[string]interface{}{
"1": "error message 1",
"2": "error message 2",
},
State: health.New_HealthState(health.HealthState_ERROR),
Type: "TEST",
},
},
}, keyedErrorSource.HealthStatus(context.Background()))
// Keep One Error
keyedErrorSource.PurgeKeys(func(key string) bool {
return key == "1"
})
assert.Equal(t, health.HealthStatus{
Checks: map[health.CheckType]health.HealthCheckResult{
"TEST": {
Message: &testMessage,
Params: map[string]interface{}{
"2": "error message 2",
},
State: health.New_HealthState(health.HealthState_ERROR),
Type: "TEST",
},
},
}, keyedErrorSource.HealthStatus(context.Background()))
// Remove other
keyedErrorSource.PurgeKeys(func(key string) bool {
return key == "2"
})
assert.Equal(t, health.HealthStatus{
Checks: map[health.CheckType]health.HealthCheckResult{
"TEST": {
Message: &testMessage,
State: health.New_HealthState(health.HealthState_HEALTHY),
Type: "TEST",
},
},
}, keyedErrorSource.HealthStatus(context.Background()))
}

0 comments on commit 8d7dc33

Please sign in to comment.