Skip to content

Commit

Permalink
bitswap/httnet: add response size metric
Browse files Browse the repository at this point in the history
  • Loading branch information
hsanjuan committed Jan 16, 2025
1 parent 016a7e5 commit 65d948d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 8 additions & 0 deletions bitswap/network/httpnet/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (

var durationHistogramBuckets = []float64{0.05, 0.1, 0.25, 0.5, 1, 2, 5, 10, 30, 60, 120, 240, 480, 960, 1920}

var blockSizesHistogramBuckets = []float64{1, 128 << 10, 256 << 10, 512 << 10, 1024 << 10, 2048 << 10, 4092 << 10}

type ctxKeyT string

var ctxKey ctxKeyT = ctxKeyT(imetrics.CtxScopeKey)
Expand Down Expand Up @@ -68,6 +70,10 @@ func requestTime(ctx context.Context) imetrics.Histogram {
return imetrics.NewCtx(ctx, "httpnet_request_duration_seconds", "Histogram of request durations").Histogram(durationHistogramBuckets)
}

func responseSize(ctx context.Context) imetrics.Histogram {
return imetrics.NewCtx(ctx, "httpnet_response_bytes", "Histogram of http response sizes").Histogram(blockSizesHistogramBuckets)
}

type metrics struct {
RequestsInFlight imetrics.Gauge
RequestsTotal imetrics.Counter
Expand All @@ -83,6 +89,7 @@ type metrics struct {
StatusInternalServerError imetrics.Counter
StatusOthers imetrics.Counter
RequestTime imetrics.Histogram
ResponseSize imetrics.Histogram
}

func newMetrics() *metrics {
Expand All @@ -103,6 +110,7 @@ func newMetrics() *metrics {
StatusInternalServerError: statusInternalServerError(ctx),
StatusOthers: statusOthers(ctx),
RequestTime: requestTime(ctx),
ResponseSize: responseSize(ctx),
}
}

Expand Down
2 changes: 1 addition & 1 deletion bitswap/network/httpnet/msg_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (sender *httpMsgSender) tryURL(ctx context.Context, u *senderURL, entry bsm
}
}
reqDuration := time.Since(reqStart)

sender.ht.metrics.ResponseSize.Observe(float64(len(body)))
sender.ht.metrics.RequestsInFlight.Dec()
sender.ht.metrics.RequestTime.Observe(float64(reqDuration) / float64(time.Second))
sender.ht.metrics.updateStatusCounter(resp.StatusCode)
Expand Down

0 comments on commit 65d948d

Please sign in to comment.