Improve the log level filtering heuristics, but also discourage their use #23577
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.
The log filtering behavior was previously completely broken because it was assuming log messages would arrive framed neatly with one message per call to
Write
, and that isn't true in Terraform due to the indirection throughpanicwrap
.While we can't get this perfect because we're losing too much information, this updated set of heuristics seem to experimentally improve the situation and make it produce closer to the correct result. Because these heuristics are relying on some conventions in Terraform itself, I've brought the
LevelFilter
implementation into Terraform itself so that we can avoid putting these odd heuristics in the sharedlogutils
package.With that said, it still can't be perfect because we don't have enough information to be filter reliably, and so this also includes an explicit warning so that anyone relying on filtered logs might get an explanation for any oddities they see and we can give them a specific course of action to improve the situation: use
TF_LOG=trace
.Previously we were using the filter in all cases, but since we know it's not 100% dependable, and these additional heuristics might make certain situations more quirky, this also includes a special case to bypass the filter when the
TRACE
level is selected, thus ensuring that we keep our promise of producing complete log output in that case, bugs in the filtering logic notwithstanding.It is very likely that this will still misbehave. The goal is only to misbehave less than the previous approach.