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

Cherry-pick #16570 to 7.x: Add support for histogram type in fields.yml #16835

Merged
merged 3 commits into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Add `aws_ec2` provider for autodiscover. {issue}12518[12518] {pull}14823[14823]
- Add monitoring variable `libbeat.config.scans` to distinguish scans of the configuration directory from actual reloads of its contents. {pull}16440[16440]
- Add support for multiple password in redis output. {issue}16058[16058] {pull}16206[16206]
- Add support for Histogram type in fields.yml {pull}16570[16570]
- Remove experimental flag from `setup.template.append_fields` {pull}16576[16576]
- Add `add_cloudfoundry_metadata` processor to annotate events with Cloud Foundry application data. {pull}16621[16621]

Expand Down
2 changes: 1 addition & 1 deletion libbeat/mapping/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func (f *Field) validateType() error {
return geoPointType.validate(f.Format)
case "date_range":
return dateRangeType.validate(f.Format)
case "boolean", "binary", "ip", "alias", "array":
case "boolean", "binary", "ip", "alias", "array", "histogram":
if f.Format != "" {
return fmt.Errorf("no format expected for field %s, found: %s", f.Name, f.Format)
}
Expand Down
17 changes: 17 additions & 0 deletions libbeat/template/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ func (p *Processor) Process(fields mapping.Fields, state *fieldState, output com
indexMapping = p.array(&field)
case "alias":
indexMapping = p.alias(&field)
case "histogram":
indexMapping = p.histogram(&field)
case "group":
indexMapping = common.MapStr{}
if field.Dynamic.Value != nil {
Expand Down Expand Up @@ -287,6 +289,18 @@ func (p *Processor) alias(f *mapping.Field) common.MapStr {
return properties
}

func (p *Processor) histogram(f *mapping.Field) common.MapStr {
// Histograms were introduced in Elasticsearch 7.6, ignore if unsupported
if p.EsVersion.LessThan(common.MustNewVersion("7.6.0")) {
return nil
}

properties := getDefaultProperties(f)
properties["type"] = "histogram"

return properties
}

func (p *Processor) object(f *mapping.Field) common.MapStr {
matchType := func(onlyType string, mt string) string {
if mt != "" {
Expand Down Expand Up @@ -324,6 +338,9 @@ func (p *Processor) object(f *mapping.Field) common.MapStr {
case "byte", "double", "float", "long", "short", "boolean":
dynProperties["type"] = otp.ObjectType
addDynamicTemplate(f, dynProperties, matchType(otp.ObjectType, otp.ObjectTypeMappingType))
case "histogram":
dynProperties["type"] = otp.ObjectType
addDynamicTemplate(f, dynProperties, matchType("*", otp.ObjectTypeMappingType))
}
}

Expand Down
9 changes: 9 additions & 0 deletions libbeat/template/processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestProcessor(t *testing.T) {
pEsVersion2 := &Processor{EsVersion: *common.MustNewVersion("2.0.0")}
pEsVersion64 := &Processor{EsVersion: *common.MustNewVersion("6.4.0")}
pEsVersion63 := &Processor{EsVersion: *common.MustNewVersion("6.3.6")}
pEsVersion76 := &Processor{EsVersion: *common.MustNewVersion("7.6.0")}

tests := []struct {
output common.MapStr
Expand Down Expand Up @@ -300,6 +301,14 @@ func TestProcessor(t *testing.T) {
output: migrationP.alias(&mapping.Field{Type: "alias", AliasPath: "a.f", MigrationAlias: true}),
expected: common.MapStr{"path": "a.f", "type": "alias"},
},
{
output: p.histogram(&mapping.Field{Type: "histogram"}),
expected: nil,
},
{
output: pEsVersion76.histogram(&mapping.Field{Type: "histogram"}),
expected: common.MapStr{"type": "histogram"},
},
}

for _, test := range tests {
Expand Down