202 lines
7.3 KiB
Go
202 lines
7.3 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/yitsushi/go-misskey/services/notes/timeline"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func collectJobqueue() {
|
|
for {
|
|
time.Sleep(1 * time.Second)
|
|
stats, err := client.Admin().Queue().Stats()
|
|
if err != nil {
|
|
logrus.WithField("type", "jobqueue").Warning(err)
|
|
continue
|
|
}
|
|
misskeyJobQueueJobsCount.WithLabelValues("deliver", "waiting").Set(float64(stats.Deliver.Waiting))
|
|
misskeyJobQueueJobsCount.WithLabelValues("deliver", "active").Set(float64(stats.Deliver.Active))
|
|
misskeyJobQueueJobsCount.WithLabelValues("deliver", "completed").Set(float64(stats.Deliver.Completed))
|
|
misskeyJobQueueJobsCount.WithLabelValues("deliver", "failed").Set(float64(stats.Deliver.Failed))
|
|
misskeyJobQueueJobsCount.WithLabelValues("deliver", "delayed").Set(float64(stats.Deliver.Delayed))
|
|
misskeyJobQueueJobsCount.WithLabelValues("deliver", "paused").Set(float64(stats.Deliver.Paused))
|
|
misskeyJobQueueJobsCount.WithLabelValues("inbox", "waiting").Set(float64(stats.Inbox.Waiting))
|
|
misskeyJobQueueJobsCount.WithLabelValues("inbox", "active").Set(float64(stats.Inbox.Active))
|
|
misskeyJobQueueJobsCount.WithLabelValues("inbox", "completed").Set(float64(stats.Inbox.Completed))
|
|
misskeyJobQueueJobsCount.WithLabelValues("inbox", "failed").Set(float64(stats.Inbox.Failed))
|
|
misskeyJobQueueJobsCount.WithLabelValues("inbox", "delayed").Set(float64(stats.Inbox.Delayed))
|
|
misskeyJobQueueJobsCount.WithLabelValues("inbox", "paused").Set(float64(stats.Inbox.Paused))
|
|
misskeyJobQueueJobsCount.WithLabelValues("db", "waiting").Set(float64(stats.DB.Waiting))
|
|
misskeyJobQueueJobsCount.WithLabelValues("db", "active").Set(float64(stats.DB.Active))
|
|
misskeyJobQueueJobsCount.WithLabelValues("db", "completed").Set(float64(stats.DB.Completed))
|
|
misskeyJobQueueJobsCount.WithLabelValues("db", "failed").Set(float64(stats.DB.Failed))
|
|
misskeyJobQueueJobsCount.WithLabelValues("db", "delayed").Set(float64(stats.DB.Delayed))
|
|
misskeyJobQueueJobsCount.WithLabelValues("db", "paused").Set(float64(stats.DB.Paused))
|
|
misskeyJobQueueJobsCount.WithLabelValues("objectstorage", "waiting").Set(float64(stats.ObjectStorage.Waiting))
|
|
misskeyJobQueueJobsCount.WithLabelValues("objectstorage", "active").Set(float64(stats.ObjectStorage.Active))
|
|
misskeyJobQueueJobsCount.WithLabelValues("objectstorage", "completed").Set(float64(stats.ObjectStorage.Completed))
|
|
misskeyJobQueueJobsCount.WithLabelValues("objectstorage", "failed").Set(float64(stats.ObjectStorage.Failed))
|
|
misskeyJobQueueJobsCount.WithLabelValues("objectstorage", "delayed").Set(float64(stats.ObjectStorage.Delayed))
|
|
misskeyJobQueueJobsCount.WithLabelValues("objectstorage", "paused").Set(float64(stats.ObjectStorage.Paused))
|
|
}
|
|
}
|
|
|
|
func collectPing() {
|
|
for {
|
|
time.Sleep(5 * time.Second)
|
|
var buf = bytes.NewBuffer([]byte(`{}`))
|
|
start := time.Now()
|
|
resp, err := http.Post("https://"+endpoint+"/api/ping", "application/json", buf)
|
|
if err != nil {
|
|
logrus.WithField("type", "ping").Warning(err)
|
|
continue
|
|
}
|
|
err = resp.Body.Close()
|
|
if err != nil {
|
|
logrus.WithField("type", "ping").Warning(err)
|
|
continue
|
|
}
|
|
misskeyPingResponseCode.Set(float64(resp.StatusCode))
|
|
t := float64(time.Since(start).Milliseconds()) / 1000
|
|
misskeyPingResponseTime.Observe(t)
|
|
misskeyPingResponseTimeRaw.Set(t)
|
|
misskeyApiResponseTime.WithLabelValues("ping", "").Observe(t)
|
|
misskeyApiResponseTimeRaw.WithLabelValues("ping", "").Set(t)
|
|
}
|
|
}
|
|
|
|
func collectMeta() {
|
|
for {
|
|
time.Sleep(1 * time.Minute)
|
|
meta, err := client.Meta().InstanceMeta(true)
|
|
if err != nil {
|
|
logrus.WithField("type", "meta").Warning(err)
|
|
continue
|
|
}
|
|
misskeyMeta.WithLabelValues(*meta.Version).Set(1)
|
|
}
|
|
}
|
|
|
|
func collectStats() {
|
|
for {
|
|
time.Sleep(30 * time.Second)
|
|
stats, err := client.Meta().Stats()
|
|
if err != nil {
|
|
logrus.WithField("type", "stats").Warning(err)
|
|
continue
|
|
}
|
|
misskeyNotesCount.Set(float64(stats.NotesCount))
|
|
misskeyUsersCount.Set(float64(stats.UsersCount))
|
|
misskeyOriginalNotesCount.Set(float64(stats.OriginalNotesCount))
|
|
misskeyOriginalUsersCount.Set(float64(stats.OriginalUsersCount))
|
|
}
|
|
}
|
|
|
|
type OnlineUsers struct {
|
|
Count int `json:"count"`
|
|
}
|
|
|
|
func collectOnlineUsers() {
|
|
for {
|
|
time.Sleep(1 * time.Minute)
|
|
response, err := http.Get("https://" + endpoint + "/api/get-online-users-count")
|
|
if err != nil {
|
|
logrus.WithField("type", "online-users").Warning(err)
|
|
continue
|
|
}
|
|
if err != nil {
|
|
logrus.WithField("type", "online-users").Warning(err)
|
|
continue
|
|
}
|
|
var onlineUsers OnlineUsers
|
|
|
|
all, err := io.ReadAll(response.Body)
|
|
err = response.Body.Close()
|
|
if err != nil {
|
|
logrus.WithField("type", "online-users").Warning(err)
|
|
continue
|
|
}
|
|
|
|
err = json.Unmarshal(all, &onlineUsers)
|
|
if err != nil {
|
|
logrus.WithField("type", "online-users").Warning(err)
|
|
}
|
|
misskeyOnlineUsers.Set(float64(onlineUsers.Count))
|
|
}
|
|
}
|
|
|
|
func collectTimeline() {
|
|
for {
|
|
time.Sleep(30 * time.Second)
|
|
go func() {
|
|
start := time.Now()
|
|
global, err := client.Notes().Timeline().Global(timeline.GlobalRequest{Limit: 10})
|
|
if err != nil {
|
|
logrus.WithField("type", "timeline").Warning(err)
|
|
return
|
|
}
|
|
t := float64(time.Since(start).Milliseconds()) / 1000
|
|
misskeyGlobalTimelineResponseTime.Observe(t)
|
|
misskeyGlobalTimelineResponseTimeRaw.Set(t)
|
|
misskeyApiResponseTime.WithLabelValues("timeline", "global").Observe(t)
|
|
misskeyApiResponseTimeRaw.WithLabelValues("timeline", "global").Set(t)
|
|
if len(global) != 0 {
|
|
misskeyGlobalTimelineLastNotePublished.Set(float64(global[0].CreatedAt.UnixMilli()))
|
|
}
|
|
}()
|
|
go func() {
|
|
start := time.Now()
|
|
global, err := client.Notes().Timeline().Local(timeline.LocalRequest{Limit: 10})
|
|
if err != nil {
|
|
logrus.WithField("type", "timeline").Warning(err)
|
|
return
|
|
}
|
|
t := float64(time.Since(start).Milliseconds()) / 1000
|
|
misskeyLocalTimelineResponseTime.Observe(t)
|
|
misskeyLocalTimelineResponseTimeRaw.Set(t)
|
|
misskeyApiResponseTime.WithLabelValues("timeline", "local").Observe(t)
|
|
misskeyApiResponseTimeRaw.WithLabelValues("timeline", "local").Set(t)
|
|
if len(global) != 0 {
|
|
misskeyLocalTimelineLastNotePublished.Set(float64(global[0].CreatedAt.UnixMilli()))
|
|
}
|
|
}()
|
|
go func() {
|
|
start := time.Now()
|
|
global, err := client.Notes().Timeline().Get(timeline.GetRequest{Limit: 10})
|
|
if err != nil {
|
|
logrus.WithField("type", "timeline").Warning(err)
|
|
return
|
|
}
|
|
t := float64(time.Since(start).Milliseconds()) / 1000
|
|
misskeyHomeTimelineResponseTime.Observe(t)
|
|
misskeyHomeTimelineResponseTimeRaw.Set(t)
|
|
misskeyApiResponseTime.WithLabelValues("timeline", "home").Observe(t)
|
|
misskeyApiResponseTimeRaw.WithLabelValues("timeline", "home").Set(t)
|
|
if len(global) != 0 {
|
|
misskeyHomeTimelineLastNotePublished.Set(float64(global[0].CreatedAt.UnixMilli()))
|
|
}
|
|
}()
|
|
go func() {
|
|
start := time.Now()
|
|
global, err := client.Notes().Timeline().Hybrid(timeline.HybridRequest{Limit: 10})
|
|
if err != nil {
|
|
logrus.WithField("type", "timeline").Warning(err)
|
|
return
|
|
}
|
|
t := float64(time.Since(start).Milliseconds()) / 1000
|
|
misskeySocialTimelineResponseTime.Observe(t)
|
|
misskeySocialTimelineResponseTimeRaw.Set(t)
|
|
misskeyApiResponseTime.WithLabelValues("timeline", "social").Observe(t)
|
|
misskeyApiResponseTimeRaw.WithLabelValues("timeline", "social").Set(t)
|
|
if len(global) != 0 {
|
|
misskeySocialTimelineLastNotePublished.Set(float64(global[0].CreatedAt.UnixMilli()))
|
|
}
|
|
}()
|
|
|
|
}
|
|
}
|