From 12312e6ac9212e2015b020b79da59eda09dd1dd1 Mon Sep 17 00:00:00 2001 From: Philipp Date: Fri, 15 Nov 2024 14:04:27 +0100 Subject: [PATCH] Add Packet statistics --- internal/metrics/collector.go | 86 +++++++++++++++++++++++++++++++++++ serverinfo/serverinfo.go | 2 + 2 files changed, 88 insertions(+) diff --git a/internal/metrics/collector.go b/internal/metrics/collector.go index 5e23ff8..8b2f2e6 100644 --- a/internal/metrics/collector.go +++ b/internal/metrics/collector.go @@ -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 +} diff --git a/serverinfo/serverinfo.go b/serverinfo/serverinfo.go index f787a5f..1232c73 100644 --- a/serverinfo/serverinfo.go +++ b/serverinfo/serverinfo.go @@ -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