Skip to content

Commit a9109ca

Browse files
committed
Implement dynamic index name
Adds %y, %m and %d variable Signed-off-by: Markus Blaschke <mblaschke82@gmail.com>
1 parent 3592d80 commit a9109ca

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Configuration
1818
| `PAGERDUTY_DATE_RANGE` | `168h` | Date range for importing historical data |
1919
| `PAGERDUTY_MAX_CONNECTIONS` | `4` | Maximum numbers of HTTP connections to PagerDuty API |
2020
| `ELASTICSEARCH_ADDRESS` | none, required | ElasticSearch cluster addresses (multiple) |
21-
| `ELASTICSEARCH_INDEX` | `pagerduty` | Name of ElasticSearch index |
21+
| `ELASTICSEARCH_INDEX` | `pagerduty` | Name of ElasticSearch index (placeholders: %y for year, %m for month and %d for day) |
2222
| `ELASTICSEARCH_BATCH_COUNT` | `50` | Number of documents which should be indexed in one request |
2323
| `ELASTICSEARCH_RETRY_COUNT` | `5` | ElasticSearch request retry count |
2424
| `ELASTICSEARCH_RETRY_DELAY` | `5s` | ElasticSearch request delay for reach retry |

exporter.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
esapi "github.com/elastic/go-elasticsearch/v7/esapi"
1010
"github.com/prometheus/client_golang/prometheus"
1111
"net/http"
12+
"strings"
1213
"sync"
1314
"time"
1415
)
@@ -226,33 +227,53 @@ func (e *PagerdutyElasticsearchExporter) runScrape() {
226227
func (e *PagerdutyElasticsearchExporter) indexIncident(incident pagerduty.Incident, callback chan<- *esapi.IndexRequest) {
227228
e.prometheus.incident.WithLabelValues().Inc()
228229

230+
createTime, err := time.Parse(time.RFC3339, incident.CreatedAt)
231+
if err != nil {
232+
panic(err)
233+
}
234+
229235
esIncident := ElasticsearchIncident{
230-
Timestamp: incident.CreatedAt,
236+
Timestamp: createTime.Format(time.RFC3339),
231237
IncidentId: incident.Id,
232238
Incident: &incident,
233239
}
234240
incidentJson, _ := json.Marshal(esIncident)
235241

236242
req := esapi.IndexRequest{
237-
Index: opts.ElasticsearchIndex,
243+
Index: e.buildIndexName(createTime),
238244
DocumentID: fmt.Sprintf("incident-%v", incident.Id),
239245
Body: bytes.NewReader(incidentJson),
240246
}
241247
callback <- &req
242248
}
243249

250+
func (e *PagerdutyElasticsearchExporter) buildIndexName(createTime time.Time) string {
251+
ret := e.elasticsearchIndexName
252+
253+
ret = strings.Replace(ret, "%y", createTime.Format("2006"), -1)
254+
ret = strings.Replace(ret, "%m", createTime.Format("01"), -1)
255+
ret = strings.Replace(ret, "%d", createTime.Format("02"), -1)
256+
257+
return ret
258+
}
259+
244260
func (e *PagerdutyElasticsearchExporter) indexIncidentLogEntry(incident pagerduty.Incident, logEntry pagerduty.LogEntry, callback chan<- *esapi.IndexRequest) {
245261
e.prometheus.incidentLogEntry.WithLabelValues().Inc()
246262

263+
createTime, err := time.Parse(time.RFC3339, logEntry.CreatedAt)
264+
if err != nil {
265+
panic(err)
266+
}
267+
247268
esLogEntry := ElasticsearchIncidentLog{
248-
Timestamp: logEntry.CreatedAt,
269+
Timestamp: createTime.Format(time.RFC3339),
249270
IncidentId: incident.Id,
250271
LogEntry: &logEntry,
251272
}
252273
logEntryJson, _ := json.Marshal(esLogEntry)
253274

254275
req := esapi.IndexRequest{
255-
Index: opts.ElasticsearchIndex,
276+
Index: e.buildIndexName(createTime),
256277
DocumentID: fmt.Sprintf("logentry-%v", logEntry.ID),
257278
Body: bytes.NewReader(logEntryJson),
258279
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ var opts struct {
4242

4343
// ElasticSearch settings
4444
ElasticsearchAddresses []string `long:"elasticsearch.address" env:"ELASTICSEARCH_ADDRESS" delim:" " description:"ElasticSearch urls" required:"true"`
45-
ElasticsearchIndex string `long:"elasticsearch.index" env:"ELASTICSEARCH_INDEX" description:"ElasticSearch index name" default:"pagerduty"`
45+
ElasticsearchIndex string `long:"elasticsearch.index" env:"ELASTICSEARCH_INDEX" description:"ElasticSearch index name (placeholders: %y for year, %m for month and %d for day)" default:"pagerduty"`
4646
ElasticsearchBatchCount int `long:"elasticsearch.batch-count" env:"ELASTICSEARCH_BATCH_COUNT" description:"Number of documents which should be indexed in one request" default:"50"`
4747
ElasticsearchRetryCount int `long:"elasticsearch.retry-count" env:"ELASTICSEARCH_RETRY_COUNT" description:"ElasticSearch request retry count" default:"5"`
4848
ElasticsearchRetryDelay time.Duration `long:"elasticsearch.retry-delay" env:"ELASTICSEARCH_RETRY_DELAY" description:"ElasticSearch request delay for reach retry" default:"5s"`

0 commit comments

Comments
 (0)