add monitoring #4

Open
alexandvvvvv wants to merge 13 commits from feature/mntr into main
5 changed files with 35 additions and 41 deletions
Showing only changes of commit a6eb3a2088 - Show all commits

View File

@@ -4,7 +4,6 @@ import (
"crypto/rand" "crypto/rand"
"database/sql" "database/sql"
"fmt" "fmt"
"log"
"net/http" "net/http"
"os" "os"
"strings" "strings"
@@ -23,7 +22,7 @@ func InitJWT() {
if secret == "" { if secret == "" {
// Generate a random secret for development // Generate a random secret for development
secret = generateRandomSecret() secret = generateRandomSecret()
log.Println("Warning: Using generated JWT secret. Set JWT_SECRET environment variable for production.") Logger.Println("Warning: Using generated JWT secret. Set JWT_SECRET environment variable for production.")
} }
jwtSecret = []byte(secret) jwtSecret = []byte(secret)
} }

View File

@@ -2,7 +2,6 @@ package main
import ( import (
"fmt" "fmt"
"log"
"os" "os"
"strings" "strings"
@@ -63,7 +62,7 @@ func loadEnvironmentFiles() {
// Get environment first (from system env or default) // Get environment first (from system env or default)
env := getEnvironmentFromSystem() env := getEnvironmentFromSystem()
log.Printf("🔍 Detected environment: %s", env) Logger.Printf("🔍 Detected environment: %s", env)
// Define file loading order (later files override earlier ones) // Define file loading order (later files override earlier ones)
files := []string{ files := []string{
@@ -75,12 +74,12 @@ func loadEnvironmentFiles() {
for _, file := range files { for _, file := range files {
if _, err := os.Stat(file); err == nil { if _, err := os.Stat(file); err == nil {
if err := godotenv.Load(file); err != nil { if err := godotenv.Load(file); err != nil {
log.Printf("⚠️ Warning: Could not load %s: %v", file, err) Logger.Printf("⚠️ Warning: Could not load %s: %v", file, err)
} else { } else {
log.Printf("✅ Loaded: %s", file) Logger.Printf("✅ Loaded: %s", file)
} }
} else { } else {
log.Printf("❌ Not found: %s", file) Logger.Printf("❌ Not found: %s", file)
} }
} }
} }
@@ -112,7 +111,7 @@ func getEnvironment() Environment {
case "production", "prod": case "production", "prod":
return Production return Production
default: default:
log.Printf("⚠️ Unknown environment '%s', defaulting to development", env) Logger.Printf("⚠️ Unknown environment '%s', defaulting to development", env)
return Development return Development
} }
} }
@@ -169,7 +168,7 @@ func getEnv(key, defaultValue string) string {
func getRequiredEnv(key string) string { func getRequiredEnv(key string) string {
value := os.Getenv(key) value := os.Getenv(key)
if value == "" { if value == "" {
log.Fatalf("❌ Required environment variable %s is not set", key) Logger.Fatalf("❌ Required environment variable %s is not set", key)
} }
return value return value
} }
@@ -177,29 +176,29 @@ func getRequiredEnv(key string) string {
// logConfig logs configuration (without sensitive data) // logConfig logs configuration (without sensitive data)
func logConfig(config *Config) { func logConfig(config *Config) {
// Environment banner // Environment banner
log.Printf("") Logger.Printf("")
log.Printf("╔══════════════════════════════════════════════════════════════╗") Logger.Printf("╔══════════════════════════════════════════════════════════════╗")
log.Printf("║ COUNTER APPLICATION ║") Logger.Printf("║ COUNTER APPLICATION ║")
log.Printf("║ ║") Logger.Printf("║ ║")
log.Printf("║ 🌍 ENVIRONMENT: %-15s ║", strings.ToUpper(string(config.Environment))) Logger.Printf("║ 🌍 ENVIRONMENT: %-15s ║", strings.ToUpper(string(config.Environment)))
log.Printf("║ 🚀 MODE: %-20s ║", config.GinMode) Logger.Printf("║ 🚀 MODE: %-20s ║", config.GinMode)
log.Printf("║ 🔧 DEBUG: %-20s ║", fmt.Sprintf("%t", config.Debug)) Logger.Printf("║ 🔧 DEBUG: %-20s ║", fmt.Sprintf("%t", config.Debug))
log.Printf("║ 📊 LOG LEVEL: %-15s ║", config.LogLevel) Logger.Printf("║ 📊 LOG LEVEL: %-15s ║", config.LogLevel)
log.Printf("║ 🌐 PORT: %-20s ║", config.Port) Logger.Printf("║ 🌐 PORT: %-20s ║", config.Port)
log.Printf("║ 📈 METRICS PORT: %-15s ║", config.MetricsPort) Logger.Printf("║ 📈 METRICS PORT: %-15s ║", config.MetricsPort)
log.Printf("║ 📝 LOG DIR: %-20s ║", config.LogDir) Logger.Printf("║ 📝 LOG DIR: %-20s ║", config.LogDir)
log.Printf("║ 📦 LOG VOLUME: %-18s ║", config.LogVolume) Logger.Printf("║ 📦 LOG VOLUME: %-18s ║", config.LogVolume)
log.Printf("║ ║") Logger.Printf("║ ║")
log.Printf("║ 📁 Configuration Files Loaded: ║") Logger.Printf("║ 📁 Configuration Files Loaded: ║")
log.Printf("║ • .env (base configuration) ║") Logger.Printf("║ • .env (base configuration) ║")
log.Printf("║ • .env.%s (environment-specific) ║", config.Environment) Logger.Printf("║ • .env.%s (environment-specific) ║", config.Environment)
log.Printf("║ ║") Logger.Printf("║ ║")
log.Printf("║ 🔐 Security: ║") Logger.Printf("║ 🔐 Security: ║")
log.Printf("║ • Database: %s ║", maskDatabaseURL(config.DatabaseURL)) Logger.Printf("║ • Database: %s ║", maskDatabaseURL(config.DatabaseURL))
log.Printf("║ • JWT Secret: %s ║", maskSecret(config.JWTSecret)) Logger.Printf("║ • JWT Secret: %s ║", maskSecret(config.JWTSecret))
log.Printf("║ ║") Logger.Printf("║ ║")
log.Printf("╚══════════════════════════════════════════════════════════════╝") Logger.Printf("╚══════════════════════════════════════════════════════════════╝")
log.Printf("") Logger.Printf("")
} }
// maskDatabaseURL masks sensitive parts of database URL // maskDatabaseURL masks sensitive parts of database URL

View File

@@ -3,7 +3,6 @@ package main
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"log"
"os" "os"
_ "github.com/lib/pq" _ "github.com/lib/pq"
@@ -41,7 +40,7 @@ func initDBWithURL(dbURL string) error {
return fmt.Errorf("failed to ping database: %w", err) return fmt.Errorf("failed to ping database: %w", err)
} }
log.Println("✅ Database connection established successfully") Logger.Println("✅ Database connection established successfully")
return nil return nil
} }
@@ -82,7 +81,7 @@ func CreateTables() error {
} }
} }
log.Println("✅ Database tables created successfully") Logger.Println("✅ Database tables created successfully")
return nil return nil
} }

View File

@@ -1,8 +1,6 @@
package main package main
import ( import (
"log"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
@@ -14,7 +12,7 @@ func main() {
// Initialize structured logger // Initialize structured logger
if err := InitLogger(config); err != nil { if err := InitLogger(config); err != nil {
log.Fatal("Failed to initialize logger:", err) Logger.Fatal("Failed to initialize logger:", err)
} }
// Set Gin mode based on configuration // Set Gin mode based on configuration

View File

@@ -1,7 +1,6 @@
package main package main
import ( import (
"log"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@@ -90,9 +89,9 @@ func StartMetricsServer(port string) {
} }
go func() { go func() {
log.Printf("📈 Metrics server starting on http://localhost:%s/metrics", port) Logger.Printf("📈 Metrics server starting on http://localhost:%s/metrics", port)
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Printf("❌ Metrics server failed to start: %v", err) Logger.Printf("❌ Metrics server failed to start: %v", err)
} }
}() }()
} }