Add Packet statistics

This commit is contained in:
Philipp Holzer 2024-11-15 14:04:27 +01:00
parent e699b52c5d
commit 12312e6ac9
Signed by: nupplaPhil
GPG key ID: 24A7501396EB5432
2 changed files with 88 additions and 0 deletions

View file

@ -73,6 +73,14 @@ var (
metricPrefix+"posts_outbound",
"Number of posts outbound.",
[]string{"type"}, nil)
packetsInbound = prometheus.NewDesc(
metricPrefix+"packets_inbound",
"Number of packets inbound.",
[]string{"protocol"}, nil)
packetsOutbound = prometheus.NewDesc(
metricPrefix+"packets_outbound",
"Number of packets outbound.",
[]string{"protocol"}, nil)
usersPending = prometheus.NewDesc(
metricPrefix+"users_pending",
"Number of pending users.",
@ -123,6 +131,8 @@ func (c *friendicaCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- usersPending
ch <- postsInbound
ch <- postsOutbound
ch <- packetsInbound
ch <- packetsOutbound
}
func (c *friendicaCollector) Collect(ch chan<- prometheus.Metric) {
@ -179,6 +189,14 @@ func readMetrics(ch chan<- prometheus.Metric, status *serverinfo.ServerInfo) err
return err
}
if err := collectPacketsPerDirection(ch, status, packetsInbound); err != nil {
return err
}
if err := collectPacketsPerDirection(ch, status, packetsOutbound); err != nil {
return err
}
return nil
}
@ -416,3 +434,71 @@ func collectPosts(ch chan<- prometheus.Metric, status *serverinfo.ServerInfo) er
return nil
}
type packetMetrics struct {
desc *prometheus.Desc
value float64
protocol string
}
func collectPacketsPerDirection(ch chan<- prometheus.Metric, status *serverinfo.ServerInfo, desc *prometheus.Desc) error {
var packetInfo serverinfo.PacketCounts
if desc == packetsInbound {
packetInfo = status.Packets.Inbound
} else {
packetInfo = status.Packets.Outbound
}
metrics := []packetMetrics{
{
desc: desc,
value: float64(packetInfo.ActivityPub),
protocol: "apub",
},
{
desc: desc,
value: float64(packetInfo.DFRN),
protocol: "dfrn",
},
{
desc: desc,
value: float64(packetInfo.Feed),
protocol: "feed",
},
{
desc: desc,
value: float64(packetInfo.Diaspora),
protocol: "dspr",
},
{
desc: desc,
value: float64(packetInfo.Mail),
protocol: "mail",
},
{
desc: desc,
value: float64(packetInfo.OStatus),
protocol: "stat",
},
{
desc: desc,
value: float64(packetInfo.Bluesky),
protocol: "bsky",
},
{
desc: desc,
value: float64(packetInfo.Thumblr),
protocol: "tmbl",
},
}
for _, m := range metrics {
metric, err := prometheus.NewConstMetric(m.desc, prometheus.GaugeValue, m.value, m.protocol)
if err != nil {
return fmt.Errorf("error creating metric for %s: %w", m.desc, err)
}
ch <- metric
}
return nil
}

View file

@ -79,6 +79,8 @@ type PacketCounts struct {
OStatus int64 `json:"stat"`
Feed int64 `json:"feed"`
Mail int64 `json:"mail"`
Bluesky int64 `json:"bsky"`
Thumblr int64 `json:"tmbl"`
}
// Packets contains statistics of inbound and outbound packages