#4369·gofr

ArangoDB: if the server is down at startup the metrics histogram is never registered, and a config error leaves a nil client that panics

Author: NitinKumar004Created Sep 24, 2026Updated Sep 24, 2026

Describe the bug

Connect() in pkg/gofr/datasource/arangodb/arango.go has two failure modes that leave the client in a bad state.

  1. Server unreachable at startup. The client is assigned before it is verified (arango.go:117-118), and on a failed Version call Connect returns before registering the histogram (:124-127 vs :130-134):
go
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.

  1. Config validation or auth setup fails. Connect returns at arango.go:96-99 or :110-113 with c.client still nil. Nothing checks for that, so HealthCheck (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

go
// 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 dereference

With 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 Version call succeeds (e.g. before connecting, or with a sync.Once like the file stores), so metrics work once the server becomes reachable.
  • When c.client is nil, HealthCheck should 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.