Remove use of route.Context

This commit is contained in:
Tom Wilkie 2017-04-25 17:36:36 -07:00
parent 762b5c479a
commit ba4fc17307
8 changed files with 72 additions and 88 deletions

View File

@ -26,7 +26,6 @@ import (
"github.com/prometheus/common/route" "github.com/prometheus/common/route"
"github.com/prometheus/common/version" "github.com/prometheus/common/version"
"github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/labels"
"golang.org/x/net/context"
"github.com/prometheus/alertmanager/config" "github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/dispatch" "github.com/prometheus/alertmanager/dispatch"
@ -83,9 +82,7 @@ type API struct {
groups groupsFn groups groupsFn
// context is an indirection for testing. mtx sync.RWMutex
context func(r *http.Request) context.Context
mtx sync.RWMutex
} }
type groupsFn func([]*labels.Matcher) dispatch.AlertOverview type groupsFn func([]*labels.Matcher) dispatch.AlertOverview
@ -93,7 +90,6 @@ type groupsFn func([]*labels.Matcher) dispatch.AlertOverview
// New returns a new API. // New returns a new API.
func New(alerts provider.Alerts, silences *silence.Silences, gf groupsFn, router *mesh.Router) *API { func New(alerts provider.Alerts, silences *silence.Silences, gf groupsFn, router *mesh.Router) *API {
return &API{ return &API{
context: route.Context,
alerts: alerts, alerts: alerts,
silences: silences, silences: silences,
groups: gf, groups: gf,
@ -412,7 +408,7 @@ func (api *API) addSilence(w http.ResponseWriter, r *http.Request) {
} }
func (api *API) getSilence(w http.ResponseWriter, r *http.Request) { func (api *API) getSilence(w http.ResponseWriter, r *http.Request) {
sid := route.Param(api.context(r), "sid") sid := route.Param(r.Context(), "sid")
sils, err := api.silences.Query(silence.QIDs(sid)) sils, err := api.silences.Query(silence.QIDs(sid))
if err != nil || len(sils) == 0 { if err != nil || len(sils) == 0 {
@ -432,7 +428,7 @@ func (api *API) getSilence(w http.ResponseWriter, r *http.Request) {
} }
func (api *API) delSilence(w http.ResponseWriter, r *http.Request) { func (api *API) delSilence(w http.ResponseWriter, r *http.Request) {
sid := route.Param(api.context(r), "sid") sid := route.Param(r.Context(), "sid")
if err := api.silences.Expire(sid); err != nil { if err := api.silences.Expire(sid); err != nil {
respondError(w, apiError{ respondError(w, apiError{

View File

@ -249,7 +249,7 @@ func main() {
os.Exit(1) os.Exit(1)
} }
router := route.New(nil) router := route.New()
webReload := make(chan struct{}) webReload := make(chan struct{})
ui.Register(router.WithPrefix(amURL.Path), webReload) ui.Register(router.WithPrefix(amURL.Path), webReload)

View File

@ -50,13 +50,13 @@ func Register(r *route.Router, reloadCh chan<- struct{}) {
r.Get("/app/*filepath", ihf("app_files", r.Get("/app/*filepath", ihf("app_files",
func(w http.ResponseWriter, req *http.Request) { func(w http.ResponseWriter, req *http.Request) {
fp := route.Param(route.Context(req), "filepath") fp := route.Param(req.Context(), "filepath")
serveAsset(w, req, filepath.Join("ui/app", fp)) serveAsset(w, req, filepath.Join("ui/app", fp))
}, },
)) ))
r.Get("/lib/*filepath", ihf("lib_files", r.Get("/lib/*filepath", ihf("lib_files",
func(w http.ResponseWriter, req *http.Request) { func(w http.ResponseWriter, req *http.Request) {
fp := route.Param(route.Context(req), "filepath") fp := route.Param(req.Context(), "filepath")
serveAsset(w, req, filepath.Join("ui/lib", fp)) serveAsset(w, req, filepath.Join("ui/lib", fp))
}, },
)) ))

View File

@ -31,6 +31,7 @@ type Decoder interface {
Decode(*dto.MetricFamily) error Decode(*dto.MetricFamily) error
} }
// DecodeOptions contains options used by the Decoder and in sample extraction.
type DecodeOptions struct { type DecodeOptions struct {
// Timestamp is added to each value from the stream that has no explicit timestamp set. // Timestamp is added to each value from the stream that has no explicit timestamp set.
Timestamp model.Time Timestamp model.Time
@ -142,6 +143,8 @@ func (d *textDecoder) Decode(v *dto.MetricFamily) error {
return nil return nil
} }
// SampleDecoder wraps a Decoder to extract samples from the metric families
// decoded by the wrapped Decoder.
type SampleDecoder struct { type SampleDecoder struct {
Dec Decoder Dec Decoder
Opts *DecodeOptions Opts *DecodeOptions
@ -149,37 +152,51 @@ type SampleDecoder struct {
f dto.MetricFamily f dto.MetricFamily
} }
// Decode calls the Decode method of the wrapped Decoder and then extracts the
// samples from the decoded MetricFamily into the provided model.Vector.
func (sd *SampleDecoder) Decode(s *model.Vector) error { func (sd *SampleDecoder) Decode(s *model.Vector) error {
if err := sd.Dec.Decode(&sd.f); err != nil { err := sd.Dec.Decode(&sd.f)
if err != nil {
return err return err
} }
*s = extractSamples(&sd.f, sd.Opts) *s, err = extractSamples(&sd.f, sd.Opts)
return nil return err
} }
// Extract samples builds a slice of samples from the provided metric families. // ExtractSamples builds a slice of samples from the provided metric
func ExtractSamples(o *DecodeOptions, fams ...*dto.MetricFamily) model.Vector { // families. If an error occurs during sample extraction, it continues to
var all model.Vector // extract from the remaining metric families. The returned error is the last
// error that has occured.
func ExtractSamples(o *DecodeOptions, fams ...*dto.MetricFamily) (model.Vector, error) {
var (
all model.Vector
lastErr error
)
for _, f := range fams { for _, f := range fams {
all = append(all, extractSamples(f, o)...) some, err := extractSamples(f, o)
if err != nil {
lastErr = err
continue
}
all = append(all, some...)
} }
return all return all, lastErr
} }
func extractSamples(f *dto.MetricFamily, o *DecodeOptions) model.Vector { func extractSamples(f *dto.MetricFamily, o *DecodeOptions) (model.Vector, error) {
switch f.GetType() { switch f.GetType() {
case dto.MetricType_COUNTER: case dto.MetricType_COUNTER:
return extractCounter(o, f) return extractCounter(o, f), nil
case dto.MetricType_GAUGE: case dto.MetricType_GAUGE:
return extractGauge(o, f) return extractGauge(o, f), nil
case dto.MetricType_SUMMARY: case dto.MetricType_SUMMARY:
return extractSummary(o, f) return extractSummary(o, f), nil
case dto.MetricType_UNTYPED: case dto.MetricType_UNTYPED:
return extractUntyped(o, f) return extractUntyped(o, f), nil
case dto.MetricType_HISTOGRAM: case dto.MetricType_HISTOGRAM:
return extractHistogram(o, f) return extractHistogram(o, f), nil
} }
panic("expfmt.extractSamples: unknown metric family type") return nil, fmt.Errorf("expfmt.extractSamples: unknown metric family type %v", f.GetType())
} }
func extractCounter(o *DecodeOptions, f *dto.MetricFamily) model.Vector { func extractCounter(o *DecodeOptions, f *dto.MetricFamily) model.Vector {

View File

@ -11,14 +11,15 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// A package for reading and writing Prometheus metrics. // Package expfmt contains tools for reading and writing Prometheus metrics.
package expfmt package expfmt
// Format specifies the HTTP content type of the different wire protocols.
type Format string type Format string
// Constants to assemble the Content-Type values for the different wire protocols.
const ( const (
TextVersion = "0.0.4" TextVersion = "0.0.4"
ProtoType = `application/vnd.google.protobuf` ProtoType = `application/vnd.google.protobuf`
ProtoProtocol = `io.prometheus.client.MetricFamily` ProtoProtocol = `io.prometheus.client.MetricFamily`
ProtoFmt = ProtoType + "; proto=" + ProtoProtocol + ";" ProtoFmt = ProtoType + "; proto=" + ProtoProtocol + ";"

View File

@ -23,6 +23,8 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
) )
var _ logrus.Formatter = (*syslogger)(nil)
func init() { func init() {
setSyslogFormatter = func(appname, local string) error { setSyslogFormatter = func(appname, local string) error {
if appname == "" { if appname == "" {
@ -43,7 +45,7 @@ func init() {
} }
} }
var ceeTag = []byte("@cee:") var prefixTag []byte
type syslogger struct { type syslogger struct {
wrap logrus.Formatter wrap logrus.Formatter
@ -56,6 +58,11 @@ func newSyslogger(appname string, facility string, fmter logrus.Formatter) (*sys
return nil, err return nil, err
} }
out, err := syslog.New(priority, appname) out, err := syslog.New(priority, appname)
_, isJSON := fmter.(*logrus.JSONFormatter)
if isJSON {
// add cee tag to json formatted syslogs
prefixTag = []byte("@cee:")
}
return &syslogger{ return &syslogger{
out: out, out: out,
wrap: fmter, wrap: fmter,
@ -92,7 +99,7 @@ func (s *syslogger) Format(e *logrus.Entry) ([]byte, error) {
} }
// only append tag to data sent to syslog (line), not to what // only append tag to data sent to syslog (line), not to what
// is returned // is returned
line := string(append(ceeTag, data...)) line := string(append(prefixTag, data...))
switch e.Level { switch e.Level {
case logrus.PanicLevel: case logrus.PanicLevel:

View File

@ -1,26 +1,12 @@
package route package route
import ( import (
"fmt"
"net/http" "net/http"
"sync"
"github.com/julienschmidt/httprouter" "github.com/julienschmidt/httprouter"
"golang.org/x/net/context" "golang.org/x/net/context"
) )
var (
mtx = sync.RWMutex{}
ctxts = map[*http.Request]context.Context{}
)
// Context returns the context for the request.
func Context(r *http.Request) context.Context {
mtx.RLock()
defer mtx.RUnlock()
return ctxts[r]
}
type param string type param string
// Param returns param p for the context. // Param returns param p for the context.
@ -33,58 +19,35 @@ func WithParam(ctx context.Context, p, v string) context.Context {
return context.WithValue(ctx, param(p), v) return context.WithValue(ctx, param(p), v)
} }
type contextFn func(r *http.Request) (context.Context, error)
// Router wraps httprouter.Router and adds support for prefixed sub-routers // Router wraps httprouter.Router and adds support for prefixed sub-routers
// and per-request context injections. // and per-request context injections.
type Router struct { type Router struct {
rtr *httprouter.Router rtr *httprouter.Router
prefix string prefix string
ctxFn contextFn
} }
// New returns a new Router. // New returns a new Router.
func New(ctxFn contextFn) *Router { func New() *Router {
if ctxFn == nil {
ctxFn = func(r *http.Request) (context.Context, error) {
return context.Background(), nil
}
}
return &Router{ return &Router{
rtr: httprouter.New(), rtr: httprouter.New(),
ctxFn: ctxFn,
} }
} }
// WithPrefix returns a router that prefixes all registered routes with prefix. // WithPrefix returns a router that prefixes all registered routes with prefix.
func (r *Router) WithPrefix(prefix string) *Router { func (r *Router) WithPrefix(prefix string) *Router {
return &Router{rtr: r.rtr, prefix: r.prefix + prefix, ctxFn: r.ctxFn} return &Router{rtr: r.rtr, prefix: r.prefix + prefix}
} }
// handle turns a HandlerFunc into an httprouter.Handle. // handle turns a HandlerFunc into an httprouter.Handle.
func (r *Router) handle(h http.HandlerFunc) httprouter.Handle { func (r *Router) handle(h http.HandlerFunc) httprouter.Handle {
return func(w http.ResponseWriter, req *http.Request, params httprouter.Params) { return func(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
reqCtx, err := r.ctxFn(req) ctx, cancel := context.WithCancel(req.Context())
if err != nil {
http.Error(w, fmt.Sprintf("Error creating request context: %v", err), http.StatusBadRequest)
return
}
ctx, cancel := context.WithCancel(reqCtx)
defer cancel() defer cancel()
for _, p := range params { for _, p := range params {
ctx = context.WithValue(ctx, param(p.Key), p.Value) ctx = context.WithValue(ctx, param(p.Key), p.Value)
} }
h(w, req.WithContext(ctx))
mtx.Lock()
ctxts[req] = ctx
mtx.Unlock()
h(w, req)
mtx.Lock()
delete(ctxts, req)
mtx.Unlock()
} }
} }
@ -131,7 +94,7 @@ func FileServe(dir string) http.HandlerFunc {
fs := http.FileServer(http.Dir(dir)) fs := http.FileServer(http.Dir(dir))
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = Param(Context(r), "filepath") r.URL.Path = Param(r.Context(), "filepath")
fs.ServeHTTP(w, r) fs.ServeHTTP(w, r)
} }
} }

30
vendor/vendor.json vendored
View File

@ -221,40 +221,40 @@
"revisionTime": "2016-11-21T14:22:35Z" "revisionTime": "2016-11-21T14:22:35Z"
}, },
{ {
"checksumSHA1": "mHyjbJ3BWOfUV6q9f5PBt0gaY1k=", "checksumSHA1": "Wtpzndm/+bdwwNU5PCTfb4oUhc8=",
"path": "github.com/prometheus/common/expfmt", "path": "github.com/prometheus/common/expfmt",
"revision": "0d5de9d6d8629cb8bee6d4674da4127cd8b615a3", "revision": "9e0844febd9e2856f839c9cb974fbd676d1755a8",
"revisionTime": "2016-11-14T13:47:43Z" "revisionTime": "2017-04-18T15:52:10Z"
}, },
{ {
"checksumSHA1": "GWlM3d2vPYyNATtTFgftS10/A9w=", "checksumSHA1": "GWlM3d2vPYyNATtTFgftS10/A9w=",
"path": "github.com/prometheus/common/internal/bitbucket.org/ww/goautoneg", "path": "github.com/prometheus/common/internal/bitbucket.org/ww/goautoneg",
"revision": "0d5de9d6d8629cb8bee6d4674da4127cd8b615a3", "revision": "9e0844febd9e2856f839c9cb974fbd676d1755a8",
"revisionTime": "2016-11-14T13:47:43Z" "revisionTime": "2017-04-18T15:52:10Z"
}, },
{ {
"checksumSHA1": "UU6hIfhVjnAYDADQEfE/3T7Ddm8=", "checksumSHA1": "ZA4MLHNAP905WiAOLy4BBzmcuxM=",
"path": "github.com/prometheus/common/log", "path": "github.com/prometheus/common/log",
"revision": "0d5de9d6d8629cb8bee6d4674da4127cd8b615a3", "revision": "9e0844febd9e2856f839c9cb974fbd676d1755a8",
"revisionTime": "2016-11-14T13:47:43Z" "revisionTime": "2017-04-18T15:52:10Z"
}, },
{ {
"checksumSHA1": "0LL9u9tfv1KPBjNEiMDP6q7lpog=", "checksumSHA1": "0LL9u9tfv1KPBjNEiMDP6q7lpog=",
"path": "github.com/prometheus/common/model", "path": "github.com/prometheus/common/model",
"revision": "49fee292b27bfff7f354ee0f64e1bc4850462edf", "revision": "9e0844febd9e2856f839c9cb974fbd676d1755a8",
"revisionTime": "2017-02-20T10:38:46Z" "revisionTime": "2017-04-18T15:52:10Z"
}, },
{ {
"checksumSHA1": "QQKJYoGcY10nIHxhBEHwjwUZQzk=", "checksumSHA1": "9aDxDuzZt1l7FQJ9qpn2kPcF7NU=",
"path": "github.com/prometheus/common/route", "path": "github.com/prometheus/common/route",
"revision": "0d5de9d6d8629cb8bee6d4674da4127cd8b615a3", "revision": "9e0844febd9e2856f839c9cb974fbd676d1755a8",
"revisionTime": "2016-11-14T13:47:43Z" "revisionTime": "2017-04-18T15:52:10Z"
}, },
{ {
"checksumSHA1": "91KYK0SpvkaMJJA2+BcxbVnyRO0=", "checksumSHA1": "91KYK0SpvkaMJJA2+BcxbVnyRO0=",
"path": "github.com/prometheus/common/version", "path": "github.com/prometheus/common/version",
"revision": "0d5de9d6d8629cb8bee6d4674da4127cd8b615a3", "revision": "9e0844febd9e2856f839c9cb974fbd676d1755a8",
"revisionTime": "2016-11-14T13:47:43Z" "revisionTime": "2017-04-18T15:52:10Z"
}, },
{ {
"checksumSHA1": "gc+7liWFv9C7S5kS2l7qLajiWdc=", "checksumSHA1": "gc+7liWFv9C7S5kS2l7qLajiWdc=",