2019-12-29 02:42:03 +01:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
ee "github.com/lbryio/lbry.go/v2/extras/errors"
|
|
|
|
"github.com/lbryio/lbry.go/v2/extras/stop"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
srv *http.Server
|
|
|
|
stop *stop.Stopper
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewServer(address string, path string) *Server {
|
|
|
|
h := http.NewServeMux()
|
|
|
|
h.Handle(path, promhttp.Handler())
|
|
|
|
return &Server{
|
|
|
|
srv: &http.Server{
|
|
|
|
Addr: address,
|
|
|
|
Handler: h,
|
|
|
|
//https://blog.cloudflare.com/the-complete-guide-to-golang-net-http-timeouts/
|
|
|
|
//https://blog.cloudflare.com/exposing-go-on-the-internet/
|
|
|
|
ReadTimeout: 5 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
IdleTimeout: 120 * time.Second,
|
|
|
|
},
|
|
|
|
stop: stop.New(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Start() {
|
|
|
|
s.stop.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer s.stop.Done()
|
|
|
|
err := s.srv.ListenAndServe()
|
|
|
|
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) Shutdown() {
|
|
|
|
s.srv.Shutdown(context.Background())
|
|
|
|
s.stop.StopAndWait()
|
|
|
|
}
|
|
|
|
|
2019-12-29 17:57:43 +01:00
|
|
|
const (
|
|
|
|
ns = "reflector"
|
|
|
|
|
|
|
|
labelDirection = "direction"
|
|
|
|
labelErrorType = "error_type"
|
|
|
|
|
|
|
|
DirectionUpload = "upload" // to reflector
|
|
|
|
DirectionDownload = "download" // from reflector
|
|
|
|
|
2020-01-13 22:16:46 +01:00
|
|
|
errConnReset = "conn_reset"
|
|
|
|
errReadConnReset = "read_conn_reset"
|
|
|
|
errWriteConnReset = "write_conn_reset"
|
|
|
|
errReadConnTimedOut = "read_conn_timed_out"
|
|
|
|
errWriteConnTimedOut = "write_conn_timed_out"
|
|
|
|
errWriteBrokenPipe = "write_broken_pipe"
|
|
|
|
errEPipe = "e_pipe"
|
|
|
|
errETimedout = "e_timedout"
|
|
|
|
errIOTimeout = "io_timeout"
|
|
|
|
errUnexpectedEOF = "unexpected_eof"
|
|
|
|
errUnexpectedEOFStr = "unexpected_eof_str"
|
|
|
|
errJSONSyntax = "json_syntax"
|
|
|
|
errBlobTooBig = "blob_too_big"
|
|
|
|
errDeadlineExceeded = "deadline_exceeded"
|
|
|
|
errHashMismatch = "hash_mismatch"
|
|
|
|
errZeroByteBlob = "zero_byte_blob"
|
|
|
|
errInvalidCharacter = "invalid_character"
|
|
|
|
errOther = "other"
|
2019-12-29 17:57:43 +01:00
|
|
|
)
|
2019-12-29 02:42:03 +01:00
|
|
|
|
|
|
|
var (
|
|
|
|
BlobDownloadCount = promauto.NewCounter(prometheus.CounterOpts{
|
|
|
|
Namespace: ns,
|
2019-12-29 17:57:43 +01:00
|
|
|
Name: "blob_download_total",
|
2019-12-29 02:42:03 +01:00
|
|
|
Help: "Total number of blobs downloaded from reflector",
|
|
|
|
})
|
|
|
|
BlobUploadCount = promauto.NewCounter(prometheus.CounterOpts{
|
|
|
|
Namespace: ns,
|
|
|
|
Name: "blob_upload_total",
|
|
|
|
Help: "Total number of blobs uploaded to reflector",
|
|
|
|
})
|
|
|
|
SDBlobUploadCount = promauto.NewCounter(prometheus.CounterOpts{
|
|
|
|
Namespace: ns,
|
|
|
|
Name: "sdblob_upload_total",
|
|
|
|
Help: "Total number of SD blobs (and therefore streams) uploaded to reflector",
|
|
|
|
})
|
2019-12-29 17:57:43 +01:00
|
|
|
ErrorCount = promauto.NewCounterVec(prometheus.CounterOpts{
|
2019-12-29 02:42:03 +01:00
|
|
|
Namespace: ns,
|
|
|
|
Name: "error_total",
|
|
|
|
Help: "Total number of errors",
|
2019-12-29 17:57:43 +01:00
|
|
|
}, []string{labelDirection, labelErrorType})
|
2019-12-29 02:42:03 +01:00
|
|
|
)
|
|
|
|
|
2019-12-29 17:57:43 +01:00
|
|
|
func TrackError(direction string, e error) (shouldLog bool) { // shouldLog is a hack, but whatever
|
2019-12-29 02:42:03 +01:00
|
|
|
if e == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := ee.Wrap(e, 0)
|
2019-12-29 17:57:43 +01:00
|
|
|
errType := errOther
|
2020-01-07 14:38:40 +01:00
|
|
|
if strings.Contains(err.Error(), "i/o timeout") {
|
2019-12-29 17:57:43 +01:00
|
|
|
errType = errIOTimeout
|
2019-12-29 02:42:03 +01:00
|
|
|
} else if errors.Is(e, syscall.ECONNRESET) {
|
2020-01-07 14:38:40 +01:00
|
|
|
// Looks like we're getting this when direction == "download", but read_conn_reset and
|
|
|
|
// write_conn_reset when its "upload"
|
2019-12-29 17:57:43 +01:00
|
|
|
errType = errConnReset
|
2020-01-03 16:27:29 +01:00
|
|
|
} else if errors.Is(e, context.DeadlineExceeded) {
|
|
|
|
errType = errDeadlineExceeded
|
2019-12-29 02:42:03 +01:00
|
|
|
} else if strings.Contains(err.Error(), "read: connection reset by peer") { // the other side closed the connection using TCP reset
|
2019-12-29 17:57:43 +01:00
|
|
|
errType = errReadConnReset
|
|
|
|
} else if strings.Contains(err.Error(), "write: connection reset by peer") { // the other side closed the connection using TCP reset
|
|
|
|
errType = errWriteConnReset
|
|
|
|
} else if errors.Is(e, syscall.ETIMEDOUT) {
|
2020-01-03 20:02:00 +01:00
|
|
|
errType = errETimedout
|
2019-12-29 17:57:43 +01:00
|
|
|
} else if strings.Contains(err.Error(), "read: connection timed out") { // the other side closed the connection using TCP reset
|
2020-01-03 20:02:00 +01:00
|
|
|
//log.Warnln("read conn timed out is not the same as ETIMEDOUT")
|
2019-12-29 17:57:43 +01:00
|
|
|
errType = errReadConnTimedOut
|
2020-01-13 22:16:46 +01:00
|
|
|
} else if strings.Contains(err.Error(), "write: connection timed out") {
|
|
|
|
errType = errWriteConnTimedOut
|
2019-12-29 02:42:03 +01:00
|
|
|
} else if errors.Is(e, io.ErrUnexpectedEOF) {
|
2019-12-29 17:57:43 +01:00
|
|
|
errType = errUnexpectedEOF
|
2019-12-29 02:42:03 +01:00
|
|
|
} else if strings.Contains(err.Error(), "unexpected EOF") { // tried to read from closed pipe or socket
|
2020-01-03 16:27:29 +01:00
|
|
|
errType = errUnexpectedEOFStr
|
2019-12-29 02:42:03 +01:00
|
|
|
} else if errors.Is(e, syscall.EPIPE) {
|
2020-01-03 16:27:29 +01:00
|
|
|
errType = errEPipe
|
2019-12-29 02:42:03 +01:00
|
|
|
} else if strings.Contains(err.Error(), "write: broken pipe") { // tried to write to a pipe or socket that was closed by the peer
|
2020-01-03 18:28:01 +01:00
|
|
|
// I believe this is the same as EPipe when direction == "download", but not for upload
|
2019-12-29 17:57:43 +01:00
|
|
|
errType = errWriteBrokenPipe
|
2020-01-02 19:22:51 +01:00
|
|
|
//} else if errors.Is(e, reflector.ErrBlobTooBig) { # this creates a circular import
|
|
|
|
// errType = errBlobTooBig
|
2019-12-29 17:57:43 +01:00
|
|
|
} else if strings.Contains(err.Error(), "blob must be at most") {
|
2020-01-02 19:22:51 +01:00
|
|
|
//log.Warnln("blob must be at most X bytes is not the same as ErrBlobTooBig")
|
2019-12-29 17:57:43 +01:00
|
|
|
errType = errBlobTooBig
|
2020-01-07 14:38:40 +01:00
|
|
|
} else if strings.Contains(err.Error(), "hash of received blob data does not match hash from send request") {
|
|
|
|
errType = errHashMismatch
|
2020-01-13 22:16:46 +01:00
|
|
|
} else if strings.Contains(err.Error(), "0-byte blob received") {
|
|
|
|
errType = errZeroByteBlob
|
|
|
|
} else if strings.Contains(err.Error(), "invalid character") {
|
|
|
|
errType = errInvalidCharacter
|
2019-12-29 17:57:43 +01:00
|
|
|
} else if _, ok := e.(*json.SyntaxError); ok {
|
|
|
|
errType = errJSONSyntax
|
2019-12-29 02:42:03 +01:00
|
|
|
} else {
|
2020-01-03 18:28:01 +01:00
|
|
|
log.Warnf("error '%s' for direction '%s' is not being tracked", err.TypeName(), direction)
|
2019-12-29 02:42:03 +01:00
|
|
|
shouldLog = true
|
|
|
|
}
|
|
|
|
|
2019-12-29 17:57:43 +01:00
|
|
|
ErrorCount.With(map[string]string{
|
|
|
|
labelDirection: direction,
|
|
|
|
labelErrorType: errType,
|
|
|
|
}).Inc()
|
2019-12-29 02:42:03 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|