Skip to content
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: make field max length configurable #432

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion input/elasticapm/internal/modeldecoder/generator/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ if k != "" && !%sRegexp.MatchString(k){

func mapRuleMaxVals(w io.Writer, f structField, rule validationRule) {
fmt.Fprintf(w, `
if utf8.RuneCountInString(t) > %s{
if enableFieldMaxLength && utf8.RuneCountInString(t) > %s{
return fmt.Errorf("'%s': validation rule '%s(%s)' violated")
}
`[1:], rule.value, jsonName(f), rule.name, rule.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ case %s:
`[1:], typ)
if maxLengthRule != (validationRule{}) {
fmt.Fprintf(w, `
if utf8.RuneCountInString(t) %s %s{
if enableFieldMaxLength && utf8.RuneCountInString(t) %s %s{
return fmt.Errorf("'%s': validation rule '%s(%s)' violated")
}
`[1:], ruleMinMaxOperator(maxLengthRule.name), maxLengthRule.value, jsonName(f), maxLengthRule.name, maxLengthRule.value)
Expand Down
11 changes: 9 additions & 2 deletions input/elasticapm/internal/modeldecoder/generator/nstring.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,17 @@ if val.%s.Val != ""{

func nstringRuleMinMax(w io.Writer, f structField, rule validationRule) {
fmt.Fprintf(w, `
if val.%s.IsSet() && utf8.RuneCountInString(val.%s.Val) %s %s{
if val.%s.IsSet() && %sutf8.RuneCountInString(val.%s.Val) %s %s{
return fmt.Errorf("'%s': validation rule '%s(%s)' violated")
}
`[1:], f.Name(), f.Name(), ruleMinMaxOperator(rule.name), rule.value, jsonName(f), rule.name, rule.value)
`[1:], f.Name(), maxIfEnabled(ruleMinMaxOperator(rule.name)), f.Name(), ruleMinMaxOperator(rule.name), rule.value, jsonName(f), rule.name, rule.value)
}

func maxIfEnabled(operator string) string {
if operator == ">" {
return "enableFieldMaxLength && "
}
return ""
}

func nstringRulePattern(w io.Writer, f structField, rule validationRule) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func sliceRuleMinMaxLength(w io.Writer, f structField, rule validationRule) erro
if basic.Kind() == types.String {
fmt.Fprintf(w, `
for _, elem := range val.%s{
if utf8.RuneCountInString(elem) %s %s{
if enableFieldMaxLength && utf8.RuneCountInString(elem) %s %s{
return fmt.Errorf("'%s': validation rule '%s(%s)' violated")
}
}
Expand Down
8 changes: 8 additions & 0 deletions input/elasticapm/internal/modeldecoder/rumv3/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ import (
"github.com/elastic/apm-data/model/modelpb"
)

var (
enableFieldMaxLength = true
)

func SetFieldMaxLength(flag bool) {
enableFieldMaxLength = flag
}

// DecodeNestedMetadata decodes metadata from d, updating out.
func DecodeNestedMetadata(d decoder.Decoder, out *modelpb.APMEvent) error {
root := &metadataRoot{}
Expand Down
Loading
Loading