-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexporter.go
46 lines (40 loc) · 1021 Bytes
/
exporter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package periskop
import (
"bytes"
"encoding/json"
"net/http"
)
// ErrorExporter exposes collected errors
type ErrorExporter struct {
collector *ErrorCollector
}
// NewErrorExporter creates a new ErrorExporter
func NewErrorExporter(collector *ErrorCollector) ErrorExporter {
return ErrorExporter{
collector: collector,
}
}
func (e *ErrorExporter) export() ([]byte, error) {
payload := e.collector.getAggregatedErrors()
res, err := json.Marshal(payload)
if err != nil {
return []byte{}, err
}
return res, nil
}
// Export exports all collected errors in json format
func (e *ErrorExporter) Export() (string, error) {
res, err := e.export()
return string(res), err
}
// PushToGateway pushes all collected errors to the pushgateway specified by `addr`
func (e *ErrorExporter) PushToGateway(addr string) error {
exportedData, err := e.export()
if err == nil {
_, err := http.Post(addr+"/errors", "application/json", bytes.NewBuffer(exportedData))
if err == nil {
return nil
}
}
return err
}