Add indexFormat field for date formatted indices

Date formatted index names are a common pattern in Elastic setups. This
commit as a config field to specify a string pattern for index names.
The pattern is searched for sequenences inside {curly braces} and passes
any text found plus the current time through the Go time formatting function.
This commit is contained in:
Aaron Brodersen
2020-01-24 11:38:38 -06:00
parent 7defd7d073
commit bbb985f8c9
+33 -1
View File
@@ -4,6 +4,9 @@ import (
"bytes"
"context"
"encoding/json"
"regexp"
"strings"
"time"
"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
"github.com/opsgenie/kubernetes-event-exporter/pkg/kube"
@@ -19,6 +22,7 @@ type ElasticsearchConfig struct {
// Indexing preferences
UseEventID bool `yaml:"useEventID"`
Index string `yaml:"index"`
IndexFormat string `yaml:"indexFormat"`
}
func NewElasticsearch(cfg *ElasticsearchConfig) (*Elasticsearch, error) {
@@ -44,15 +48,43 @@ type Elasticsearch struct {
cfg *ElasticsearchConfig
}
var regex = regexp.MustCompile(`(?s){(.*)}`)
func formatIndexName(pattern string, when time.Time) string {
m := regex.FindAllStringSubmatchIndex(pattern, -1)
current := 0;
var builder strings.Builder
for i := 0; i < len(m); i++ {
pair := m[i]
builder.WriteString(pattern[current:pair[0]])
builder.WriteString(when.Format(pattern[pair[0]+1:pair[1]-1]))
current = pair[1]
}
builder.WriteString(pattern[current:])
return builder.String()
}
func (e *Elasticsearch) Send(ctx context.Context, ev *kube.EnhancedEvent) error {
b, err := json.Marshal(ev)
if err != nil {
return err
}
var index string
if len(e.cfg.IndexFormat) > 0 {
now := time.Now()
index = formatIndexName(e.cfg.IndexFormat, now)
} else {
index = e.cfg.Index
}
req := esapi.IndexRequest{
Body: bytes.NewBuffer(b),
Index: e.cfg.Index,
Index: index,
}
if e.cfg.UseEventID {