feat: add bounded observability and verified SLO incident alerts

This commit is contained in:
Vladislav Agapov
2026-10-29 18:01:11 +03:00
parent 470d9d6f1e
commit 6c7601802d
325 changed files with 24988 additions and 44 deletions
+106
View File
@@ -0,0 +1,106 @@
package main
import (
"time"
"go.uber.org/fx"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/admission"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/controller"
fiberctrl "gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/controller/http/fiber"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/cursor"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/password"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/service"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/service/production"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/store"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/store/postgres"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/valkey"
)
const applicationStopTimeout = 9 * time.Second
// CreateApp defines the dependency graph and Fx lifecycle for the lab2 server.
// Constructors do not open sockets; controller.RunControllerFx registers startup
// and shutdown hooks. The outer stop deadline bounds all component cleanup.
func CreateApp() fx.Option {
return fx.Options(
fx.StopTimeout(applicationStopTimeout),
fx.Supply(fx.Annotate(password.Hasher{}, fx.As(new(service.Passwords)))),
fx.Provide(
config.New,
config.NewPersistence,
config.NewObjectStore,
apiTelemetry,
observedObjects,
newMedia,
observedDatabase,
newValkey,
newAuthentication,
fx.Annotate(newMediaReadiness, fx.As(new(fiberctrl.Readiness))),
httpConfig,
loggerConfig,
newLogger,
fx.Annotate(newAdmissionLimiter, fx.As(new(admission.Distributed))),
fiberctrl.NewErrorRegistry,
fx.Annotate(databaseStore, fx.As(new(store.Interface))),
fx.Annotate(databaseAccounts, fx.As(new(service.Accounts))),
fx.Annotate(profileCache, fx.As(new(service.ProfileCache))),
fx.Annotate(newCursor, fx.As(new(service.CursorCodec))),
fx.Annotate(production.NewCached, fx.As(new(service.Service))),
fx.Annotate(observedHTTP, fx.As(new(controller.Controller))),
),
fx.WithLogger(newFxLogger),
fx.Invoke(controller.RunControllerFx, observeAPI),
)
}
// httpConfig provides the HTTP configuration section to Fx.
func httpConfig(cfg *config.Config) *config.Controller {
return cfg.HTTP
}
// loggerConfig provides the logging configuration section to Fx.
func loggerConfig(cfg *config.Config) *config.Logger {
return cfg.Logger
}
// newAdmissionLimiter translates configured bounds into separate shared policy budgets.
func newAdmissionLimiter(cfg *config.Controller, client *valkey.Client) (*valkey.Limiter, error) {
global := valkey.Budget{Rate: cfg.Admission.RequestsPerSecond, Slots: cfg.Admission.MaxInFlight}
return valkey.NewLimiter(client, valkey.Limits{
Global: global,
Groups: map[string]valkey.Budget{
"auth": {Rate: cfg.Admission.AuthRate, Slots: cfg.Admission.AuthSlots},
"read": global,
"write": global,
},
User: valkey.Budget{Rate: cfg.Admission.UserRate, Slots: cfg.Admission.UserSlots},
UserGroups: map[string]valkey.Budget{
"auth": {Rate: cfg.Admission.UserAuthRate, Slots: cfg.Admission.UserAuthSlots},
"read": {Rate: cfg.Admission.UserRate, Slots: cfg.Admission.UserSlots},
"write": {Rate: cfg.Admission.UserWriteRate, Slots: cfg.Admission.UserWriteSlots},
},
})
}
// databaseStore exposes business persistence through its consumer interface.
func databaseStore(database *postgres.Store) store.Interface {
return database
}
// databaseAccounts exposes account and session persistence through its consumer interface.
func databaseAccounts(database *postgres.Store) service.Accounts {
return database
}
// profileCache exposes immutable profiles without leaking the Valkey client.
func profileCache(client *valkey.Client) service.ProfileCache {
return client
}
// newCursor binds the shared signing secret at the composition root.
func newCursor(cfg *config.Persistence) (*cursor.Codec, error) {
return cursor.NewShared(cfg.CursorKey)
}
+108
View File
@@ -0,0 +1,108 @@
package main
import (
"context"
"net"
"os"
"testing"
"time"
"go.uber.org/fx"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
)
// TestDependencyGraph ensures constructor signatures and interface bindings agree.
func TestDependencyGraph(t *testing.T) {
if err := fx.ValidateApp(CreateApp(), fx.NopLogger); err != nil {
t.Fatal(err)
}
}
// TestInvalidConfigurationStopsStartup checks failure before a listener is acquired.
func TestInvalidConfigurationStopsStartup(t *testing.T) {
t.Setenv("APP_PORT", "0")
app := fx.New(CreateApp(), fx.NopLogger)
if err := app.Start(context.Background()); err == nil {
t.Fatal("invalid listener configuration accepted")
}
}
// TestCommandValidation ensures unsupported arguments cannot accidentally start a server.
func TestCommandValidation(t *testing.T) {
if err := run([]string{"unknown"}); err == nil {
t.Fatal("unknown command accepted")
}
}
// TestLoggerConfiguration checks that process logging uses its own validated section.
func TestLoggerConfiguration(t *testing.T) {
if _, err := newLogger(&config.Logger{Level: "invalid"}); err == nil {
t.Fatal("invalid logger configuration accepted")
}
}
// TestApplicationLifecycle starts the complete Fx graph and drains its real HTTP listener.
func TestApplicationLifecycle(t *testing.T) {
requirePersistence(t)
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
cfg := config.Default()
cfg.HTTP.Host = "127.0.0.1"
cfg.HTTP.Port = listener.Addr().(*net.TCPAddr).Port
cfg.Logger.Level = "error"
if err := listener.Close(); err != nil {
t.Fatal(err)
}
app := fx.New(CreateApp(), fx.Replace(cfg))
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
defer cancel()
if err := app.Start(ctx); err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
stopCtx, stopCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer stopCancel()
if err := app.Stop(stopCtx); err != nil {
t.Error(err)
}
})
if err := checkReadiness(cfg.HTTP); err != nil {
t.Fatal(err)
}
}
// TestOccupiedListenerFailsStartup ensures bind failures propagate through the Fx lifecycle.
func TestOccupiedListenerFailsStartup(t *testing.T) {
requirePersistence(t)
listener, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
defer func() {
_ = listener.Close()
}()
cfg := config.Default()
cfg.HTTP.Host = "127.0.0.1"
cfg.HTTP.Port = listener.Addr().(*net.TCPAddr).Port
cfg.Logger.Level = "error"
app := fx.New(CreateApp(), fx.Replace(cfg), fx.NopLogger)
if err := app.Start(t.Context()); err == nil {
_ = app.Stop(t.Context())
t.Fatal("startup succeeded on an occupied listener")
}
}
// requirePersistence prevents infrastructure scenarios from silently passing on configuration failure.
func requirePersistence(t *testing.T) {
t.Helper()
if os.Getenv("DATABASE_URL") == "" || os.Getenv("VALKEY_SHARDS") == "" {
t.Skip("DATABASE_URL and VALKEY_SHARDS are required for real application lifecycle tests")
}
t.Setenv("CURSOR_SIGNING_KEY", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=")
if err := migrate(); err != nil {
t.Fatal(err)
}
}
+57
View File
@@ -0,0 +1,57 @@
package main
import (
"context"
"fmt"
"time"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/store/postgres"
"go.uber.org/fx"
)
// run dispatches the server command or the container healthcheck.
func run(args []string) error {
if len(args) == 1 && args[0] == "worker" {
fx.New(workerApp()).Run()
return nil
}
if len(args) == 3 && args[0] == "replay" {
return replayJob(args[1], args[2])
}
if len(args) == 1 && args[0] == "s3-bootstrap" {
return bootstrapS3()
}
if len(args) == 0 {
fx.New(CreateApp()).Run()
return nil
}
if len(args) == 1 && args[0] == "reconcile" {
return reconcile(false)
}
if len(args) == 2 && args[0] == "reconcile" && args[1] == "--apply" {
return reconcile(true)
}
if len(args) == 1 && args[0] == "migrate" {
return migrate()
}
if len(args) == 1 && args[0] == "healthcheck" {
return healthcheck()
}
return fmt.Errorf("usage: server [healthcheck|migrate]")
}
// migrate applies versioned DDL once from an operator job, independently of the API.
func migrate() error {
cfg, err := config.NewPersistence()
if err != nil {
return err
}
database, err := postgres.New(postgres.Options{URL: cfg.DatabaseURL, MaxConnections: 1, Timeout: 30 * time.Second})
if err != nil {
return err
}
defer database.Close()
return database.Start(context.Background())
}
+46
View File
@@ -0,0 +1,46 @@
package main
import (
"fmt"
"net"
"net/http"
"strconv"
"time"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
)
const readinessProbeTimeout = 2 * time.Second
// healthcheck loads configuration and runs the container readiness probe.
func healthcheck() error {
cfg, err := config.New()
if err != nil {
return err
}
return checkReadiness(cfg.HTTP)
}
// checkReadiness checks the local readiness endpoint within the probe timeout.
func checkReadiness(cfg *config.Controller) error {
host := cfg.Host
if host == "0.0.0.0" {
host = "127.0.0.1"
}
if host == "::" {
host = "::1"
}
address := "http://" + net.JoinHostPort(host, strconv.Itoa(cfg.Port)) + "/health/ready"
client := http.Client{Timeout: readinessProbeTimeout}
response, err := client.Get(address)
if err != nil {
return fmt.Errorf("readiness probe: %w", err)
}
defer func() {
_ = response.Body.Close()
}()
if response.StatusCode != http.StatusOK {
return fmt.Errorf("not ready: %d", response.StatusCode)
}
return nil
}
+111
View File
@@ -0,0 +1,111 @@
package main
import (
"errors"
"net"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"testing"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
)
// TestRun_Healthcheck verifies command routing to the readiness endpoint.
func TestRun_Healthcheck(t *testing.T) {
// Arrange.
requests := make(chan string, 1)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requests <- r.Method + " " + r.URL.Path
w.WriteHeader(http.StatusOK)
}))
t.Cleanup(server.Close)
address := server.Listener.Addr().(*net.TCPAddr)
t.Setenv("APP_HOST", "0.0.0.0")
t.Setenv("APP_PORT", strconv.Itoa(address.Port))
// Act.
err := run([]string{"healthcheck"})
// Assert.
if err != nil {
t.Fatal(err)
}
select {
case got := <-requests:
if got != "GET /health/ready" {
t.Fatalf("probe=%q", got)
}
default:
t.Fatal("command returned success without probing readiness")
}
}
// TestHealthcheck_InvalidConfig rejects invalid startup settings before attempting HTTP.
func TestHealthcheck_InvalidConfig(t *testing.T) {
// Arrange.
t.Setenv("APP_PORT", "invalid")
// Act.
err := healthcheck()
// Assert.
if err == nil {
t.Fatal("invalid port accepted")
}
}
// TestCheckReadiness distinguishes unhealthy responses from connection failures.
func TestCheckReadiness(t *testing.T) {
t.Run("Negative: unhealthy response", func(t *testing.T) {
// Arrange.
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
}))
t.Cleanup(server.Close)
address := server.Listener.Addr().(*net.TCPAddr)
cfg := config.Default().HTTP
cfg.Host = "127.0.0.1"
cfg.Port = address.Port
// Act.
err := checkReadiness(cfg)
// Assert.
if err == nil {
t.Fatalf("probe accepted %s", "Negative: unhealthy response")
}
if err.Error() != "not ready: 503" {
t.Fatalf("unexpected health failure: %v", err)
}
})
t.Run("Negative: server unavailable", func(t *testing.T) {
// Arrange.
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
}))
t.Cleanup(server.Close)
address := server.Listener.Addr().(*net.TCPAddr)
cfg := config.Default().HTTP
cfg.Host = "127.0.0.1"
cfg.Port = address.Port
server.Close()
// Act.
err := checkReadiness(cfg)
// Assert.
if err == nil {
t.Fatalf("probe accepted %s", "Negative: server unavailable")
}
if _, ok := errors.AsType[*url.Error](err); !ok {
t.Fatalf("connection failure was not preserved: %v", err)
}
})
}
+29
View File
@@ -0,0 +1,29 @@
package main
import (
"go.uber.org/fx/fxevent"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
)
// newLogger creates a structured logger with the configured severity threshold.
func newLogger(cfg *config.Logger) (*zap.Logger, error) {
if err := cfg.Validate(); err != nil {
return nil, err
}
level, err := zapcore.ParseLevel(cfg.Level)
if err != nil {
return nil, err
}
settings := zap.NewProductionConfig()
settings.Level = zap.NewAtomicLevelAt(level)
settings.DisableStacktrace = true
return settings.Build()
}
// newFxLogger adapts the application logger for Fx lifecycle events.
func newFxLogger(log *zap.Logger) fxevent.Logger {
return &fxevent.ZapLogger{Logger: log.Named("fx")}
}
+15
View File
@@ -0,0 +1,15 @@
// Command server runs the messenger or its container readiness probe.
package main
import (
"fmt"
"os"
)
// main runs the selected command and reports failures to stderr.
func main() {
if err := run(os.Args[1:]); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
+39
View File
@@ -0,0 +1,39 @@
package main
import (
"go.lab260.ru/fielderr"
"go.uber.org/zap"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/admission"
"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/media"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/objectstore"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/service"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/store/postgres"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/valkey"
)
// newObjects binds the S3 adapter at the composition root.
func newObjects(cfg *config.ObjectStore) (*objectstore.Client, error) {
return objectstore.New(cfg)
}
// newMedia injects infrastructure through the media service's consumer interfaces.
func newMedia(database *postgres.Store, objects *objectstore.Client, chats service.Service) (*media.Service, error) {
return media.New(database, objects, chats)
}
// newHTTPController connects the independently tested transport and attachment service.
func newHTTPController(log *zap.Logger, cfg *config.Controller, svc service.Service, registry *fielderr.Registry, limiter admission.Distributed, auth service.Authentication, readiness fiberctrl.Readiness, attachments *media.Service) (*fiberctrl.Controller, error) {
ctrl, err := fiberctrl.New(log, cfg, svc, registry, limiter, auth, readiness)
if err != nil {
return nil, err
}
return ctrl.WithAttachments(attachments), nil
}
// newMediaReadiness includes private originals in the mandatory synchronous dependency path.
func newMediaReadiness(database *postgres.Store, cache *valkey.Client, objects *objectstore.Client) *dependencyReadiness {
return &dependencyReadiness{database: database, cache: cache, objects: objects}
}
+88
View File
@@ -0,0 +1,88 @@
package main
import (
"os"
"testing"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
)
// TestMediaHTTPConfigurationFailure rejects invalid listener settings before serving files.
func TestMediaHTTPConfigurationFailure(t *testing.T) {
if _, err := newHTTPController(nil, &config.Controller{}, nil, nil, nil, nil, nil, nil); err == nil {
t.Fatal("invalid HTTP composition accepted")
}
}
// TestReconcileConfigurationFailures checks operator commands fail closed before side effects.
func TestReconcileConfigurationFailures(t *testing.T) {
cases := []struct {
name string
key string
value string
}{
{"database missing", "DATABASE_URL", ""},
{"s3 missing", "S3_ENDPOINT", ""},
{"database malformed", "DATABASE_URL", "://invalid"},
{"s3 malformed", "S3_ENDPOINT", "http://invalid/path"},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("DATABASE_URL", "postgres://localhost/test")
t.Setenv("VALKEY_SHARDS", "shard0=localhost:6379")
t.Setenv("S3_ENDPOINT", "localhost:9000")
t.Setenv("S3_BUCKET", "attachments")
t.Setenv("S3_ACCESS_KEY", "test")
t.Setenv("S3_SECRET_KEY", "synthetic")
t.Setenv(tc.key, tc.value)
if err := run([]string{"reconcile"}); err == nil {
t.Fatal("invalid reconciliation configuration accepted")
}
})
}
}
// TestReconcileRealInventory verifies the operator command can scan both real dependencies.
func TestReconcileRealInventory(t *testing.T) {
requirePersistence(t)
if os.Getenv("S3_ENDPOINT") == "" {
t.Skip("isolated S3 required")
}
if err := run([]string{"reconcile"}); err != nil {
t.Fatal(err)
}
if err := run([]string{"reconcile", "--apply"}); err != nil {
t.Fatal(err)
}
}
// TestBootstrapConfigurationFailures rejects missing credentials and malformed endpoints.
func TestBootstrapConfigurationFailures(t *testing.T) {
t.Run("missing", func(t *testing.T) {
t.Setenv("S3_ENDPOINT", "")
if err := run([]string{"s3-bootstrap"}); err == nil {
t.Fatal("missing config accepted")
}
})
t.Run("malformed", func(t *testing.T) {
t.Setenv("S3_ENDPOINT", "http://invalid/path")
t.Setenv("S3_BUCKET", "attachments")
t.Setenv("S3_ACCESS_KEY", "test")
t.Setenv("S3_SECRET_KEY", "synthetic")
if err := run([]string{"s3-bootstrap"}); err == nil {
t.Fatal("malformed endpoint accepted")
}
})
}
// TestBootstrapExistingBucket verifies repeatable operator execution against the isolated bucket.
func TestBootstrapExistingBucket(t *testing.T) {
if os.Getenv("S3_ENDPOINT") == "" || os.Getenv("S3_ADMIN_SECRET") == "" {
t.Skip("isolated S3 operator credentials required")
}
t.Setenv("S3_ACCESS_KEY", "operator")
t.Setenv("S3_SECRET_KEY", os.Getenv("S3_ADMIN_SECRET"))
if err := run([]string{"s3-bootstrap"}); err != nil {
t.Fatal(err)
}
}
+98
View File
@@ -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}
}
+66
View File
@@ -0,0 +1,66 @@
package main
import (
"testing"
"time"
"go.uber.org/fx"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/store/postgres"
)
// TestDatabaseProviderRejectsInvalidConfiguration verifies dependency errors propagate before startup.
func TestDatabaseProviderRejectsInvalidConfiguration(t *testing.T) {
var lifecycle fx.Lifecycle
app := fx.New(fx.NopLogger, fx.Populate(&lifecycle))
if err := app.Err(); err != nil {
t.Fatal(err)
}
if _, err := newDatabase(lifecycle, &config.Persistence{}); err == nil {
t.Fatal("invalid database settings accepted")
}
}
// TestValkeyProviderRejectsInvalidConfiguration verifies the cache constructor is a real boundary.
func TestValkeyProviderRejectsInvalidConfiguration(t *testing.T) {
var lifecycle fx.Lifecycle
app := fx.New(fx.NopLogger, fx.Populate(&lifecycle))
if err := app.Err(); err != nil {
t.Fatal(err)
}
if _, err := newValkey(lifecycle, &config.Persistence{}); err == nil {
t.Fatal("invalid cache settings accepted")
}
}
// TestReadinessStopsAtUnavailableDatabase checks that mandatory persistence failure is visible.
func TestReadinessStopsAtUnavailableDatabase(t *testing.T) {
database, err := postgres.New(postgres.Options{URL: "postgres://localhost/test", MaxConnections: 1, Timeout: time.Second})
if err != nil {
t.Fatal(err)
}
database.Close()
readiness := newReadiness(database, nil)
if err := readiness.Ping(t.Context()); err == nil {
t.Fatal("closed database reported ready")
}
}
// TestMigrationCommandRejectsMissingConfiguration fails before opening a database.
func TestMigrationCommandRejectsMissingConfiguration(t *testing.T) {
t.Setenv("DATABASE_URL", "")
t.Setenv("VALKEY_SHARDS", "shard0=localhost:6379")
if err := run([]string{"migrate"}); err == nil {
t.Fatal("missing database configuration accepted")
}
}
// TestMigrationCommandRejectsMalformedDatabaseURL fails without logging credentials.
func TestMigrationCommandRejectsMalformedDatabaseURL(t *testing.T) {
t.Setenv("DATABASE_URL", "://invalid")
t.Setenv("VALKEY_SHARDS", "shard0=localhost:6379")
if err := run([]string{"migrate"}); err == nil {
t.Fatal("malformed database configuration accepted")
}
}
+43
View File
@@ -0,0 +1,43 @@
package main
import (
"context"
"os"
"time"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/media"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/objectstore"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/pipeline"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/store/postgres"
)
// reconcile runs a bounded dry-run scan unless the operator explicitly supplies apply.
func reconcile(apply bool) error {
cfg, err := config.NewPersistence()
if err != nil {
return err
}
s3, err := config.NewObjectStore()
if err != nil {
return err
}
database, err := postgres.New(postgres.Options{URL: cfg.DatabaseURL, MaxConnections: 1, Timeout: 5 * time.Second})
if err != nil {
return err
}
defer database.Close()
objects, err := objectstore.New(s3)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
if apply {
if _, err := pipeline.Cleanup(ctx, database, objects); err != nil {
return err
}
}
return media.RunReconciliation(ctx, database, objects, os.Stdout, apply)
}
+24
View File
@@ -0,0 +1,24 @@
package main
import (
"context"
"time"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/objectstore"
)
// bootstrapS3 creates the private bucket using separate operator credentials.
func bootstrapS3() error {
cfg, err := config.NewObjectStore()
if err != nil {
return err
}
objects, err := objectstore.New(cfg)
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
return objects.Bootstrap(ctx)
}
+93
View File
@@ -0,0 +1,93 @@
package main
import (
"context"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/admission"
fiberctrl "gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/controller/http/fiber"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/media"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/service"
"go.lab260.ru/fielderr"
"go.uber.org/fx"
"go.uber.org/zap"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/objectstore"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/store/postgres"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/telemetry"
)
// telemetryFor constructs one process-owned SDK runtime with bounded lifecycle shutdown.
func telemetryFor(lc fx.Lifecycle, service string) (*telemetry.Runtime, error) {
cfg, err := config.NewTelemetry(service)
if err != nil {
return nil, err
}
runtime, err := telemetry.New(context.Background(), cfg)
if err != nil {
return nil, err
}
lc.Append(fx.Hook{OnStop: runtime.Shutdown})
return runtime, nil
}
// apiTelemetry assigns the API's resource identity.
func apiTelemetry(lc fx.Lifecycle) (*telemetry.Runtime, error) {
return telemetryFor(lc, "messenger-api")
}
// workerTelemetry assigns the worker's resource identity.
func workerTelemetry(lc fx.Lifecycle) (*telemetry.Runtime, error) {
return telemetryFor(lc, "messenger-worker")
}
// observedObjects adds S3 operation spans at the infrastructure composition boundary.
func observedObjects(cfg *config.ObjectStore, runtime *telemetry.Runtime) (*objectstore.Client, error) {
client, err := newObjects(cfg)
if err != nil {
return nil, err
}
return client.WithTracer(runtime), nil
}
// observeAPI registers pool and backlog metrics outside the HTTP request path.
func observeAPI(runtime *telemetry.Runtime, database *postgres.Store) error {
return runtime.ObserveSaturation(database, false)
}
// observeWorker registers worker occupancy and database observations.
func observeWorker(runtime *telemetry.Runtime, database *postgres.Store) error {
return runtime.ObserveSaturation(database, true)
}
// observedHTTP binds the transport observer after constructing its business dependencies.
func observedHTTP(params observedHTTPParams) (*fiberctrl.Controller, error) {
ctrl, err := newHTTPController(params.Log, params.Config, params.Service, params.Errors, params.Limiter, params.Auth, params.Readiness, params.Attachments)
if err != nil {
return nil, err
}
return ctrl.WithObserver(params.Telemetry), nil
}
// observedHTTPParams declares the complete HTTP composition dependencies.
type observedHTTPParams struct {
fx.In
// Log emits process diagnostics.
Log *zap.Logger
// Config provides listener policy.
Config *config.Controller
// Service owns messaging operations.
Service service.Service
// Errors maps domain failures.
Errors *fielderr.Registry
// Limiter bounds shared admission.
Limiter admission.Distributed
// Auth resolves bearer sessions.
Auth service.Authentication
// Readiness checks mandatory dependencies.
Readiness fiberctrl.Readiness
// Attachments owns file authorization and intake.
Attachments *media.Service
// Telemetry observes safe transport metadata.
Telemetry *telemetry.Runtime
}
+29
View File
@@ -0,0 +1,29 @@
package main
import (
"testing"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
)
// TestTelemetryInvalidConfiguration rejects invalid sampling before lifecycle registration.
func TestTelemetryInvalidConfiguration(t *testing.T) {
t.Setenv("OTEL_SAMPLE_RATIO", "invalid")
if _, err := apiTelemetry(nil); err == nil {
t.Fatal("invalid sampling accepted")
}
}
// TestObservedObjectsInvalidEndpoint propagates S3 construction errors before instrumentation.
func TestObservedObjectsInvalidEndpoint(t *testing.T) {
if _, err := observedObjects(&config.ObjectStore{Endpoint: "http://invalid/path"}, nil); err == nil {
t.Fatal("invalid object endpoint accepted")
}
}
// TestObservedHTTPInvalidConfiguration rejects invalid listener settings before adding an observer.
func TestObservedHTTPInvalidConfiguration(t *testing.T) {
if _, err := observedHTTP(observedHTTPParams{Config: &config.Controller{}}); err == nil {
t.Fatal("invalid listener accepted")
}
}
+89
View File
@@ -0,0 +1,89 @@
package main
import (
"context"
"fmt"
"strconv"
"time"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/telemetry"
"github.com/google/uuid"
"go.uber.org/fx"
"go.uber.org/zap"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/imageprocessor"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/objectstore"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/pipeline"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/store/postgres"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/streamqueue"
)
// workerApp composes an independently deployable worker with owned Fx lifecycle.
func workerApp() fx.Option {
return fx.Options(fx.StopTimeout(55*time.Second), fx.Provide(config.New, config.NewPersistence, config.NewObjectStore, config.NewWorker, loggerConfig, newLogger, workerTelemetry, observedDatabase, observedObjects, newQueue, imageprocessor.New, newWorker), fx.WithLogger(newFxLogger), fx.Invoke(startWorker, observeWorker))
}
// newQueue closes the queue after the worker has relinquished its processing loop.
func newQueue(lc fx.Lifecycle, cfg *config.Worker) (*streamqueue.Queue, error) {
queue, err := streamqueue.New(cfg.BrokerAddress, cfg.Consumer)
if err != nil {
return nil, err
}
lc.Append(fx.Hook{OnStop: func(context.Context) error {
return queue.Close()
}})
return queue, nil
}
// newWorker binds concrete adapters only at the composition root.
func newWorker(database *postgres.Store, queue *streamqueue.Queue, objects *objectstore.Client, processor *imageprocessor.Processor, log *zap.Logger, runtime *telemetry.Runtime) (*pipeline.Worker, error) {
worker, err := pipeline.New(database, queue, objects, processor, log)
if err != nil {
return nil, err
}
return worker.WithObserver(runtime), nil
}
// startWorker owns the background context and joins its goroutine during shutdown.
func startWorker(lc fx.Lifecycle, worker *pipeline.Worker) {
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
lc.Append(fx.Hook{OnStart: func(context.Context) error {
go func() {
defer close(done)
worker.Run(ctx)
}()
return nil
}, OnStop: func(stop context.Context) error {
cancel()
select {
case <-done:
return nil
case <-stop.Done():
return stop.Err()
}
}})
}
// replayJob explicitly retries a failed version or advances a pipeline version.
func replayJob(rawID, rawVersion string) error {
id, err := uuid.Parse(rawID)
if err != nil {
return fmt.Errorf("invalid attachment ID")
}
version, err := strconv.Atoi(rawVersion)
if err != nil || version < 1 {
return fmt.Errorf("positive pipeline version required")
}
cfg, err := config.NewPersistence()
if err != nil {
return err
}
database, err := postgres.New(postgres.Options{URL: cfg.DatabaseURL, MaxConnections: 1, Timeout: 5 * time.Second})
if err != nil {
return err
}
defer database.Close()
return database.Replay(context.Background(), id, version)
}
+80
View File
@@ -0,0 +1,80 @@
package main
import (
"os"
"testing"
"time"
"github.com/google/uuid"
"go.uber.org/fx"
"gitea.privcloud.tech/vlad_marlo/mtusi2026lab6/internal/config"
)
// TestWorkerLifecycle starts and joins the real worker without exposing an HTTP listener.
func TestWorkerLifecycle(t *testing.T) {
requirePersistence(t)
if os.Getenv("BROKER_ADDRESS") == "" || os.Getenv("S3_ENDPOINT") == "" {
t.Skip("isolated broker and S3 required")
}
app := fx.New(workerApp())
if err := app.Start(t.Context()); err != nil {
t.Fatal(err)
}
time.Sleep(30 * time.Millisecond)
if err := app.Stop(t.Context()); err != nil {
t.Fatal(err)
}
}
// TestQueueProviderRejectsMissingAddress propagates configuration failure before startup.
func TestQueueProviderRejectsMissingAddress(t *testing.T) {
var lc fx.Lifecycle
app := fx.New(fx.NopLogger, fx.Populate(&lc))
if err := app.Err(); err != nil {
t.Fatal(err)
}
if _, err := newQueue(lc, &config.Worker{}); err == nil {
t.Fatal("missing broker accepted")
}
}
// TestReplayCommandRejectsInvalidInput prevents unintended operator transitions.
func TestReplayCommandRejectsInvalidInput(t *testing.T) {
for _, tc := range []struct {
name string
id string
version string
}{{"invalid ID", "invalid", "1"}, {"invalid version", uuid.NewString(), "x"}, {"negative version", uuid.NewString(), "-1"}} {
t.Run(tc.name, func(t *testing.T) {
if err := run([]string{"replay", tc.id, tc.version}); err == nil {
t.Fatal("invalid operator input accepted")
}
})
}
}
// TestReplayCommandConfiguration checks failure before database access.
func TestReplayCommandConfiguration(t *testing.T) {
t.Run("missing", func(t *testing.T) {
t.Setenv("DATABASE_URL", "")
if err := replayJob(uuid.NewString(), "1"); err == nil {
t.Fatal("missing database accepted")
}
})
t.Run("malformed", func(t *testing.T) {
t.Setenv("DATABASE_URL", "://invalid")
t.Setenv("VALKEY_SHARDS", "shard0=localhost:6379")
if err := replayJob(uuid.NewString(), "1"); err == nil {
t.Fatal("malformed database accepted")
}
})
}
// TestReplayUnknownAttachment verifies safe operator failure against the actual schema.
func TestReplayUnknownAttachment(t *testing.T) {
requirePersistence(t)
if err := replayJob(uuid.NewString(), "1"); err == nil {
t.Fatal("unknown attachment replayed")
}
}