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
This commit is contained in:
Bjoern Rabenstein 2014-06-20 20:40:48 +02:00
parent 5d40912fd2
commit 8234d12ed0
3 changed files with 15 additions and 6 deletions

View File

@ -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(

View File

@ -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)

View File

@ -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)