Add index by week number to Elasticsearch output (#3490)

This commit is contained in:
Leandro Piccilli 2017-11-20 23:22:29 +01:00 committed by Daniel Nelson
parent db8e767f1f
commit 4d1bc620b2
3 changed files with 19 additions and 5 deletions

View File

@ -172,6 +172,7 @@ This plugin will format the events in the following way:
# %m - month (01..12)
# %d - day of month (e.g., 01)
# %H - hour (00..23)
# %V - week of the year (ISO week) (01..53)
index_name = "telegraf-%Y.%m.%d" # required.
## Optional SSL Config

View File

@ -3,15 +3,16 @@ package elasticsearch
import (
"context"
"fmt"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/outputs"
"gopkg.in/olivere/elastic.v5"
"log"
"net/http"
"strconv"
"strings"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/plugins/outputs"
"gopkg.in/olivere/elastic.v5"
)
type Elasticsearch struct {
@ -58,6 +59,7 @@ var sampleConfig = `
# %m - month (01..12)
# %d - day of month (e.g., 01)
# %H - hour (00..23)
# %V - week of the year (ISO week) (01..53)
index_name = "telegraf-%Y.%m.%d" # required.
## Optional SSL Config
@ -301,6 +303,7 @@ func (a *Elasticsearch) GetIndexName(indexName string, eventTime time.Time) stri
"%m", eventTime.UTC().Format("01"),
"%d", eventTime.UTC().Format("02"),
"%H", eventTime.UTC().Format("15"),
"%V", getISOWeek(eventTime.UTC()),
)
indexName = dateReplacer.Replace(indexName)
@ -310,6 +313,11 @@ func (a *Elasticsearch) GetIndexName(indexName string, eventTime time.Time) stri
}
func getISOWeek(eventTime time.Time) string {
_, week := eventTime.ISOWeek()
return strconv.Itoa(week)
}
func (a *Elasticsearch) SampleConfig() string {
return sampleConfig
}

View File

@ -120,6 +120,11 @@ func TestGetIndexName(t *testing.T) {
"indexname-%y-%m",
"indexname-14-12",
},
{
time.Date(2014, 12, 01, 23, 30, 00, 00, time.UTC),
"indexname-%Y-%V",
"indexname-2014-49",
},
}
for _, test := range tests {
indexName := e.GetIndexName(test.IndexName, test.EventTime)