Skip to content

Commit

Permalink
add metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
Cory Schwartz committed Apr 21, 2022
1 parent 9558e9f commit 9bf9843
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/lotus-gateway/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ var runCmd = &cli.Command{

// Register all metric views
if err := view.Register(
metrics.ChainNodeViews...,
metrics.GatewayNodeViews...,
); err != nil {
log.Fatalf("Cannot register the view: %v", err)
}
Expand Down
8 changes: 4 additions & 4 deletions gateway/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/ipfs/go-cid"
"go.opencensus.io/stats"
"golang.org/x/time/rate"
"golang.org/x/xerrors"

Expand All @@ -22,6 +23,7 @@ import (
"github.com/filecoin-project/lotus/lib/sigs"
_ "github.com/filecoin-project/lotus/lib/sigs/bls"
_ "github.com/filecoin-project/lotus/lib/sigs/secp"
"github.com/filecoin-project/lotus/metrics"
"github.com/filecoin-project/lotus/node/impl/full"
)

Expand Down Expand Up @@ -165,12 +167,10 @@ func (gw *Node) checkTimestamp(at time.Time) error {
func (gw *Node) limit(ctx context.Context, tokens int) error {
ctx2, cancel := context.WithTimeout(ctx, gw.rateLimitTimeout)
defer cancel()
if !gw.rateLimiter.AllowN(time.Now(), tokens) {
return fmt.Errorf("server busy")
}
err := gw.rateLimiter.WaitN(ctx2, tokens)
if err != nil {
return fmt.Errorf("server busy, cannot complete before timeout %w", err)
stats.Record(ctx, metrics.RateLimitCount.M(1))
return fmt.Errorf("server busy. %w", err)
}
return nil
}
Expand Down
11 changes: 11 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ var (
RcmgrBlockSvcPeer = stats.Int64("rcmgr/block_svc", "Number of blocked blocked streams attached to a service for a specific peer", stats.UnitDimensionless)
RcmgrAllowMem = stats.Int64("rcmgr/allow_mem", "Number of allowed memory reservations", stats.UnitDimensionless)
RcmgrBlockMem = stats.Int64("rcmgr/block_mem", "Number of blocked memory reservations", stats.UnitDimensionless)

// gateway rate limit
RateLimitCount = stats.Int64("ratelimit/limited", "rate limited connections", stats.UnitDimensionless)
)

var (
Expand Down Expand Up @@ -599,6 +602,10 @@ var (
Measure: RcmgrBlockMem,
Aggregation: view.Count(),
}
RateLimitedView = &view.View{
Measure: RateLimitCount,
Aggregation: view.Count(),
}
)

// DefaultViews is an array of OpenCensus views for metric gathering purposes
Expand Down Expand Up @@ -711,6 +718,10 @@ var MinerNodeViews = append([]*view.View{
DagStorePRSeekForwardBytesView,
}, DefaultViews...)

var GatewayNodeViews = append([]*view.View{
RateLimitedView,
}, ChainNodeViews...)

// SinceInMilliseconds returns the duration of time since the provide time as a float64.
func SinceInMilliseconds(startTime time.Time) float64 {
return float64(time.Since(startTime).Nanoseconds()) / 1e6
Expand Down

0 comments on commit 9bf9843

Please sign in to comment.