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: add custom time/date format field for elasticsearch_query #9838

Merged
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
8 changes: 8 additions & 0 deletions plugins/inputs/elasticsearch_query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ Currently it is known to break on 7.x or greater versions.
## The date/time field in the Elasticsearch index (mandatory).
date_field = "@timestamp"

## If the field used for the date/time field in Elasticsearch is also using
## a custom date/time format it may be required to provide the format to
## correctly parse the field.
##
## If using one of the built in elasticsearch formats this is not required.
# date_field_custom_format = ""

## Time window to query (eg. "1m" to query documents from last minute).
## Normally should be set to same as collection interval
query_period = "1m"
Expand Down Expand Up @@ -150,6 +157,7 @@ Please note that the `[[inputs.elasticsearch_query]]` is still required for all

### Optional parameters

- `date_field_custom_format`: Not needed if using one of the built in date/time formats of Elasticsearch, but may be required if using a custom date/time format.
- `filter_query`: Lucene query to filter the results (default: "\*")
- `metric_fields`: The list of fields to perform metric aggregation (these must be indexed as numeric fields)
- `metric_funcion`: The single-value metric aggregation function to be performed on the `metric_fields` defined. Currently supported aggregations are "avg", "min", "max", "sum". (see [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics.html)
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/elasticsearch_query/aggregation_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (e *ElasticsearchQuery) runAggregationQuery(ctx context.Context, aggregatio

query := elastic5.NewBoolQuery()
query = query.Filter(elastic5.NewQueryStringQuery(filterQuery))
query = query.Filter(elastic5.NewRangeQuery(aggregation.DateField).From(from).To(now))
query = query.Filter(elastic5.NewRangeQuery(aggregation.DateField).From(from).To(now).Format(aggregation.DateFieldFormat))

src, err := query.Source()
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions plugins/inputs/elasticsearch_query/elasticsearch_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ const sampleConfig = `
## The date/time field in the Elasticsearch index (mandatory).
date_field = "@timestamp"

## If the field used for the date/time field in Elasticsearch is also using
## a custom date/time format it may be required to provide the format to
## correctly parse the field.
##
## If using one of the built in elasticsearch formats this is not required.
# date_field_custom_format = ""

## Time window to query (eg. "1m" to query documents from last minute).
## Normally should be set to same as collection interval
query_period = "1m"
Expand Down Expand Up @@ -104,6 +111,7 @@ type esAggregation struct {
Index string `toml:"index"`
MeasurementName string `toml:"measurement_name"`
DateField string `toml:"date_field"`
DateFieldFormat string `toml:"date_field_custom_format"`
QueryPeriod config.Duration `toml:"query_period"`
FilterQuery string `toml:"filter_query"`
MetricFields []string `toml:"metric_fields"`
Expand Down
32 changes: 32 additions & 0 deletions plugins/inputs/elasticsearch_query/elasticsearch_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,38 @@ var testEsAggregationData = []esAggregationQueryTest{
false,
false,
},
{
"query 14 - custom date/time format",
esAggregation{
Index: testindex,
MeasurementName: "measurement14",
MetricFields: []string{"size"},
FilterQuery: "downloads",
MetricFunction: "max",
DateField: "@timestamp",
DateFieldFormat: "yyyy",
QueryPeriod: queryPeriod,
Tags: []string{},
mapMetricFields: map[string]string{"size": "long"},
},
[]aggregationQueryData{
{
aggKey: aggKey{measurement: "measurement14", name: "size_max", function: "max", field: "size"},
isParent: true,
},
},
[]telegraf.Metric{
testutil.MustMetric(
"measurement14",
map[string]string{},
map[string]interface{}{"size_max": float64(3318)},
time.Date(2018, 6, 14, 5, 51, 53, 266176036, time.UTC),
),
},
false,
false,
false,
},
}

func setupIntegrationTest() error {
Expand Down