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

Reduce debug logging when sanitizing documents with multifields #2043

Merged
merged 1 commit into from
Aug 28, 2024
Merged
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
30 changes: 24 additions & 6 deletions internal/fields/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,7 @@ func (v *Validator) validateScalarElement(key string, val any, doc common.MapStr

func (v *Validator) SanitizeSyntheticSourceDocs(docs []common.MapStr) ([]common.MapStr, error) {
var newDocs []common.MapStr
var multifields []string
for _, doc := range docs {
for key, contents := range doc {
shouldBeArray := false
Expand All @@ -726,12 +727,23 @@ func (v *Validator) SanitizeSyntheticSourceDocs(docs []common.MapStr) ([]common.
}
}
}
expandedDoc, err := createDocExpandingObjects(doc)
expandedDoc, newMultifields, err := createDocExpandingObjects(doc, v.Schema)
if err != nil {
return nil, fmt.Errorf("failure while expanding objects from doc: %w", err)
}

newDocs = append(newDocs, expandedDoc)

for _, multifield := range newMultifields {
if slices.Contains(multifields, multifield) {
continue
}
multifields = append(multifields, multifield)
}
}
if len(multifields) > 0 {
sort.Strings(multifields)
logger.Debugf("Some keys were not included in sanitized docs because they are multifields: %s", strings.Join(multifields, ", "))
}
return newDocs, nil
}
Expand All @@ -750,18 +762,19 @@ func (v *Validator) shouldValueBeArray(definition *FieldDefinition) bool {
return false
}

func createDocExpandingObjects(doc common.MapStr) (common.MapStr, error) {
func createDocExpandingObjects(doc common.MapStr, schema []FieldDefinition) (common.MapStr, []string, error) {
keys := make([]string, 0)
for k := range doc {
keys = append(keys, k)
}
sort.Strings(keys)

newDoc := make(common.MapStr)
var multifields []string
for _, k := range keys {
value, err := doc.GetValue(k)
if err != nil {
return nil, fmt.Errorf("not found key %s: %w", k, err)
return nil, nil, fmt.Errorf("not found key %s: %w", k, err)
}

_, err = newDoc.Put(k, value)
Expand All @@ -773,12 +786,17 @@ func createDocExpandingObjects(doc common.MapStr) (common.MapStr, error) {
// - expected map but type is string
// - expected map but type is []any
if strings.HasPrefix(err.Error(), "expected map but type is") {
logger.Debugf("not able to add key %s, is this a multifield?: %s", k, err)
if couldBeMultifield(k, schema) {
// We cannot add multifields and they are not in source documents ignore them.
multifields = append(multifields, k)
continue
}
logger.Warnf("not able to add key %s: %s", k, err)
continue
}
return nil, fmt.Errorf("not added key %s with value %s: %w", k, value, err)
return nil, nil, fmt.Errorf("not added key %s with value %s: %w", k, value, err)
}
return newDoc, nil
return newDoc, multifields, nil
}

// isNumericKeyword is used to identify values that can be numbers in the documents, but are ingested
Expand Down