mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-01-30 15:22:07 +00:00
lib/httpserver: add -tls
, -tlsCertFile
and -tlsKeyFile
command-line flags in every vm binary
This makes such binaries compatible with binaries from `master` branch (aka single-node version) See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/677
This commit is contained in:
parent
307281e922
commit
67cacb22ac
3 changed files with 34 additions and 7 deletions
|
@ -109,7 +109,7 @@ func newManager(ctx context.Context) (*manager, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to init datasource: %w", err)
|
return nil, fmt.Errorf("failed to init datasource: %w", err)
|
||||||
}
|
}
|
||||||
eu, err := getExternalURL(*externalURL, *httpListenAddr, false)
|
eu, err := getExternalURL(*externalURL, *httpListenAddr, httpserver.IsTLS())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to init `external.url`: %w", err)
|
return nil, fmt.Errorf("failed to init `external.url`: %w", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,9 +25,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
tlsEnable = flag.Bool("tls", false, "Whether to enable TLS (aka HTTPS) for incoming requests. -tlsCertFile and -tlsKeyFile must be set if -tls is set")
|
||||||
|
tlsCertFile = flag.String("tlsCertFile", "", "Path to file with TLS certificate. Used only if -tls is set. Prefer ECDSA certs instead of RSA certs, since RSA certs are slow")
|
||||||
|
tlsKeyFile = flag.String("tlsKeyFile", "", "Path to file with TLS key. Used only if -tls is set")
|
||||||
|
|
||||||
pathPrefix = flag.String("http.pathPrefix", "", "An optional prefix to add to all the paths handled by http server. For example, if '-http.pathPrefix=/foo/bar' is set, "+
|
pathPrefix = flag.String("http.pathPrefix", "", "An optional prefix to add to all the paths handled by http server. For example, if '-http.pathPrefix=/foo/bar' is set, "+
|
||||||
"then all the http requests will be handled on '/foo/bar/*' paths. This may be useful for proxied requests. "+
|
"then all the http requests will be handled on '/foo/bar/*' paths. This may be useful for proxied requests. "+
|
||||||
"See https://www.robustperception.io/using-external-urls-and-proxies-with-prometheus")
|
"See https://www.robustperception.io/using-external-urls-and-proxies-with-prometheus")
|
||||||
|
|
||||||
disableResponseCompression = flag.Bool("http.disableResponseCompression", false, "Disable compression of HTTP responses for saving CPU resources. By default compression is enabled to save network bandwidth")
|
disableResponseCompression = flag.Bool("http.disableResponseCompression", false, "Disable compression of HTTP responses for saving CPU resources. By default compression is enabled to save network bandwidth")
|
||||||
maxGracefulShutdownDuration = flag.Duration("http.maxGracefulShutdownDuration", 7*time.Second, "The maximum duration for graceful shutdown of HTTP server. "+
|
maxGracefulShutdownDuration = flag.Duration("http.maxGracefulShutdownDuration", 7*time.Second, "The maximum duration for graceful shutdown of HTTP server. "+
|
||||||
"Highly loaded server may require increased value for graceful shutdown")
|
"Highly loaded server may require increased value for graceful shutdown")
|
||||||
|
@ -61,11 +66,27 @@ type RequestHandler func(w http.ResponseWriter, r *http.Request) bool
|
||||||
//
|
//
|
||||||
// The compression is also disabled if -http.disableResponseCompression flag is set.
|
// The compression is also disabled if -http.disableResponseCompression flag is set.
|
||||||
func Serve(addr string, rh RequestHandler) {
|
func Serve(addr string, rh RequestHandler) {
|
||||||
logger.Infof("starting http server at http://%s/", addr)
|
scheme := "http"
|
||||||
logger.Infof("pprof handlers are exposed at http://%s/debug/pprof/", addr)
|
if *tlsEnable {
|
||||||
ln, err := netutil.NewTCPListener("http", addr)
|
scheme = "https"
|
||||||
|
}
|
||||||
|
logger.Infof("starting http server at %s://%s/", scheme, addr)
|
||||||
|
logger.Infof("pprof handlers are exposed at %s://%s/debug/pprof/", scheme, addr)
|
||||||
|
lnTmp, err := netutil.NewTCPListener(scheme, addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Panicf("FATAL: cannot start http server at %s: %s", addr, err)
|
logger.Fatalf("cannot start http server at %s: %s", addr, err)
|
||||||
|
}
|
||||||
|
ln := net.Listener(lnTmp)
|
||||||
|
|
||||||
|
if *tlsEnable {
|
||||||
|
cert, err := tls.LoadX509KeyPair(*tlsCertFile, *tlsKeyFile)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatalf("cannot load TLS cert from tlsCertFile=%q, tlsKeyFile=%q: %s", *tlsCertFile, *tlsKeyFile, err)
|
||||||
|
}
|
||||||
|
cfg := &tls.Config{
|
||||||
|
Certificates: []tls.Certificate{cert},
|
||||||
|
}
|
||||||
|
ln = tls.NewListener(ln, cfg)
|
||||||
}
|
}
|
||||||
serveWithListener(addr, ln, rh)
|
serveWithListener(addr, ln, rh)
|
||||||
}
|
}
|
||||||
|
@ -187,7 +208,7 @@ func handlerWrapper(s *server, w http.ResponseWriter, r *http.Request, rh Reques
|
||||||
metricsRequests.Inc()
|
metricsRequests.Inc()
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
w.Header().Set("Content-Type", "text/plain")
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
writePrometheusMetrics(w)
|
WritePrometheusMetrics(w)
|
||||||
metricsHandlerDuration.UpdateDuration(startTime)
|
metricsHandlerDuration.UpdateDuration(startTime)
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
|
@ -485,3 +506,8 @@ func isTrivialNetworkError(err error) bool {
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsTLS indicates is tls enabled or not
|
||||||
|
func IsTLS() bool {
|
||||||
|
return *tlsEnable
|
||||||
|
}
|
||||||
|
|
|
@ -12,7 +12,8 @@ import (
|
||||||
"github.com/VictoriaMetrics/metrics"
|
"github.com/VictoriaMetrics/metrics"
|
||||||
)
|
)
|
||||||
|
|
||||||
func writePrometheusMetrics(w io.Writer) {
|
// WritePrometheusMetrics writes all the registered metrics to w in Prometheus exposition format.
|
||||||
|
func WritePrometheusMetrics(w io.Writer) {
|
||||||
metrics.WritePrometheus(w, true)
|
metrics.WritePrometheus(w, true)
|
||||||
|
|
||||||
fmt.Fprintf(w, "vm_app_version{version=%q} 1\n", buildinfo.Version)
|
fmt.Fprintf(w, "vm_app_version{version=%q} 1\n", buildinfo.Version)
|
||||||
|
|
Loading…
Reference in a new issue