Skip to content

Commit

Permalink
Add support for gzip in csv output format
Browse files Browse the repository at this point in the history
Refactor:

- Add closeFn for CSV collector
- Made writeToFile protected

Signed-off-by: thejas <[email protected]>
  • Loading branch information
thejasbabu committed Jul 21, 2020
1 parent 73d5ee7 commit 5fc1307
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 25 deletions.
58 changes: 33 additions & 25 deletions stats/csv/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ package csv

import (
"bytes"
"compress/gzip"
"context"
"encoding/csv"
"fmt"
"io"
"os"
"sort"
"strings"
"sync"
"time"

Expand All @@ -40,7 +41,7 @@ import (

// Collector saving output to csv implements the lib.Collector interface
type Collector struct {
outfile io.WriteCloser
closeFn func() error
fname string
resTags []string
ignoredTags []string
Expand All @@ -55,13 +56,6 @@ type Collector struct {
// Verify that Collector implements lib.Collector
var _ lib.Collector = &Collector{}

// Similar to ioutil.NopCloser, but for writers
type nopCloser struct {
io.Writer
}

func (nopCloser) Close() error { return nil }

// New Creates new instance of CSV collector
func New(fs afero.Fs, tags stats.TagSet, config Config) (*Collector, error) {
resTags := []string{}
Expand All @@ -80,32 +74,43 @@ func New(fs afero.Fs, tags stats.TagSet, config Config) (*Collector, error) {
fname := config.FileName.String

if fname == "" || fname == "-" {
logfile := nopCloser{os.Stdout}
return &Collector{
outfile: logfile,
fname: "-",
resTags: resTags,
ignoredTags: ignoredTags,
csvWriter: csv.NewWriter(logfile),
csvWriter: csv.NewWriter(os.Stdout),
row: make([]string, 3+len(resTags)+1),
saveInterval: saveInterval,
closeFn: func() error { return nil },
}, nil
}

logfile, err := fs.Create(fname)
logFile, err := fs.Create(fname)
if err != nil {
return nil, err
}

return &Collector{
outfile: logfile,
c := Collector{
fname: fname,
resTags: resTags,
ignoredTags: ignoredTags,
csvWriter: csv.NewWriter(logfile),
row: make([]string, 3+len(resTags)+1),
saveInterval: saveInterval,
}, nil
}

if strings.HasSuffix(fname, ".gz") {
outfile := gzip.NewWriter(logFile)
c.csvWriter = csv.NewWriter(outfile)
c.closeFn = func() error {
_ = outfile.Close()
return logFile.Close()
}
} else {
c.csvWriter = csv.NewWriter(logFile)
c.closeFn = logFile.Close
}

return &c, nil
}

// Init writes column names to csv file
Expand All @@ -125,16 +130,19 @@ func (c *Collector) SetRunStatus(status lib.RunStatus) {}
// Run just blocks until the context is done
func (c *Collector) Run(ctx context.Context) {
ticker := time.NewTicker(c.saveInterval)
defer func() {
err := c.closeFn()
if err != nil {
logrus.WithField("filename", c.fname).Errorf("CSV: Error closing the file: %v", err)
}
}()

for {
select {
case <-ticker.C:
c.WriteToFile()
c.writeToFile()
case <-ctx.Done():
c.WriteToFile()
err := c.outfile.Close()
if err != nil {
logrus.WithField("filename", c.fname).Error("CSV: Error closing the file")
}
c.writeToFile()
return
}
}
Expand All @@ -149,8 +157,8 @@ func (c *Collector) Collect(scs []stats.SampleContainer) {
}
}

// WriteToFile Writes samples to the csv file
func (c *Collector) WriteToFile() {
// writeToFile Writes samples to the csv file
func (c *Collector) writeToFile() {
c.bufferLock.Lock()
samples := c.buffer
c.buffer = nil
Expand Down
2 changes: 2 additions & 0 deletions stats/csv/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ func TestNew(t *testing.T) {
fname string
resTags []string
ignoredTags []string
closeFn func() error
}{
{
fname: "name",
Expand Down Expand Up @@ -365,6 +366,7 @@ func TestNew(t *testing.T) {
sort.Strings(expected.ignoredTags)
sort.Strings(collector.ignoredTags)
assert.Equal(t, expected.ignoredTags, collector.ignoredTags)
assert.NoError(t, collector.closeFn())
})
}
}
Expand Down

0 comments on commit 5fc1307

Please sign in to comment.