From 8234d12ed060295aeddeda6fb83a4dd5ee514435 Mon Sep 17 00:00:00 2001 From: Bjoern Rabenstein Date: Fri, 20 Jun 2014 20:40:48 +0200 Subject: [PATCH] Add InstrumentHandlerFunc. Also, fix seconds to microseconds fot the http instrumentation to match the metric name. Fix Desc.String(). Simplify http error display. Change-Id: Ib7397f4eac1eeed92b291e1c9cc88c080aee99ca --- prometheus/desc.go | 2 +- prometheus/http.go | 15 +++++++++++++-- prometheus/registry.go | 4 +--- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/prometheus/desc.go b/prometheus/desc.go index ec7a506..e3397e4 100644 --- a/prometheus/desc.go +++ b/prometheus/desc.go @@ -168,7 +168,7 @@ func (d *Desc) String() string { for _, lp := range d.constLabelPairs { lpStrings = append( lpStrings, - fmt.Sprintf("%s=%q", lp.Name, lp.Value), + fmt.Sprintf("%s=%q", lp.GetName(), lp.GetValue()), ) } return fmt.Sprintf( diff --git a/prometheus/http.go b/prometheus/http.go index e9b6689..e927fe5 100644 --- a/prometheus/http.go +++ b/prometheus/http.go @@ -92,6 +92,17 @@ func nowSeries(t ...time.Time) nower { // (SummaryVec). Each has three labels: handler, method, code. The value of the // handler label is set by the handlerName parameter of this function. func InstrumentHandler(handlerName string, handler http.Handler) http.HandlerFunc { + return InstrumentHandlerFunc(handlerName, handler.ServeHTTP) +} + +// InstrumentHandlerFunc wraps the given function for instrumentation. It +// registers four metric vector collectors (if not already done) and reports +// http metrics to the (newly or already) registered collectors: +// http_requests_total (CounterVec), http_request_duration_microseconds +// (SummaryVec), http_request_size_bytes (SummaryVec), http_response_size_bytes +// (SummaryVec). Each has three labels: handler, method, code. The value of the +// handler label is set by the handlerName parameter of this function. +func InstrumentHandlerFunc(handlerName string, handlerFunc func(http.ResponseWriter, *http.Request)) http.HandlerFunc { regReqCnt := MustRegisterOrGet(reqCnt).(*CounterVec) regReqDur := MustRegisterOrGet(reqDur).(*SummaryVec) regReqSz := MustRegisterOrGet(reqSz).(*SummaryVec) @@ -103,9 +114,9 @@ func InstrumentHandler(handlerName string, handler http.Handler) http.HandlerFun delegate := &responseWriterDelegator{ResponseWriter: w} out := make(chan int) go computeApproximateRequestSize(r, out) - handler.ServeHTTP(delegate, r) + handlerFunc(delegate, r) - elapsed := float64(time.Since(now)) / float64(time.Second) + elapsed := float64(time.Since(now)) / float64(time.Microsecond) method := sanitizeMethod(r.Method) code := sanitizeCode(delegate.status) diff --git a/prometheus/registry.go b/prometheus/registry.go index 2b5fdff..d513e58 100644 --- a/prometheus/registry.go +++ b/prometheus/registry.go @@ -334,9 +334,7 @@ func (r *registry) ServeHTTP(w http.ResponseWriter, req *http.Request) { if r.panicOnCollectError { panic(err) } - w.WriteHeader(http.StatusInternalServerError) - header.Set(contentTypeHeader, "text/plain") - fmt.Fprintf(w, "An error has occurred:\n\n%s", err) + http.Error(w, "An error has occurred:\n\n"+err.Error(), http.StatusInternalServerError) return } header.Set(contentTypeHeader, contentType)