mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-11-21 16:55:38 +03:00
[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:
parent
46c06b1b8f
commit
142b7ec54f
4 changed files with 74 additions and 11 deletions
|
@ -82,11 +82,6 @@ var Start action.GTSAction = func(ctx context.Context) error {
|
|||
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 @@ var Start action.GTSAction = func(ctx context.Context) error {
|
|||
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
|
||||
*/
|
||||
|
|
|
@ -69,10 +69,6 @@ var Start action.GTSAction = func(ctx context.Context) error {
|
|||
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 @@ var Start action.GTSAction = func(ctx context.Context) error {
|
|||
|
||||
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
|
||||
*/
|
||||
|
|
|
@ -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 @@ const (
|
|||
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
|
||||
}
|
||||
|
||||
|
|
|
@ -24,10 +24,11 @@ import (
|
|||
|
||||
"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")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue