Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce p2p sdk metrics #2980

Merged
merged 2 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 20 additions & 85 deletions network/p2p/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ var (
_ common.AppHandler = (*Network)(nil)
_ NodeSampler = (*peerSampler)(nil)

opLabel = "op"
handlerLabel = "handlerID"
labelNames = []string{handlerLabel}
labelNames = []string{opLabel, handlerLabel}
)

// ClientOption configures Client
Expand Down Expand Up @@ -62,93 +63,27 @@ func NewNetwork(
namespace string,
) (*Network, error) {
metrics := metrics{
appRequestTime: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "app_request_time",
Help: "app request time (ns)",
}, labelNames),
appRequestCount: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "app_request_count",
Help: "app request count (n)",
}, labelNames),
appResponseTime: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "app_response_time",
Help: "app response time (ns)",
}, labelNames),
appResponseCount: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "app_response_count",
Help: "app response count (n)",
}, labelNames),
appRequestFailedTime: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "app_request_failed_time",
Help: "app request failed time (ns)",
}, labelNames),
appRequestFailedCount: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "app_request_failed_count",
Help: "app request failed count (ns)",
}, labelNames),
appGossipTime: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "app_gossip_time",
Help: "app gossip time (ns)",
}, labelNames),
appGossipCount: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "app_gossip_count",
Help: "app gossip count (n)",
}, labelNames),
crossChainAppRequestTime: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "cross_chain_app_request_time",
Help: "cross chain app request time (ns)",
}, labelNames),
crossChainAppRequestCount: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "cross_chain_app_request_count",
Help: "cross chain app request count (n)",
}, labelNames),
crossChainAppResponseTime: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "cross_chain_app_response_time",
Help: "cross chain app response time (ns)",
}, labelNames),
crossChainAppResponseCount: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "cross_chain_app_response_count",
Help: "cross chain app response count (n)",
}, labelNames),
crossChainAppRequestFailedTime: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "cross_chain_app_request_failed_time",
Help: "cross chain app request failed time (ns)",
}, labelNames),
crossChainAppRequestFailedCount: prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: "cross_chain_app_request_failed_count",
Help: "cross chain app request failed count (n)",
}, labelNames),
msgTime: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Name: "msg_time",
Help: "message handling time (ns)",
},
labelNames,
),
msgCount: prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: namespace,
Name: "msg_count",
Help: "message count (n)",
},
labelNames,
),
}

err := utils.Err(
registerer.Register(metrics.appRequestTime),
registerer.Register(metrics.appRequestCount),
registerer.Register(metrics.appResponseTime),
registerer.Register(metrics.appResponseCount),
registerer.Register(metrics.appRequestFailedTime),
registerer.Register(metrics.appRequestFailedCount),
registerer.Register(metrics.appGossipTime),
registerer.Register(metrics.appGossipCount),
registerer.Register(metrics.crossChainAppRequestTime),
registerer.Register(metrics.crossChainAppRequestCount),
registerer.Register(metrics.crossChainAppResponseTime),
registerer.Register(metrics.crossChainAppResponseCount),
registerer.Register(metrics.crossChainAppRequestFailedTime),
registerer.Register(metrics.crossChainAppRequestFailedCount),
registerer.Register(metrics.msgTime),
registerer.Register(metrics.msgCount),
)
if err != nil {
return nil, err
Expand Down
207 changes: 67 additions & 140 deletions network/p2p/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,24 @@ type meteredHandler struct {
}

type metrics struct {
appRequestTime *prometheus.CounterVec
appRequestCount *prometheus.CounterVec
appResponseTime *prometheus.CounterVec
appResponseCount *prometheus.CounterVec
appRequestFailedTime *prometheus.CounterVec
appRequestFailedCount *prometheus.CounterVec
appGossipTime *prometheus.CounterVec
appGossipCount *prometheus.CounterVec
crossChainAppRequestTime *prometheus.CounterVec
crossChainAppRequestCount *prometheus.CounterVec
crossChainAppResponseTime *prometheus.CounterVec
crossChainAppResponseCount *prometheus.CounterVec
crossChainAppRequestFailedTime *prometheus.CounterVec
crossChainAppRequestFailedCount *prometheus.CounterVec
msgTime *prometheus.CounterVec
msgCount *prometheus.CounterVec
}

func (m *metrics) observe(labels prometheus.Labels, start time.Time) error {
metricTime, err := m.msgTime.GetMetricWith(labels)
if err != nil {
return err
}

metricCount, err := m.msgCount.GetMetricWith(labels)
if err != nil {
return err
}

metricTime.Add(float64(time.Since(start)))
metricCount.Inc()
return nil
}

// router routes incoming application messages to the corresponding registered
Expand Down Expand Up @@ -139,24 +143,13 @@ func (r *router) AppRequest(ctx context.Context, nodeID ids.NodeID, requestID ui
return err
}

labels := prometheus.Labels{
handlerLabel: handlerID,
}

metricCount, err := r.metrics.appRequestCount.GetMetricWith(labels)
if err != nil {
return err
}

metricTime, err := r.metrics.appRequestTime.GetMetricWith(labels)
if err != nil {
return err
}

metricCount.Inc()
metricTime.Add(float64(time.Since(start)))

return nil
return r.metrics.observe(
prometheus.Labels{
opLabel: message.AppRequestOp.String(),
handlerLabel: handlerID,
},
start,
)
}

// AppRequestFailed routes an AppRequestFailed message to the callback
Expand All @@ -174,24 +167,13 @@ func (r *router) AppRequestFailed(ctx context.Context, nodeID ids.NodeID, reques

pending.callback(ctx, nodeID, nil, appErr)

labels := prometheus.Labels{
handlerLabel: pending.handlerID,
}

metricCount, err := r.metrics.appRequestFailedCount.GetMetricWith(labels)
if err != nil {
return err
}

metricTime, err := r.metrics.appRequestFailedTime.GetMetricWith(labels)
if err != nil {
return err
}

metricCount.Inc()
metricTime.Add(float64(time.Since(start)))

return nil
return r.metrics.observe(
prometheus.Labels{
opLabel: message.AppErrorOp.String(),
handlerLabel: pending.handlerID,
},
start,
)
}

// AppResponse routes an AppResponse message to the callback corresponding to
Expand All @@ -209,24 +191,13 @@ func (r *router) AppResponse(ctx context.Context, nodeID ids.NodeID, requestID u

pending.callback(ctx, nodeID, response, nil)

labels := prometheus.Labels{
handlerLabel: pending.handlerID,
}

metricCount, err := r.metrics.appResponseCount.GetMetricWith(labels)
if err != nil {
return err
}

metricTime, err := r.metrics.appResponseTime.GetMetricWith(labels)
if err != nil {
return err
}

metricCount.Inc()
metricTime.Add(float64(time.Since(start)))

return nil
return r.metrics.observe(
prometheus.Labels{
opLabel: message.AppResponseOp.String(),
handlerLabel: pending.handlerID,
},
start,
)
}

// AppGossip routes an AppGossip message to a Handler based on the handler
Expand All @@ -248,24 +219,13 @@ func (r *router) AppGossip(ctx context.Context, nodeID ids.NodeID, gossip []byte

handler.AppGossip(ctx, nodeID, parsedMsg)

labels := prometheus.Labels{
handlerLabel: handlerID,
}

metricCount, err := r.metrics.appGossipCount.GetMetricWith(labels)
if err != nil {
return err
}

metricTime, err := r.metrics.appGossipTime.GetMetricWith(labels)
if err != nil {
return err
}

metricCount.Inc()
metricTime.Add(float64(time.Since(start)))

return nil
return r.metrics.observe(
prometheus.Labels{
opLabel: message.AppGossipOp.String(),
handlerLabel: handlerID,
},
start,
)
}

// CrossChainAppRequest routes a CrossChainAppRequest message to a Handler
Expand Down Expand Up @@ -298,24 +258,13 @@ func (r *router) CrossChainAppRequest(
return err
}

labels := prometheus.Labels{
handlerLabel: handlerID,
}

metricCount, err := r.metrics.crossChainAppRequestCount.GetMetricWith(labels)
if err != nil {
return err
}

metricTime, err := r.metrics.crossChainAppRequestTime.GetMetricWith(labels)
if err != nil {
return err
}

metricCount.Inc()
metricTime.Add(float64(time.Since(start)))

return nil
return r.metrics.observe(
prometheus.Labels{
opLabel: message.CrossChainAppRequestOp.String(),
handlerLabel: handlerID,
},
start,
)
}

// CrossChainAppRequestFailed routes a CrossChainAppRequestFailed message to
Expand All @@ -333,24 +282,13 @@ func (r *router) CrossChainAppRequestFailed(ctx context.Context, chainID ids.ID,

pending.callback(ctx, chainID, nil, appErr)

labels := prometheus.Labels{
handlerLabel: pending.handlerID,
}

metricCount, err := r.metrics.crossChainAppRequestFailedCount.GetMetricWith(labels)
if err != nil {
return err
}

metricTime, err := r.metrics.crossChainAppRequestFailedTime.GetMetricWith(labels)
if err != nil {
return err
}

metricCount.Inc()
metricTime.Add(float64(time.Since(start)))

return nil
return r.metrics.observe(
prometheus.Labels{
opLabel: message.CrossChainAppErrorOp.String(),
handlerLabel: pending.handlerID,
},
start,
)
}

// CrossChainAppResponse routes a CrossChainAppResponse message to the callback
Expand All @@ -368,24 +306,13 @@ func (r *router) CrossChainAppResponse(ctx context.Context, chainID ids.ID, requ

pending.callback(ctx, chainID, response, nil)

labels := prometheus.Labels{
handlerLabel: pending.handlerID,
}

metricCount, err := r.metrics.crossChainAppResponseCount.GetMetricWith(labels)
if err != nil {
return err
}

metricTime, err := r.metrics.crossChainAppResponseTime.GetMetricWith(labels)
if err != nil {
return err
}

metricCount.Inc()
metricTime.Add(float64(time.Since(start)))

return nil
return r.metrics.observe(
prometheus.Labels{
opLabel: message.CrossChainAppResponseOp.String(),
handlerLabel: pending.handlerID,
},
start,
)
}

// Parse parses a gossip or request message and maps it to a corresponding
Expand Down
Loading