tercul-backend/cmd/api/main.go
google-labs-jules[bot] 3bcd8d08f5 feat: Implement observability stack
This commit introduces a comprehensive observability stack to the application, addressing a key objective from the TODO list.

The following features have been implemented:

- **Centralized Logging:** Replaced the existing custom logger with `zerolog` for structured, leveled, and performant logging. The logger is configured to output human-friendly console logs in development and JSON logs in production.

- **Distributed Tracing:** Integrated OpenTelemetry to provide distributed tracing capabilities. A new middleware has been added to create spans for all incoming HTTP requests, and the trace context is propagated.

- **Prometheus Metrics:** Added Prometheus metrics to monitor HTTP request latency and total request counts. A new `/metrics` endpoint is exposed on port 9090 to serve these metrics.

- **Request ID:** Implemented a middleware to add a unique request ID to every incoming request and response, improving traceability.

The new observability components are encapsulated in the `internal/observability` package, and the existing `internal/platform/log` package has been refactored to be a backward-compatible wrapper around the new logger.

The main application entry point (`cmd/api/main.go`) has been updated to initialize and gracefully shut down the new observability components.
2025-10-03 16:43:01 +00:00

201 lines
5.8 KiB
Go

package main
import (
"context"
"net/http"
"os"
"os/signal"
"path/filepath"
"runtime"
"syscall"
"tercul/internal/app"
"tercul/internal/app/analytics"
graph "tercul/internal/adapters/graphql"
dbsql "tercul/internal/data/sql"
"tercul/internal/jobs/linguistics"
"tercul/internal/observability"
"tercul/internal/platform/auth"
"tercul/internal/platform/config"
"tercul/internal/platform/db"
"tercul/internal/platform/log"
"tercul/internal/platform/search"
"time"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/pressly/goose/v3"
"github.com/prometheus/client_golang/prometheus"
"github.com/weaviate/weaviate-go-client/v5/weaviate"
"gorm.io/gorm"
)
// runMigrations applies database migrations using goose.
func runMigrations(gormDB *gorm.DB) error {
sqlDB, err := gormDB.DB()
if err != nil {
return err
}
if err := goose.SetDialect("postgres"); err != nil {
return err
}
// This is brittle. A better approach might be to use an env var or config.
_, b, _, _ := runtime.Caller(0)
migrationsDir := filepath.Join(filepath.Dir(b), "../../internal/data/migrations")
log.LogInfo("Applying database migrations", log.F("directory", migrationsDir))
if err := goose.Up(sqlDB, migrationsDir); err != nil {
return err
}
log.LogInfo("Database migrations applied successfully")
return nil
}
// main is the entry point for the Tercul application.
func main() {
// Load configuration from environment variables
config.LoadConfig()
// Initialize logger
log.Init("tercul-api", config.Cfg.Environment)
// Initialize OpenTelemetry Tracer Provider
tp, err := observability.TracerProvider("tercul-api", config.Cfg.Environment)
if err != nil {
log.LogFatal("Failed to initialize OpenTelemetry tracer", log.F("error", err))
}
defer func() {
if err := tp.Shutdown(context.Background()); err != nil {
log.LogError("Error shutting down tracer provider", log.F("error", err))
}
}()
// Initialize Prometheus metrics
reg := prometheus.NewRegistry()
metrics := observability.NewMetrics(reg) // Metrics are registered automatically
log.LogInfo("Starting Tercul application",
log.F("environment", config.Cfg.Environment),
log.F("version", "1.0.0"))
// Initialize database connection
database, err := db.InitDB()
if err != nil {
log.LogFatal("Failed to initialize database", log.F("error", err))
}
defer db.Close()
if err := runMigrations(database); err != nil {
log.LogFatal("Failed to apply database migrations", log.F("error", err))
}
// Initialize Weaviate client
weaviateCfg := weaviate.Config{
Host: config.Cfg.WeaviateHost,
Scheme: config.Cfg.WeaviateScheme,
}
weaviateClient, err := weaviate.NewClient(weaviateCfg)
if err != nil {
log.LogFatal("Failed to create weaviate client", log.F("error", err))
}
// Create search client
searchClient := search.NewWeaviateWrapper(weaviateClient)
// Create repositories
repos := dbsql.NewRepositories(database)
// Create linguistics dependencies
analysisRepo := linguistics.NewGORMAnalysisRepository(database)
sentimentProvider, err := linguistics.NewGoVADERSentimentProvider()
if err != nil {
log.LogFatal("Failed to create sentiment provider", log.F("error", err))
}
// Create application services
analyticsService := analytics.NewService(repos.Analytics, analysisRepo, repos.Translation, repos.Work, sentimentProvider)
// Create application
application := app.NewApplication(repos, searchClient, analyticsService)
// Create GraphQL server
resolver := &graph.Resolver{
App: application,
}
jwtManager := auth.NewJWTManager()
srv := NewServerWithAuth(resolver, jwtManager, metrics)
graphQLServer := &http.Server{
Addr: config.Cfg.ServerPort,
Handler: srv,
}
log.LogInfo("GraphQL server created successfully", log.F("port", config.Cfg.ServerPort))
// Create GraphQL playground
playgroundHandler := playground.Handler("GraphQL", "/query")
playgroundServer := &http.Server{
Addr: config.Cfg.PlaygroundPort,
Handler: playgroundHandler,
}
log.LogInfo("GraphQL playground created successfully", log.F("port", config.Cfg.PlaygroundPort))
// Create metrics server
metricsServer := &http.Server{
Addr: ":9090",
Handler: observability.PrometheusHandler(reg),
}
log.LogInfo("Metrics server created successfully", log.F("port", ":9090"))
// Start HTTP servers in goroutines
go func() {
log.LogInfo("Starting GraphQL server",
log.F("port", config.Cfg.ServerPort))
if err := graphQLServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.LogFatal("Failed to start GraphQL server",
log.F("error", err))
}
}()
go func() {
log.LogInfo("Starting GraphQL playground",
log.F("port", config.Cfg.PlaygroundPort))
if err := playgroundServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.LogFatal("Failed to start GraphQL playground",
log.F("error", err))
}
}()
go func() {
log.LogInfo("Starting metrics server", log.F("port", ":9090"))
if err := metricsServer.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.LogFatal("Failed to start metrics server", log.F("error", err))
}
}()
// Wait for interrupt signal to gracefully shutdown the servers
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
log.LogInfo("Shutting down servers...")
// Graceful shutdown
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := graphQLServer.Shutdown(ctx); err != nil {
log.LogError("GraphQL server forced to shutdown",
log.F("error", err))
}
if err := playgroundServer.Shutdown(ctx); err != nil {
log.LogError("GraphQL playground forced to shutdown",
log.F("error", err))
}
if err := metricsServer.Shutdown(ctx); err != nil {
log.LogError("Metrics server forced to shutdown", log.F("error", err))
}
log.LogInfo("All servers shutdown successfully")
}