From f26a98895a4bf503471ce5de07ea662ef0caaf05 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Tue, 14 Jul 2020 16:35:21 -0700 Subject: [PATCH] Add Metrics (Fix: #21) --- networks/tor/BaseOnionService.go | 20 ++++++++++++++++++++ service.go | 6 ++++++ 2 files changed, 26 insertions(+) diff --git a/networks/tor/BaseOnionService.go b/networks/tor/BaseOnionService.go index fe9c2e5..feaf56a 100644 --- a/networks/tor/BaseOnionService.go +++ b/networks/tor/BaseOnionService.go @@ -23,6 +23,26 @@ type BaseOnionService struct { lock sync.Mutex } +// Metrics provides a report of useful information about the status of the service e.g. the number of active +// connections +func (s *BaseOnionService) Metrics() tapir.ServiceMetrics { + s.lock.Lock() + defer s.lock.Unlock() + + count := 0 + s.connections.Range(func(key, value interface{}) bool { + connection := value.(tapir.Connection) + if !connection.IsClosed() { + count++ + } + return true + }) + + return tapir.ServiceMetrics{ + ConnectionCount: count, + } +} + // Init initializes a BaseOnionService with a given private key and identity // The private key is needed to initialize the Onion listen socket, ideally we could just pass an Identity in here. func (s *BaseOnionService) Init(acn connectivity.ACN, sk ed25519.PrivateKey, id *primitives.Identity) { diff --git a/service.go b/service.go index 4534644..0c1a9fd 100644 --- a/service.go +++ b/service.go @@ -12,12 +12,18 @@ import ( "sync" ) +// ServiceMetrics outlines higher level information about the service e.g. counts of connections +type ServiceMetrics struct { + ConnectionCount int +} + // Service defines the interface for a Tapir Service type Service interface { Init(acn connectivity.ACN, privateKey ed25519.PrivateKey, identity *primitives.Identity) Connect(hostname string, application Application) (bool, error) Listen(application Application) error GetConnection(connectionID string) (Connection, error) + Metrics() ServiceMetrics Broadcast(message []byte, capability Capability) error WaitForCapabilityOrClose(connectionID string, capability Capability) (Connection, error) Shutdown()