Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/datasources/arangodb/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ To connect to `ArangoDB`, you need to provide the following environment variable
- `PASSWORD`: The password for the specified user.
- `PORT`: The port number

If any of these values is missing, GoFr logs the error, the ArangoDB health check reports `DOWN` with the reason in `details.error`, and ArangoDB calls return a "not connected to ArangoDB" error instead of panicking. If the server is unreachable at startup, GoFr logs the error and keeps the client; the health check reports `DOWN` (with the connection error in `details.error`) until the server becomes reachable, after which queries and the `app_arango_stats` metric work without a restart.

## Setup

GoFr supports injecting `ArangoDB` that implements the following interface. Any driver that implements the interface can be
Expand Down
37 changes: 29 additions & 8 deletions pkg/gofr/datasource/arangodb/arango.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
const (
defaultTimeout = 5 * time.Second
arangoEdgeCollectionType = 3
statusDown = "DOWN"
)

// Client represents an ArangoDB client.
Expand Down Expand Up @@ -56,6 +57,7 @@ var (
ErrDatabaseExists = errors.New("database already exists")
ErrCollectionExists = errors.New("collection already exists")
ErrGraphExists = errors.New("graph already exists")
errNotConnected = errors.New("not connected to ArangoDB, check the Connect error logs")
)

// New creates a new ArangoDB client with the provided configuration.
Expand Down Expand Up @@ -94,6 +96,14 @@ func (c *Client) UseTracer(tracer any) {

// Connect establishes a connection to the ArangoDB server.
func (c *Client) Connect() {
// Register the histogram before any step that can fail, so operations record latency even when
// the server is unreachable at startup and becomes available later.
arangoBuckets := []float64{
50, 75, 100, 125, 150, 200, 300, 500, 750, 1000, 2000, 3000, 5000, 7500, 10000, // 50µs-10ms
25000, 50000, 100000, 250000, 500000, 1000000, 5000000, 10000000, 30000000, 60000000, 120000000, 180000000, // 25ms-3min
}
c.metrics.NewHistogram("app_arango_stats", "Response time of ArangoDB operations in microseconds.", arangoBuckets...)

if err := c.validateConfig(); err != nil {
c.logger.Errorf("config validation error: %v", err)
return
Expand Down Expand Up @@ -127,14 +137,16 @@ func (c *Client) Connect() {
return
}

// Initialize metrics
arangoBuckets := []float64{
50, 75, 100, 125, 150, 200, 300, 500, 750, 1000, 2000, 3000, 5000, 7500, 10000, // 50µs-10ms
25000, 50000, 100000, 250000, 500000, 1000000, 5000000, 10000000, 30000000, 60000000, 120000000, 180000000, // 25ms-3min
c.logger.Logf("Connected to ArangoDB successfully at %s", c.endpoint)
}

// arangoClient returns the underlying driver client, or errNotConnected when Connect failed before creating it.
func (c *Client) arangoClient() (arangodb.Client, error) {
if c.client == nil {
return nil, errNotConnected
}
c.metrics.NewHistogram("app_arango_stats", "Response time of ArangoDB operations in microseconds.", arangoBuckets...)

c.logger.Logf("Connected to ArangoDB successfully at %s", c.endpoint)
return c.client, nil
}

func (c *Client) validateConfig() error {
Expand Down Expand Up @@ -239,7 +251,7 @@ func (c *Client) Query(ctx context.Context, dbName, query string, bindVars map[s
ctx, done := c.instrumentOp(ctx, &QueryLog{Operation: "query", Database: dbName, Query: query})
defer done()

db, err := c.client.GetDatabase(ctx, dbName, nil)
db, err := c.database(ctx, dbName)
if err != nil {
return err
}
Expand Down Expand Up @@ -363,9 +375,18 @@ func (c *Client) HealthCheck(ctx context.Context) (any, error) {
},
}

if c.client == nil {
h.Status = statusDown
h.Details["error"] = errNotConnected.Error()

return &h, errNotConnected
}

version, err := c.client.Version(ctx)
if err != nil {
h.Status = "DOWN"
h.Status = statusDown
h.Details["error"] = err.Error()

return &h, errStatusDown
}

Expand Down
20 changes: 15 additions & 5 deletions pkg/gofr/datasource/arangodb/arango_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ func (d *DB) CreateDB(ctx context.Context, database string) error {
ctx, done := d.client.instrumentOp(ctx, &QueryLog{Operation: "createDB", Database: database})
defer done()

client, err := d.client.arangoClient()
if err != nil {
return err
}

// Check if the database already exists
exists, err := d.client.client.DatabaseExists(ctx, database)
exists, err := client.DatabaseExists(ctx, database)
if err != nil {
return err
}
Expand All @@ -28,7 +33,7 @@ func (d *DB) CreateDB(ctx context.Context, database string) error {
return ErrDatabaseExists
}

_, err = d.client.client.CreateDatabase(ctx, database, nil)
_, err = client.CreateDatabase(ctx, database, nil)

return err
}
Expand All @@ -41,7 +46,12 @@ func (d *DB) DropDB(ctx context.Context, database string) error {
ctx, done := d.client.instrumentOp(ctx, &QueryLog{Operation: opDropDB, Database: database})
defer done()

db, err := d.client.client.GetDatabase(ctx, database, &arangodb.GetDatabaseOptions{})
client, err := d.client.arangoClient()
if err != nil {
return err
}

db, err := client.GetDatabase(ctx, database, &arangodb.GetDatabaseOptions{})
if err != nil {
return err
}
Expand All @@ -62,7 +72,7 @@ func (d *DB) CreateCollection(ctx context.Context, database, collection string,
Collection: collection, Filter: isEdge})
defer done()

db, err := d.client.client.GetDatabase(ctx, database, nil)
db, err := d.client.database(ctx, database)
if err != nil {
return err
}
Expand Down Expand Up @@ -105,7 +115,7 @@ func (d *DB) DropCollection(ctx context.Context, database, collectionName string
}

func (d *DB) getCollection(ctx context.Context, dbName, collectionName string) (arangodb.Collection, error) {
db, err := d.client.client.GetDatabase(ctx, dbName, nil)
db, err := d.client.database(ctx, dbName)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/gofr/datasource/arangodb/arango_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (g *Graph) CreateGraph(ctx context.Context, database, graph string, edgeDef
Database: database, Graph: graph})
defer done()

db, err := g.client.client.GetDatabase(ctx, database, nil)
db, err := g.client.database(ctx, database)
if err != nil {
return err
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func (g *Graph) DropGraph(ctx context.Context, database, graphName string) error
Database: database, Graph: graphName})
defer done()

db, err := g.client.client.GetDatabase(ctx, database, nil)
db, err := g.client.database(ctx, database)
if err != nil {
return err
}
Expand Down Expand Up @@ -140,7 +140,7 @@ func (c *Client) GetEdges(ctx context.Context, dbName, graphName, edgeCollection
})
defer done()

db, err := c.client.GetDatabase(ctx, dbName, nil)
db, err := c.database(ctx, dbName)
if err != nil {
return err
}
Expand Down
32 changes: 26 additions & 6 deletions pkg/gofr/datasource/arangodb/arango_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ import (
)

func (c *Client) user(ctx context.Context, username string) (arangodb.User, error) {
return c.client.User(ctx, username)
client, err := c.arangoClient()
if err != nil {
return nil, err
}

return client.User(ctx, username)
}

func (c *Client) database(ctx context.Context, name string) (arangodb.Database, error) {
return c.client.GetDatabase(ctx, name, nil)
client, err := c.arangoClient()
if err != nil {
return nil, err
}

return client.GetDatabase(ctx, name, nil)
}

// createUser creates a new user in ArangoDB.
Expand All @@ -25,7 +35,12 @@ func (c *Client) createUser(ctx context.Context, username string, options any) e
return fmt.Errorf("%w", errInvalidUserOptionsType)
}

_, err := c.client.CreateUser(ctx, username, userOptions.toArangoUserOptions())
client, err := c.arangoClient()
if err != nil {
return err
}

_, err = client.CreateUser(ctx, username, userOptions.toArangoUserOptions())
if err != nil {
return err
}
Expand All @@ -38,7 +53,12 @@ func (c *Client) dropUser(ctx context.Context, username string) error {
ctx, done := c.instrumentOp(ctx, &QueryLog{Operation: "dropUser", ID: username})
defer done()

err := c.client.RemoveUser(ctx, username)
client, err := c.arangoClient()
if err != nil {
return err
}

err = client.RemoveUser(ctx, username)
if err != nil {
return err
}
Expand All @@ -51,7 +71,7 @@ func (c *Client) grantDB(ctx context.Context, database, username, permission str
ctx, done := c.instrumentOp(ctx, &QueryLog{Operation: "grantDB", Database: database, ID: username})
defer done()

user, err := c.client.User(ctx, username)
user, err := c.user(ctx, username)
if err != nil {
return err
}
Expand All @@ -67,7 +87,7 @@ func (c *Client) grantCollection(ctx context.Context, database, collection, user
Database: database, Collection: collection, ID: username})
defer done()

user, err := c.client.User(ctx, username)
user, err := c.user(ctx, username)
if err != nil {
return err
}
Expand Down
Loading
Loading