-
Notifications
You must be signed in to change notification settings - Fork 175
/
localfile.go
60 lines (50 loc) · 1.43 KB
/
localfile.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
package localfile
import (
"compress/gzip"
"context"
"encoding/csv"
"fmt"
"io"
"os"
"time"
"github.com/sirupsen/logrus"
"github.com/stripe/veneur/plugins"
"github.com/stripe/veneur/plugins/s3"
"github.com/stripe/veneur/samplers"
)
var _ plugins.Plugin = &Plugin{}
// Plugin is the LocalFile plugin that we'll use in Veneur
type Plugin struct {
FilePath string
Logger *logrus.Logger
hostname string
interval int
}
// Delimiter defines what kind of delimiter we'll use in the CSV format -- in this case, we want TSV
const Delimiter = '\t'
// Flush the metrics from the LocalFilePlugin
func (p *Plugin) Flush(ctx context.Context, metrics []samplers.InterMetric) error {
f, err := os.OpenFile(p.FilePath, os.O_RDWR|os.O_APPEND|os.O_CREATE, os.ModePerm)
defer f.Close()
if err != nil {
return fmt.Errorf("couldn't open %s for appending: %s", p.FilePath, err)
}
appendToWriter(f, metrics, p.hostname, p.interval)
return nil
}
func appendToWriter(appender io.Writer, metrics []samplers.InterMetric, hostname string, interval int) error {
gzW := gzip.NewWriter(appender)
csvW := csv.NewWriter(gzW)
csvW.Comma = Delimiter
partitionDate := time.Now()
for _, metric := range metrics {
s3.EncodeInterMetricCSV(metric, csvW, &partitionDate, hostname, interval)
}
csvW.Flush()
gzW.Close()
return csvW.Error()
}
// Name is the name of the LocalFilePlugin, i.e., "localfile"
func (p *Plugin) Name() string {
return "localfile"
}