ArangoDB: if the server is down at startup the metrics histogram is never registered, and a config error leaves a nil client that panics
Describe the bug
Connect() in pkg/gofr/datasource/arangodb/arango.go has two failure modes that leave the client in a bad state.
- Server unreachable at startup. The client is assigned before it is verified (
arango.go:117-118), and on a failedVersioncallConnectreturns before registering the histogram (:124-127vs:130-134):
client := arangodb.NewClient(conn)
c.client = client
...
_, err := c.client.Version(ctx)
if err != nil {
c.logger.Errorf("failed to verify connection: %v", err)
return
}
// Initialize metrics
...
c.metrics.NewHistogram("app_arango_stats", ...)Keeping c.client is actually fine here: the HTTP client connects lazily, HealthCheck pings Version and correctly reports DOWN, and operations start working once the server is up. But app_arango_stats is never registered, and Connect isn't retried. So every operation afterwards goes through sendOperationStats → RecordHistogram("app_arango_stats", ...) (arango.go:342), which the metrics manager rejects and logs as an error on every call. The ArangoDB latency metric is missing for the whole life of the process.
- Config validation or auth setup fails.
Connectreturns atarango.go:96-99or:110-113withc.clientstill nil. Nothing checks for that, soHealthCheck(arango.go:366,c.client.Version(ctx)) and every DB/document/graph method panic with a nil pointer dereference instead of returning an error.
To Reproduce
// 1) server down at startup
c := arangodb.New(arangodb.Config{Host: "127.0.0.1", Port: 1, User: "u", Password: "p"})
c.UseLogger(logger); c.UseMetrics(metrics)
c.Connect()
// logs "failed to verify connection", NewHistogram is never called;
// every later operation logs that app_arango_stats is not registered
// 2) incomplete config
c2 := arangodb.New(arangodb.Config{Host: "127.0.0.1", Port: 8529, User: "u"}) // no password
c2.UseLogger(logger); c2.UseMetrics(metrics)
c2.Connect() // logs "config validation error: ... password is empty"
c2.HealthCheck(ctx) // panic: runtime error: invalid memory address or nil pointer dereferenceWith app.AddArangoDB(...) and a missing password, the health endpoint only survives because the container recovers panics in health checks (it reports "health check panicked: ..."). Any handler that uses ctx.ArangoDB panics.
Expected behavior
- Register the histogram regardless of whether the first
Versioncall succeeds (e.g. before connecting, or with async.Oncelike the file stores), so metrics work once the server becomes reachable. - When
c.clientis nil,HealthCheckshould return DOWN with an error and the other methods should return a "not connected" error instead of panicking.
Additional context
Noticed while working on #4359 (SurrealDB Connect leaving the client looking connected, #4313). ArangoDB doesn't have that exact problem because its health check really pings the server, but its failed-connect paths have the issues above.
Source: gofr-dev/gofr