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 (
|
2020-02-25 17:09:46 +00:00
|
|
|
writeRequests = metrics.NewCounter(`vm_ingestserver_requests_total{type="opentsdbhttp", name="write", net="tcp"}`)
|
|
|
|
writeErrors = metrics.NewCounter(`vm_ingestserver_request_errors_total{type="opentsdbhttp", name="write", net="tcp"}`)
|
2019-08-22 09:27:18 +00:00
|
|
|
)
|
|
|
|
|
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.
|
|
|
|
//
|
2023-01-27 07:08:35 +00:00
|
|
|
// If useProxyProtocol is set to true, then the incoming connections are accepted via proxy protocol.
|
|
|
|
// See https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt
|
|
|
|
//
|
2019-12-13 22:29:14 +00:00
|
|
|
// MustStop must be called on the returned server when it is no longer needed.
|
2023-01-27 07:08:35 +00:00
|
|
|
func MustStart(addr string, useProxyProtocol bool, insertHandler func(r *http.Request) error) *Server {
|
2019-08-22 09:27:18 +00:00
|
|
|
logger.Infof("starting HTTP OpenTSDB server at %q", addr)
|
2023-01-27 07:08:35 +00:00
|
|
|
lnTCP, err := netutil.NewTCPListener("opentsdbhttp", addr, useProxyProtocol, nil)
|
2019-12-13 22:29:14 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatalf("cannot start HTTP OpenTSDB collector at %q: %s", addr, err)
|
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
return MustServe(lnTCP, insertHandler)
|
2019-12-13 22:29:14 +00:00
|
|
|
}
|
|
|
|
|
2020-01-16 11:03:28 +00:00
|
|
|
// MustServe serves OpenTSDB HTTP put requests from ln.
|
2019-12-13 22:29:14 +00:00
|
|
|
//
|
|
|
|
// MustStop must be called on the returned server when it is no longer needed.
|
2020-07-20 11:00:33 +00:00
|
|
|
func MustServe(ln net.Listener, insertHandler func(r *http.Request) error) *Server {
|
2020-02-23 11:35:47 +00:00
|
|
|
h := newRequestHandler(insertHandler)
|
2019-12-13 22:29:14 +00:00
|
|
|
hs := &http.Server{
|
2020-05-07 11:10:40 +00:00
|
|
|
Handler: h,
|
|
|
|
ReadHeaderTimeout: 5 * time.Second,
|
|
|
|
IdleTimeout: time.Minute,
|
|
|
|
// Do not set ReadTimeout and WriteTimeout here,
|
|
|
|
// since these timeouts must be controlled by request handler.
|
2019-08-22 09:27:18 +00:00
|
|
|
}
|
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())
|
|
|
|
}
|
|
|
|
|
2020-07-20 11:00:33 +00:00
|
|
|
func newRequestHandler(insertHandler func(r *http.Request) error) http.Handler {
|
|
|
|
rh := func(w http.ResponseWriter, r *http.Request) {
|
2023-01-10 23:46:13 +00:00
|
|
|
if !httpserver.CheckBasicAuth(w, r) {
|
|
|
|
return
|
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
writeRequests.Inc()
|
2020-07-20 11:00:33 +00:00
|
|
|
if err := insertHandler(r); err != nil {
|
2020-02-23 11:35:47 +00:00
|
|
|
writeErrors.Inc()
|
2021-07-07 09:59:03 +00:00
|
|
|
httpserver.Errorf(w, r, "%s", err)
|
2020-02-23 11:35:47 +00:00
|
|
|
return
|
2019-12-13 22:29:14 +00:00
|
|
|
}
|
2020-02-23 11:35:47 +00:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
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
|
|
|
}
|