2023-06-20 07:55:12 +02:00
package main
import (
"flag"
"fmt"
"net/http"
"os"
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vlinsert"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vlselect"
"github.com/VictoriaMetrics/VictoriaMetrics/app/vlstorage"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/buildinfo"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/envflag"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/flagutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fs"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/httpserver"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/procutil"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/pushmetrics"
)
var (
2024-02-09 02:15:04 +01:00
httpListenAddrs = flagutil . NewArrayString ( "httpListenAddr" , "TCP address to listen for incoming http requests. See also -httpListenAddr.useProxyProtocol" )
useProxyProtocol = flagutil . NewArrayBool ( "httpListenAddr.useProxyProtocol" , "Whether to use proxy protocol for connections accepted at the given -httpListenAddr . " +
2023-06-20 07:55:12 +02:00
"See https://www.haproxy.org/download/1.8/doc/proxy-protocol.txt . " +
"With enabled proxy protocol http server cannot serve regular /metrics endpoint. Use -pushmetrics.url for metrics pushing" )
)
func main ( ) {
// Write flags and help message to stdout, since it is easier to grep or pipe.
flag . CommandLine . SetOutput ( os . Stdout )
flag . Usage = usage
envflag . Parse ( )
buildinfo . Init ( )
logger . Init ( )
2024-02-09 02:15:04 +01:00
listenAddrs := * httpListenAddrs
if len ( listenAddrs ) == 0 {
listenAddrs = [ ] string { ":9428" }
}
logger . Infof ( "starting VictoriaLogs at %q..." , listenAddrs )
2023-06-20 07:55:12 +02:00
startTime := time . Now ( )
vlstorage . Init ( )
vlselect . Init ( )
vlinsert . Init ( )
2024-02-09 02:15:04 +01:00
go httpserver . Serve ( listenAddrs , useProxyProtocol , requestHandler )
2024-05-25 00:30:58 +02:00
logger . Infof ( "started VictoriaLogs in %.3f seconds; see https://docs.victoriametrics.com/victorialogs/" , time . Since ( startTime ) . Seconds ( ) )
2023-06-20 07:55:12 +02:00
2024-01-15 12:37:02 +01:00
pushmetrics . Init ( )
2023-06-20 07:55:12 +02:00
sig := procutil . WaitForSigterm ( )
logger . Infof ( "received signal %s" , sig )
2024-01-15 12:37:02 +01:00
pushmetrics . Stop ( )
2023-06-20 07:55:12 +02:00
2024-02-09 02:15:04 +01:00
logger . Infof ( "gracefully shutting down webservice at %q" , listenAddrs )
2023-06-20 07:55:12 +02:00
startTime = time . Now ( )
2024-02-09 02:15:04 +01:00
if err := httpserver . Stop ( listenAddrs ) ; err != nil {
2023-06-20 07:55:12 +02:00
logger . Fatalf ( "cannot stop the webservice: %s" , err )
}
logger . Infof ( "successfully shut down the webservice in %.3f seconds" , time . Since ( startTime ) . Seconds ( ) )
vlinsert . Stop ( )
vlselect . Stop ( )
vlstorage . Stop ( )
fs . MustStopDirRemover ( )
logger . Infof ( "the VictoriaLogs has been stopped in %.3f seconds" , time . Since ( startTime ) . Seconds ( ) )
}
func requestHandler ( w http . ResponseWriter , r * http . Request ) bool {
if r . URL . Path == "/" {
if r . Method != http . MethodGet {
return false
}
w . Header ( ) . Add ( "Content-Type" , "text/html; charset=utf-8" )
fmt . Fprintf ( w , "<h2>Single-node VictoriaLogs</h2></br>" )
2024-05-25 00:30:58 +02:00
fmt . Fprintf ( w , "See docs at <a href='https://docs.victoriametrics.com/victorialogs/'>https://docs.victoriametrics.com/victorialogs/</a></br>" )
2023-06-20 07:55:12 +02:00
fmt . Fprintf ( w , "Useful endpoints:</br>" )
httpserver . WriteAPIHelp ( w , [ ] [ 2 ] string {
2023-06-22 04:52:48 +02:00
{ "select/vmui" , "Web UI for VictoriaLogs" } ,
2023-06-20 07:55:12 +02:00
{ "metrics" , "available service metrics" } ,
{ "flags" , "command-line flags" } ,
} )
return true
}
if vlinsert . RequestHandler ( w , r ) {
return true
}
if vlselect . RequestHandler ( w , r ) {
return true
}
2024-10-13 22:20:31 +02:00
if vlstorage . RequestHandler ( w , r ) {
return true
}
2023-06-20 07:55:12 +02:00
return false
}
func usage ( ) {
const s = `
victoria - logs is a log management and analytics service .
2024-05-25 00:30:58 +02:00
See the docs at https : //docs.victoriametrics.com/victorialogs/
2023-06-20 07:55:12 +02:00
`
flagutil . Usage ( s )
}