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

[ES] indices to be created with daywise timestamp #430

Merged
merged 5 commits into from
Nov 9, 2020
Merged
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
26 changes: 17 additions & 9 deletions pkg/notify/elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ package notify
import (
"context"
"fmt"
"time"

"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
Expand All @@ -37,7 +38,7 @@ import (

const (
// indexSuffixFormat is the date format that would be appended to the index name
indexSuffixFormat = "02-01-2006"
indexSuffixFormat = "2006-01-02" // YYYY-MM-DD
// awsService for the AWS client to authenticate against
awsService = "es"
)
Expand Down Expand Up @@ -68,11 +69,17 @@ func NewElasticSearch(c config.ElasticSearch) (Notifier, error) {

signer := v4.NewSigner(creds)
awsClient, err := aws_signing_client.New(signer, nil, awsService, c.AWSSigning.AWSRegion)

if err != nil {
return nil, err
}
elsClient, err = elastic.NewClient(elastic.SetURL(c.Server), elastic.SetScheme("https"), elastic.SetHttpClient(awsClient), elastic.SetSniff(false), elastic.SetHealthcheck(false), elastic.SetGzip(false))
elsClient, err = elastic.NewClient(
elastic.SetURL(c.Server),
elastic.SetScheme("https"),
elastic.SetHttpClient(awsClient),
elastic.SetSniff(false),
elastic.SetHealthcheck(false),
elastic.SetGzip(false),
)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -111,8 +118,10 @@ type index struct {
}

func (e *ElasticSearch) flushIndex(ctx context.Context, event interface{}) error {
// Construct the ELS Index Name with timestamp suffix
indexName := e.Index + "-" + time.Now().Format(indexSuffixFormat)
// Create index if not exists
exists, err := e.ELSClient.IndexExists(e.Index).Do(ctx)
exists, err := e.ELSClient.IndexExists(indexName).Do(ctx)
if err != nil {
log.Error(fmt.Sprintf("Failed to get index. Error:%s", err.Error()))
return err
Expand All @@ -127,25 +136,25 @@ func (e *ElasticSearch) flushIndex(ctx context.Context, event interface{}) error
},
},
}
_, err := e.ELSClient.CreateIndex(e.Index).BodyJson(mapping).Do(ctx)
_, err := e.ELSClient.CreateIndex(indexName).BodyJson(mapping).Do(ctx)
if err != nil {
log.Error(fmt.Sprintf("Failed to create index. Error:%s", err.Error()))
return err
}
}

// Send event to els
_, err = e.ELSClient.Index().Index(e.Index).Type(e.Type).BodyJson(event).Do(ctx)
_, err = e.ELSClient.Index().Index(indexName).Type(e.Type).BodyJson(event).Do(ctx)
if err != nil {
log.Error(fmt.Sprintf("Failed to post data to els. Error:%s", err.Error()))
return err
}
_, err = e.ELSClient.Flush().Index(e.Index).Do(ctx)
_, err = e.ELSClient.Flush().Index(indexName).Do(ctx)
if err != nil {
log.Error(fmt.Sprintf("Failed to flush data to els. Error:%s", err.Error()))
return err
}
log.Debugf("Event successfully sent to ElasticSearch index %s", e.Index)
log.Debugf("Event successfully sent to ElasticSearch index %s", indexName)
return nil
}

Expand All @@ -158,7 +167,6 @@ func (e *ElasticSearch) SendEvent(event events.Event) (err error) {
if err := e.flushIndex(ctx, event); err != nil {
return err
}

return nil
}

Expand Down