-
Notifications
You must be signed in to change notification settings - Fork 1.7k
InfluxDB Connector for GoFr #2072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Changes from 6 commits
7eab5de
495cca8
d12d464
359e1d3
1b287d3
b926361
725354d
f5c1422
5069344
e4d573f
080eb8f
9cf1c3d
5896876
b544346
e2b7d69
aabc2b5
2090fc1
68b2ab8
06d9a6f
e2e4a8f
ad1116c
9befb08
1588bf7
2f0e55f
14b3048
6b549db
91d4b2f
de341d3
cb68b3d
f2002ac
f8bc4c4
dcafa99
c2e7036
be1f3f5
70933b7
8859e29
cb85903
e797747
b1e3086
f2b8ab8
5262ff0
e6038ed
3a21868
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
# InfluxDB | ||
GoFr supports injecting InfluxDB using an interface that defines the necessary methods to interact with InfluxDB v2+. | ||
Any driver that implements this interface can be injected via the `app.AddInfluxDB()` method. | ||
|
||
--- | ||
|
||
## Interface | ||
|
||
```go | ||
// InfluxDB defines the methods for interacting with an InfluxDB database. | ||
type InfluxDB interface { | ||
Connect() | ||
CreateOrganization(ctx context.Context, orgName string) (string, error) | ||
DeleteOrganization(ctx context.Context, orgID string) error | ||
ListOrganization(ctx context.Context) (map[string]string, error) | ||
|
||
CreateBucket(ctx context.Context, orgID string, bucketName string, retentionPeriod time.Duration) (string, error) | ||
DeleteBucket(ctx context.Context, orgID, bucketID string) error | ||
ListBuckets(ctx context.Context, org string) (map[string]string, error) | ||
|
||
Ping(ctx context.Context) (bool, error) | ||
HealthCheck(ctx context.Context) (any, error) | ||
|
||
Query(ctx context.Context, org string, fluxQuery string) ([]map[string]any, error) | ||
WritePoints(ctx context.Context, bucket string, org string, points []container.InfluxPoint) error) | ||
|
||
UseLogger(logger any) | ||
UseMetrics(metrics any) | ||
UseTracer(tracer any) | ||
Comment on lines
+27
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not exposed to user. Refer to mongoDB datasource addition for more info. |
||
} | ||
``` | ||
|
||
This structure supports all essential InfluxDB operations including organization/bucket management, health checks, and metrics ingestion. | ||
|
||
Import the gofr's external driver for influxdb: | ||
|
||
```bash | ||
go get gofr.dev/pkg/gofr/datasource/influxdb@latest | ||
``` | ||
|
||
## Example | ||
```go | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"gofr.dev/pkg/gofr" | ||
"gofr.dev/pkg/gofr/datasource/influxdb" | ||
) | ||
|
||
func main() { | ||
|
||
// Create a new GoFr application | ||
app := gofr.New() | ||
|
||
// Initialize InfluxDB client | ||
client := influxdb.New(influxdb.Config{ | ||
Url: "http://localhost:8086", | ||
Username: "admin", | ||
Password: "admin1234", | ||
Token: "<your-token>", | ||
}) | ||
|
||
// Add InfluxDB to application context | ||
app.AddInfluxDB(client) | ||
|
||
// Sample route | ||
app.GET("/greet", func(ctx *gofr.Context) (any, error) { | ||
return "Hello World!", nil | ||
}) | ||
|
||
// Ping InfluxDB | ||
ok, err := client.Ping(context.Background()) | ||
if err != nil { | ||
fmt.Println("Ping failed:", err) | ||
return | ||
} | ||
fmt.Println("InfluxDB connected:", ok) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use app.logger() |
||
|
||
// Create organization | ||
orgID, err := client.CreateOrganization(context.Background(), "demo-org") | ||
if err != nil { | ||
fmt.Println("CreateOrganization error:", err) | ||
return | ||
} | ||
|
||
// List organizations | ||
orgs, _ := client.ListOrganization(context.Background()) | ||
fmt.Println("Organizations:") | ||
for id, name := range orgs { | ||
fmt.Printf("- %s: %s\n", id, name) | ||
} | ||
|
||
// Create bucket | ||
bucketID, err := client.CreateBucket(context.Background(), orgID, "demo-bucket", time.Hour) | ||
if err != nil { | ||
fmt.Println("CreateBucket error:", err) | ||
return | ||
} | ||
|
||
// List buckets for organization | ||
buckets, err := client.ListBuckets(context.Background(), "demo-org") | ||
if err != nil { | ||
fmt.Println("ListBuckets error:", err) | ||
return | ||
} | ||
fmt.Println("Buckets:", buckets) | ||
|
||
// Delete bucket | ||
if err := client.DeleteBucket(context.Background(), orgID, bucketID); err != nil { | ||
fmt.Println("DeleteBucket error:", err) | ||
return | ||
} | ||
fmt.Println("Bucket deleted successfully") | ||
|
||
// Delete organization | ||
if err := client.DeleteOrganization(context.Background(), orgID); err != nil { | ||
fmt.Println("DeleteOrganization error:", err) | ||
return | ||
} | ||
fmt.Println("Organization deleted successfully") | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Write a setup function .... pseudo and then make REST endpoints for adding, retrieving data. |
||
// Start the server | ||
app.Run() | ||
} | ||
``` |
Uh oh!
There was an error while loading. Please reload this page.