From cf7c6ca4f45aa0eea5780f34e739dc221f66fb11 Mon Sep 17 00:00:00 2001 From: Jacob Vosmaer Date: Mon, 21 Oct 2019 20:45:45 +0000 Subject: Count streamio method calls --- streamio/stream.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'streamio') diff --git a/streamio/stream.go b/streamio/stream.go index 379556afe..3e90f0e42 100644 --- a/streamio/stream.go +++ b/streamio/stream.go @@ -8,9 +8,22 @@ import ( "io" "os" "strconv" + + "github.com/prometheus/client_golang/prometheus" +) + +var ( + methodCount = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Name: "gitaly_streamio_method_calls_total", + Help: "Usage counters of gitaly streamio methods", + }, []string{"method"}, + ) ) func init() { + prometheus.MustRegister(methodCount) + bufSize64, err := strconv.ParseInt(os.Getenv("GITALY_STREAMIO_WRITE_BUFFER_SIZE"), 10, 32) if err == nil && bufSize64 > 0 { WriteBufferSize = int(bufSize64) @@ -30,7 +43,11 @@ type receiveReader struct { err error } +func countMethod(method string) { methodCount.WithLabelValues(method).Inc() } + func (rr *receiveReader) Read(p []byte) (int, error) { + countMethod("reader.Read") + if len(rr.data) == 0 { rr.data, rr.err = rr.receiver() } @@ -44,6 +61,8 @@ func (rr *receiveReader) Read(p []byte) (int, error) { // WriteTo implements io.WriterTo. func (rr *receiveReader) WriteTo(w io.Writer) (int64, error) { + countMethod("reader.WriteTo") + var written int64 // Deal with left-over state in rr.data and rr.err, if any @@ -93,6 +112,8 @@ type sendWriter struct { } func (sw *sendWriter) Write(p []byte) (int, error) { + countMethod("writer.Write") + var sent int for len(p) > 0 { @@ -114,6 +135,8 @@ func (sw *sendWriter) Write(p []byte) (int, error) { // ReadFrom implements io.ReaderFrom. func (sw *sendWriter) ReadFrom(r io.Reader) (int64, error) { + countMethod("writer.ReadFrom") + var nRead int64 buf := make([]byte, WriteBufferSize) -- cgit v1.2.3