mirror of
https://github.com/weaveworks/scope.git
synced 2026-08-18 03:46:45 +00:00
Helper for reading & writing from binary
This commit is contained in:
@@ -2,7 +2,6 @@ package multitenant
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"crypto/md5"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -18,7 +17,6 @@ import (
|
||||
"github.com/bluele/gcache"
|
||||
"github.com/nats-io/nats"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/ugorji/go/codec"
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/weaveworks/scope/app"
|
||||
@@ -312,15 +310,7 @@ func (c *dynamoDBCollector) getNonCachedReport(reportKey string) (*report.Report
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reader, err := gzip.NewReader(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rep := report.MakeReport()
|
||||
if err := codec.NewDecoder(reader, &codec.MsgpackHandle{}).Decode(&rep); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &rep, nil
|
||||
return report.MakeFromBinary(resp.Body)
|
||||
}
|
||||
|
||||
func (c *dynamoDBCollector) getReports(userid string, row int64, start, end time.Time) ([]report.Report, error) {
|
||||
@@ -387,14 +377,7 @@ func (c *dynamoDBCollector) Add(ctx context.Context, rep report.Report) error {
|
||||
|
||||
// first, encode the report into a buffer and record its size
|
||||
var buf bytes.Buffer
|
||||
writer, err := gzip.NewWriterLevel(&buf, gzip.BestCompression)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := codec.NewEncoder(writer, &codec.MsgpackHandle{}).Encode(&rep); err != nil {
|
||||
return err
|
||||
}
|
||||
writer.Close()
|
||||
rep.WriteBinary(&buf)
|
||||
reportSize.Add(float64(buf.Len()))
|
||||
|
||||
// second, put the report on s3
|
||||
|
||||
@@ -2,9 +2,6 @@ package appclient
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"github.com/ugorji/go/codec"
|
||||
|
||||
"github.com/weaveworks/scope/report"
|
||||
)
|
||||
|
||||
@@ -24,11 +21,6 @@ func NewReportPublisher(publisher Publisher) *ReportPublisher {
|
||||
// Publish serialises and compresses a report, then passes it to a publisher
|
||||
func (p *ReportPublisher) Publish(r report.Report) error {
|
||||
buf := &bytes.Buffer{}
|
||||
gzwriter := gzip.NewWriter(buf)
|
||||
if err := codec.NewEncoder(gzwriter, &codec.MsgpackHandle{}).Encode(r); err != nil {
|
||||
return err
|
||||
}
|
||||
gzwriter.Close() // otherwise the content won't get flushed to the output stream
|
||||
|
||||
r.WriteBinary(buf)
|
||||
return p.publisher.Publish(buf)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package report
|
||||
|
||||
import (
|
||||
"compress/gzip"
|
||||
"io"
|
||||
|
||||
"github.com/ugorji/go/codec"
|
||||
)
|
||||
|
||||
// WriteBinary writes a Report as a gzipped msgpack.
|
||||
func (rep Report) WriteBinary(w io.Writer) error {
|
||||
gzwriter, err := gzip.NewWriterLevel(w, gzip.BestCompression)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err = codec.NewEncoder(gzwriter, &codec.MsgpackHandle{}).Encode(&rep); err != nil {
|
||||
return err
|
||||
}
|
||||
gzwriter.Close() // otherwise the content won't get flushed to the output stream
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadBinary reads into a Report from a gzipped msgpack.
|
||||
func (rep *Report) ReadBinary(r io.Reader) error {
|
||||
reader, err := gzip.NewReader(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := codec.NewDecoder(reader, &codec.MsgpackHandle{}).Decode(&rep); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// MakeFromBinary constructs a Report from a gzipped msgpack.
|
||||
func MakeFromBinary(r io.Reader) (*Report, error) {
|
||||
rep := MakeReport()
|
||||
if err := rep.ReadBinary(r); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &rep, nil
|
||||
}
|
||||
Reference in New Issue
Block a user