Add OpenTracing span for report.ReadBinary()

So we can see the timing and size in Jaeger.

Signed-off-by: Bryan Boreham <bjboreham@gmail.com>
This commit is contained in:
Bryan Boreham
2019-05-10 14:26:33 +00:00
parent 72882835d5
commit 711aa66bd5
7 changed files with 20 additions and 12 deletions

View File

@@ -30,7 +30,7 @@ func readReportFiles(b *testing.B, path string) []report.Report {
if info.IsDir() {
return nil
}
rpt, err := report.MakeFromFile(p)
rpt, err := report.MakeFromFile(context.Background(), p)
if err != nil {
return err
}

View File

@@ -278,7 +278,7 @@ func NewFileCollector(path string, window time.Duration) (Collector, error) {
}
timestamps = append(timestamps, t)
rpt, err := report.MakeFromFile(p)
rpt, err := report.MakeFromFile(context.Background(), p)
if err != nil {
return err
}

View File

@@ -83,7 +83,7 @@ func (store *S3Store) fetchReport(ctx context.Context, key string) (*report.Repo
return nil, err
}
defer resp.Body.Close()
return report.MakeFromBinary(resp.Body)
return report.MakeFromBinary(ctx, resp.Body)
}
// StoreReportBytes stores a report.

View File

@@ -140,7 +140,7 @@ func RegisterReportPostHandler(a Adder, router *mux.Router) {
return
}
if err := rpt.ReadBinary(reader, gzipped, handle); err != nil {
if err := rpt.ReadBinary(ctx, reader, gzipped, handle); err != nil {
respondWith(w, http.StatusBadRequest, err)
return
}

View File

@@ -2,6 +2,7 @@
package main
import (
"context"
"flag"
"log"
@@ -15,7 +16,7 @@ func main() {
log.Fatal("usage: copyreport src.(json|msgpack)[.gz] dst.(json|msgpack)[.gz]")
}
rpt, err := report.MakeFromFile(flag.Arg(0))
rpt, err := report.MakeFromFile(context.Background(), flag.Arg(0))
if err != nil {
log.Fatal(err)
}

View File

@@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"compress/gzip"
"context"
"fmt"
"io"
"os"
@@ -11,6 +12,8 @@ import (
"strings"
"sync"
opentracing "github.com/opentracing/opentracing-go"
otlog "github.com/opentracing/opentracing-go/log"
log "github.com/sirupsen/logrus"
"github.com/ugorji/go/codec"
)
@@ -73,7 +76,9 @@ var gzipWriterPool = &sync.Pool{
//
// Will decompress the binary if gzipped is true, and will use the given
// codecHandle to decode it.
func (rep *Report) ReadBinary(r io.Reader, gzipped bool, codecHandle codec.Handle) error {
func (rep *Report) ReadBinary(ctx context.Context, r io.Reader, gzipped bool, codecHandle codec.Handle) error {
span, ctx := opentracing.StartSpanFromContext(ctx, "report.ReadBinary")
defer span.Finish()
var err error
var compressedSize uint64
@@ -106,13 +111,14 @@ func (rep *Report) ReadBinary(r io.Reader, gzipped bool, codecHandle codec.Handl
uncompressedSize,
float32(compressedSize)/float32(uncompressedSize)*100,
)
span.LogFields(otlog.Uint64("compressedSize", compressedSize), otlog.Int64("uncompressedSize", uncompressedSize))
return nil
}
// MakeFromBinary constructs a Report from a gzipped msgpack.
func MakeFromBinary(r io.Reader) (*Report, error) {
func MakeFromBinary(ctx context.Context, r io.Reader) (*Report, error) {
rep := MakeReport()
if err := rep.ReadBinary(r, true, &codec.MsgpackHandle{}); err != nil {
if err := rep.ReadBinary(ctx, r, true, &codec.MsgpackHandle{}); err != nil {
return nil, err
}
return &rep, nil
@@ -153,7 +159,7 @@ func MakeFromBytes(buf []byte) (*Report, error) {
// MakeFromFile construct a Report from a file, with the encoding
// determined by the extension (".msgpack" or ".json", with an
// optional ".gz").
func MakeFromFile(path string) (rpt Report, _ error) {
func MakeFromFile(ctx context.Context, path string) (rpt Report, _ error) {
f, err := os.Open(path)
if err != nil {
return rpt, err
@@ -165,7 +171,7 @@ func MakeFromFile(path string) (rpt Report, _ error) {
return rpt, err
}
err = rpt.ReadBinary(f, gzipped, handle)
err = rpt.ReadBinary(ctx, f, gzipped, handle)
return rpt, err
}

View File

@@ -1,6 +1,7 @@
package report_test
import (
"context"
"reflect"
"testing"
"time"
@@ -14,7 +15,7 @@ func TestRoundtrip(t *testing.T) {
r1 := report.MakeReport()
buf, _ := r1.WriteBinary()
bytes := append([]byte{}, buf.Bytes()...) // copy the contents for later
r2, err := report.MakeFromBinary(buf)
r2, err := report.MakeFromBinary(context.Background(), buf)
if err != nil {
t.Error(err)
}
@@ -71,7 +72,7 @@ func makeTestReport() report.Report {
func TestBiggerRoundtrip(t *testing.T) {
r1 := makeTestReport()
buf, _ := r1.WriteBinary()
r2, err := report.MakeFromBinary(buf)
r2, err := report.MakeFromBinary(context.Background(), buf)
if err != nil {
t.Error(err)
}