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 #43 from microservices-demo/refactoring/prometheus
Browse files Browse the repository at this point in the history
Use standard prometheus naming
  • Loading branch information
nustiueudinastea committed Mar 13, 2017
2 parents ca1adfe + 2708659 commit ed60362
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 5 deletions.
4 changes: 2 additions & 2 deletions api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"net/http"
"strings"

"github.com/go-kit/kit/circuitbreaker"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/tracing/opentracing"
httptransport "github.com/go-kit/kit/transport/http"
"github.com/go-kit/kit/circuitbreaker"
"github.com/gorilla/mux"
"github.com/microservices-demo/user/users"
stdopentracing "github.com/opentracing/opentracing-go"
Expand All @@ -25,7 +25,7 @@ var (
)

// MakeHTTPHandler mounts the endpoints into a REST-y HTTP handler.
func MakeHTTPHandler(ctx context.Context, e Endpoints, logger log.Logger, tracer stdopentracing.Tracer) http.Handler {
func MakeHTTPHandler(ctx context.Context, e Endpoints, logger log.Logger, tracer stdopentracing.Tracer) *mux.Router {
r := mux.NewRouter().StrictSlash(false)
options := []httptransport.ServerOption{
httptransport.ServerErrorLogger(logger),
Expand Down
6 changes: 4 additions & 2 deletions glide.lock

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

1 change: 1 addition & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ import:
- package: github.com/afex/hystrix-go
subpackages:
- hystrix
- package: github.com/felixge/httpsnoop
16 changes: 15 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/microservices-demo/user/api"
"github.com/microservices-demo/user/db"
"github.com/microservices-demo/user/db/mongodb"
"github.com/microservices-demo/user/middleware"
stdopentracing "github.com/opentracing/opentracing-go"
zipkin "github.com/openzipkin/zipkin-go-opentracing"
stdprometheus "github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -120,10 +121,23 @@ func main() {
// Endpoint domain.
endpoints := api.MakeEndpoints(service, tracer)

// HTTP router
router := api.MakeHTTPHandler(ctx, endpoints, logger, tracer)

httpMiddleware := []middleware.Interface{
middleware.Instrument{
Duration: middleware.HTTPLatency,
RouteMatcher: router,
Service: ServiceName,
},
}

// Handler
handler := middleware.Merge(httpMiddleware...).Wrap(router)

// Create and launch the HTTP server.
go func() {
logger.Log("transport", "HTTP", "port", port)
handler := api.MakeHTTPHandler(ctx, endpoints, logger, tracer)
errc <- http.ListenAndServe(fmt.Sprintf(":%v", port), handler)
}()

Expand Down
94 changes: 94 additions & 0 deletions middleware/instrument.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package middleware

import (
"net/http"
"regexp"
"strconv"
"strings"
"time"

"github.com/felixge/httpsnoop"
"github.com/gorilla/mux"
"github.com/prometheus/client_golang/prometheus"
)

var (
HTTPLatency = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Name: "request_duration_seconds",
Help: "Time (in seconds) spent serving HTTP requests.",
Buckets: prometheus.DefBuckets,
}, []string{"service", "method", "route", "status_code"})
)

func init() {
prometheus.MustRegister(HTTPLatency)
}

// RouteMatcher matches routes
type RouteMatcher interface {
Match(*http.Request, *mux.RouteMatch) bool
}

// Instrument is a Middleware which records timings for every HTTP request
type Instrument struct {
RouteMatcher RouteMatcher
Duration *prometheus.HistogramVec
Service string
}

// Wrap implements middleware.Interface
func (i Instrument) Wrap(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
begin := time.Now()
interceptor := httpsnoop.CaptureMetrics(next, w, r)
route := i.getRouteName(r)
var (
status = strconv.Itoa(interceptor.Code)
took = time.Since(begin)
)
i.Duration.WithLabelValues(i.Service, r.Method, route, status).Observe(took.Seconds())
})
}

// Return a name identifier for ths request. There are three options:
// 1. The request matches a gorilla mux route, with a name. Use that.
// 2. The request matches an unamed gorilla mux router. Munge the path
// template such that templates like '/api/{org}/foo' come out as
// 'api_org_foo'.
// 3. The request doesn't match a mux route. Munge the Path in the same
// manner as (2).
// We do all this as we do not wish to emit high cardinality labels to
// prometheus.
func (i Instrument) getRouteName(r *http.Request) string {
var routeMatch mux.RouteMatch
if i.RouteMatcher != nil && i.RouteMatcher.Match(r, &routeMatch) {
if name := routeMatch.Route.GetName(); name != "" {
return name
}
if tmpl, err := routeMatch.Route.GetPathTemplate(); err == nil {
return MakeLabelValue(tmpl)
}
}
return MakeLabelValue(r.URL.Path)
}

var invalidChars = regexp.MustCompile(`[^a-zA-Z0-9]+`)

// MakeLabelValue converts a Gorilla mux path to a string suitable for use in
// a Prometheus label value.
func MakeLabelValue(path string) string {
// Convert non-alnums to underscores.
result := invalidChars.ReplaceAllString(path, "_")

// Trim leading and trailing underscores.
result = strings.Trim(result, "_")

// Make it all lowercase
result = strings.ToLower(result)

// Special case.
if result == "" {
result = "root"
}
return result
}
33 changes: 33 additions & 0 deletions middleware/middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package middleware

import (
"net/http"
)

// Interface is the shared contract for all middlesware, and allows middlesware
// to wrap handlers.
type Interface interface {
Wrap(http.Handler) http.Handler
}

// Func is to Interface as http.HandlerFunc is to http.Handler
type Func func(http.Handler) http.Handler

// Wrap implements Interface
func (m Func) Wrap(next http.Handler) http.Handler {
return m(next)
}

// Identity is an Interface which doesn't do anything.
var Identity Interface = Func(func(h http.Handler) http.Handler { return h })

// Merge produces a middleware that applies multiple middlesware in turn;
// ie Merge(f,g,h).Wrap(handler) == f.Wrap(g.Wrap(h.Wrap(handler)))
func Merge(middlesware ...Interface) Interface {
return Func(func(next http.Handler) http.Handler {
for i := len(middlesware) - 1; i >= 0; i-- {
next = middlesware[i].Wrap(next)
}
return next
})
}

0 comments on commit ed60362

Please sign in to comment.