Skip to content

Commit

Permalink
create a metrics reader, add to PutFile calls to count bytes written
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Nov 24, 2023
1 parent ef6a5bf commit de38983
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
2 changes: 2 additions & 0 deletions zipserver/gcs_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func (c *GcsStorage) PutFileWithSetup(ctx context.Context, bucket, key string, c
return err
}

contents = metricsReader(contents, &globalMetrics.TotalBytesUploaded)

req, err := http.NewRequestWithContext(ctx, http.MethodPut, c.url(bucket, key, "PUT"), contents)
if err != nil {
return err
Expand Down
21 changes: 16 additions & 5 deletions zipserver/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package zipserver

import (
"fmt"
"io"
"net/http"
"os"
"reflect"
Expand All @@ -12,11 +13,12 @@ import (
var globalMetrics = &MetricsCounter{}

type MetricsCounter struct {
TotalRequests atomic.Int64 `metric:"zipserver_requests_total""`
TotalErrors atomic.Int64 `metric:"zipserver_errors_total""`
TotalExtractedFiles atomic.Int64 `metric:"zipserver_extracted_files_total"`
TotalCopiedFiles atomic.Int64 `metric:"zipserver_copied_files_total"`
// TODO: bytes downloaded, bytes uploaded
TotalRequests atomic.Int64 `metric:"zipserver_requests_total""`
TotalErrors atomic.Int64 `metric:"zipserver_errors_total""`
TotalExtractedFiles atomic.Int64 `metric:"zipserver_extracted_files_total"`
TotalCopiedFiles atomic.Int64 `metric:"zipserver_copied_files_total"`
TotalBytesDownloaded atomic.Int64 `metric:"zipserver_downloaded_bytes_total"`
TotalBytesUploaded atomic.Int64 `metric:"zipserver_uploaded_bytes_total"`
}

// render the metrics in a prometheus compatible format
Expand Down Expand Up @@ -44,6 +46,15 @@ func (m *MetricsCounter) RenderMetrics(config *Config) string {
return metrics.String()
}

// wrap a reader to count bytes read into the counter
func metricsReader(reader io.Reader, counter *atomic.Int64) readerClosure {
return func(p []byte) (int, error) {
bytesRead, err := reader.Read(p)
counter.Add(int64(bytesRead))
return bytesRead, err
}
}

// render the global metrics
func metricsHandler(w http.ResponseWriter, r *http.Request) error {
w.Header().Set("Content-Type", "text/plain")
Expand Down
11 changes: 11 additions & 0 deletions zipserver/metrics_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package zipserver

import (
"bytes"
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -19,6 +21,13 @@ func Test_Metrics(t *testing.T) {
metrics.TotalExtractedFiles.Add(1)
assert.Equal(t, int64(1), metrics.TotalExtractedFiles.Load())

// create a temp byte buffer wrapped in metricsReader to test updating BytesDownloaded
buf := bytes.NewBufferString("testing")
reader := metricsReader(buf, &metrics.TotalBytesDownloaded)

// Read from the reader to trigger the metrics update
_, _ = ioutil.ReadAll(reader)

config := &Config{
MetricsHost: "localhost",
}
Expand All @@ -27,6 +36,8 @@ func Test_Metrics(t *testing.T) {
zipserver_errors_total{host="localhost"} 0
zipserver_extracted_files_total{host="localhost"} 1
zipserver_copied_files_total{host="localhost"} 0
zipserver_downloaded_bytes_total{host="localhost"} 7
zipserver_uploaded_bytes_total{host="localhost"} 0
`
assert.Equal(t, expectedMetrics, metrics.RenderMetrics(config))
}
2 changes: 2 additions & 0 deletions zipserver/s3_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func NewS3Storage(config *StorageConfig) (*S3Storage, error) {
func (c *S3Storage) PutFile(ctx context.Context, bucket, key string, contents io.Reader, uploadHeaders http.Header) (string, error) {
uploader := s3manager.NewUploaderWithClient(s3.New(c.Session))

contents = metricsReader(contents, &globalMetrics.TotalBytesUploaded)

hash := md5.New()

// duplicate reads into the md5 hasher
Expand Down

0 comments on commit de38983

Please sign in to comment.