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:
parent
5d40912fd2
commit
8234d12ed0
|
@ -168,7 +168,7 @@ func (d *Desc) String() string {
|
||||||
for _, lp := range d.constLabelPairs {
|
for _, lp := range d.constLabelPairs {
|
||||||
lpStrings = append(
|
lpStrings = append(
|
||||||
lpStrings,
|
lpStrings,
|
||||||
fmt.Sprintf("%s=%q", lp.Name, lp.Value),
|
fmt.Sprintf("%s=%q", lp.GetName(), lp.GetValue()),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
|
|
|
@ -92,6 +92,17 @@ func nowSeries(t ...time.Time) nower {
|
||||||
// (SummaryVec). Each has three labels: handler, method, code. The value of the
|
// (SummaryVec). Each has three labels: handler, method, code. The value of the
|
||||||
// handler label is set by the handlerName parameter of this function.
|
// handler label is set by the handlerName parameter of this function.
|
||||||
func InstrumentHandler(handlerName string, handler http.Handler) http.HandlerFunc {
|
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)
|
regReqCnt := MustRegisterOrGet(reqCnt).(*CounterVec)
|
||||||
regReqDur := MustRegisterOrGet(reqDur).(*SummaryVec)
|
regReqDur := MustRegisterOrGet(reqDur).(*SummaryVec)
|
||||||
regReqSz := MustRegisterOrGet(reqSz).(*SummaryVec)
|
regReqSz := MustRegisterOrGet(reqSz).(*SummaryVec)
|
||||||
|
@ -103,9 +114,9 @@ func InstrumentHandler(handlerName string, handler http.Handler) http.HandlerFun
|
||||||
delegate := &responseWriterDelegator{ResponseWriter: w}
|
delegate := &responseWriterDelegator{ResponseWriter: w}
|
||||||
out := make(chan int)
|
out := make(chan int)
|
||||||
go computeApproximateRequestSize(r, out)
|
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)
|
method := sanitizeMethod(r.Method)
|
||||||
code := sanitizeCode(delegate.status)
|
code := sanitizeCode(delegate.status)
|
||||||
|
|
|
@ -334,9 +334,7 @@ func (r *registry) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
if r.panicOnCollectError {
|
if r.panicOnCollectError {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
http.Error(w, "An error has occurred:\n\n"+err.Error(), http.StatusInternalServerError)
|
||||||
header.Set(contentTypeHeader, "text/plain")
|
|
||||||
fmt.Fprintf(w, "An error has occurred:\n\n%s", err)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
header.Set(contentTypeHeader, contentType)
|
header.Set(contentTypeHeader, contentType)
|
||||||
|
|
Loading…
Reference in New Issue