add Allow metrics

This commit is contained in:
vyzo 2022-02-14 17:20:12 +02:00
parent 4db2cad715
commit 55f8c59909
3 changed files with 73 additions and 2 deletions

View File

@ -8,21 +8,38 @@ import (
// MetricsReporter is an interface for collecting metrics from resource manager actions
type MetricsReporter interface {
// AllowConn is invoked when opening a connection is allowed
AllowConn(dir network.Direction, usefd bool)
// BlockConn is invoked when opening a connection is blocked
BlockConn(dir network.Direction, usefd bool)
// AllowStream is invoked when opening a stream is allowed
AllowStream(p peer.ID, dir network.Direction)
// BlockStream is invoked when opening a stream is blocked
BlockStream(p peer.ID, dir network.Direction)
// AllowPeer is invoked when attaching ac onnection to a peer is allowed
AllowPeer(p peer.ID)
// BlockPeer is invoked when attaching ac onnection to a peer is blocked
BlockPeer(p peer.ID)
// AllowProtocol is invoked when setting the protocol for a stream is allowed
AllowProtocol(proto protocol.ID)
// BlockProtocol is invoked when setting the protocol for a stream is blocked
BlockProtocol(proto protocol.ID)
// BlockedProtocolPeer is invoekd when setting the protocol for a stream is blocked at the per protocol peer scope
BlockProtocolPeer(proto protocol.ID, p peer.ID)
// AllowPService is invoked when setting the protocol for a stream is allowed
AllowService(svc string)
// BlockPService is invoked when setting the protocol for a stream is blocked
BlockService(svc string)
// BlockedServicePeer is invoekd when setting the service for a stream is blocked at the per service peer scope
// BlockedServicePeer is invoked when setting the service for a stream is blocked at the per service peer scope
BlockServicePeer(svc string, p peer.ID)
// BlockMemory is invoked when a memory reservation fails
// AllowMemory is invoked when a memory reservation is allowed
AllowMemory(size int)
// BlockMemory is invoked when a memory reservation is blocked
BlockMemory(size int)
}
@ -38,6 +55,14 @@ func WithMetrics(reporter MetricsReporter) Option {
}
}
func (m *metrics) AllowConn(dir network.Direction, usefd bool) {
if m == nil {
return
}
m.reporter.AllowConn(dir, usefd)
}
func (m *metrics) BlockConn(dir network.Direction, usefd bool) {
if m == nil {
return
@ -46,6 +71,14 @@ func (m *metrics) BlockConn(dir network.Direction, usefd bool) {
m.reporter.BlockConn(dir, usefd)
}
func (m *metrics) AllowStream(p peer.ID, dir network.Direction) {
if m == nil {
return
}
m.reporter.AllowStream(p, dir)
}
func (m *metrics) BlockStream(p peer.ID, dir network.Direction) {
if m == nil {
return
@ -54,6 +87,14 @@ func (m *metrics) BlockStream(p peer.ID, dir network.Direction) {
m.reporter.BlockStream(p, dir)
}
func (m *metrics) AllowPeer(p peer.ID) {
if m == nil {
return
}
m.reporter.AllowPeer(p)
}
func (m *metrics) BlockPeer(p peer.ID) {
if m == nil {
return
@ -62,6 +103,14 @@ func (m *metrics) BlockPeer(p peer.ID) {
m.reporter.BlockPeer(p)
}
func (m *metrics) AllowProtocol(proto protocol.ID) {
if m == nil {
return
}
m.reporter.AllowProtocol(proto)
}
func (m *metrics) BlockProtocol(proto protocol.ID) {
if m == nil {
return
@ -78,6 +127,14 @@ func (m *metrics) BlockProtocolPeer(proto protocol.ID, p peer.ID) {
m.reporter.BlockProtocolPeer(proto, p)
}
func (m *metrics) AllowService(svc string) {
if m == nil {
return
}
m.reporter.AllowService(svc)
}
func (m *metrics) BlockService(svc string) {
if m == nil {
return
@ -94,6 +151,14 @@ func (m *metrics) BlockServicePeer(svc string, p peer.ID) {
m.reporter.BlockServicePeer(svc, p)
}
func (m *metrics) AllowMemory(size int) {
if m == nil {
return
}
m.reporter.AllowMemory(size)
}
func (m *metrics) BlockMemory(size int) {
if m == nil {
return

View File

@ -264,6 +264,7 @@ func (r *resourceManager) OpenConnection(dir network.Direction, usefd bool) (net
return nil, err
}
r.metrics.AllowConn(dir, usefd)
return conn, nil
}
@ -279,6 +280,7 @@ func (r *resourceManager) OpenStream(p peer.ID, dir network.Direction) (network.
return nil, err
}
r.metrics.AllowStream(p, dir)
return stream, nil
}
@ -526,6 +528,7 @@ func (s *connectionScope) SetPeer(p peer.ID) error {
}
s.resourceScope.edges = edges
s.rcmgr.metrics.AllowPeer(p)
return nil
}
@ -583,6 +586,7 @@ func (s *streamScope) SetProtocol(proto protocol.ID) error {
}
s.resourceScope.edges = edges
s.rcmgr.metrics.AllowProtocol(proto)
return nil
}
@ -643,6 +647,7 @@ func (s *streamScope) SetService(svc string) error {
}
s.resourceScope.edges = edges
s.rcmgr.metrics.AllowService(svc)
return nil
}

View File

@ -254,6 +254,7 @@ func (s *resourceScope) ReserveMemory(size int, prio uint8) error {
}
s.trace.ReserveMemory(s.name, prio, int64(size), s.rc.memory)
s.metrics.AllowMemory(size)
return nil
}