(Feat): Initial Commit
This commit is contained in:
46
internal/metrics/service.go
Normal file
46
internal/metrics/service.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"allaboutapps.dev/aw/go-starter/internal/config"
|
||||
"allaboutapps.dev/aw/go-starter/internal/metrics/users"
|
||||
"allaboutapps.dev/aw/go-starter/internal/util"
|
||||
"github.com/dlmiddlecote/sqlstats"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
config config.Server
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func New(config config.Server, db *sql.DB) (*Service, error) {
|
||||
return &Service{
|
||||
config: config,
|
||||
db: db,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) RegisterMetrics(ctx context.Context) error {
|
||||
log := util.LogFromContext(ctx)
|
||||
|
||||
var metrics []prometheus.Collector
|
||||
|
||||
// custom metrics
|
||||
metrics = append(metrics, users.Metrics(ctx, users.NewDatabaseMetricsCollector(s.db))...)
|
||||
|
||||
// sqlstats metrics, see https://github.com/dlmiddlecote/sqlstats?tab=readme-ov-file#exposed-metrics for the exposed metrics
|
||||
metrics = append(metrics, sqlstats.NewStatsCollector(s.config.Database.Database, s.db))
|
||||
|
||||
for _, metric := range metrics {
|
||||
if err := prometheus.Register(metric); err != nil {
|
||||
log.Error().Err(err).Msg("Failed to register metric")
|
||||
return fmt.Errorf("failed to register metric: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
29
internal/metrics/users/collector.go
Normal file
29
internal/metrics/users/collector.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"allaboutapps.dev/aw/go-starter/internal/models"
|
||||
"allaboutapps.dev/aw/go-starter/internal/util"
|
||||
)
|
||||
|
||||
type DatabaseMetricsCollector struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewDatabaseMetricsCollector(db *sql.DB) *DatabaseMetricsCollector {
|
||||
return &DatabaseMetricsCollector{db: db}
|
||||
}
|
||||
|
||||
func (c DatabaseMetricsCollector) GetTotalUsersCount(ctx context.Context) float64 {
|
||||
log := util.LogFromContext(ctx)
|
||||
|
||||
count, err := models.Users().Count(ctx, c.db)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("Failed to total user count")
|
||||
return 0
|
||||
}
|
||||
|
||||
return float64(count)
|
||||
}
|
||||
27
internal/metrics/users/metrics.go
Normal file
27
internal/metrics/users/metrics.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
type MetricsCollector interface {
|
||||
GetTotalUsersCount(ctx context.Context) float64
|
||||
}
|
||||
|
||||
const (
|
||||
MetricNameTotalUsers = "total_users"
|
||||
)
|
||||
|
||||
func Metrics(ctx context.Context, collector MetricsCollector) []prometheus.Collector {
|
||||
return []prometheus.Collector{
|
||||
prometheus.NewGaugeFunc(
|
||||
prometheus.GaugeOpts{
|
||||
Name: MetricNameTotalUsers,
|
||||
Help: "Total users",
|
||||
},
|
||||
func() float64 { return collector.GetTotalUsersCount(ctx) },
|
||||
),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user