2019-08-22 09:27:18 +00:00
|
|
|
package opentsdbhttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-12-13 22:29:14 +00:00
|
|
|
"net"
|
2019-08-22 09:27:18 +00:00
|
|
|
"net/http"
|
2019-12-13 22:29:14 +00:00
|
|
|
"sync"
|
2019-08-22 09:27:18 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2019-12-13 22:29:14 +00:00
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/netutil"
|
2019-08-22 09:27:18 +00:00
|
|
|
"github.com/VictoriaMetrics/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
writeRequests = metrics.NewCounter(`vm_http_requests_total{path="/api/put", protocol="opentsdb-http"}`)
|
|
|
|
writeErrors = metrics.NewCounter(`vm_http_request_errors_total{path="/api/put", protocol="opentsdb-http"}`)
|
|
|
|
)
|
|
|
|
|
2019-12-13 22:29:14 +00:00
|
|
|
// Server represents HTTP OpenTSDB server.
|
|
|
|
type Server struct {
|
|
|
|
s *http.Server
|
|
|
|
ln net.Listener
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
2019-08-22 09:27:18 +00:00
|
|
|
|
2019-12-13 22:29:14 +00:00
|
|
|
// MustStart starts HTTP OpenTSDB server on the given addr.
|
|
|
|
//
|
|
|
|
// MustStop must be called on the returned server when it is no longer needed.
|
|
|
|
func MustStart(addr string, maxRequestSize int64) *Server {
|
2019-08-22 09:27:18 +00:00
|
|
|
logger.Infof("starting HTTP OpenTSDB server at %q", addr)
|
2019-12-13 22:29:14 +00:00
|
|
|
lnTCP, err := netutil.NewTCPListener("opentsdbhttp", addr)
|
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("cannot start HTTP OpenTSDB collector at %q: %s", addr, err)
|
|
|
|
}
|
|
|
|
return MustServe(lnTCP, maxRequestSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MustServe serves OpenTSDB HTTP put requests from ln with up to maxRequestSize size.
|
|
|
|
//
|
|
|
|
// MustStop must be called on the returned server when it is no longer needed.
|
|
|
|
func MustServe(ln net.Listener, maxRequestSize int64) *Server {
|
|
|
|
h := newRequestHandler(maxRequestSize)
|
|
|
|
hs := &http.Server{
|
|
|
|
Handler: h,
|
2019-08-22 09:27:18 +00:00
|
|
|
ReadTimeout: 30 * time.Second,
|
|
|
|
WriteTimeout: 10 * time.Second,
|
|
|
|
}
|
2019-12-13 22:29:14 +00:00
|
|
|
s := &Server{
|
|
|
|
s: hs,
|
|
|
|
ln: ln,
|
|
|
|
}
|
|
|
|
s.wg.Add(1)
|
2019-08-22 09:27:18 +00:00
|
|
|
go func() {
|
2019-12-13 22:29:14 +00:00
|
|
|
defer s.wg.Done()
|
|
|
|
err := s.s.Serve(s.ln)
|
2019-08-22 09:27:18 +00:00
|
|
|
if err == http.ErrServerClosed {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-12-13 22:29:14 +00:00
|
|
|
logger.Fatalf("error serving HTTP OpenTSDB at %q: %s", s.ln.Addr(), err)
|
2019-08-22 09:27:18 +00:00
|
|
|
}
|
|
|
|
}()
|
2019-12-13 22:29:14 +00:00
|
|
|
return s
|
2019-08-22 09:27:18 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 22:29:14 +00:00
|
|
|
// Wait waits until the server is stopped with MustStop.
|
|
|
|
func (s *Server) Wait() {
|
|
|
|
s.wg.Wait()
|
2019-08-22 09:27:18 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 22:29:14 +00:00
|
|
|
// MustStop stops HTTP OpenTSDB server.
|
|
|
|
func (s *Server) MustStop() {
|
|
|
|
logger.Infof("stopping HTTP OpenTSDB server at %q...", s.ln.Addr())
|
2019-08-22 09:27:18 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
2019-12-13 22:29:14 +00:00
|
|
|
if err := s.s.Shutdown(ctx); err != nil {
|
|
|
|
logger.Fatalf("cannot close HTTP OpenTSDB server at %q: %s", s.ln.Addr(), err)
|
|
|
|
}
|
|
|
|
s.wg.Wait()
|
|
|
|
logger.Infof("OpenTSDB HTTP server at %q has been stopped", s.ln.Addr())
|
|
|
|
}
|
|
|
|
|
|
|
|
func newRequestHandler(maxRequestSize int64) http.Handler {
|
|
|
|
rh := func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.URL.Path {
|
|
|
|
case "/api/put":
|
|
|
|
writeRequests.Inc()
|
|
|
|
if err := insertHandler(r, maxRequestSize); err != nil {
|
|
|
|
writeErrors.Inc()
|
|
|
|
httpserver.Errorf(w, "error in %q: %s", r.URL.Path, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
default:
|
|
|
|
httpserver.Errorf(w, "unexpected path requested on HTTP OpenTSDB server: %q", r.URL.Path)
|
|
|
|
}
|
2019-08-22 09:27:18 +00:00
|
|
|
}
|
2019-12-13 22:29:14 +00:00
|
|
|
return http.HandlerFunc(rh)
|
2019-08-22 09:27:18 +00:00
|
|
|
}
|