2019-06-11 16:23:20 +00:00
|
|
|
package handler
|
2016-05-24 15:04:28 +00:00
|
|
|
|
|
|
|
import (
|
2017-02-28 19:39:25 +00:00
|
|
|
"sync"
|
2016-05-24 15:04:28 +00:00
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Metrics provides numbers about the usage of the tusd handler. Since these may
|
|
|
|
// be accessed from multiple goroutines, it is necessary to read and modify them
|
|
|
|
// atomically using the functions exposed in the sync/atomic package, such as
|
|
|
|
// atomic.LoadUint64. In addition the maps must not be modified to prevent data
|
|
|
|
// races.
|
|
|
|
type Metrics struct {
|
|
|
|
// RequestTotal counts the number of incoming requests per method
|
|
|
|
RequestsTotal map[string]*uint64
|
|
|
|
// ErrorsTotal counts the number of returned errors by their message
|
2017-02-28 19:39:25 +00:00
|
|
|
ErrorsTotal *ErrorsTotalMap
|
2016-05-24 15:04:28 +00:00
|
|
|
BytesReceived *uint64
|
|
|
|
UploadsFinished *uint64
|
|
|
|
UploadsCreated *uint64
|
|
|
|
UploadsTerminated *uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
// incRequestsTotal increases the counter for this request method atomically by
|
|
|
|
// one. The method must be one of GET, HEAD, POST, PATCH, DELETE.
|
|
|
|
func (m Metrics) incRequestsTotal(method string) {
|
2016-05-24 15:27:07 +00:00
|
|
|
if ptr, ok := m.RequestsTotal[method]; ok {
|
|
|
|
atomic.AddUint64(ptr, 1)
|
|
|
|
}
|
2016-05-24 15:04:28 +00:00
|
|
|
}
|
|
|
|
|
2021-11-15 12:23:59 +00:00
|
|
|
// TODO: Rework to only store error code
|
2016-05-24 15:04:28 +00:00
|
|
|
// incErrorsTotal increases the counter for this error atomically by one.
|
2017-02-28 19:39:25 +00:00
|
|
|
func (m Metrics) incErrorsTotal(err HTTPError) {
|
|
|
|
ptr := m.ErrorsTotal.retrievePointerFor(err)
|
|
|
|
atomic.AddUint64(ptr, 1)
|
2016-05-24 15:04:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// incBytesReceived increases the number of received bytes atomically be the
|
|
|
|
// specified number.
|
|
|
|
func (m Metrics) incBytesReceived(delta uint64) {
|
|
|
|
atomic.AddUint64(m.BytesReceived, delta)
|
|
|
|
}
|
|
|
|
|
|
|
|
// incUploadsFinished increases the counter for finished uploads atomically by one.
|
|
|
|
func (m Metrics) incUploadsFinished() {
|
|
|
|
atomic.AddUint64(m.UploadsFinished, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// incUploadsCreated increases the counter for completed uploads atomically by one.
|
|
|
|
func (m Metrics) incUploadsCreated() {
|
|
|
|
atomic.AddUint64(m.UploadsCreated, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// incUploadsTerminated increases the counter for completed uploads atomically by one.
|
|
|
|
func (m Metrics) incUploadsTerminated() {
|
|
|
|
atomic.AddUint64(m.UploadsTerminated, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMetrics() Metrics {
|
|
|
|
return Metrics{
|
|
|
|
RequestsTotal: map[string]*uint64{
|
2016-05-24 15:27:07 +00:00
|
|
|
"GET": new(uint64),
|
|
|
|
"HEAD": new(uint64),
|
|
|
|
"POST": new(uint64),
|
|
|
|
"PATCH": new(uint64),
|
|
|
|
"DELETE": new(uint64),
|
|
|
|
"OPTIONS": new(uint64),
|
2016-05-24 15:04:28 +00:00
|
|
|
},
|
|
|
|
ErrorsTotal: newErrorsTotalMap(),
|
|
|
|
BytesReceived: new(uint64),
|
|
|
|
UploadsFinished: new(uint64),
|
|
|
|
UploadsCreated: new(uint64),
|
|
|
|
UploadsTerminated: new(uint64),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-28 19:39:25 +00:00
|
|
|
// ErrorsTotalMap stores the counters for the different HTTP errors.
|
|
|
|
type ErrorsTotalMap struct {
|
|
|
|
lock sync.RWMutex
|
2021-10-25 09:54:02 +00:00
|
|
|
counter map[ErrorsTotalMapEntry]*uint64
|
2017-02-28 19:39:25 +00:00
|
|
|
}
|
|
|
|
|
2021-10-25 09:54:02 +00:00
|
|
|
type ErrorsTotalMapEntry struct {
|
|
|
|
ErrorCode string
|
2017-02-28 19:39:25 +00:00
|
|
|
StatusCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
func newErrorsTotalMap() *ErrorsTotalMap {
|
2021-10-25 09:54:02 +00:00
|
|
|
m := make(map[ErrorsTotalMapEntry]*uint64, 20)
|
2017-02-28 19:39:25 +00:00
|
|
|
return &ErrorsTotalMap{
|
|
|
|
counter: m,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrievePointerFor returns (after creating it if necessary) the pointer to
|
|
|
|
// the counter for the error.
|
|
|
|
func (e *ErrorsTotalMap) retrievePointerFor(err HTTPError) *uint64 {
|
2021-10-25 09:54:02 +00:00
|
|
|
serr := ErrorsTotalMapEntry{
|
|
|
|
ErrorCode: err.ErrorCode(),
|
|
|
|
StatusCode: err.StatusCode(),
|
|
|
|
}
|
|
|
|
|
2017-02-28 19:39:25 +00:00
|
|
|
e.lock.RLock()
|
|
|
|
ptr, ok := e.counter[serr]
|
|
|
|
e.lock.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return ptr
|
|
|
|
}
|
|
|
|
|
|
|
|
// For pointer creation, a write-lock is required
|
|
|
|
e.lock.Lock()
|
|
|
|
// We ensure that the pointer wasn't created in the meantime
|
|
|
|
if ptr, ok = e.counter[serr]; !ok {
|
|
|
|
ptr = new(uint64)
|
|
|
|
e.counter[serr] = ptr
|
|
|
|
}
|
|
|
|
e.lock.Unlock()
|
|
|
|
|
|
|
|
return ptr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load retrieves the map of the counter pointers atomically
|
2021-10-25 09:54:02 +00:00
|
|
|
func (e *ErrorsTotalMap) Load() map[ErrorsTotalMapEntry]*uint64 {
|
|
|
|
m := make(map[ErrorsTotalMapEntry]*uint64, len(e.counter))
|
2017-02-28 19:39:25 +00:00
|
|
|
e.lock.RLock()
|
|
|
|
for err, ptr := range e.counter {
|
2021-10-25 09:54:02 +00:00
|
|
|
m[err] = ptr
|
2017-02-28 19:39:25 +00:00
|
|
|
}
|
|
|
|
e.lock.RUnlock()
|
|
|
|
|
2016-05-24 15:04:28 +00:00
|
|
|
return m
|
|
|
|
}
|