From 5770fc2a185bca981389137578e0e47ea233ba52 Mon Sep 17 00:00:00 2001 From: Philipp Date: Fri, 15 Nov 2024 13:43:39 +0100 Subject: [PATCH] Add posts stats --- internal/metrics/collector.go | 55 +++++++++++++++++++++++++++++++++++ serverinfo/serverinfo.go | 3 +- 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/internal/metrics/collector.go b/internal/metrics/collector.go index e32d735..87d37ca 100644 --- a/internal/metrics/collector.go +++ b/internal/metrics/collector.go @@ -65,6 +65,10 @@ var ( metricPrefix+"users_active_half_year", "Number of active users of the last half year.", nil, nil) + posts = prometheus.NewDesc( + metricPrefix+"posts", + "Number of posts.", + []string{"direction", "type"}, nil) usersPending = prometheus.NewDesc( metricPrefix+"users_pending", "Number of pending users.", @@ -113,6 +117,7 @@ func (c *friendicaCollector) Describe(ch chan<- *prometheus.Desc) { ch <- usersActiveMonth ch <- usersActiveHalfYear ch <- usersPending + ch <- posts } func (c *friendicaCollector) Collect(ch chan<- prometheus.Metric) { @@ -165,6 +170,10 @@ func readMetrics(ch chan<- prometheus.Metric, status *serverinfo.ServerInfo) err return err } + if err := collectPosts(ch, status); err != nil { + return err + } + return nil } @@ -361,3 +370,49 @@ func collectLastExecutions(ch chan<- prometheus.Metric, status *serverinfo.Serve return nil } + +type postMetric struct { + desc *prometheus.Desc + value float64 + direction string + postType string +} + +func collectPosts(ch chan<- prometheus.Metric, status *serverinfo.ServerInfo) error { + metrics := []postMetric{ + { + desc: posts, + value: float64(status.Posts.Inbound.Posts), + direction: "inbound", + postType: "posts", + }, + { + desc: posts, + value: float64(status.Posts.Inbound.Comments), + direction: "inbound", + postType: "comments", + }, + { + desc: posts, + value: float64(status.Posts.Outbound.Posts), + direction: "outbound", + postType: "posts", + }, + { + desc: posts, + value: float64(status.Posts.Outbound.Comments), + direction: "outbound", + postType: "comments", + }, + } + + for _, m := range metrics { + metric, err := prometheus.NewConstMetric(m.desc, prometheus.GaugeValue, m.value, m.direction, m.postType) + 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 8eb9d51..f787a5f 100644 --- a/serverinfo/serverinfo.go +++ b/serverinfo/serverinfo.go @@ -5,6 +5,7 @@ type ServerInfo struct { Cron Cron `json:"cron"` Worker Worker `json:"worker"` Users Users `json:"users"` + Posts Posts `json:"posts"` Packets Packets `json:"packets"` Reports Reports `json:"reports"` Update Update `json:"update"` @@ -45,7 +46,7 @@ type Worker struct { LastExecution DateTimeTimestamp `json:"lastExecution"` JPM JPM `json:"jpm"` Active WorkerCount `json:"active"` - Defferd []int64 `json:"defferd"` + Deferred []int64 `json:"deferrd"` Total WorkerCount `json:"total"` }