2016-05-24 15:04:28 +00:00
|
|
|
package tusd
|
|
|
|
|
|
|
|
import (
|
2017-02-22 11:22:19 +00:00
|
|
|
"net"
|
2017-02-01 07:37:46 +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-27 21:12:45 +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.
|
2017-02-27 21:12:45 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// incErrorsTotal increases the counter for this error atomically by one.
|
2017-02-27 21:12:45 +00:00
|
|
|
func (m Metrics) incErrorsTotal(err HTTPError) {
|
2017-02-20 15:38:27 +00:00
|
|
|
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.
|
2017-02-27 21:12:45 +00:00
|
|
|
func (m Metrics) incBytesReceived(delta uint64) {
|
2016-05-24 15:04:28 +00:00
|
|
|
atomic.AddUint64(m.BytesReceived, delta)
|
|
|
|
}
|
|
|
|
|
|
|
|
// incUploadsFinished increases the counter for finished uploads atomically by one.
|
2017-02-27 21:12:45 +00:00
|
|
|
func (m Metrics) incUploadsFinished() {
|
2016-05-24 15:04:28 +00:00
|
|
|
atomic.AddUint64(m.UploadsFinished, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// incUploadsCreated increases the counter for completed uploads atomically by one.
|
2017-02-27 21:12:45 +00:00
|
|
|
func (m Metrics) incUploadsCreated() {
|
2016-05-24 15:04:28 +00:00
|
|
|
atomic.AddUint64(m.UploadsCreated, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// incUploadsTerminated increases the counter for completed uploads atomically by one.
|
2017-02-27 21:12:45 +00:00
|
|
|
func (m Metrics) incUploadsTerminated() {
|
2016-05-24 15:04:28 +00:00
|
|
|
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-01 07:37:46 +00:00
|
|
|
// ErrorsTotalMap stores the counter for the different http errors.
|
|
|
|
type ErrorsTotalMap struct {
|
|
|
|
sync.RWMutex
|
2017-02-22 10:44:05 +00:00
|
|
|
m map[simpleHTTPError]*uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type simpleHTTPError struct {
|
|
|
|
Msg string
|
|
|
|
Code int
|
|
|
|
}
|
|
|
|
|
2017-02-22 10:51:56 +00:00
|
|
|
func simplifiedHTTPError(err HTTPError) simpleHTTPError {
|
2017-02-22 11:22:19 +00:00
|
|
|
var msg string
|
|
|
|
// Errors for read timeouts contain too much information which is not
|
|
|
|
// necessary for us and makes grouping for the metrics harder. The error
|
|
|
|
// message looks like: read tcp 127.0.0.1:1080->127.0.0.1:53673: i/o timeout
|
|
|
|
// Therefore, we use a common error message for all of them.
|
|
|
|
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
|
|
|
msg = "read tcp: i/o timeout"
|
|
|
|
} else {
|
|
|
|
msg = err.Error()
|
|
|
|
}
|
2017-02-22 10:44:05 +00:00
|
|
|
return simpleHTTPError{
|
2017-02-22 11:22:19 +00:00
|
|
|
Msg: msg,
|
2017-02-22 10:44:05 +00:00
|
|
|
Code: err.StatusCode(),
|
|
|
|
}
|
2017-02-01 07:37:46 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 21:12:45 +00:00
|
|
|
func newErrorsTotalMap() *ErrorsTotalMap {
|
2017-02-22 10:44:05 +00:00
|
|
|
m := make(map[simpleHTTPError]*uint64, 20)
|
2017-02-27 21:12:45 +00:00
|
|
|
return &ErrorsTotalMap{
|
2017-02-01 07:37:46 +00:00
|
|
|
m: m,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-20 15:38:27 +00:00
|
|
|
// retrievePointerFor returns (after creating it if necessary) the pointer to
|
|
|
|
// the counter for the error.
|
|
|
|
func (e *ErrorsTotalMap) retrievePointerFor(err HTTPError) *uint64 {
|
2017-02-22 10:51:56 +00:00
|
|
|
serr := simplifiedHTTPError(err)
|
2017-02-01 07:37:46 +00:00
|
|
|
e.RLock()
|
2017-02-22 10:44:05 +00:00
|
|
|
ptr, ok := e.m[serr]
|
2017-02-02 07:25:42 +00:00
|
|
|
e.RUnlock()
|
2017-02-20 15:38:27 +00:00
|
|
|
if ok {
|
|
|
|
return ptr
|
2017-02-01 07:37:46 +00:00
|
|
|
}
|
2017-02-20 15:38:27 +00:00
|
|
|
|
|
|
|
// For pointer creation, a WriteLock is required
|
|
|
|
e.Lock()
|
|
|
|
// We ensure that the ptr wasn't created in the meantime
|
2017-02-22 10:44:05 +00:00
|
|
|
if ptr, ok = e.m[serr]; !ok {
|
2017-02-20 15:38:27 +00:00
|
|
|
ptr = new(uint64)
|
2017-02-22 10:44:05 +00:00
|
|
|
e.m[serr] = ptr
|
2017-02-20 15:38:27 +00:00
|
|
|
}
|
|
|
|
e.Unlock()
|
|
|
|
return ptr
|
2017-02-01 07:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load retrieves the map of the counter pointers atomically
|
2017-02-22 10:44:05 +00:00
|
|
|
func (e *ErrorsTotalMap) Load() (m map[simpleHTTPError]*uint64) {
|
|
|
|
m = make(map[simpleHTTPError]*uint64, len(e.m))
|
2017-02-01 07:37:46 +00:00
|
|
|
e.RLock()
|
|
|
|
for err, ptr := range e.m {
|
|
|
|
m[err] = ptr
|
|
|
|
}
|
|
|
|
e.RUnlock()
|
|
|
|
return
|
2016-05-24 15:04:28 +00:00
|
|
|
}
|