-
Notifications
You must be signed in to change notification settings - Fork 175
/
reporter_output.go
184 lines (150 loc) · 3.81 KB
/
reporter_output.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package reporters
import (
"bufio"
"encoding/json"
"fmt"
"os"
"path"
"sync"
"time"
"github.com/rs/zerolog"
)
// TODO janezp: we should be able to swithch the report writer to write to a database.
type ReportWriterFactory interface {
ReportWriter(dataNamespace string) ReportWriter
}
type ReportFileWriterFactory struct {
fileSuffix int32
outputDir string
log zerolog.Logger
}
func NewReportFileWriterFactory(outputDir string, log zerolog.Logger) *ReportFileWriterFactory {
return &ReportFileWriterFactory{
fileSuffix: int32(time.Now().Unix()),
outputDir: outputDir,
log: log,
}
}
func (r *ReportFileWriterFactory) Filename(dataNamespace string) string {
return path.Join(r.outputDir, fmt.Sprintf("%s_%d.json", dataNamespace, r.fileSuffix))
}
func (r *ReportFileWriterFactory) ReportWriter(dataNamespace string) ReportWriter {
fn := r.Filename(dataNamespace)
return NewReportFileWriter(fn, r.log)
}
var _ ReportWriterFactory = &ReportFileWriterFactory{}
// ReportWriter writes data from reports
type ReportWriter interface {
Write(dataPoint interface{})
Close()
}
// ReportNilWriter does nothing. Can be used as the final fallback writer
type ReportNilWriter struct {
}
var _ ReportWriter = &ReportNilWriter{}
func (r ReportNilWriter) Write(_ interface{}) {
}
func (r ReportNilWriter) Close() {
}
var _ ReportWriter = &ReportFileWriter{}
type ReportFileWriter struct {
f *os.File
fileName string
wg *sync.WaitGroup
writeChan chan interface{}
writer *bufio.Writer
log zerolog.Logger
faulty bool
firstWrite bool
}
const reportFileWriteBufferSize = 100
func NewReportFileWriter(fileName string, log zerolog.Logger) ReportWriter {
f, err := os.Create(fileName)
if err != nil {
log.Warn().Err(err).Msg("Error creating ReportFileWriter, defaulting to ReportNilWriter")
return ReportNilWriter{}
}
writer := bufio.NewWriter(f)
// open a json array
_, err = writer.WriteRune('[')
if err != nil {
log.Warn().Err(err).Msg("Error opening json array")
// time to clean up
err = writer.Flush()
if err != nil {
log.Error().Err(err).Msg("Error closing flushing writer")
panic(err)
}
err = f.Close()
if err != nil {
log.Error().Err(err).Msg("Error closing report file")
panic(err)
}
return ReportNilWriter{}
}
fw := &ReportFileWriter{
f: f,
fileName: fileName,
writer: writer,
log: log,
firstWrite: true,
writeChan: make(chan interface{}, reportFileWriteBufferSize),
wg: &sync.WaitGroup{},
}
fw.wg.Add(1)
go func() {
for d := range fw.writeChan {
fw.write(d)
}
fw.wg.Done()
}()
return fw
}
func (r *ReportFileWriter) Write(dataPoint interface{}) {
r.writeChan <- dataPoint
}
func (r *ReportFileWriter) write(dataPoint interface{}) {
if r.faulty {
return
}
tc, err := json.Marshal(dataPoint)
if err != nil {
r.log.Warn().Err(err).Msg("Error converting data point to json")
r.faulty = true
}
// delimit the json records with commas
if !r.firstWrite {
_, err = r.writer.WriteRune(',')
if err != nil {
r.log.Warn().Err(err).Msg("Error Writing json to file")
r.faulty = true
}
} else {
r.firstWrite = false
}
_, err = r.writer.Write(tc)
if err != nil {
r.log.Warn().Err(err).Msg("Error Writing json to file")
r.faulty = true
}
}
func (r *ReportFileWriter) Close() {
close(r.writeChan)
r.wg.Wait()
_, err := r.writer.WriteRune(']')
if err != nil {
r.log.Warn().Err(err).Msg("Error finishing json array")
// nothing to do, we will be closing the file now
}
err = r.writer.Flush()
if err != nil {
r.log.Error().Err(err).Msg("Error closing flushing writer")
panic(err)
}
err = r.f.Close()
if err != nil {
r.log.Error().Err(err).Msg("Error closing report file")
panic(err)
}
r.log.Info().Str("filename", r.fileName).Msg("Created report file")
}