Skip to content

Commit 1d1d257

Browse files
aryanmehrotraUmang01-hasharyan-mehrotra-zssrijan-27
authored
Add Redis Healthcheck & Refactor Health check (#216)
* fix sql healthcheck * add redis health check * remove health handler test as it is not checking functionality --------- Co-authored-by: umang01-hash <[email protected]> Co-authored-by: aryan-mehrotra-zs <[email protected]> Co-authored-by: Srijan Rastogi <[email protected]>
1 parent 8e31673 commit 1d1d257

File tree

6 files changed

+48
-17
lines changed

6 files changed

+48
-17
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ go 1.21
44

55
require (
66
github.com/DATA-DOG/go-sqlmock v1.5.2
7-
github.com/alicebob/miniredis/v2 v2.31.1
87
github.com/go-sql-driver/mysql v1.7.1
98
github.com/gorilla/mux v1.8.1
109
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0
@@ -26,6 +25,7 @@ require (
2625

2726
require (
2827
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
28+
github.com/alicebob/miniredis/v2 v2.31.1 // indirect
2929
github.com/cespare/xxhash/v2 v2.2.0 // indirect
3030
github.com/davecgh/go-spew v1.1.1 // indirect
3131
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect

pkg/gofr/container/container.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ type Container struct {
2626
func (c *Container) Health() interface{} {
2727
datasources := make(map[string]interface{})
2828

29-
datasources["sql"] = c.DB.HealthCheck()
29+
if c.DB != nil {
30+
datasources["sql"] = c.DB.HealthCheck()
31+
}
32+
33+
if c.Redis != nil {
34+
datasources["redis"] = c.Redis.HealthCheck()
35+
}
3036

3137
return datasources
3238
}
@@ -48,6 +54,7 @@ func NewContainer(conf config.Config) *Container {
4854
c.Redis, err = redis.NewClient(redis.Config{
4955
HostName: host,
5056
Port: port,
57+
Options: nil,
5158
}, c.Logger)
5259

5360
if err != nil {

pkg/gofr/datasource/health.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package datasource
22

3+
const (
4+
StatusUp = "UP"
5+
StatusDown = "DOWN"
6+
)
7+
38
type Health struct {
49
Status string `json:"status,omitempty"`
510
Details map[string]interface{} `json:"details,omitempty"`
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package redis
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"gofr.dev/pkg/gofr/datasource"
8+
)
9+
10+
func (r *Redis) HealthCheck() datasource.Health {
11+
h := datasource.Health{
12+
Details: make(map[string]interface{}),
13+
}
14+
15+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
16+
defer cancel()
17+
18+
info, err := r.InfoMap(ctx, "Stats").Result()
19+
if err != nil {
20+
h.Status = datasource.StatusDown
21+
h.Details["error"] = err.Error()
22+
23+
return h
24+
}
25+
26+
h.Status = datasource.StatusUp
27+
h.Details["stats"] = info
28+
29+
return h
30+
}

pkg/gofr/datasource/sql/health.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ func (d *DB) HealthCheck() datasource.Health {
1616
defer cancel()
1717

1818
err := d.PingContext(ctx)
19-
2019
if err != nil {
21-
h.Status = "DOWN"
20+
h.Status = datasource.StatusDown
21+
22+
return h
2223
}
2324

24-
h.Status = "UP"
25+
h.Status = datasource.StatusUp
2526
h.Details["stats"] = d.Stats()
2627

2728
return h

pkg/gofr/handler_test.go

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,6 @@ func TestHandler_ServeHTTP(t *testing.T) {
4848
}
4949
}
5050

51-
func TestHandler_healthHandler(t *testing.T) {
52-
c := Context{
53-
Context: context.Background(),
54-
}
55-
56-
data, err := healthHandler(&c)
57-
58-
assert.Equal(t, "OK", data, "TEST Failed.\n")
59-
60-
assert.NoError(t, err, "TEST Failed.\n")
61-
}
62-
6351
func TestHandler_faviconHandler(t *testing.T) {
6452
c := Context{
6553
Context: context.Background(),

0 commit comments

Comments
 (0)