Skip to content

Commit

Permalink
instrumenting
Browse files Browse the repository at this point in the history
  • Loading branch information
Snawoot committed Dec 25, 2024
1 parent e71535b commit ff35253
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"log"
"net"
"net/http"
"net/http/pprof"
"net/netip"
"os"
"path/filepath"
Expand Down Expand Up @@ -176,8 +177,9 @@ type proxyArg struct {
}

type CLIArgs struct {
bind_address string
bind_reuseport bool
bindAddress string
bindReusePort bool
bindPprof string
auth string
verbosity int
cert, key, cafile string
Expand Down Expand Up @@ -230,8 +232,9 @@ func parse_args() CLIArgs {
netip.MustParsePrefix("fe80::/10"),
},
}
flag.StringVar(&args.bind_address, "bind-address", ":8080", "HTTP proxy listen address. Set empty value to use systemd socket activation.")
flag.BoolVar(&args.bind_reuseport, "bind-reuseport", false, "allow multiple server instances on the same port")
flag.StringVar(&args.bindAddress, "bind-address", ":8080", "HTTP proxy listen address. Set empty value to use systemd socket activation.")
flag.BoolVar(&args.bindReusePort, "bind-reuseport", false, "allow multiple server instances on the same port")
flag.StringVar(&args.bindPprof, "bind-pprof", "", "enables pprof debug endpoints")
flag.StringVar(&args.auth, "auth", "none://", "auth parameters")
flag.IntVar(&args.verbosity, "verbosity", 20, "logging verbosity "+
"(10 - debug, 20 - info, 30 - warning, 40 - error, 50 - critical)")
Expand Down Expand Up @@ -429,7 +432,7 @@ func run() int {
}

server := http.Server{
Addr: args.bind_address,
Addr: args.bindAddress,
Handler: handler.NewProxyHandler(&handler.Config{
Dialer: dialerRoot,
Auth: auth,
Expand All @@ -451,7 +454,7 @@ func run() int {

mainLogger.Info("Starting proxy server...")
var listener net.Listener
if args.bind_address == "" {
if args.bindAddress == "" {
// socket activation
listeners, err := activation.Listeners()
if err != nil {
Expand All @@ -470,14 +473,14 @@ func run() int {
listener = listeners[0]
} else {
listenerFactory := net.Listen
if args.bind_reuseport {
if args.bindReusePort {
if reuseport.Available() {
listenerFactory = reuseport.Listen
} else {
mainLogger.Warning("reuseport was requested but not available!")
}
}
newListener, err := listenerFactory("tcp", args.bind_address)
newListener, err := listenerFactory("tcp", args.bindAddress)
if err != nil {
mainLogger.Critical("listen failed: %v", err)
return 3
Expand Down Expand Up @@ -518,6 +521,17 @@ func run() int {
}
listener = tls.NewListener(listener, cfg)
}
// debug endpoints setup
if args.bindPprof != "" {
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
go func() { log.Fatal(http.ListenAndServe(args.bindPprof, mux)) }()
}

mainLogger.Info("Proxy server started.")

// setup done
Expand Down

0 comments on commit ff35253

Please sign in to comment.