[feature] Add metrics for instance user count, statuses count and federating instances count (#2592)

Co-authored-by: Tsuribori <none@example.org>
This commit is contained in:
Tsuribori 2024-02-14 12:58:55 +02:00 committed by GitHub
parent 46c06b1b8f
commit 142b7ec54f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 74 additions and 11 deletions

View file

@ -82,11 +82,6 @@
return fmt.Errorf("error initializing tracing: %w", err)
}
// Initialize Metrics
if err := metrics.Initialize(); err != nil {
return fmt.Errorf("error initializing metrics: %w", err)
}
// Open connection to the database
dbService, err := bundb.NewBunDBService(ctx, &state)
if err != nil {
@ -218,6 +213,11 @@ func(context.Context, time.Time) {
return fmt.Errorf("error scheduling poll expiries: %w", err)
}
// Initialize metrics.
if err := metrics.Initialize(state.DB); err != nil {
return fmt.Errorf("error initializing metrics: %w", err)
}
/*
HTTP router initialization
*/

View file

@ -69,10 +69,6 @@
return fmt.Errorf("error initializing tracing: %w", err)
}
if err := metrics.Initialize(); err != nil {
return fmt.Errorf("error initializing metrics: %w", err)
}
// Initialize caches and database
state.DB = testrig.NewTestDB(&state)
@ -143,6 +139,11 @@
processor := testrig.NewTestProcessor(&state, federator, emailSender, mediaManager)
// Initialize metrics.
if err := metrics.Initialize(state.DB); err != nil {
return fmt.Errorf("error initializing metrics: %w", err)
}
/*
HTTP router initialization
*/

View file

@ -20,15 +20,18 @@
package metrics
import (
"context"
"errors"
"github.com/gin-gonic/gin"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/technologize/otel-go-contrib/otelginmetrics"
"github.com/uptrace/bun"
"github.com/uptrace/bun/extra/bunotel"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/metric"
sdk "go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
@ -38,7 +41,8 @@
serviceName = "GoToSocial"
)
func Initialize() error {
func Initialize(db db.DB) error {
if !config.GetMetricsEnabled() {
return nil
}
@ -54,6 +58,7 @@ func Initialize() error {
resource.NewWithAttributes(
semconv.SchemaURL,
semconv.ServiceName(serviceName),
semconv.ServiceVersion(config.GetSoftwareVersion()),
),
)
@ -66,8 +71,64 @@ func Initialize() error {
sdk.WithResource(r),
sdk.WithReader(prometheusExporter),
)
otel.SetMeterProvider(meterProvider)
meter := meterProvider.Meter(serviceName)
thisInstance := config.GetHost()
_, err = meter.Int64ObservableGauge(
"gotosocial.instance.total_users",
metric.WithDescription("Total number of users on this instance"),
metric.WithInt64Callback(func(c context.Context, o metric.Int64Observer) error {
userCount, err := db.CountInstanceUsers(c, thisInstance)
if err != nil {
return err
}
o.Observe(int64(userCount))
return nil
}),
)
if err != nil {
return err
}
_, err = meter.Int64ObservableGauge(
"gotosocial.instance.total_statuses",
metric.WithDescription("Total number of statuses on this instance"),
metric.WithInt64Callback(func(c context.Context, o metric.Int64Observer) error {
statusCount, err := db.CountInstanceStatuses(c, thisInstance)
if err != nil {
return err
}
o.Observe(int64(statusCount))
return nil
}),
)
if err != nil {
return err
}
_, err = meter.Int64ObservableGauge(
"gotosocial.instance.total_federating_instances",
metric.WithDescription("Total number of other instances this instance is federating with"),
metric.WithInt64Callback(func(c context.Context, o metric.Int64Observer) error {
federatingCount, err := db.CountInstanceDomains(c, thisInstance)
if err != nil {
return err
}
o.Observe(int64(federatingCount))
return nil
}),
)
if err != nil {
return err
}
return nil
}

View file

@ -24,10 +24,11 @@
"github.com/gin-gonic/gin"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/uptrace/bun"
)
func Initialize() error {
func Initialize(db db.DB) error {
if config.GetMetricsEnabled() {
return errors.New("metrics was disabled at build time")
}