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

[Metricbeat] Convert millis-since-epoch timestamps in elasticsearch/ml_job metricset to ints #14222

Merged
merged 5 commits into from
Oct 28, 2019
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Change kubernetes.event.message to text. {pull}13964[13964]
- Fix performance counter values for windows/perfmon metricset. {issue}14036[14036] {pull}14039[14039]
- Add FailOnRequired when applying schema and fix metric names in mongodb metrics metricset. {pull}14143[14143]
- Convert indexed ms-since-epoch timestamp fields in `elasticsearch/ml_job` metricset to ints from float64s. {issue}14220[14220] {pull}14222[14222]

*Packetbeat*

Expand Down
21 changes: 21 additions & 0 deletions metricbeat/helper/elastic/elastic.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,24 @@ func ReportAndLogError(err error, r mb.ReporterV2, l *logp.Logger) {
r.Error(err)
l.Error(err)
}

// FixTimestampField converts the given timestamp field in the given map from a float64 to an
// int, so that it is not serialized in scientific notation in the event. This is because
// Elasticsearch cannot accepts scientific notation to represent millis-since-epoch values
// for it's date fields: https://github.com/elastic/elasticsearch/pull/36691
func FixTimestampField(m common.MapStr, field string) error {
v, err := m.GetValue(field)
if err == common.ErrKeyNotFound {
return nil
}
if err != nil {
return err
}

switch vv := v.(type) {
case float64:
_, err := m.Put(field, int(vv))
return err
}
return nil
}
53 changes: 53 additions & 0 deletions metricbeat/helper/elastic/elastic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,56 @@ func TestReportErrorForMissingField(t *testing.T) {
assert.Equal(t, expectedError, err)
assert.Equal(t, expectedError, currentErr)
}

func TestFixTimestampField(t *testing.T) {
tests := []struct {
Name string
OriginalValue map[string]interface{}
ExpectedValue map[string]interface{}
}{
{
"converts float64s in scientific notation to ints",
map[string]interface{}{
"foo": 1.571284349E12,
},
map[string]interface{}{
"foo": 1571284349000,
},
},
{
"converts regular notation float64s to ints",
map[string]interface{}{
"foo": float64(1234),
},
map[string]interface{}{
"foo": 1234,
},
},
{
"ignores missing fields",
map[string]interface{}{
"bar": 12345,
},
map[string]interface{}{
"bar": 12345,
},
},
{
"leaves strings untouched",
map[string]interface{}{
"foo": "bar",
},
map[string]interface{}{
"foo": "bar",
},
},
}

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
err := FixTimestampField(test.OriginalValue, "foo")
assert.NoError(t, err)
assert.Equal(t, test.ExpectedValue, test.OriginalValue)
})
}
}
13 changes: 11 additions & 2 deletions metricbeat/module/elasticsearch/ml_job/data_xpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,22 @@ func eventsMappingXPack(r mb.ReporterV2, m *MetricSet, info elasticsearch.Info,
}

var errs multierror.Errors
for _, job := range jobsArr {
job, ok = job.(map[string]interface{})
for _, j := range jobsArr {
job, ok := j.(map[string]interface{})
if !ok {
errs = append(errs, fmt.Errorf("job is not a map"))
continue
}

if err := elastic.FixTimestampField(job, "data_counts.earliest_record_timestamp"); err != nil {
errs = append(errs, err)
continue
}
if err := elastic.FixTimestampField(job, "data_counts.latest_record_timestamp"); err != nil {
errs = append(errs, err)
continue
}

event := mb.Event{}
event.RootFields = common.MapStr{
"cluster_uuid": info.ClusterID,
Expand Down