Move example LabeledMiddleware out of generated code (#187)
* Move example labeled middleware to _example * Rename handler to endpoint in endpoints.go * Export example middlewares * Rename RequestLatency to Latency
This commit is contained in:
Родитель
a41ee29fb6
Коммит
eb6fc21c66
|
@ -0,0 +1,12 @@
|
|||
# \_example
|
||||
|
||||
`echo.proto` is the service definition for `echo-service`.
|
||||
Try generating it with:
|
||||
|
||||
```
|
||||
truss echo.proto
|
||||
```
|
||||
|
||||
### middlewares
|
||||
|
||||
`middlewares` contains a few example middlewares, including LabeledMiddlewares.
|
|
@ -0,0 +1,42 @@
|
|||
package middlewares
|
||||
|
||||
import (
|
||||
"golang.org/x/net/context"
|
||||
"time"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/go-kit/kit/metrics"
|
||||
)
|
||||
|
||||
// LabeledMiddleware will get passed the endpoint name when passed to
|
||||
// WrapAllLabeledExcept, this can be used to write a generic metrics
|
||||
// middleware which can send the endpoint name to the metrics collector.
|
||||
type LabeledMiddleware func(string, endpoint.Endpoint) endpoint.Endpoint
|
||||
|
||||
// ErrorCounter is a LabeledMiddleware, when applied with WrapAllLabeledExcept name will be populated with the endpoint name, and such this middleware will
|
||||
// report errors to the metric provider with the endpoint name. Feel free to
|
||||
// copy this example middleware to your service.
|
||||
func ErrorCounter(errCount metrics.Counter) LabeledMiddleware {
|
||||
return func(name string, in endpoint.Endpoint) endpoint.Endpoint {
|
||||
return func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
resp, err := in(ctx, req)
|
||||
if err != nil {
|
||||
errCount.With("endpoint", name).Add(1)
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Latency is a LabeledMiddleware, reporting the request time of and
|
||||
// endpoint along with its name
|
||||
func Latency(h metrics.Histogram) LabeledMiddleware {
|
||||
return func(name string, in endpoint.Endpoint) endpoint.Endpoint {
|
||||
return func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
defer func(begin time.Time) {
|
||||
h.With("endpoint", name).Observe(time.Since(begin).Seconds())
|
||||
}(time.Now())
|
||||
return in(ctx, req)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -4,11 +4,6 @@ const EndpointsBase = `
|
|||
package middlewares
|
||||
|
||||
import (
|
||||
"golang.org/x/net/context"
|
||||
|
||||
"github.com/go-kit/kit/endpoint"
|
||||
"github.com/go-kit/kit/metrics"
|
||||
|
||||
"{{.ImportPath -}} /svc"
|
||||
)
|
||||
|
||||
|
@ -20,36 +15,21 @@ import (
|
|||
// (i.e. applied first)
|
||||
func WrapEndpoints(in svc.Endpoints) svc.Endpoints {
|
||||
|
||||
// Pass in the middlewares you want applied to every endpoint.
|
||||
// optionally pass in handlers by name that you want to be excluded
|
||||
// Pass a middleware you want applied to every endpoint.
|
||||
// optionally pass in endpoints by name that you want to be excluded
|
||||
// e.g.
|
||||
// in.WrapAllExcept(authMiddleware, "Status", "Ping")
|
||||
|
||||
// Pass in LabeledMiddlewares you want applied to every endpoint.
|
||||
// These middlewares get passed the handlers name as their first argument when applied.
|
||||
// Pass in a svc.LabeledMiddleware you want applied to every endpoint.
|
||||
// These middlewares get passed the endpoints name as their first argument when applied.
|
||||
// This can be used to write generic metric gathering middlewares that can
|
||||
// report the handler name for free.
|
||||
// in.WrapAllLabeledExcept(errCounter(statsdCounter), "Status", "Ping")
|
||||
// report the endpoint name for free.
|
||||
// github.com/TuneLab/go-truss/_example/middlewares/labeledmiddlewares.go for examples.
|
||||
// in.WrapAllLabeledExcept(errorCounter(statsdCounter), "Status", "Ping")
|
||||
|
||||
// How to apply a middleware to a single endpoint.
|
||||
// in.ExampleEndpoint = authMiddleware(in.ExampleEndpoint)
|
||||
|
||||
return in
|
||||
}
|
||||
|
||||
// errCounter is a LabeledMiddleware, when applied with WrapAllLabeledExcept
|
||||
// name will be populated with the handler name, and such this middleware will
|
||||
// report errors to the metric provider with the handler name. Feel free to
|
||||
// remove this example middleware
|
||||
func errorCounter(errCount metrics.Counter) svc.LabeledMiddleware {
|
||||
return func(name string, in endpoint.Endpoint) endpoint.Endpoint {
|
||||
return func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
resp, err := in(ctx, req)
|
||||
if err != nil {
|
||||
errCount.With("handler", name).Add(1)
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
|
|
@ -9,7 +9,7 @@ package svc
|
|||
// This file contains methods to make individual endpoints from services,
|
||||
// request and response types to serve those endpoints, as well as encoders and
|
||||
// decoders for those types, for all of our supported transport serialization
|
||||
// formats. It also includes endpoint middlewares.
|
||||
// formats.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
@ -94,13 +94,13 @@ func (e *Endpoints) WrapAllExcept(middleware endpoint.Middleware, excluded ...st
|
|||
}
|
||||
}
|
||||
|
||||
// LabeledMiddleware will get passed the handler name when passed to
|
||||
// LabeledMiddleware will get passed the endpoint name when passed to
|
||||
// WrapAllLabeledExcept, this can be used to write a generic metrics
|
||||
// middleware which can send the handler name to the metrics collector.
|
||||
// middleware which can send the endpoint name to the metrics collector.
|
||||
type LabeledMiddleware func(string, endpoint.Endpoint) endpoint.Endpoint
|
||||
|
||||
// WrapAllLabeledExcept wraps each Endpoint field of struct Endpoints with a
|
||||
// LabeledMiddleware, which will receive the name of the handler. See
|
||||
// LabeledMiddleware, which will receive the name of the endpoint. See
|
||||
// LabeldMiddleware. See method WrapAllExept for details on excluded
|
||||
// functionality.
|
||||
func (e *Endpoints) WrapAllLabeledExcept(middleware LabeledMiddleware, excluded ...string) {
|
||||
|
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
Загрузка…
Ссылка в новой задаче