2023-08-16 01:40:00 +00:00
|
|
|
package relay
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/libp2p/go-libp2p/p2p/metricshelper"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/waku-org/go-waku/logging"
|
|
|
|
waku_proto "github.com/waku-org/go-waku/waku/v2/protocol"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
var messages = prometheus.NewCounterVec(
|
|
|
|
prometheus.CounterOpts{
|
|
|
|
Name: "waku_node_messages",
|
|
|
|
Help: "The number of the messages received",
|
|
|
|
},
|
|
|
|
[]string{"pubsubTopic"},
|
|
|
|
)
|
|
|
|
|
|
|
|
var messageSize = prometheus.NewHistogram(prometheus.HistogramOpts{
|
|
|
|
Name: "waku_histogram_message_size",
|
|
|
|
Help: "message size histogram in kB",
|
|
|
|
Buckets: []float64{0.0, 5.0, 15.0, 50.0, 100.0, 300.0, 700.0, 1000.0},
|
|
|
|
})
|
|
|
|
|
2024-01-03 05:41:11 +00:00
|
|
|
var pubsubTopics = prometheus.NewGauge(
|
|
|
|
prometheus.GaugeOpts{
|
|
|
|
Name: "waku_pubsub_topics",
|
|
|
|
Help: "Number of PubSub Topics node is subscribed to",
|
|
|
|
})
|
|
|
|
|
2023-08-16 01:40:00 +00:00
|
|
|
var collectors = []prometheus.Collector{
|
|
|
|
messages,
|
|
|
|
messageSize,
|
2024-01-03 05:41:11 +00:00
|
|
|
pubsubTopics,
|
2023-08-16 01:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Metrics exposes the functions required to update prometheus metrics for relay protocol
|
|
|
|
type Metrics interface {
|
|
|
|
RecordMessage(envelope *waku_proto.Envelope)
|
2024-01-03 05:41:11 +00:00
|
|
|
SetPubSubTopics(int)
|
2023-08-16 01:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type metricsImpl struct {
|
|
|
|
log *zap.Logger
|
|
|
|
reg prometheus.Registerer
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMetrics(reg prometheus.Registerer, logger *zap.Logger) Metrics {
|
|
|
|
metricshelper.RegisterCollectors(reg, collectors...)
|
|
|
|
return &metricsImpl{
|
|
|
|
log: logger,
|
|
|
|
reg: reg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RecordMessage is used to increase the counter for the number of messages received via waku relay
|
|
|
|
func (m *metricsImpl) RecordMessage(envelope *waku_proto.Envelope) {
|
|
|
|
go func() {
|
|
|
|
payloadSizeInBytes := len(envelope.Message().Payload)
|
|
|
|
payloadSizeInKb := float64(payloadSizeInBytes) / 1000
|
|
|
|
messageSize.Observe(payloadSizeInKb)
|
|
|
|
pubsubTopic := envelope.PubsubTopic()
|
|
|
|
messages.WithLabelValues(pubsubTopic).Inc()
|
2024-05-03 16:07:03 +00:00
|
|
|
m.log.Debug("waku.relay received", zap.String("pubsubTopic", pubsubTopic), logging.Hash(envelope.Hash()), zap.Int64("receivedTime", envelope.Index().ReceiverTime), zap.Int("payloadSizeBytes", payloadSizeInBytes))
|
2023-08-16 01:40:00 +00:00
|
|
|
}()
|
|
|
|
}
|
2024-01-03 05:41:11 +00:00
|
|
|
|
|
|
|
func (m *metricsImpl) SetPubSubTopics(size int) {
|
|
|
|
pubsubTopics.Set(float64(size))
|
|
|
|
}
|