Files
mtusi2026lab6/cmd/server/telemetry.go
T

94 lines
3.2 KiB
Go

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
}