-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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
feat: Support usage trackers for received and discarded bytes. #11840
Merged
Merged
Changes from 31 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
154a6d1
Support custom trackers for received bytes.
jeschkies 79fff6b
Implement custom trackers config
jeschkies fd1ff01
Note todos
jeschkies a787cf4
Fix test
jeschkies ac8ae3a
Move code block
jeschkies dcbd58e
Merge remote-tracking branch 'grafana/main' into karsten/custom-trackers
jeschkies d37343b
Start tracking discarded bytes
jeschkies d5be74f
Calrify todo
jeschkies 77efea5
Change return type
jeschkies 51cbaa4
Comment discarded bytes
jeschkies b0b4c7c
generate docs
jeschkies fe34b32
Update docs
jeschkies be4d2c8
Add changelog entry
jeschkies e15746c
Merge remote-tracking branch 'grafana/main' into karsten/custom-trackers
jeschkies 659a9ef
Test push metrics
jeschkies 90099ef
Return empty trackers instead of nil
jeschkies 4f0697a
Test oltp push
jeschkies b6b47a5
Merge remote-tracking branch 'grafana/main' into karsten/custom-trackers
jeschkies 457b002
Merge remote-tracking branch 'grafana/main' into karsten/custom-trackers
jeschkies 4c3292d
Use label names intead of regex matchers
jeschkies ca417c2
correct doc
jeschkies ee947b3
Test unmarshalling
jeschkies e650cf0
Introduce custom tracker interface
jeschkies 4ce9aaf
fix tests
jeschkies c57c493
Pass empty limits
jeschkies 97a5af9
test tracker
jeschkies d077823
Merge remote-tracking branch 'grafana/main' into karsten/custom-trackers
jeschkies 22e1d8e
Report on discarded stream bytes
jeschkies a9a17fa
Merge remote-tracking branch 'grafana/main' into karsten/custom-trackers
jeschkies c564f40
Document custom tracker structs and methods
jeschkies fede4cf
Rename tracker interface and methods
jeschkies 9498628
Remove labels filtering and configuration.
jeschkies ab4f152
Rename tracker
jeschkies 9df9532
Correct changelog
jeschkies c6a7cb4
Inject usage tracker
jeschkies d07ac85
Do not presist label string
jeschkies c01afd7
Correct push test
jeschkies 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 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
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 |
---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |
|
||
"github.com/prometheus/prometheus/model/labels" | ||
|
||
"github.com/grafana/loki/pkg/loghttp/push" | ||
"github.com/grafana/loki/pkg/logproto" | ||
"github.com/grafana/loki/pkg/validation" | ||
) | ||
|
@@ -18,13 +19,14 @@ const ( | |
|
||
type Validator struct { | ||
Limits | ||
customStreamsTracker push.CustomStreamsTracker | ||
} | ||
|
||
func NewValidator(l Limits) (*Validator, error) { | ||
if l == nil { | ||
return nil, errors.New("nil Limits") | ||
} | ||
return &Validator{l}, nil | ||
return &Validator{l, nil}, nil | ||
} | ||
|
||
type validationContext struct { | ||
|
@@ -46,6 +48,8 @@ type validationContext struct { | |
maxStructuredMetadataCount int | ||
|
||
userID string | ||
|
||
customTrackerConfig *push.CustomTrackersConfig | ||
} | ||
|
||
func (v Validator) getValidationContextForTime(now time.Time, userID string) validationContext { | ||
|
@@ -63,11 +67,12 @@ func (v Validator) getValidationContextForTime(now time.Time, userID string) val | |
allowStructuredMetadata: v.AllowStructuredMetadata(userID), | ||
maxStructuredMetadataSize: v.MaxStructuredMetadataSize(userID), | ||
maxStructuredMetadataCount: v.MaxStructuredMetadataCount(userID), | ||
customTrackerConfig: v.CustomTrackersConfig(userID), | ||
} | ||
} | ||
|
||
// ValidateEntry returns an error if the entry is invalid and report metrics for invalid entries accordingly. | ||
func (v Validator) ValidateEntry(ctx validationContext, labels string, entry logproto.Entry) error { | ||
func (v Validator) ValidateEntry(ctx validationContext, labels labels.Labels, entry logproto.Entry) error { | ||
ts := entry.Timestamp.UnixNano() | ||
validation.LineLengthHist.Observe(float64(len(entry.Line))) | ||
|
||
|
@@ -77,13 +82,23 @@ func (v Validator) ValidateEntry(ctx validationContext, labels string, entry log | |
formatedRejectMaxAgeTime := time.Unix(0, ctx.rejectOldSampleMaxAge).Format(timeFormat) | ||
validation.DiscardedSamples.WithLabelValues(validation.GreaterThanMaxSampleAge, ctx.userID).Inc() | ||
validation.DiscardedBytes.WithLabelValues(validation.GreaterThanMaxSampleAge, ctx.userID).Add(float64(len(entry.Line))) | ||
if v.customStreamsTracker != nil { | ||
for _, matchedLbs := range ctx.customTrackerConfig.MatchTrackers(labels) { | ||
v.customStreamsTracker.DiscardedBytesAdd(ctx.userID, matchedLbs, float64(len(entry.Line))) | ||
} | ||
} | ||
return fmt.Errorf(validation.GreaterThanMaxSampleAgeErrorMsg, labels, formatedEntryTime, formatedRejectMaxAgeTime) | ||
} | ||
|
||
if ts > ctx.creationGracePeriod { | ||
formatedEntryTime := entry.Timestamp.Format(timeFormat) | ||
validation.DiscardedSamples.WithLabelValues(validation.TooFarInFuture, ctx.userID).Inc() | ||
validation.DiscardedBytes.WithLabelValues(validation.TooFarInFuture, ctx.userID).Add(float64(len(entry.Line))) | ||
if v.customStreamsTracker != nil { | ||
for _, matchedLbs := range ctx.customTrackerConfig.MatchTrackers(labels) { | ||
v.customStreamsTracker.DiscardedBytesAdd(ctx.userID, matchedLbs, float64(len(entry.Line))) | ||
} | ||
} | ||
return fmt.Errorf(validation.TooFarInFutureErrorMsg, labels, formatedEntryTime) | ||
} | ||
|
||
|
@@ -94,13 +109,23 @@ func (v Validator) ValidateEntry(ctx validationContext, labels string, entry log | |
// for parity. | ||
validation.DiscardedSamples.WithLabelValues(validation.LineTooLong, ctx.userID).Inc() | ||
validation.DiscardedBytes.WithLabelValues(validation.LineTooLong, ctx.userID).Add(float64(len(entry.Line))) | ||
if v.customStreamsTracker != nil { | ||
for _, matchedLbs := range ctx.customTrackerConfig.MatchTrackers(labels) { | ||
v.customStreamsTracker.DiscardedBytesAdd(ctx.userID, matchedLbs, float64(len(entry.Line))) | ||
} | ||
} | ||
return fmt.Errorf(validation.LineTooLongErrorMsg, maxSize, labels, len(entry.Line)) | ||
} | ||
|
||
if len(entry.StructuredMetadata) > 0 { | ||
if !ctx.allowStructuredMetadata { | ||
validation.DiscardedSamples.WithLabelValues(validation.DisallowedStructuredMetadata, ctx.userID).Inc() | ||
validation.DiscardedBytes.WithLabelValues(validation.DisallowedStructuredMetadata, ctx.userID).Add(float64(len(entry.Line))) | ||
if v.customStreamsTracker != nil { | ||
for _, matchedLbs := range ctx.customTrackerConfig.MatchTrackers(labels) { | ||
v.customStreamsTracker.DiscardedBytesAdd(ctx.userID, matchedLbs, float64(len(entry.Line))) | ||
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. It would be useful to pass the reason why the bytes have been discarded |
||
} | ||
} | ||
return fmt.Errorf(validation.DisallowedStructuredMetadataErrorMsg, labels) | ||
} | ||
|
||
|
@@ -113,12 +138,22 @@ func (v Validator) ValidateEntry(ctx validationContext, labels string, entry log | |
if maxSize := ctx.maxStructuredMetadataSize; maxSize != 0 && structuredMetadataSizeBytes > maxSize { | ||
validation.DiscardedSamples.WithLabelValues(validation.StructuredMetadataTooLarge, ctx.userID).Inc() | ||
validation.DiscardedBytes.WithLabelValues(validation.StructuredMetadataTooLarge, ctx.userID).Add(float64(len(entry.Line))) | ||
if v.customStreamsTracker != nil { | ||
for _, matchedLbs := range ctx.customTrackerConfig.MatchTrackers(labels) { | ||
v.customStreamsTracker.DiscardedBytesAdd(ctx.userID, matchedLbs, float64(len(entry.Line))) | ||
} | ||
} | ||
return fmt.Errorf(validation.StructuredMetadataTooLargeErrorMsg, labels, structuredMetadataSizeBytes, ctx.maxStructuredMetadataSize) | ||
} | ||
|
||
if maxCount := ctx.maxStructuredMetadataCount; maxCount != 0 && structuredMetadataCount > maxCount { | ||
validation.DiscardedSamples.WithLabelValues(validation.StructuredMetadataTooMany, ctx.userID).Inc() | ||
validation.DiscardedBytes.WithLabelValues(validation.StructuredMetadataTooMany, ctx.userID).Add(float64(len(entry.Line))) | ||
if v.customStreamsTracker != nil { | ||
for _, matchedLbs := range ctx.customTrackerConfig.MatchTrackers(labels) { | ||
v.customStreamsTracker.DiscardedBytesAdd(ctx.userID, matchedLbs, float64(len(entry.Line))) | ||
} | ||
} | ||
return fmt.Errorf(validation.StructuredMetadataTooManyErrorMsg, labels, structuredMetadataCount, ctx.maxStructuredMetadataCount) | ||
} | ||
} | ||
|
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
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.
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.
could we modify this struct to just have the
ls
? or do we call access thelabels
enough thatls.String()
would be too expensive?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.
It's really weird. We use it to override
stream.Labels
. I think this is because the parsing also sorts the labels.