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 #8 from microservices-demo/monitoring/prometheus
Browse files Browse the repository at this point in the history
Added prometheus monitoring.
  • Loading branch information
pidster authored Sep 22, 2016
2 parents 11e9e89 + b4ee669 commit 3f10c16
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ before_install:
- go get github.com/go-kit/kit/log
- go get github.com/go-kit/kit/endpoint
- go get github.com/go-kit/kit/transport/http
- go get github.com/go-sql-driver/mysql
- go get github.com/go-kit/kit/metrics/prometheus
- go get github.com/prometheus/client_golang/prometheus
- go get github.com/go-sql-driver/mysql
- go get github.com/jmoiron/sqlx
- go get gopkg.in/DATA-DOG/go-sqlmock.v1

Expand Down
20 changes: 20 additions & 0 deletions cmd/cataloguesvc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"syscall"

"github.com/go-kit/kit/log"
kitprometheus "github.com/go-kit/kit/metrics/prometheus"
stdprometheus "github.com/prometheus/client_golang/prometheus"

"net/http"

Expand Down Expand Up @@ -60,11 +62,29 @@ func main() {
logger.Log("Error", "Unable to connect to Database", "DSN", dsn)
}

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

// Endpoint domain.
Expand Down
4 changes: 2 additions & 2 deletions docker/catalogue/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ RUN mkdir /app
COPY . /go/src/github.com/microservices-demo/catalogue/
COPY images/ /app/images/

RUN go get github.com/gorilla/mux github.com/go-kit/kit/log github.com/go-kit/kit/endpoint github.com/go-kit/kit/transport/http github.com/go-sql-driver/mysql github.com/jmoiron/sqlx
RUN go get github.com/gorilla/mux github.com/go-kit/kit/log github.com/go-kit/kit/endpoint github.com/go-kit/kit/transport/http github.com/go-sql-driver/mysql github.com/jmoiron/sqlx github.com/go-kit/kit/metrics/prometheus github.com/prometheus/client_golang/prometheus

RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /app/main github.com/microservices-demo/catalogue/cmd/cataloguesvc

CMD ["/app/main", "-port=80"]

EXPOSE 80
EXPOSE 80
52 changes: 52 additions & 0 deletions middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/metrics"
)

// Middleware decorates a service.
Expand Down Expand Up @@ -78,3 +79,54 @@ func (mw loggingMiddleware) Tags() (tags []string, err error) {
}(time.Now())
return mw.next.Tags()
}

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) List(tags []string, order string, pageNum, pageSize int) ([]Sock, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "list").Add(1)
s.requestLatency.With("method", "list").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.List(tags, order, pageNum, pageSize)
}

func (s *instrumentingService) Count(tags []string) (int, error) {
defer func(begin time.Time) {
s.requestCount.With("method", "count").Add(1)
s.requestLatency.With("method", "count").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.Count(tags)
}

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

return s.Service.Get(id)
}

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

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

Expand Down Expand Up @@ -68,6 +69,7 @@ func MakeHTTPHandler(ctx context.Context, e Endpoints, imagePath string, logger
encodeHealthResponse,
options...,
))
r.Handle("/metrics", promhttp.Handler())
return r
}

Expand Down

0 comments on commit 3f10c16

Please sign in to comment.