Skip to content

Commit

Permalink
Add support for histogram type in fields.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Pérez-Aradros Herce committed Feb 25, 2020
1 parent c4671eb commit 0c14840
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Include network information by default on add_host_metadata and add_observer_metadata. {issue}15347[15347] {pull}16077[16077]
- Add `aws_ec2` provider for autodiscover. {issue}12518[12518] {pull}14823[14823]
- 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]

*Auditbeat*

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

0 comments on commit 0c14840

Please sign in to comment.