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 #11 from microservices-demo/expand_health_endpoint
Browse files Browse the repository at this point in the history
Initial checkin for expansion
  • Loading branch information
pidster authored Nov 2, 2016
2 parents 07ea2c2 + 9c3166b commit d22c556
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 11 deletions.
8 changes: 3 additions & 5 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package catalogue
// transport.

import (
"time"

"github.com/go-kit/kit/endpoint"
"golang.org/x/net/context"
)
Expand Down Expand Up @@ -70,7 +68,8 @@ func MakeTagsEndpoint(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 @@ -118,6 +117,5 @@ type healthRequest struct {
}

type healthResponse struct {
Status string `json:"status"`
Time string `json:"time"`
Health []Health `json:"health"`
}
19 changes: 19 additions & 0 deletions middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ func (mw loggingMiddleware) Tags() (tags []string, err error) {
return mw.next.Tags()
}

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 @@ -130,3 +141,11 @@ func (s *instrumentingService) Tags() ([]string, error) {

return s.Service.Tags()
}

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()
}
31 changes: 29 additions & 2 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package catalogue
import (
"errors"
"strings"
"time"

"github.com/go-kit/kit/log"
"github.com/jmoiron/sqlx"
Expand All @@ -18,6 +19,7 @@ type Service interface {
Count(tags []string) (int, error) // GET /catalogue/size
Get(id string) (Sock, error) // GET /catalogue/{id}
Tags() ([]string, error) // GET /tags
Health() []Health // GET /health
}

// Sock describes the thing on offer in the catalogue.
Expand All @@ -34,6 +36,13 @@ type Sock struct {
TagString string `json:"-" db:"tag_name"`
}

// Health describes the health of a service
type Health struct {
Service string `json:"service"`
Status string `json:"status"`
Time string `json:"time"`
}

// ErrNotFound is returned when there is no sock for a given ID.
var ErrNotFound = errors.New("not found")

Expand All @@ -46,13 +55,13 @@ var baseQuery = "SELECT sock.sock_id AS id, sock.name, sock.description, sock.pr
// with connection to an SQL database.
func NewCatalogueService(db *sqlx.DB, logger log.Logger) Service {
return &catalogueService{
db: db,
db: db,
logger: logger,
}
}

type catalogueService struct {
db *sqlx.DB
db *sqlx.DB
logger log.Logger
}

Expand Down Expand Up @@ -148,6 +157,24 @@ func (s *catalogueService) Get(id string) (Sock, error) {
return sock, nil
}

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

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

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

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

return health
}

func (s *catalogueService) Tags() ([]string, error) {
var tags []string
query := "SELECT name FROM tag;"
Expand Down
12 changes: 9 additions & 3 deletions test/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,19 @@ def __init__(self, methodName='runTest'):
self.ip = ""

def setUp(self):
Docker().start_container(container_name=self.mysql_container_name, image="mysql", host=self.mysql_container_name, env=[("MYSQL_ROOT_PASSWORD", "abc123"), ("MYSQL_DATABASE", "catalogue")])
Docker().start_container(container_name=self.mysql_container_name,
image="weaveworksdemos/catalogue-db:" + self.TAG,
host=self.mysql_container_name,
env=[("MYSQL_ROOT_PASSWORD", ""),
("MYSQL_ALLOW_EMPTY_PASSWORD", True),
("MYSQL_DATABASE", "socksdb")]
)
# todo: a better way to ensure mysql is up
sleep(15)
sleep(30)
command = ['docker', 'run',
'-d',
'--name', CatalogueContainerTest.container_name,
'--link', "{}:mysql".format(self.mysql_container_name),
'--link', "{}:catalogue-db".format(self.mysql_container_name),
'-h', CatalogueContainerTest.container_name,
'weaveworksdemos/catalogue:' + self.TAG]
Docker().execute(command)
Expand Down
2 changes: 1 addition & 1 deletion transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func MakeHTTPHandler(ctx context.Context, e Endpoints, imagePath string, logger
// GET /catalogue/size Count
// GET /catalogue/{id} Get
// GET /tags Tags
// GET /health Health Check
// GET /health Health Check

r.Methods("GET").Path("/catalogue").Handler(httptransport.NewServer(
ctx,
Expand Down

0 comments on commit d22c556

Please sign in to comment.