From 669a3770ade9e16000ee7658167eccfd791f87b1 Mon Sep 17 00:00:00 2001 From: Hexilee Date: Fri, 23 Mar 2018 05:05:58 +0800 Subject: [PATCH] Automatically lowercase the index name when custom fields is used to build it --- libbeat/outputs/elasticsearch/client.go | 2 +- libbeat/outputs/elasticsearch/utils.go | 18 ++++++++++++++++++ libbeat/outputs/elasticsearch/utils_test.go | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 libbeat/outputs/elasticsearch/utils.go create mode 100644 libbeat/outputs/elasticsearch/utils_test.go diff --git a/libbeat/outputs/elasticsearch/client.go b/libbeat/outputs/elasticsearch/client.go index 93c3522e74d1..0cec817c3281 100644 --- a/libbeat/outputs/elasticsearch/client.go +++ b/libbeat/outputs/elasticsearch/client.go @@ -390,7 +390,7 @@ func createEventBulkMeta( } meta := bulkEventMeta{ - Index: index, + Index: TryLowercaseIndex(index), DocType: eventType, Pipeline: pipeline, ID: id, diff --git a/libbeat/outputs/elasticsearch/utils.go b/libbeat/outputs/elasticsearch/utils.go new file mode 100644 index 000000000000..4cc923771c4c --- /dev/null +++ b/libbeat/outputs/elasticsearch/utils.go @@ -0,0 +1,18 @@ +package elasticsearch + +import ( + "unicode" + "github.com/elastic/beats/libbeat/logp" + "strings" +) + +func TryLowercaseIndex(index string) string { + for _, u := range []rune(index) { + if unicode.IsUpper(u) { + newIndex := strings.ToLower(index) + logp.Warn("Index name %s is invalid, replace it with %s", index, newIndex) + return newIndex + } + } + return index +} diff --git a/libbeat/outputs/elasticsearch/utils_test.go b/libbeat/outputs/elasticsearch/utils_test.go new file mode 100644 index 000000000000..476d0e731e35 --- /dev/null +++ b/libbeat/outputs/elasticsearch/utils_test.go @@ -0,0 +1,18 @@ +package elasticsearch + +import ( + "testing" + "github.com/stretchr/testify/assert" +) + +func TestTryLowercaseIndex(t *testing.T) { + testCases := map[string]string{ + "ElasticSearch": "elasticsearch", + "ELASTIC": "elastic", + "beats": "beats", + } + + for former, latter := range testCases { + assert.Equal(t, latter, TryLowercaseIndex(former)) + } +}