fix: 接线 Prometheus 指标到连接与投递事件
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
// SampleStoreGauges 按库内真实计数刷新端总数、待投递、定时消息(无对应行则为 0)。
|
||||
func SampleStoreGauges(ctx context.Context, r *Registry, db *sql.DB) error {
|
||||
if r == nil || db == nil {
|
||||
return nil
|
||||
}
|
||||
var endpoints, pending, scheduled int
|
||||
if err := db.QueryRowContext(ctx, `SELECT COUNT(*) FROM endpoints`).Scan(&endpoints); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := db.QueryRowContext(ctx, `SELECT COUNT(*) FROM deliveries WHERE state = 'pending'`).Scan(&pending); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := db.QueryRowContext(ctx, `SELECT COUNT(*) FROM messages WHERE state = 'scheduled'`).Scan(&scheduled); err != nil {
|
||||
return err
|
||||
}
|
||||
r.EndpointsTotal.Set(float64(endpoints))
|
||||
r.DeliveriesPending.Set(float64(pending))
|
||||
r.MessagesScheduled.Set(float64(scheduled))
|
||||
return nil
|
||||
}
|
||||
|
||||
// SampleQueues 刷新写队列与密码哈希排队长度(传入当前真实长度,不做估算)。
|
||||
func SampleQueues(r *Registry, writeQueueLen, passwordHashQueueLen int) {
|
||||
if r == nil {
|
||||
return
|
||||
}
|
||||
r.WriteQueueLength.Set(float64(writeQueueLen))
|
||||
r.PasswordHashQueue.Set(float64(passwordHashQueueLen))
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"context"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
"git.asio.asia/nixevol/NixMsg/internal/store"
|
||||
)
|
||||
|
||||
func TestSampleStoreGaugesPendingNonZero(t *testing.T) {
|
||||
t.Parallel()
|
||||
dir := t.TempDir()
|
||||
db, err := store.Open(filepath.Join(dir, "data"), "FULL")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
|
||||
ctx := context.Background()
|
||||
_, err = db.Write.ExecContext(ctx, `
|
||||
INSERT INTO endpoints(id, name, login_hash, enabled, source, created_at)
|
||||
VALUES ('alice', 'A', 'x', 1, 'admin', 1)`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = db.Write.ExecContext(ctx, `
|
||||
INSERT INTO messages(sender_id, id, dest_kind, dest_id, send_at, keep, ttl_seconds, receipt, state, reason, created_at, content_type, body_enc, meta)
|
||||
VALUES ('alice', 'm1', 'endpoint', 'bob', 100, 1, 0, 0, 'dispatched', '', 1, 'text/plain', 'utf8', '{}')`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var seq int64
|
||||
if err := db.Write.QueryRowContext(ctx, `SELECT seq FROM messages WHERE id='m1'`).Scan(&seq); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = db.Write.ExecContext(ctx, `
|
||||
INSERT INTO deliveries(seq, endpoint_id, send_at, keep, state, reason, attempts, updated_at)
|
||||
VALUES (?, 'bob', 100, 1, 'pending', '', 0, 1)`, seq)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = db.Write.ExecContext(ctx, `
|
||||
INSERT INTO messages(sender_id, id, dest_kind, dest_id, send_at, keep, ttl_seconds, receipt, state, reason, created_at, content_type, body_enc, meta)
|
||||
VALUES ('alice', 'm2', 'endpoint', 'bob', 999999, 0, 0, 0, 'scheduled', '', 1, 'text/plain', 'utf8', '{}')`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
reg := New()
|
||||
if err := SampleStoreGauges(ctx, reg, db.Read); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
SampleQueues(reg, 3, 1)
|
||||
|
||||
mfs, err := reg.Gatherer().Gather()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got := map[string]float64{}
|
||||
for _, mf := range mfs {
|
||||
switch mf.GetName() {
|
||||
case "nixmsg_endpoints", "nixmsg_deliveries_pending", "nixmsg_messages_scheduled",
|
||||
"nixmsg_write_queue_length", "nixmsg_password_hash_queue_length":
|
||||
if len(mf.GetMetric()) > 0 {
|
||||
got[mf.GetName()] = mf.GetMetric()[0].GetGauge().GetValue()
|
||||
}
|
||||
}
|
||||
}
|
||||
if got["nixmsg_endpoints"] != 1 {
|
||||
t.Fatalf("endpoints=%v", got["nixmsg_endpoints"])
|
||||
}
|
||||
if got["nixmsg_deliveries_pending"] != 1 {
|
||||
t.Fatalf("pending=%v", got["nixmsg_deliveries_pending"])
|
||||
}
|
||||
if got["nixmsg_messages_scheduled"] != 1 {
|
||||
t.Fatalf("scheduled=%v", got["nixmsg_messages_scheduled"])
|
||||
}
|
||||
if got["nixmsg_write_queue_length"] != 3 || got["nixmsg_password_hash_queue_length"] != 1 {
|
||||
t.Fatalf("queues=%v", got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user