/
writer_es.go
110 lines (86 loc) · 2.48 KB
/
writer_es.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package metric
import (
"bytes"
"fmt"
"github.com/justtrackio/gosoline/pkg/cfg"
"github.com/justtrackio/gosoline/pkg/clock"
"github.com/justtrackio/gosoline/pkg/encoding/json"
"github.com/justtrackio/gosoline/pkg/es"
"github.com/justtrackio/gosoline/pkg/log"
)
type esMetricDatum struct {
*Datum
Namespace string `json:"namespace"`
}
type esWriter struct {
logger log.Logger
clock clock.Clock
client *es.ClientV7
namespace string
}
func NewEsWriter(config cfg.Config, logger log.Logger) (*esWriter, error) {
client, err := es.ProvideClient(config, logger, "metric")
if err != nil {
return nil, fmt.Errorf("can not create es client: %w", err)
}
testClock := clock.NewRealClock()
appId := cfg.GetAppIdFromConfig(config)
namespace := fmt.Sprintf("%s/%s/%s/%s-%s", appId.Project, appId.Environment, appId.Family, appId.Group, appId.Application)
return NewEsWriterWithInterfaces(logger, client, testClock, namespace), nil
}
func NewEsWriterWithInterfaces(logger log.Logger, client *es.ClientV7, clock clock.Clock, namespace string) *esWriter {
return &esWriter{
logger: logger.WithChannel("metrics"),
clock: clock,
client: client,
namespace: namespace,
}
}
func (w esWriter) GetPriority() int {
return PriorityLow
}
func (w esWriter) bulkWriteToES(buf bytes.Buffer) {
batchReader := bytes.NewReader(buf.Bytes())
res, err := w.client.Bulk(batchReader)
if err != nil {
w.logger.Error("could not write metric data to es: %w", err)
return
}
if res.IsError() {
// A successful response might still contain errors for particular documents
w.logger.WithFields(log.Fields{
"status_code": res.StatusCode,
}).Error("not all metrics have been written to es: %w", err)
}
}
func (w esWriter) Write(batch Data) {
var buf bytes.Buffer
if len(batch) == 0 {
return
}
for i := range batch {
if batch[i].Timestamp.IsZero() {
batch[i].Timestamp = w.clock.Now()
}
m := esMetricDatum{
Datum: batch[i],
Namespace: w.namespace,
}
data, err := json.Marshal(m)
if err != nil {
w.logger.Error("could not marshal metric data and write to es: %w", err)
continue
}
index := m.Timestamp.Format("20060102")
buf.Write([]byte(
fmt.Sprintf(`{ "index" : { "_index" : "metrics-%s", "_type" : "_doc" } }%s`, index, "\n"),
))
buf.Write(data)
buf.Write([]byte{10})
}
w.bulkWriteToES(buf)
w.logger.Debug("written %d metric data sets to elasticsearch", len(batch))
}
func (w esWriter) WriteOne(data *Datum) {
w.Write(Data{data})
}