forked from mtusi2026/mtusi2026lab6
feat: add bounded observability and verified SLO incident alerts
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/telemetry"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.uber.org/fx"
|
||||
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
|
||||
fiberctrl "gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/controller/http/fiber"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/service"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/service/authentication"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/store/postgres"
|
||||
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/valkey"
|
||||
)
|
||||
|
||||
type dependencyReadiness struct {
|
||||
database fiberctrl.Readiness
|
||||
cache fiberctrl.Readiness
|
||||
objects fiberctrl.Readiness
|
||||
}
|
||||
|
||||
// Ping checks persistence and all limiter shards under a shared deadline.
|
||||
func (d *dependencyReadiness) Ping(ctx context.Context) error {
|
||||
if err := d.database.Ping(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := d.cache.Ping(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if d.objects != nil {
|
||||
return d.objects.Ping(ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// newDatabase creates the pool and registers its migration/start and shutdown hooks.
|
||||
func newDatabase(lifecycle fx.Lifecycle, cfg *config.Persistence) (*postgres.Store, error) {
|
||||
return databaseWithTracer(lifecycle, cfg, nil)
|
||||
}
|
||||
|
||||
// observedDatabase binds the SDK tracer to the driver at composition time.
|
||||
func observedDatabase(lifecycle fx.Lifecycle, cfg *config.Persistence, runtime *telemetry.Runtime) (*postgres.Store, error) {
|
||||
return databaseWithTracer(lifecycle, cfg, runtime)
|
||||
}
|
||||
|
||||
// databaseWithTracer creates and owns the optionally instrumented SQL pool.
|
||||
func databaseWithTracer(lifecycle fx.Lifecycle, cfg *config.Persistence, tracer pgx.QueryTracer) (*postgres.Store, error) {
|
||||
database, err := postgres.New(postgres.Options{
|
||||
Tracer: tracer,
|
||||
URL: cfg.DatabaseURL,
|
||||
MaxConnections: int32(cfg.PoolSize),
|
||||
Timeout: time.Duration(cfg.TimeoutMilliseconds) * time.Millisecond,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lifecycle.Append(fx.Hook{
|
||||
OnStart: database.CheckSchema,
|
||||
OnStop: func(context.Context) error {
|
||||
database.Close()
|
||||
return nil
|
||||
},
|
||||
})
|
||||
return database, nil
|
||||
}
|
||||
|
||||
// newValkey creates bounded clients and registers connectivity and cleanup hooks.
|
||||
func newValkey(lifecycle fx.Lifecycle, cfg *config.Persistence) (*valkey.Client, error) {
|
||||
client, err := valkey.New(valkey.Options{
|
||||
Nodes: cfg.Shards,
|
||||
Coordinator: cfg.Coordinator,
|
||||
PoolSize: cfg.PoolSize,
|
||||
Timeout: time.Duration(cfg.TimeoutMilliseconds) * time.Millisecond,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
lifecycle.Append(fx.Hook{
|
||||
OnStart: client.Ping,
|
||||
OnStop: func(context.Context) error {
|
||||
return client.Close()
|
||||
},
|
||||
})
|
||||
return client, nil
|
||||
}
|
||||
|
||||
// newAuthentication binds credentials and KDF implementations to authentication policy.
|
||||
func newAuthentication(accounts service.Accounts, passwords service.Passwords, cfg *config.Persistence) (service.Authentication, error) {
|
||||
return authentication.New(accounts, passwords, time.Duration(cfg.SessionTTLSeconds)*time.Second)
|
||||
}
|
||||
|
||||
// newReadiness assembles infrastructure checks at the composition boundary.
|
||||
func newReadiness(database *postgres.Store, cache *valkey.Client) *dependencyReadiness {
|
||||
return &dependencyReadiness{database: database, cache: cache}
|
||||
}
|
||||
Reference in New Issue
Block a user