aboutsummaryrefslogtreecommitdiff
path: root/cmd/mi
diff options
context:
space:
mode:
authorXe Iaso <me@xeiaso.net>2024-05-23 18:00:00 -0400
committerXe Iaso <me@xeiaso.net>2024-05-23 18:00:00 -0400
commit21ba75567eca66b73dc3d4641ab4d5808362bc37 (patch)
tree500acaa307f72512be843f49b5efe88c3e7d637d /cmd/mi
parent54bb74b2dc75af9ec1e34a396382b8558c1ef0a5 (diff)
downloadx-21ba75567eca66b73dc3d4641ab4d5808362bc37.tar.xz
x-21ba75567eca66b73dc3d4641ab4d5808362bc37.zip
cmd/mi: add some metrics
Signed-off-by: Xe Iaso <me@xeiaso.net>
Diffstat (limited to 'cmd/mi')
-rw-r--r--cmd/mi/main.go4
-rw-r--r--cmd/mi/manifest.yaml4
-rw-r--r--cmd/mi/models/blogpost.go1
-rw-r--r--cmd/mi/models/dao.go23
-rw-r--r--cmd/mi/posse.go23
5 files changed, 46 insertions, 9 deletions
diff --git a/cmd/mi/main.go b/cmd/mi/main.go
index 1d71cdb..835ba85 100644
--- a/cmd/mi/main.go
+++ b/cmd/mi/main.go
@@ -8,6 +8,7 @@ import (
"net/http"
"os"
+ "github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/sync/errgroup"
"within.website/x/cmd/mi/models"
"within.website/x/internal"
@@ -67,7 +68,7 @@ func main() {
i := &Importer{db: dao.DB()}
i.Mount(http.DefaultServeMux)
- mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
+ http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
if err := dao.Ping(r.Context()); err != nil {
slog.Error("database not healthy", "err", err)
http.Error(w, "database not healthy", http.StatusInternalServerError)
@@ -76,6 +77,7 @@ func main() {
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "OK")
})
+ http.Handle("/metrics", promhttp.Handler())
g, _ := errgroup.WithContext(ctx)
diff --git a/cmd/mi/manifest.yaml b/cmd/mi/manifest.yaml
index cc4fc95..310d698 100644
--- a/cmd/mi/manifest.yaml
+++ b/cmd/mi/manifest.yaml
@@ -103,7 +103,7 @@ spec:
livenessProbe:
httpGet:
path: /healthz
- port: 8080
+ port: 9195
httpHeaders:
- name: X-Kubernetes
value: "is kinda okay"
@@ -226,4 +226,4 @@ data:
kind: ConfigMap
metadata:
name: mi-front-public-headers
- namespace: ingress-nginx
+ namespace: mi
diff --git a/cmd/mi/models/blogpost.go b/cmd/mi/models/blogpost.go
index ad5bd81..553be3b 100644
--- a/cmd/mi/models/blogpost.go
+++ b/cmd/mi/models/blogpost.go
@@ -17,6 +17,7 @@ type Blogpost struct {
URL string `gorm:"uniqueIndex"` // URL of the blogpost
Title string // title of the blogpost
BodyHTML string // HTML body of the blogpost
+ Image string // URL of the image for the blogpost
PublishedAt time.Time // when the blogpost was published
}
diff --git a/cmd/mi/models/dao.go b/cmd/mi/models/dao.go
index afb3f9e..2d77b98 100644
--- a/cmd/mi/models/dao.go
+++ b/cmd/mi/models/dao.go
@@ -4,19 +4,26 @@ import (
"context"
"crypto/rand"
"errors"
- "log/slog"
- "os"
+ "fmt"
"time"
_ "github.com/ncruces/go-sqlite3/embed"
"github.com/ncruces/go-sqlite3/gormlite"
"github.com/oklog/ulid/v2"
slogGorm "github.com/orandin/slog-gorm"
+ "github.com/prometheus/client_golang/prometheus"
+ "github.com/prometheus/client_golang/prometheus/promauto"
"gorm.io/gorm"
+ gormPrometheus "gorm.io/plugin/prometheus"
)
var (
ErrCantSwitchToYourself = errors.New("models: you can't switch to yourself")
+
+ pingCount = promauto.NewGauge(prometheus.GaugeOpts{
+ Name: "mi_db_ping",
+ Help: "Number of times the database has been pinged.",
+ })
)
type DAO struct {
@@ -32,6 +39,8 @@ func (d *DAO) Ping(ctx context.Context) error {
return err
}
+ pingCount.Inc()
+
return nil
}
@@ -43,15 +52,17 @@ func New(dbLoc string) (*DAO, error) {
),
})
if err != nil {
- slog.Error("failed to connect to database", "err", err)
- os.Exit(1)
+ return nil, fmt.Errorf("failed to connect to database: %w", err)
}
if err := db.AutoMigrate(&Member{}, &Switch{}, &Blogpost{}); err != nil {
- slog.Error("failed to migrate schema", "err", err)
- os.Exit(1)
+ return nil, fmt.Errorf("failed to migrate schema: %w", err)
}
+ db.Use(gormPrometheus.New(gormPrometheus.Config{
+ DBName: "mi",
+ }))
+
return &DAO{db: db}, nil
}
diff --git a/cmd/mi/posse.go b/cmd/mi/posse.go
index 381cc02..b5fb29f 100644
--- a/cmd/mi/posse.go
+++ b/cmd/mi/posse.go
@@ -9,6 +9,8 @@ import (
"strings"
bsky "github.com/danrusei/gobot-bsky"
+ "github.com/prometheus/client_golang/prometheus"
+ "github.com/prometheus/client_golang/prometheus/promauto"
"golang.org/x/sync/errgroup"
"google.golang.org/protobuf/types/known/emptypb"
"within.website/x/cmd/mi/models"
@@ -17,6 +19,18 @@ import (
"within.website/x/web/mastodon"
)
+var (
+ possePosts = promauto.NewGaugeVec(prometheus.GaugeOpts{
+ Name: "mi_posse_posts",
+ Help: "Number of posts sent to social networks.",
+ }, []string{"service"})
+
+ posseErrors = promauto.NewCounterVec(prometheus.CounterOpts{
+ Name: "mi_posse_errors",
+ Help: "Number of errors encountered while sending posts to social networks.",
+ }, []string{"service"})
+)
+
type Announcer struct {
dao *models.DAO
mastodon *mastodon.Client
@@ -60,21 +74,26 @@ func (a *Announcer) Announce(ctx context.Context, it *jsonfeed.Item) (*emptypb.E
Status: sb.String(),
})
if err != nil {
+ posseErrors.WithLabelValues("mastodon").Inc()
slog.Error("failed to announce to mastodon", "err", err)
return err
}
+ possePosts.WithLabelValues("mastodon").Inc()
slog.Info("posted to mastodon", "blogpost_url", it.GetUrl(), "mastodon_url", post.URL)
return nil
})
g.Go(func() error {
if err := a.bluesky.Connect(gCtx); err != nil {
+ posseErrors.WithLabelValues("bluesky").Inc()
slog.Error("failed to connect to bluesky", "err", err)
return err
}
u, err := url.Parse(it.GetUrl())
if err != nil {
+ posseErrors.WithLabelValues("bluesky").Inc()
+ slog.Error("failed to parse url", "err", err)
return err
}
post, err := bsky.NewPostBuilder(sb.String()).
@@ -82,16 +101,19 @@ func (a *Announcer) Announce(ctx context.Context, it *jsonfeed.Item) (*emptypb.E
WithFacet(bsky.Facet_Link, it.GetUrl(), it.GetUrl()).
Build()
if err != nil {
+ posseErrors.WithLabelValues("bluesky").Inc()
slog.Error("failed to build bluesky post", "err", err)
return err
}
cid, uri, err := a.bluesky.PostToFeed(ctx, post)
if err != nil {
+ posseErrors.WithLabelValues("bluesky").Inc()
slog.Error("failed to post to bluesky", "err", err)
return err
}
+ possePosts.WithLabelValues("bluesky").Inc()
slog.Info("posted to bluesky", "blogpost_url", it.GetUrl(), "bluesky_cid", cid, "bluesky_uri", uri)
return nil
})
@@ -101,6 +123,7 @@ func (a *Announcer) Announce(ctx context.Context, it *jsonfeed.Item) (*emptypb.E
slog.Error("failed to announce to mimi", "err", err)
return err
}
+ possePosts.WithLabelValues("irc").Inc()
return nil
})