Use a simplfied error structure for error counting
This commit is contained in:
parent
3da4095c08
commit
e2b0050b8d
27
metrics.go
27
metrics.go
|
@ -77,11 +77,23 @@ func newMetrics() Metrics {
|
||||||
// ErrorsTotalMap stores the counter for the different http errors.
|
// ErrorsTotalMap stores the counter for the different http errors.
|
||||||
type ErrorsTotalMap struct {
|
type ErrorsTotalMap struct {
|
||||||
sync.RWMutex
|
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 {
|
func newErrorsTotalMap() ErrorsTotalMap {
|
||||||
m := make(map[HTTPError]*uint64, 20)
|
m := make(map[simpleHTTPError]*uint64, 20)
|
||||||
return ErrorsTotalMap{
|
return ErrorsTotalMap{
|
||||||
m: m,
|
m: m,
|
||||||
}
|
}
|
||||||
|
@ -90,8 +102,9 @@ func newErrorsTotalMap() ErrorsTotalMap {
|
||||||
// retrievePointerFor returns (after creating it if necessary) the pointer to
|
// retrievePointerFor returns (after creating it if necessary) the pointer to
|
||||||
// the counter for the error.
|
// the counter for the error.
|
||||||
func (e *ErrorsTotalMap) retrievePointerFor(err HTTPError) *uint64 {
|
func (e *ErrorsTotalMap) retrievePointerFor(err HTTPError) *uint64 {
|
||||||
|
serr := serializedHTTPError(err)
|
||||||
e.RLock()
|
e.RLock()
|
||||||
ptr, ok := e.m[err]
|
ptr, ok := e.m[serr]
|
||||||
e.RUnlock()
|
e.RUnlock()
|
||||||
if ok {
|
if ok {
|
||||||
return ptr
|
return ptr
|
||||||
|
@ -100,17 +113,17 @@ func (e *ErrorsTotalMap) retrievePointerFor(err HTTPError) *uint64 {
|
||||||
// For pointer creation, a WriteLock is required
|
// For pointer creation, a WriteLock is required
|
||||||
e.Lock()
|
e.Lock()
|
||||||
// We ensure that the ptr wasn't created in the meantime
|
// 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)
|
ptr = new(uint64)
|
||||||
e.m[err] = ptr
|
e.m[serr] = ptr
|
||||||
}
|
}
|
||||||
e.Unlock()
|
e.Unlock()
|
||||||
return ptr
|
return ptr
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load retrieves the map of the counter pointers atomically
|
// Load retrieves the map of the counter pointers atomically
|
||||||
func (e *ErrorsTotalMap) Load() (m map[HTTPError]*uint64) {
|
func (e *ErrorsTotalMap) Load() (m map[simpleHTTPError]*uint64) {
|
||||||
m = make(map[HTTPError]*uint64, len(e.m))
|
m = make(map[simpleHTTPError]*uint64, len(e.m))
|
||||||
e.RLock()
|
e.RLock()
|
||||||
for err, ptr := range e.m {
|
for err, ptr := range e.m {
|
||||||
m[err] = ptr
|
m[err] = ptr
|
||||||
|
|
|
@ -25,8 +25,8 @@ var (
|
||||||
[]string{"method"}, nil)
|
[]string{"method"}, nil)
|
||||||
errorsTotalDesc = prometheus.NewDesc(
|
errorsTotalDesc = prometheus.NewDesc(
|
||||||
"tusd_errors_total",
|
"tusd_errors_total",
|
||||||
"Total number of errors per cause.",
|
"Total number of errors per status.",
|
||||||
[]string{"status", "cause"}, nil)
|
[]string{"status", "message"}, nil)
|
||||||
bytesReceivedDesc = prometheus.NewDesc(
|
bytesReceivedDesc = prometheus.NewDesc(
|
||||||
"tusd_bytes_received",
|
"tusd_bytes_received",
|
||||||
"Number of bytes received for uploads.",
|
"Number of bytes received for uploads.",
|
||||||
|
@ -80,8 +80,8 @@ func (c Collector) Collect(metrics chan<- prometheus.Metric) {
|
||||||
errorsTotalDesc,
|
errorsTotalDesc,
|
||||||
prometheus.GaugeValue,
|
prometheus.GaugeValue,
|
||||||
float64(atomic.LoadUint64(valuePtr)),
|
float64(atomic.LoadUint64(valuePtr)),
|
||||||
strconv.Itoa(httpError.StatusCode()),
|
strconv.Itoa(httpError.Code),
|
||||||
httpError.Error(),
|
httpError.Msg,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue