@@ -19,9 +19,11 @@ import (
1919 dssync "github.com/ipfs/go-datastore/sync"
2020 "github.com/ipfs/go-log/v2"
2121 "github.com/libp2p/go-libp2p"
22+ "github.com/libp2p/go-libp2p/core/peer"
2223 "github.com/libp2p/go-libp2p/p2p/host/eventbus"
2324 "github.com/libp2p/go-libp2p/p2p/host/peerstore/pstoremem"
2425 rcmgr "github.com/libp2p/go-libp2p/p2p/host/resource-manager"
26+ "github.com/prometheus/client_golang/prometheus"
2527 "go.uber.org/zap"
2628 "go.uber.org/zap/zapcore"
2729 "golang.zx2c4.com/wireguard/tun"
@@ -30,6 +32,7 @@ import (
3032 "github.com/anywherelan/awl/awldns"
3133 "github.com/anywherelan/awl/awlevent"
3234 "github.com/anywherelan/awl/config"
35+ "github.com/anywherelan/awl/metrics"
3336 "github.com/anywherelan/awl/p2p"
3437 "github.com/anywherelan/awl/protocol"
3538 "github.com/anywherelan/awl/ringbuffer"
@@ -160,6 +163,11 @@ func (a *Application) Init(ctx context.Context, tunDevice tun.Device) error {
160163 }
161164 }
162165
166+ // Metrics
167+ metrics .SetNodeInfo (config .Version , p2pHost .ID ().String ())
168+ cma := & configMetricsAdapter {conf : a .Conf , authStatus : a .AuthStatus , p2p : a .P2p }
169+ go metrics .StartBackgroundUpdater (a .ctx , cma , a .P2p )
170+
163171 a .logger .Info ("Application initialized successfully" )
164172
165173 return nil
@@ -288,6 +296,7 @@ func (a *Application) makeP2pHostConfig() p2p.HostConfig {
288296 libp2p .ResourceManager (mgr ),
289297 libp2p .EnableHolePunching (),
290298 libp2p .NATPortMap (),
299+ libp2p .PrometheusRegisterer (prometheus .DefaultRegisterer ),
291300 }, a .ExtraLibp2pOpts ... ),
292301 ConnManager : struct {
293302 LowWater int
@@ -414,3 +423,38 @@ func (a *DNSService) AwlDNSAddress() string {
414423func (a * DNSService ) IsAwlDNSSetAsSystem () bool {
415424 return a .isAwlDNSSetAsSystem
416425}
426+
427+ // configMetricsAdapter implements metrics.ConfigMetrics by combining Config and AuthStatus.
428+ type configMetricsAdapter struct {
429+ conf * config.Config
430+ authStatus * service.AuthStatus
431+ p2p * p2p.P2p
432+ }
433+
434+ func (a * configMetricsAdapter ) GetKnownPeersSnapshot () (total , confirmed , connected int , peerIDs []peer.ID ) {
435+ a .conf .RLock ()
436+ defer a .conf .RUnlock ()
437+ total = len (a .conf .KnownPeers )
438+ peerIDs = make ([]peer.ID , 0 , total )
439+ for _ , kp := range a .conf .KnownPeers {
440+ pid := kp .PeerId ()
441+ peerIDs = append (peerIDs , pid )
442+ if kp .Confirmed {
443+ confirmed ++
444+ }
445+ if a .p2p .IsConnected (pid ) {
446+ connected ++
447+ }
448+ }
449+ return
450+ }
451+
452+ func (a * configMetricsAdapter ) GetBlockedPeersCount () int {
453+ a .conf .RLock ()
454+ defer a .conf .RUnlock ()
455+ return len (a .conf .BlockedPeers )
456+ }
457+
458+ func (a * configMetricsAdapter ) GetAuthRequestCounts () (ingoing , outgoing int ) {
459+ return a .authStatus .GetAuthRequestCounts ()
460+ }
0 commit comments