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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
server+plugins: Integrate server + NDBCache in decision logs. #5147
Merged
philipaconrad
merged 1 commit into
open-policy-agent:main
from
philipaconrad:issue-1514-integrate-ndbcache-for-decision-logging
Oct 6, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
|
@@ -44,21 +44,22 @@ type Logger interface { | |
// the struct. Any changes here MUST be reflected in the AST() | ||
// implementation below. | ||
type EventV1 struct { | ||
Labels map[string]string `json:"labels"` | ||
DecisionID string `json:"decision_id"` | ||
Revision string `json:"revision,omitempty"` // Deprecated: Use Bundles instead | ||
Bundles map[string]BundleInfoV1 `json:"bundles,omitempty"` | ||
Path string `json:"path,omitempty"` | ||
Query string `json:"query,omitempty"` | ||
Input *interface{} `json:"input,omitempty"` | ||
Result *interface{} `json:"result,omitempty"` | ||
MappedResult *interface{} `json:"mapped_result,omitempty"` | ||
Erased []string `json:"erased,omitempty"` | ||
Masked []string `json:"masked,omitempty"` | ||
Error error `json:"error,omitempty"` | ||
RequestedBy string `json:"requested_by,omitempty"` | ||
Timestamp time.Time `json:"timestamp"` | ||
Metrics map[string]interface{} `json:"metrics,omitempty"` | ||
Labels map[string]string `json:"labels"` | ||
DecisionID string `json:"decision_id"` | ||
Revision string `json:"revision,omitempty"` // Deprecated: Use Bundles instead | ||
Bundles map[string]BundleInfoV1 `json:"bundles,omitempty"` | ||
Path string `json:"path,omitempty"` | ||
Query string `json:"query,omitempty"` | ||
Input *interface{} `json:"input,omitempty"` | ||
Result *interface{} `json:"result,omitempty"` | ||
MappedResult *interface{} `json:"mapped_result,omitempty"` | ||
NDBuiltinCache *interface{} `json:"nd_builtin_cache,omitempty"` | ||
Erased []string `json:"erased,omitempty"` | ||
Masked []string `json:"masked,omitempty"` | ||
Error error `json:"error,omitempty"` | ||
RequestedBy string `json:"requested_by,omitempty"` | ||
Timestamp time.Time `json:"timestamp"` | ||
Metrics map[string]interface{} `json:"metrics,omitempty"` | ||
|
||
inputAST ast.Value | ||
} | ||
|
@@ -87,6 +88,7 @@ var queryKey = ast.StringTerm("query") | |
var inputKey = ast.StringTerm("input") | ||
var resultKey = ast.StringTerm("result") | ||
var mappedResultKey = ast.StringTerm("mapped_result") | ||
var ndBuiltinCacheKey = ast.StringTerm("nd_builtin_cache") | ||
var erasedKey = ast.StringTerm("erased") | ||
var maskedKey = ast.StringTerm("masked") | ||
var errorKey = ast.StringTerm("error") | ||
|
@@ -159,6 +161,14 @@ func (e *EventV1) AST() (ast.Value, error) { | |
event.Insert(mappedResultKey, ast.NewTerm(mResults)) | ||
} | ||
|
||
if e.NDBuiltinCache != nil { | ||
ndbCache, err := roundtripJSONToAST(e.NDBuiltinCache) | ||
if err != nil { | ||
return nil, err | ||
} | ||
event.Insert(ndBuiltinCacheKey, ast.NewTerm(ndbCache)) | ||
} | ||
|
||
if len(e.Erased) > 0 { | ||
erased := make([]*ast.Term, len(e.Erased)) | ||
for i, v := range e.Erased { | ||
|
@@ -226,6 +236,7 @@ const ( | |
defaultBufferSizeLimitBytes = int64(0) // unlimited | ||
defaultMaskDecisionPath = "/system/log/mask" | ||
logDropCounterName = "decision_logs_dropped" | ||
logNDBDropCounterName = "decision_logs_nd_builtin_cache_dropped" | ||
defaultResourcePath = "/logs" | ||
) | ||
|
||
|
@@ -248,6 +259,7 @@ type Config struct { | |
MaskDecision *string `json:"mask_decision"` | ||
ConsoleLogs bool `json:"console"` | ||
Resource *string `json:"resource"` | ||
NDBuiltinCache bool `json:"nd_builtin_cache,omitempty"` | ||
maskDecisionRef ast.Ref | ||
} | ||
|
||
|
@@ -557,18 +569,19 @@ func (p *Plugin) Log(ctx context.Context, decision *server.Info) error { | |
} | ||
|
||
event := EventV1{ | ||
Labels: p.manager.Labels(), | ||
DecisionID: decision.DecisionID, | ||
Revision: decision.Revision, | ||
Bundles: bundles, | ||
Path: decision.Path, | ||
Query: decision.Query, | ||
Input: decision.Input, | ||
Result: decision.Results, | ||
MappedResult: decision.MappedResults, | ||
RequestedBy: decision.RemoteAddr, | ||
Timestamp: decision.Timestamp, | ||
inputAST: decision.InputAST, | ||
Labels: p.manager.Labels(), | ||
DecisionID: decision.DecisionID, | ||
Revision: decision.Revision, | ||
Bundles: bundles, | ||
Path: decision.Path, | ||
Query: decision.Query, | ||
Input: decision.Input, | ||
Result: decision.Results, | ||
MappedResult: decision.MappedResults, | ||
NDBuiltinCache: decision.NDBuiltinCache, | ||
RequestedBy: decision.RemoteAddr, | ||
Timestamp: decision.Timestamp, | ||
inputAST: decision.InputAST, | ||
} | ||
|
||
if decision.Metrics != nil { | ||
|
@@ -793,6 +806,9 @@ func (p *Plugin) reconfigure(config interface{}) { | |
p.config = *newConfig | ||
} | ||
|
||
// NOTE(philipc): Because ND builtins caching can cause unbounded growth in | ||
// decision log entry size, we do best-effort event encoding here, and when we | ||
// run out of space, we drop the ND builtins cache, and try encoding again. | ||
func (p *Plugin) encodeAndBufferEvent(event EventV1) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is where the retry-encoding-without-NDBCache logic lives. If encoding the event succeeds after dropping the NDBCache, we print an error message + increment a counter metric, so that this behavior can be tracked. |
||
if p.limiter != nil { | ||
if !p.limiter.Allow() { | ||
|
@@ -807,11 +823,29 @@ func (p *Plugin) encodeAndBufferEvent(event EventV1) { | |
|
||
result, err := p.enc.Write(event) | ||
if err != nil { | ||
// TODO(tsandall): revisit this now that we have an API that | ||
// can return an error. Should the default behaviour be to | ||
// fail-closed as we do for plugins? | ||
p.logger.Error("Log encoding failed: %v.", err) | ||
return | ||
// If there's no ND builtins cache in the event, then we don't | ||
// need to retry encoding anything. | ||
if event.NDBuiltinCache == nil { | ||
// TODO(tsandall): revisit this now that we have an API that | ||
// can return an error. Should the default behaviour be to | ||
// fail-closed as we do for plugins? | ||
p.logger.Error("Log encoding failed: %v.", err) | ||
return | ||
} | ||
|
||
// Attempt to encode the event again, dropping the ND builtins cache. | ||
newEvent := event | ||
philipaconrad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
newEvent.NDBuiltinCache = nil | ||
|
||
result, err = p.enc.Write(newEvent) | ||
if err != nil { | ||
p.logger.Error("Log encoding failed: %v.", err) | ||
return | ||
} | ||
|
||
// Re-encoding was successful, but we still need to alert users. | ||
p.logger.Error("ND builtins cache dropped from this event to fit under maximum upload size limits. Increase upload size limit or change usage of non-deterministic builtins.") | ||
p.metrics.Counter(logNDBDropCounterName).Incr() | ||
} | ||
|
||
for _, chunk := range result { | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because of the careful interface/type-wrangling we do earlier in the server's
Log
function, we can just copy/paste the masking logic here for the NDBCache. It behaves exactly like theInput
andResult
masking targets, so the logic is safe to reuse with minimal changes. 馃槃