mirror of
https://github.com/resmoio/kubernetes-event-exporter.git
synced 2026-08-24 01:06:22 +00:00
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:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user