Skip to content

Commit

Permalink
Merge pull request #461 from ava-labs/connection_metrics
Browse files Browse the repository at this point in the history
add metrics for connect/disconnect events
  • Loading branch information
iansuvak authored Aug 27, 2024
2 parents 60116f5 + e047507 commit df148f8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion peers/app_request_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewNetwork(
}

// Create the handler for handling inbound app responses
handler, err := NewRelayerExternalHandler(logger, prometheus.DefaultRegisterer)
handler, err := NewRelayerExternalHandler(logger, metrics)
if err != nil {
logger.Error(
"Failed to create p2p network handler",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var (
type AppRequestNetworkMetrics struct {
infoAPICallLatencyMS prometheus.Histogram
pChainAPICallLatencyMS prometheus.Histogram
connects prometheus.Counter
disconnects prometheus.Counter
}

func newAppRequestNetworkMetrics(registerer prometheus.Registerer) (*AppRequestNetworkMetrics, error) {
Expand Down Expand Up @@ -40,8 +42,32 @@ func newAppRequestNetworkMetrics(registerer prometheus.Registerer) (*AppRequestN
}
registerer.MustRegister(pChainAPICallLatencyMS)

connects := prometheus.NewCounter(
prometheus.CounterOpts{
Name: "connects",
Help: "Number of connected events",
},
)
if connects == nil {
return nil, ErrFailedToCreateAppRequestNetworkMetrics
}
registerer.MustRegister(connects)

disconnects := prometheus.NewCounter(
prometheus.CounterOpts{
Name: "disconnects",
Help: "Number of disconnected events",
},
)
if disconnects == nil {
return nil, ErrFailedToCreateAppRequestNetworkMetrics
}
registerer.MustRegister(disconnects)

return &AppRequestNetworkMetrics{
infoAPICallLatencyMS: infoAPICallLatencyMS,
pChainAPICallLatencyMS: pChainAPICallLatencyMS,
connects: connects,
disconnects: disconnects,
}, nil
}
8 changes: 6 additions & 2 deletions peers/external_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type RelayerExternalHandler struct {
responseChans map[uint32]chan message.InboundMessage
responsesCount map[uint32]expectedResponses
timeoutManager timer.AdaptiveTimeoutManager
metrics *AppRequestNetworkMetrics
}

// expectedResponses counts the number of responses and compares against the expected number of responses
Expand All @@ -41,7 +42,7 @@ type expectedResponses struct {
// Teleporter application relayer, as well as handle timeouts.
func NewRelayerExternalHandler(
logger logging.Logger,
registerer prometheus.Registerer,
metrics *AppRequestNetworkMetrics,
) (*RelayerExternalHandler, error) {
// TODO: Leaving this static for now, but we may want to have this as a config option
cfg := timer.AdaptiveTimeoutConfig{
Expand All @@ -52,7 +53,7 @@ func NewRelayerExternalHandler(
TimeoutHalflife: constants.DefaultNetworkTimeoutHalflife,
}

timeoutManager, err := timer.NewAdaptiveTimeoutManager(&cfg, registerer)
timeoutManager, err := timer.NewAdaptiveTimeoutManager(&cfg, prometheus.DefaultRegisterer)
if err != nil {
logger.Error(
"Failed to create timeout manager",
Expand All @@ -69,6 +70,7 @@ func NewRelayerExternalHandler(
responseChans: make(map[uint32]chan message.InboundMessage),
responsesCount: make(map[uint32]expectedResponses),
timeoutManager: timeoutManager,
metrics: metrics,
}, nil
}

Expand Down Expand Up @@ -102,13 +104,15 @@ func (h *RelayerExternalHandler) Connected(nodeID ids.NodeID, version *version.A
zap.Stringer("version", version),
zap.Stringer("subnetID", subnetID),
)
h.metrics.connects.Inc()
}

func (h *RelayerExternalHandler) Disconnected(nodeID ids.NodeID) {
h.log.Debug(
"Disconnected",
zap.Stringer("nodeID", nodeID),
)
h.metrics.disconnects.Inc()
}

// RegisterRequestID registers an AppRequest by requestID, and marks the number of
Expand Down

0 comments on commit df148f8

Please sign in to comment.