Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meassure report sizes #1458

Merged
merged 1 commit into from
May 10, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions app/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package app
import (
"compress/gzip"
"encoding/gob"
"io"
"net/http"
"net/url"
"strings"
"sync"

"github.com/PuerkitoBio/ghost/handlers"
log "github.com/Sirupsen/logrus"
"github.com/gorilla/mux"
"github.com/ugorji/go/codec"
"golang.org/x/net/context"
Expand Down Expand Up @@ -100,23 +102,46 @@ func RegisterTopologyRoutes(router *mux.Router, r Reporter) {
gzipHandler(requestContextDecorator(makeProbeHandler(r))))
}

type byteCounter struct {
next io.ReadCloser
count *uint64
}

func (c byteCounter) Read(p []byte) (n int, err error) {
n, err = c.next.Read(p)
*c.count += uint64(n)
return n, err
}

func (c byteCounter) Close() error {
return c.next.Close()
}

// RegisterReportPostHandler registers the handler for report submission
func RegisterReportPostHandler(a Adder, router *mux.Router) {
post := router.Methods("POST").Subrouter()
post.HandleFunc("/api/report", requestContextDecorator(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
var (
rpt report.Report
reader = r.Body
err error
rpt report.Report
reader = r.Body
err error
compressedSize, uncompressedSize uint64
)

if log.GetLevel() == log.DebugLevel {
reader = byteCounter{next: reader, count: &compressedSize}
}
if strings.Contains(r.Header.Get("Content-Encoding"), "gzip") {
reader, err = gzip.NewReader(r.Body)
reader, err = gzip.NewReader(reader)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}

if log.GetLevel() == log.DebugLevel {
reader = byteCounter{next: reader, count: &uncompressedSize}
}
decoder := gob.NewDecoder(reader).Decode
if strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
decoder = codec.NewDecoder(reader, &codec.JsonHandle{}).Decode
Expand All @@ -128,6 +153,13 @@ func RegisterReportPostHandler(a Adder, router *mux.Router) {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
log.Debugf(
"Received report sizes: compressed %d bytes, uncompressed %d bytes (%.2f%%)",
compressedSize,
uncompressedSize,
float32(compressedSize)/float32(uncompressedSize)*100,
)

a.Add(ctx, rpt)
w.WriteHeader(http.StatusOK)
}))
Expand Down