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 #17 from microservices-demo/monitoring/prometheus
Browse files Browse the repository at this point in the history
Added prometheus monitoring.
  • Loading branch information
Phil Winder committed Sep 23, 2016
2 parents f5af6b3 + 5f85dc7 commit 17a7275
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 5 deletions.
97 changes: 97 additions & 0 deletions api/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/metrics"
"github.com/microservices-demo/user/users"
)

Expand Down Expand Up @@ -146,3 +147,99 @@ func (mw loggingMiddleware) Delete(entity, id string) (err error) {
}(time.Now())
return mw.next.Delete(entity, id)
}

type instrumentingService struct {
requestCount metrics.Counter
requestLatency metrics.Histogram
Service
}

// NewInstrumentingService returns an instance of an instrumenting Service.
func NewInstrumentingService(requestCount metrics.Counter, requestLatency metrics.Histogram, s Service) Service {
return &instrumentingService{
requestCount: requestCount,
requestLatency: requestLatency,
Service: s,
}
}

func (s *instrumentingService) Login(username, password string) (users.User, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "login").Add(1)
s.requestLatency.With("method", "login").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.Login(username, password)
}

func (s *instrumentingService) Register(username, password, email, first, last string) (string, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "register").Add(1)
s.requestLatency.With("method", "register").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.Register(username, password, email, first, last)
}

func (s *instrumentingService) PostUser(user users.User) (string, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "postUser").Add(1)
s.requestLatency.With("method", "postUser").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.PostUser(user)
}

func (s *instrumentingService) GetUsers(id string) (u []users.User, err error) {
defer func(begin time.Time) {
s.requestCount.With("method", "getUsers").Add(1)
s.requestLatency.With("method", "getUsers").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.GetUsers(id)
}

func (s *instrumentingService) PostAddress(add users.Address, id string) (string, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "postAddress").Add(1)
s.requestLatency.With("method", "postAddress").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.PostAddress(add, id)
}

func (s *instrumentingService) GetAddresses(id string) ([]users.Address, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "getAddresses").Add(1)
s.requestLatency.With("method", "getAddresses").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.GetAddresses(id)
}

func (s *instrumentingService) PostCard(card users.Card, id string) (string, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "postCard").Add(1)
s.requestLatency.With("method", "postCard").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.PostCard(card, id)
}

func (s *instrumentingService) GetCards(id string) ([]users.Card, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "getCards").Add(1)
s.requestLatency.With("method", "getCards").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.GetCards(id)
}

func (s *instrumentingService) Delete(entity, id string) error {
defer func(begin time.Time) {
s.requestCount.With("method", "delete").Add(1)
s.requestLatency.With("method", "delete").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.Delete(entity, id)
}
2 changes: 2 additions & 0 deletions api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
httptransport "github.com/go-kit/kit/transport/http"
"github.com/gorilla/mux"
"github.com/microservices-demo/user/users"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -102,6 +103,7 @@ func MakeHTTPHandler(ctx context.Context, e Endpoints, logger log.Logger) http.H
encodeHealthResponse,
options...,
))
r.Handle("/metrics", promhttp.Handler())
return r
}

Expand Down
41 changes: 36 additions & 5 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ import:
subpackages:
- endpoint
- log
- metrics
- metrics/prometheus
- transport/http
- package: github.com/gorilla/mux
- package: github.com/prometheus/client_golang
subpackages:
- prometheus
- prometheus/promhttp
- package: golang.org/x/net
subpackages:
- context
- package: gopkg.in/mgo.v2
subpackages:
- bson
- package: gopkg.in/tomb.v2

20 changes: 20 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
corelog "log"

"github.com/go-kit/kit/log"
kitprometheus "github.com/go-kit/kit/metrics/prometheus"
"github.com/microservices-demo/user/api"
"github.com/microservices-demo/user/db"
"github.com/microservices-demo/user/db/mongodb"
stdprometheus "github.com/prometheus/client_golang/prometheus"
"golang.org/x/net/context"
)

Expand Down Expand Up @@ -53,11 +55,29 @@ func main() {
}
}

fieldKeys := []string{"method"}
// Service domain.
var service api.Service
{
service = api.NewFixedService()
service = api.LoggingMiddleware(logger)(service)
service = api.NewInstrumentingService(
kitprometheus.NewCounterFrom(
stdprometheus.CounterOpts{
Namespace: "microservices_demo",
Subsystem: "user",
Name: "request_count",
Help: "Number of requests received.",
},
fieldKeys),
kitprometheus.NewSummaryFrom(stdprometheus.SummaryOpts{
Namespace: "microservices_demo",
Subsystem: "user",
Name: "request_latency_microseconds",
Help: "Total duration of requests in microseconds.",
}, fieldKeys),
service,
)
}

// Endpoint domain.
Expand Down

0 comments on commit 17a7275

Please sign in to comment.