Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Merge pull request #20 from microservices-demo/expand_health_endpoint
Browse files Browse the repository at this point in the history
Inital commit for health endpoint expansion
  • Loading branch information
vlal authored Nov 2, 2016
2 parents 17a7275 + a9e45d0 commit 180a32b
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
bin
docker/user/bin
vendor
8 changes: 3 additions & 5 deletions api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package api
// transport.

import (
"time"

"github.com/go-kit/kit/endpoint"
"github.com/microservices-demo/user/db"
"github.com/microservices-demo/user/users"
Expand Down Expand Up @@ -163,7 +161,8 @@ func MakeDeleteEndpoint(s Service) endpoint.Endpoint {
// MakeHealthEndpoint returns current health of the given service.
func MakeHealthEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
return healthResponse{Status: "OK", Time: time.Now().String()}, nil
health := s.Health()
return healthResponse{Health: health}, nil
}
}

Expand Down Expand Up @@ -229,8 +228,7 @@ type healthRequest struct {
}

type healthResponse struct {
Status string `json:"status"`
Time string `json:"time"`
Health []Health `json:"health"`
}

type EmbedStruct struct {
Expand Down
20 changes: 20 additions & 0 deletions api/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,17 @@ func (mw loggingMiddleware) Delete(entity, id string) (err error) {
return mw.next.Delete(entity, id)
}

func (mw loggingMiddleware) Health() (health []Health) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "Health",
"result", len(health),
"took", time.Since(begin),
)
}(time.Now())
return mw.next.Health()
}

type instrumentingService struct {
requestCount metrics.Counter
requestLatency metrics.Histogram
Expand Down Expand Up @@ -243,3 +254,12 @@ func (s *instrumentingService) Delete(entity, id string) error {

return s.Service.Delete(entity, id)
}

func (s *instrumentingService) Health() []Health {
defer func(begin time.Time) {
s.requestCount.With("method", "health").Add(1)
s.requestLatency.With("method", "health").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.Health()
}
26 changes: 26 additions & 0 deletions api/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"io"
"time"

"github.com/microservices-demo/user/db"
"github.com/microservices-demo/user/users"
Expand All @@ -28,6 +29,7 @@ type Service interface {
GetCards(id string) ([]users.Card, error)
PostCard(u users.Card, userid string) (string, error)
Delete(entity, id string) error
Health() []Health // GET /health
}

// NewFixedService returns a simple implementation of the Service interface,
Expand All @@ -37,6 +39,12 @@ func NewFixedService() Service {

type fixedService struct{}

type Health struct {
Service string `json:"service"`
Status string `json:"status"`
Time string `json:"time"`
}

func (s *fixedService) Login(username, password string) (users.User, error) {
u, err := db.GetUserByName(username)
if err != nil {
Expand Down Expand Up @@ -125,6 +133,24 @@ func (s *fixedService) Delete(entity, id string) error {
return db.Delete(entity, id)
}

func (s *fixedService) Health() []Health {
var health []Health
dbstatus := "OK"

err := db.Ping()
if err != nil {
dbstatus = "err"
}

app := Health{"user", "OK", time.Now().String()}
db := Health{"user-db", dbstatus, time.Now().String()}

health = append(health, app)
health = append(health, db)

return health
}

func calculatePassHash(pass, salt string) string {
h := sha1.New()
io.WriteString(h, salt)
Expand Down
6 changes: 6 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Database interface {
GetCards() ([]users.Card, error)
Delete(string, string) error
CreateCard(*users.Card, string) error
Ping() error
}

var (
Expand Down Expand Up @@ -163,3 +164,8 @@ func GetCards() ([]users.Card, error) {
func Delete(entity, id string) error {
return DefaultDb.Delete(entity, id)
}

//Ping invokes DefaultDB method
func Ping() error {
return DefaultDb.Ping()
}
12 changes: 12 additions & 0 deletions db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ func TestGetUserAttributes(t *testing.T) {
}
}

func TestPing(t *testing.T) {
err := Ping()
if err != ErrFakeError {
t.Error("expected fake db error from ping")
}

}

type fake struct{}

func (f fake) Init() error {
Expand Down Expand Up @@ -145,3 +153,7 @@ func (f fake) CreateAddress(u *users.Address, id string) error {
func (f fake) Delete(entity, id string) error {
return ErrFakeError
}

func (f fake) Ping() error {
return ErrFakeError
}
9 changes: 8 additions & 1 deletion db/mongodb/mongodb.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/url"
"os"
"time"

"github.com/microservices-demo/user/users"

Expand Down Expand Up @@ -38,7 +39,7 @@ type Mongo struct {
func (m *Mongo) Init() error {
u := getURL()
var err error
m.Session, err = mgo.Dial(u.String())
m.Session, err = mgo.DialWithTimeout(u.String(), time.Duration(5)*time.Second)
if err != nil {
return err
}
Expand Down Expand Up @@ -456,3 +457,9 @@ func (m *Mongo) EnsureIndexes() error {
c := s.DB("").C("customers")
return c.EnsureIndex(i)
}

func (m *Mongo) Ping() error {
s := m.Session.Copy()
defer s.Close()
return s.Ping()
}
15 changes: 14 additions & 1 deletion db/mongodb/mongodb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,12 @@ func TestGetUserByName(t *testing.T) {
func TestGetUser(t *testing.T) {
TestMongo.Session = TestServer.Session()
defer TestMongo.Session.Close()

_, err := TestMongo.GetUser("1")
if err != nil {
t.Error(err)
}
}

func TestGetUserAttributes(t *testing.T) {
TestMongo.Session = TestServer.Session()
defer TestMongo.Session.Close()
Expand All @@ -149,3 +153,12 @@ func TestGetURL(t *testing.T) {
t.Error("expected url mismatch")
}
}

func TestPing(t *testing.T) {
TestMongo.Session = TestServer.Session()
defer TestMongo.Session.Close()
err := TestMongo.Ping()
if err != nil {
t.Error(err)
}
}

0 comments on commit 180a32b

Please sign in to comment.