Use a simplfied error structure for error counting

This commit is contained in:
oliverpool 2017-02-22 11:44:05 +01:00
parent 3da4095c08
commit e2b0050b8d
2 changed files with 24 additions and 11 deletions

View File

@ -77,11 +77,23 @@ func newMetrics() Metrics {
// ErrorsTotalMap stores the counter for the different http errors.
type ErrorsTotalMap struct {
sync.RWMutex
m map[HTTPError]*uint64
m map[simpleHTTPError]*uint64
}
type simpleHTTPError struct {
Msg string
Code int
}
func serializedHTTPError(err HTTPError) simpleHTTPError {
return simpleHTTPError{
Msg: err.Error(),
Code: err.StatusCode(),
}
}
func newErrorsTotalMap() ErrorsTotalMap {
m := make(map[HTTPError]*uint64, 20)
m := make(map[simpleHTTPError]*uint64, 20)
return ErrorsTotalMap{
m: m,
}
@ -90,8 +102,9 @@ func newErrorsTotalMap() ErrorsTotalMap {
// retrievePointerFor returns (after creating it if necessary) the pointer to
// the counter for the error.
func (e *ErrorsTotalMap) retrievePointerFor(err HTTPError) *uint64 {
serr := serializedHTTPError(err)
e.RLock()
ptr, ok := e.m[err]
ptr, ok := e.m[serr]
e.RUnlock()
if ok {
return ptr
@ -100,17 +113,17 @@ func (e *ErrorsTotalMap) retrievePointerFor(err HTTPError) *uint64 {
// For pointer creation, a WriteLock is required
e.Lock()
// We ensure that the ptr wasn't created in the meantime
if ptr, ok = e.m[err]; !ok {
if ptr, ok = e.m[serr]; !ok {
ptr = new(uint64)
e.m[err] = ptr
e.m[serr] = ptr
}
e.Unlock()
return ptr
}
// Load retrieves the map of the counter pointers atomically
func (e *ErrorsTotalMap) Load() (m map[HTTPError]*uint64) {
m = make(map[HTTPError]*uint64, len(e.m))
func (e *ErrorsTotalMap) Load() (m map[simpleHTTPError]*uint64) {
m = make(map[simpleHTTPError]*uint64, len(e.m))
e.RLock()
for err, ptr := range e.m {
m[err] = ptr

View File

@ -25,8 +25,8 @@ var (
[]string{"method"}, nil)
errorsTotalDesc = prometheus.NewDesc(
"tusd_errors_total",
"Total number of errors per cause.",
[]string{"status", "cause"}, nil)
"Total number of errors per status.",
[]string{"status", "message"}, nil)
bytesReceivedDesc = prometheus.NewDesc(
"tusd_bytes_received",
"Number of bytes received for uploads.",
@ -80,8 +80,8 @@ func (c Collector) Collect(metrics chan<- prometheus.Metric) {
errorsTotalDesc,
prometheus.GaugeValue,
float64(atomic.LoadUint64(valuePtr)),
strconv.Itoa(httpError.StatusCode()),
httpError.Error(),
strconv.Itoa(httpError.Code),
httpError.Msg,
)
}