Commit 1714eca3 authored by Phil Winder's avatar Phil Winder Committed by GitHub
Browse files

Merge pull request #45 from microservices-demo/refactor/weave-common

Reuse middleware from weaveworks/common.
parents e147999a c06f85b0
language: go
go: 1.6
go: 1.7
sudo: required
env:
- GROUP=weaveworksdemos COMMIT=$TRAVIS_COMMIT TAG=$TRAVIS_TAG
......
FROM golang:1.6-alpine
FROM golang:1.7-alpine
ENV sourcesdir /go/src/github.com/microservices-demo/user/
ENV MONGO_HOST mytestdb:27017
ENV HATEAOS user
......
......@@ -6,7 +6,7 @@ ENV PATH $GOPATH/bin:/usr/local/go/bin:$PATH
RUN apt-get update && apt-get install -yq git curl
RUN curl -sSL https://storage.googleapis.com/golang/go1.6.linux-amd64.tar.gz -o go.tar.gz && \
RUN curl -sSL https://storage.googleapis.com/golang/go1.7.linux-amd64.tar.gz -o go.tar.gz && \
tar -C /usr/local -xvf go.tar.gz
RUN go get -v github.com/Masterminds/glide
......
hash: b6132ca44a02e5b84ad8f27e24283918ff7c566256ebd76fac73564646aec258
updated: 2017-03-13T12:37:17.157991208Z
hash: 77eab7ed7829910ee3d4f0266da67abc35fc1c8635bf11e577842222cf0e4791
updated: 2017-03-21T15:35:06.768490243Z
imports:
- name: github.com/afex/hystrix-go
version: 39520ddd07a9d9a071d615f7476798659f5a3b89
subpackages:
- hystrix
- hystrix/metric_collector
- hystrix/rolling
- name: github.com/apache/thrift
version: e0ccbd6e62e14f32d7c5fe0f9cec6eff3259b863
subpackages:
......@@ -106,21 +104,41 @@ imports:
version: 1f30fe9094a513ce4c700b9a54458bbb0c96996c
- name: github.com/Shopify/sarama
version: 0fb560e5f7fbcaee2f75e3c34174320709f69944
- name: github.com/sony/gobreaker
version: 809bcd5a528f344e95c2368796c9225d128f9b3e
- name: github.com/streadway/handy
version: f450267a206e480d863d2a92846a40f6e2896b2a
subpackages:
- breaker
- name: github.com/Sirupsen/logrus
version: 10f801ebc38b33738c9d17d50860f484a0988ff5
- name: github.com/weaveworks/common
version: f94043b3da140c7a735b1f2f286d72d19014b200
subpackages:
- errors
- middleware
- user
- name: golang.org/x/net
version: 7394c112eae4dba7e96bfcfe738e6373d61772b4
subpackages:
- context
- context/ctxhttp
- http2
- http2/hpack
- internal/timeseries
- lex/httplex
- trace
- name: golang.org/x/sys
version: 99f16d856c9836c42d24e7ab64ea72916925fa97
subpackages:
- unix
- name: google.golang.org/grpc
version: 50955793b0183f9de69bd78e2ec251cf20aab121
subpackages:
- codes
- credentials
- grpclog
- internal
- metadata
- naming
- peer
- stats
- tap
- transport
- name: gopkg.in/mgo.v2
version: 3f83fa5005286a7fe593b055f0d7771a7dce4655
subpackages:
......
......@@ -27,3 +27,6 @@ import:
subpackages:
- hystrix
- package: github.com/felixge/httpsnoop
- package: github.com/weaveworks/common
subpackages:
- middleware
......@@ -15,26 +15,32 @@ 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"
commonMiddleware "github.com/weaveworks/common/middleware"
"golang.org/x/net/context"
)
var (
dev bool
port string
acc string
zip string
)
var (
HTTPLatency = stdprometheus.NewHistogramVec(stdprometheus.HistogramOpts{
Name: "request_duration_seconds",
Help: "Time (in seconds) spent serving HTTP requests.",
Buckets: stdprometheus.DefBuckets,
}, []string{"method", "route", "status_code", "isWS"})
)
const (
ServiceName = "user"
)
func init() {
stdprometheus.MustRegister(HTTPLatency)
flag.StringVar(&zip, "zipkin", os.Getenv("ZIPKIN"), "Zipkin address")
flag.StringVar(&port, "port", "8084", "Port on which to run")
db.Register("mongodb", &mongodb.Mongo{})
......@@ -124,16 +130,15 @@ func main() {
// HTTP router
router := api.MakeHTTPHandler(ctx, endpoints, logger, tracer)
httpMiddleware := []middleware.Interface{
middleware.Instrument{
Duration: middleware.HTTPLatency,
httpMiddleware := []commonMiddleware.Interface{
commonMiddleware.Instrument{
Duration: HTTPLatency,
RouteMatcher: router,
Service: ServiceName,
},
}
// Handler
handler := middleware.Merge(httpMiddleware...).Wrap(router)
handler := commonMiddleware.Merge(httpMiddleware...).Wrap(router)
// Create and launch the HTTP server.
go func() {
......
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
}
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
})
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment