-
Notifications
You must be signed in to change notification settings - Fork 4
CXP-383 Correct audit-log pagination cursor and sample skip-org warnings #193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package connector | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "sync/atomic" | ||
|
|
||
| "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| type sampledWarn struct { | ||
| n atomic.Uint64 | ||
| } | ||
|
|
||
| func (s *sampledWarn) log(ctx context.Context, msg string, fields ...zap.Field) { | ||
| n := s.n.Add(1) | ||
| if !shouldLogSample(n) { | ||
| return | ||
| } | ||
| ctxzap.Extract(ctx).Warn(msg, append(fields, zap.Uint64("total_occurrences", n))...) | ||
| } | ||
|
|
||
| type perKeySampledWarn struct { | ||
| m sync.Map // key -> *sampledWarn | ||
| } | ||
|
|
||
| func (p *perKeySampledWarn) log(ctx context.Context, key, msg string, fields ...zap.Field) { | ||
| actual, _ := p.m.LoadOrStore(key, &sampledWarn{}) | ||
| actual.(*sampledWarn).log(ctx, msg, fields...) | ||
| } | ||
|
|
||
| // shouldLogSample reports whether the nth occurrence should be logged. | ||
| func shouldLogSample(n uint64) bool { | ||
| switch { | ||
| case n <= 1, n == 10, n == 100: | ||
| return true | ||
| default: | ||
| return n%1000 == 0 | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package connector | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap" | ||
| "github.com/stretchr/testify/require" | ||
| "go.uber.org/zap" | ||
| "go.uber.org/zap/zapcore" | ||
| "go.uber.org/zap/zaptest/observer" | ||
| ) | ||
|
|
||
| func TestShouldLogSample(t *testing.T) { | ||
| tests := []struct { | ||
| n uint64 | ||
| want bool | ||
| }{ | ||
| {0, true}, | ||
| {1, true}, | ||
| {2, false}, | ||
| {9, false}, | ||
| {10, true}, | ||
| {11, false}, | ||
| {99, false}, | ||
| {100, true}, | ||
| {101, false}, | ||
| {999, false}, | ||
| {1000, true}, | ||
| {1001, false}, | ||
| {1999, false}, | ||
| {2000, true}, | ||
| } | ||
| for _, tt := range tests { | ||
| require.Equal(t, tt.want, shouldLogSample(tt.n), "n=%d", tt.n) | ||
| } | ||
| } | ||
|
|
||
| func TestSampledWarn_LogsOnlyOnSampledOccurrences(t *testing.T) { | ||
| core, logs := observer.New(zapcore.DebugLevel) | ||
| ctx := ctxzap.ToContext(context.Background(), zap.New(core)) | ||
|
|
||
| var s sampledWarn | ||
| for i := 0; i < 12; i++ { | ||
| s.log(ctx, "org lacks audit-log access, skipping it for this pass", zap.String("org", "octo-org")) | ||
| } | ||
|
|
||
| // Occurrences 1 and 10 should be logged; 2-9 and 11-12 should not. | ||
| require.Equal(t, 2, logs.Len(), "expected exactly 2 sampled log lines out of 12 occurrences") | ||
|
|
||
| first := logs.All()[0] | ||
| require.Equal(t, uint64(1), first.ContextMap()["total_occurrences"]) | ||
|
|
||
| second := logs.All()[1] | ||
| require.Equal(t, uint64(10), second.ContextMap()["total_occurrences"]) | ||
| } | ||
|
|
||
| func TestPerKeySampledWarn_EachKeyGetsItsOwnBudget(t *testing.T) { | ||
| // skippedOrgs is keyed per org so one noisy org's sampling budget can't | ||
| // starve another org's first occurrence out of the log. | ||
| core, logs := observer.New(zapcore.DebugLevel) | ||
| ctx := ctxzap.ToContext(context.Background(), zap.New(core)) | ||
|
|
||
| var p perKeySampledWarn | ||
| orgs := []string{"org-a", "org-b", "org-c"} | ||
| for i := 0; i < 9; i++ { | ||
| org := orgs[i%len(orgs)] | ||
| p.log(ctx, org, "org lacks audit-log access, skipping it for this pass", zap.String("org", org)) | ||
| } | ||
|
|
||
| // Each org's first occurrence is its own occurrence 1, so all three log. | ||
| require.Equal(t, 3, logs.Len()) | ||
| gotOrgs := make([]string, len(logs.All())) | ||
| for i, entry := range logs.All() { | ||
| gotOrgs[i] = entry.ContextMap()["org"].(string) | ||
| } | ||
| require.ElementsMatch(t, orgs, gotOrgs) | ||
| } | ||
|
|
||
| func TestPerKeySampledWarn_NoisyKeyDoesNotStarveNewKey(t *testing.T) { | ||
| // Reproduces the diagnosability gap a shared counter has: org-a alone | ||
| // drives the budget deep into a high sampling gap, then org-b's very | ||
| // first failure must still surface immediately rather than landing on | ||
| // org-a's non-sampled occurrence. | ||
| core, logs := observer.New(zapcore.DebugLevel) | ||
| ctx := ctxzap.ToContext(context.Background(), zap.New(core)) | ||
|
|
||
| var p perKeySampledWarn | ||
| for i := 0; i < 400; i++ { | ||
| p.log(ctx, "org-a", "org lacks audit-log access, skipping it for this pass", zap.String("org", "org-a")) | ||
| } | ||
| logs.TakeAll() // discard org-a's own sampled lines; only org-b matters here | ||
|
|
||
| p.log(ctx, "org-b", "org lacks audit-log access, skipping it for this pass", zap.String("org", "org-b")) | ||
|
|
||
| require.Equal(t, 1, logs.Len(), "org-b's first failure must be logged even though org-a's counter is at 400") | ||
| require.Equal(t, "org-b", logs.All()[0].ContextMap()["org"]) | ||
| require.Equal(t, uint64(1), logs.All()[0].ContextMap()["total_occurrences"]) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.