add metrics

This commit is contained in:
Cory Schwartz 2022-04-20 19:48:41 -07:00
parent 9558e9f0ee
commit 9bf9843ff6
3 changed files with 16 additions and 5 deletions

View File

@ -149,7 +149,7 @@ var runCmd = &cli.Command{
// Register all metric views // Register all metric views
if err := view.Register( if err := view.Register(
metrics.ChainNodeViews..., metrics.GatewayNodeViews...,
); err != nil { ); err != nil {
log.Fatalf("Cannot register the view: %v", err) log.Fatalf("Cannot register the view: %v", err)
} }

View File

@ -6,6 +6,7 @@ import (
"time" "time"
"github.com/ipfs/go-cid" "github.com/ipfs/go-cid"
"go.opencensus.io/stats"
"golang.org/x/time/rate" "golang.org/x/time/rate"
"golang.org/x/xerrors" "golang.org/x/xerrors"
@ -22,6 +23,7 @@ import (
"github.com/filecoin-project/lotus/lib/sigs" "github.com/filecoin-project/lotus/lib/sigs"
_ "github.com/filecoin-project/lotus/lib/sigs/bls" _ "github.com/filecoin-project/lotus/lib/sigs/bls"
_ "github.com/filecoin-project/lotus/lib/sigs/secp" _ "github.com/filecoin-project/lotus/lib/sigs/secp"
"github.com/filecoin-project/lotus/metrics"
"github.com/filecoin-project/lotus/node/impl/full" "github.com/filecoin-project/lotus/node/impl/full"
) )
@ -165,12 +167,10 @@ func (gw *Node) checkTimestamp(at time.Time) error {
func (gw *Node) limit(ctx context.Context, tokens int) error { func (gw *Node) limit(ctx context.Context, tokens int) error {
ctx2, cancel := context.WithTimeout(ctx, gw.rateLimitTimeout) ctx2, cancel := context.WithTimeout(ctx, gw.rateLimitTimeout)
defer cancel() defer cancel()
if !gw.rateLimiter.AllowN(time.Now(), tokens) {
return fmt.Errorf("server busy")
}
err := gw.rateLimiter.WaitN(ctx2, tokens) err := gw.rateLimiter.WaitN(ctx2, tokens)
if err != nil { 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 return nil
} }

View File

@ -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) 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) 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) 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 ( var (
@ -599,6 +602,10 @@ var (
Measure: RcmgrBlockMem, Measure: RcmgrBlockMem,
Aggregation: view.Count(), Aggregation: view.Count(),
} }
RateLimitedView = &view.View{
Measure: RateLimitCount,
Aggregation: view.Count(),
}
) )
// DefaultViews is an array of OpenCensus views for metric gathering purposes // DefaultViews is an array of OpenCensus views for metric gathering purposes
@ -711,6 +718,10 @@ var MinerNodeViews = append([]*view.View{
DagStorePRSeekForwardBytesView, DagStorePRSeekForwardBytesView,
}, DefaultViews...) }, DefaultViews...)
var GatewayNodeViews = append([]*view.View{
RateLimitedView,
}, ChainNodeViews...)
// SinceInMilliseconds returns the duration of time since the provide time as a float64. // SinceInMilliseconds returns the duration of time since the provide time as a float64.
func SinceInMilliseconds(startTime time.Time) float64 { func SinceInMilliseconds(startTime time.Time) float64 {
return float64(time.Since(startTime).Nanoseconds()) / 1e6 return float64(time.Since(startTime).Nanoseconds()) / 1e6