Junior — Middle
Kuidas rakendada andmebaasi ühenduse probleemide jälgimist ja diagnoosimist?
sobes.tech AI
Vastus AI-lt
For monitoring and diagnosing database connection issues in Go, the following approaches can be used:
- Logging connection errors with detailed information.
- Using contexts (context.Context) with timeouts to control the waiting time for connections.
- Periodic checking of connection status (ping) with the database.
- Metrics and alerts via Prometheus or other monitoring systems.
Example of a simple database ping with error logging:
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
err := db.PingContext(ctx)
if err != nil {
log.Printf("Database connection error: %v", err)
}
This approach helps quickly identify and respond to connection problems.