From ba4fc17307cb6d8f15d78ae48025bad22cd1619c Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Tue, 25 Apr 2017 17:36:36 -0700 Subject: [PATCH] Remove use of route.Context --- api/api.go | 10 ++-- cmd/alertmanager/main.go | 2 +- ui/web.go | 4 +- .../prometheus/common/expfmt/decode.go | 47 ++++++++++++------ .../prometheus/common/expfmt/expfmt.go | 7 +-- .../prometheus/common/log/syslog_formatter.go | 11 ++++- .../prometheus/common/route/route.go | 49 +++---------------- vendor/vendor.json | 30 ++++++------ 8 files changed, 72 insertions(+), 88 deletions(-) diff --git a/api/api.go b/api/api.go index 4ced08f2..4fcc086b 100644 --- a/api/api.go +++ b/api/api.go @@ -26,7 +26,6 @@ import ( "github.com/prometheus/common/route" "github.com/prometheus/common/version" "github.com/prometheus/prometheus/pkg/labels" - "golang.org/x/net/context" "github.com/prometheus/alertmanager/config" "github.com/prometheus/alertmanager/dispatch" @@ -83,9 +82,7 @@ type API struct { groups groupsFn - // context is an indirection for testing. - context func(r *http.Request) context.Context - mtx sync.RWMutex + mtx sync.RWMutex } type groupsFn func([]*labels.Matcher) dispatch.AlertOverview @@ -93,7 +90,6 @@ type groupsFn func([]*labels.Matcher) dispatch.AlertOverview // New returns a new API. func New(alerts provider.Alerts, silences *silence.Silences, gf groupsFn, router *mesh.Router) *API { return &API{ - context: route.Context, alerts: alerts, silences: silences, 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) { - sid := route.Param(api.context(r), "sid") + sid := route.Param(r.Context(), "sid") sils, err := api.silences.Query(silence.QIDs(sid)) 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) { - sid := route.Param(api.context(r), "sid") + sid := route.Param(r.Context(), "sid") if err := api.silences.Expire(sid); err != nil { respondError(w, apiError{ diff --git a/cmd/alertmanager/main.go b/cmd/alertmanager/main.go index 8493ed59..b615b89e 100644 --- a/cmd/alertmanager/main.go +++ b/cmd/alertmanager/main.go @@ -249,7 +249,7 @@ func main() { os.Exit(1) } - router := route.New(nil) + router := route.New() webReload := make(chan struct{}) ui.Register(router.WithPrefix(amURL.Path), webReload) diff --git a/ui/web.go b/ui/web.go index fa8c799e..48885cf6 100644 --- a/ui/web.go +++ b/ui/web.go @@ -50,13 +50,13 @@ func Register(r *route.Router, reloadCh chan<- struct{}) { r.Get("/app/*filepath", ihf("app_files", 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)) }, )) r.Get("/lib/*filepath", ihf("lib_files", 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)) }, )) diff --git a/vendor/github.com/prometheus/common/expfmt/decode.go b/vendor/github.com/prometheus/common/expfmt/decode.go index 487fdc6c..a7a42d5e 100644 --- a/vendor/github.com/prometheus/common/expfmt/decode.go +++ b/vendor/github.com/prometheus/common/expfmt/decode.go @@ -31,6 +31,7 @@ type Decoder interface { Decode(*dto.MetricFamily) error } +// DecodeOptions contains options used by the Decoder and in sample extraction. type DecodeOptions struct { // Timestamp is added to each value from the stream that has no explicit timestamp set. Timestamp model.Time @@ -142,6 +143,8 @@ func (d *textDecoder) Decode(v *dto.MetricFamily) error { return nil } +// SampleDecoder wraps a Decoder to extract samples from the metric families +// decoded by the wrapped Decoder. type SampleDecoder struct { Dec Decoder Opts *DecodeOptions @@ -149,37 +152,51 @@ type SampleDecoder struct { 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 { - if err := sd.Dec.Decode(&sd.f); err != nil { + err := sd.Dec.Decode(&sd.f) + if err != nil { return err } - *s = extractSamples(&sd.f, sd.Opts) - return nil + *s, err = extractSamples(&sd.f, sd.Opts) + return err } -// Extract samples builds a slice of samples from the provided metric families. -func ExtractSamples(o *DecodeOptions, fams ...*dto.MetricFamily) model.Vector { - var all model.Vector +// ExtractSamples builds a slice of samples from the provided metric +// families. If an error occurs during sample extraction, it continues to +// 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 { - 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() { case dto.MetricType_COUNTER: - return extractCounter(o, f) + return extractCounter(o, f), nil case dto.MetricType_GAUGE: - return extractGauge(o, f) + return extractGauge(o, f), nil case dto.MetricType_SUMMARY: - return extractSummary(o, f) + return extractSummary(o, f), nil case dto.MetricType_UNTYPED: - return extractUntyped(o, f) + return extractUntyped(o, f), nil 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 { diff --git a/vendor/github.com/prometheus/common/expfmt/expfmt.go b/vendor/github.com/prometheus/common/expfmt/expfmt.go index fae10f6e..371ac750 100644 --- a/vendor/github.com/prometheus/common/expfmt/expfmt.go +++ b/vendor/github.com/prometheus/common/expfmt/expfmt.go @@ -11,14 +11,15 @@ // See the License for the specific language governing permissions and // limitations under the License. -// A package for reading and writing Prometheus metrics. +// Package expfmt contains tools for reading and writing Prometheus metrics. package expfmt +// Format specifies the HTTP content type of the different wire protocols. type Format string +// Constants to assemble the Content-Type values for the different wire protocols. const ( - TextVersion = "0.0.4" - + TextVersion = "0.0.4" ProtoType = `application/vnd.google.protobuf` ProtoProtocol = `io.prometheus.client.MetricFamily` ProtoFmt = ProtoType + "; proto=" + ProtoProtocol + ";" diff --git a/vendor/github.com/prometheus/common/log/syslog_formatter.go b/vendor/github.com/prometheus/common/log/syslog_formatter.go index fd8c6fbe..64f5fdac 100644 --- a/vendor/github.com/prometheus/common/log/syslog_formatter.go +++ b/vendor/github.com/prometheus/common/log/syslog_formatter.go @@ -23,6 +23,8 @@ import ( "github.com/Sirupsen/logrus" ) +var _ logrus.Formatter = (*syslogger)(nil) + func init() { setSyslogFormatter = func(appname, local string) error { if appname == "" { @@ -43,7 +45,7 @@ func init() { } } -var ceeTag = []byte("@cee:") +var prefixTag []byte type syslogger struct { wrap logrus.Formatter @@ -56,6 +58,11 @@ func newSyslogger(appname string, facility string, fmter logrus.Formatter) (*sys return nil, err } out, err := syslog.New(priority, appname) + _, isJSON := fmter.(*logrus.JSONFormatter) + if isJSON { + // add cee tag to json formatted syslogs + prefixTag = []byte("@cee:") + } return &syslogger{ out: out, 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 // is returned - line := string(append(ceeTag, data...)) + line := string(append(prefixTag, data...)) switch e.Level { case logrus.PanicLevel: diff --git a/vendor/github.com/prometheus/common/route/route.go b/vendor/github.com/prometheus/common/route/route.go index 930b52d4..bb468817 100644 --- a/vendor/github.com/prometheus/common/route/route.go +++ b/vendor/github.com/prometheus/common/route/route.go @@ -1,26 +1,12 @@ package route import ( - "fmt" "net/http" - "sync" "github.com/julienschmidt/httprouter" "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 // 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) } -type contextFn func(r *http.Request) (context.Context, error) - // Router wraps httprouter.Router and adds support for prefixed sub-routers // and per-request context injections. type Router struct { rtr *httprouter.Router prefix string - ctxFn contextFn } // New returns a new Router. -func New(ctxFn contextFn) *Router { - if ctxFn == nil { - ctxFn = func(r *http.Request) (context.Context, error) { - return context.Background(), nil - } - } +func New() *Router { return &Router{ - rtr: httprouter.New(), - ctxFn: ctxFn, + rtr: httprouter.New(), } } // WithPrefix returns a router that prefixes all registered routes with prefix. 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. func (r *Router) handle(h http.HandlerFunc) httprouter.Handle { return func(w http.ResponseWriter, req *http.Request, params httprouter.Params) { - reqCtx, err := r.ctxFn(req) - if err != nil { - http.Error(w, fmt.Sprintf("Error creating request context: %v", err), http.StatusBadRequest) - return - } - ctx, cancel := context.WithCancel(reqCtx) + ctx, cancel := context.WithCancel(req.Context()) defer cancel() for _, p := range params { ctx = context.WithValue(ctx, param(p.Key), p.Value) } - - mtx.Lock() - ctxts[req] = ctx - mtx.Unlock() - - h(w, req) - - mtx.Lock() - delete(ctxts, req) - mtx.Unlock() + h(w, req.WithContext(ctx)) } } @@ -131,7 +94,7 @@ func FileServe(dir string) http.HandlerFunc { fs := http.FileServer(http.Dir(dir)) 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) } } diff --git a/vendor/vendor.json b/vendor/vendor.json index 0d5c062b..191050ca 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -221,40 +221,40 @@ "revisionTime": "2016-11-21T14:22:35Z" }, { - "checksumSHA1": "mHyjbJ3BWOfUV6q9f5PBt0gaY1k=", + "checksumSHA1": "Wtpzndm/+bdwwNU5PCTfb4oUhc8=", "path": "github.com/prometheus/common/expfmt", - "revision": "0d5de9d6d8629cb8bee6d4674da4127cd8b615a3", - "revisionTime": "2016-11-14T13:47:43Z" + "revision": "9e0844febd9e2856f839c9cb974fbd676d1755a8", + "revisionTime": "2017-04-18T15:52:10Z" }, { "checksumSHA1": "GWlM3d2vPYyNATtTFgftS10/A9w=", "path": "github.com/prometheus/common/internal/bitbucket.org/ww/goautoneg", - "revision": "0d5de9d6d8629cb8bee6d4674da4127cd8b615a3", - "revisionTime": "2016-11-14T13:47:43Z" + "revision": "9e0844febd9e2856f839c9cb974fbd676d1755a8", + "revisionTime": "2017-04-18T15:52:10Z" }, { - "checksumSHA1": "UU6hIfhVjnAYDADQEfE/3T7Ddm8=", + "checksumSHA1": "ZA4MLHNAP905WiAOLy4BBzmcuxM=", "path": "github.com/prometheus/common/log", - "revision": "0d5de9d6d8629cb8bee6d4674da4127cd8b615a3", - "revisionTime": "2016-11-14T13:47:43Z" + "revision": "9e0844febd9e2856f839c9cb974fbd676d1755a8", + "revisionTime": "2017-04-18T15:52:10Z" }, { "checksumSHA1": "0LL9u9tfv1KPBjNEiMDP6q7lpog=", "path": "github.com/prometheus/common/model", - "revision": "49fee292b27bfff7f354ee0f64e1bc4850462edf", - "revisionTime": "2017-02-20T10:38:46Z" + "revision": "9e0844febd9e2856f839c9cb974fbd676d1755a8", + "revisionTime": "2017-04-18T15:52:10Z" }, { - "checksumSHA1": "QQKJYoGcY10nIHxhBEHwjwUZQzk=", + "checksumSHA1": "9aDxDuzZt1l7FQJ9qpn2kPcF7NU=", "path": "github.com/prometheus/common/route", - "revision": "0d5de9d6d8629cb8bee6d4674da4127cd8b615a3", - "revisionTime": "2016-11-14T13:47:43Z" + "revision": "9e0844febd9e2856f839c9cb974fbd676d1755a8", + "revisionTime": "2017-04-18T15:52:10Z" }, { "checksumSHA1": "91KYK0SpvkaMJJA2+BcxbVnyRO0=", "path": "github.com/prometheus/common/version", - "revision": "0d5de9d6d8629cb8bee6d4674da4127cd8b615a3", - "revisionTime": "2016-11-14T13:47:43Z" + "revision": "9e0844febd9e2856f839c9cb974fbd676d1755a8", + "revisionTime": "2017-04-18T15:52:10Z" }, { "checksumSHA1": "gc+7liWFv9C7S5kS2l7qLajiWdc=",