|
9 | 9 | esapi "github.com/elastic/go-elasticsearch/v7/esapi" |
10 | 10 | "github.com/prometheus/client_golang/prometheus" |
11 | 11 | "net/http" |
| 12 | + "strings" |
12 | 13 | "sync" |
13 | 14 | "time" |
14 | 15 | ) |
@@ -226,33 +227,53 @@ func (e *PagerdutyElasticsearchExporter) runScrape() { |
226 | 227 | func (e *PagerdutyElasticsearchExporter) indexIncident(incident pagerduty.Incident, callback chan<- *esapi.IndexRequest) { |
227 | 228 | e.prometheus.incident.WithLabelValues().Inc() |
228 | 229 |
|
| 230 | + createTime, err := time.Parse(time.RFC3339, incident.CreatedAt) |
| 231 | + if err != nil { |
| 232 | + panic(err) |
| 233 | + } |
| 234 | + |
229 | 235 | esIncident := ElasticsearchIncident{ |
230 | | - Timestamp: incident.CreatedAt, |
| 236 | + Timestamp: createTime.Format(time.RFC3339), |
231 | 237 | IncidentId: incident.Id, |
232 | 238 | Incident: &incident, |
233 | 239 | } |
234 | 240 | incidentJson, _ := json.Marshal(esIncident) |
235 | 241 |
|
236 | 242 | req := esapi.IndexRequest{ |
237 | | - Index: opts.ElasticsearchIndex, |
| 243 | + Index: e.buildIndexName(createTime), |
238 | 244 | DocumentID: fmt.Sprintf("incident-%v", incident.Id), |
239 | 245 | Body: bytes.NewReader(incidentJson), |
240 | 246 | } |
241 | 247 | callback <- &req |
242 | 248 | } |
243 | 249 |
|
| 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 | + |
244 | 260 | func (e *PagerdutyElasticsearchExporter) indexIncidentLogEntry(incident pagerduty.Incident, logEntry pagerduty.LogEntry, callback chan<- *esapi.IndexRequest) { |
245 | 261 | e.prometheus.incidentLogEntry.WithLabelValues().Inc() |
246 | 262 |
|
| 263 | + createTime, err := time.Parse(time.RFC3339, logEntry.CreatedAt) |
| 264 | + if err != nil { |
| 265 | + panic(err) |
| 266 | + } |
| 267 | + |
247 | 268 | esLogEntry := ElasticsearchIncidentLog{ |
248 | | - Timestamp: logEntry.CreatedAt, |
| 269 | + Timestamp: createTime.Format(time.RFC3339), |
249 | 270 | IncidentId: incident.Id, |
250 | 271 | LogEntry: &logEntry, |
251 | 272 | } |
252 | 273 | logEntryJson, _ := json.Marshal(esLogEntry) |
253 | 274 |
|
254 | 275 | req := esapi.IndexRequest{ |
255 | | - Index: opts.ElasticsearchIndex, |
| 276 | + Index: e.buildIndexName(createTime), |
256 | 277 | DocumentID: fmt.Sprintf("logentry-%v", logEntry.ID), |
257 | 278 | Body: bytes.NewReader(logEntryJson), |
258 | 279 | } |
|
0 commit comments