TUN-4698: Add cloudflared metrics endpoint to serve quick tunnel hostname

This commit is contained in:
Rishabh Bector 2021-07-09 12:10:43 -05:00 committed by Sudarsan Reddy
parent d678584d89
commit 59cae0f622
5 changed files with 12 additions and 5 deletions

View File

@ -73,7 +73,7 @@ func Run(c *cli.Context) error {
log.Fatal().Err(err).Msg("Failed to open the metrics listener") log.Fatal().Err(err).Msg("Failed to open the metrics listener")
} }
go metrics.ServeMetrics(metricsListener, nil, nil, log) go metrics.ServeMetrics(metricsListener, nil, nil, "", log)
listener, err := tunneldns.CreateListener( listener, err := tunneldns.CreateListener(
c.String("address"), c.String("address"),

View File

@ -206,7 +206,7 @@ func runAdhocNamedTunnel(sc *subcommandContext, name, credentialsOutputPath stri
// runClassicTunnel creates a "classic" non-named tunnel // runClassicTunnel creates a "classic" non-named tunnel
func runClassicTunnel(sc *subcommandContext) error { func runClassicTunnel(sc *subcommandContext) error {
return StartServer(sc.c, version, nil, sc.log, sc.isUIEnabled) return StartServer(sc.c, version, nil, sc.log, sc.isUIEnabled, "")
} }
func routeFromFlag(c *cli.Context) (route tunnelstore.Route, ok bool) { func routeFromFlag(c *cli.Context) (route tunnelstore.Route, ok bool) {
@ -225,6 +225,7 @@ func StartServer(
namedTunnel *connection.NamedTunnelConfig, namedTunnel *connection.NamedTunnelConfig,
log *zerolog.Logger, log *zerolog.Logger,
isUIEnabled bool, isUIEnabled bool,
quickTunnelHostname string,
) error { ) error {
_ = raven.SetDSN(sentryDSN) _ = raven.SetDSN(sentryDSN)
var wg sync.WaitGroup var wg sync.WaitGroup
@ -341,7 +342,7 @@ func StartServer(
defer wg.Done() defer wg.Done()
readinessServer := metrics.NewReadyServer(log) readinessServer := metrics.NewReadyServer(log)
observer.RegisterSink(readinessServer) observer.RegisterSink(readinessServer)
errC <- metrics.ServeMetrics(metricsListener, ctx.Done(), readinessServer, log) errC <- metrics.ServeMetrics(metricsListener, ctx.Done(), readinessServer, quickTunnelHostname, log)
}() }()
if err := ingressRules.StartOrigins(&wg, log, ctx.Done(), errC); err != nil { if err := ingressRules.StartOrigins(&wg, log, ctx.Done(), errC); err != nil {

View File

@ -64,6 +64,7 @@ func RunQuickTunnel(sc *subcommandContext) error {
&connection.NamedTunnelConfig{Credentials: credentials}, &connection.NamedTunnelConfig{Credentials: credentials},
sc.log, sc.log,
sc.isUIEnabled, sc.isUIEnabled,
data.Result.Hostname,
) )
} }

View File

@ -286,6 +286,7 @@ func (sc *subcommandContext) run(tunnelID uuid.UUID) error {
&connection.NamedTunnelConfig{Credentials: credentials}, &connection.NamedTunnelConfig{Credentials: credentials},
sc.log, sc.log,
sc.isUIEnabled, sc.isUIEnabled,
"",
) )
} }

View File

@ -22,7 +22,7 @@ const (
startupTime = time.Millisecond * 500 startupTime = time.Millisecond * 500
) )
func newMetricsHandler(readyServer *ReadyServer) *mux.Router { func newMetricsHandler(readyServer *ReadyServer, quickTunnelHostname string) *mux.Router {
router := mux.NewRouter() router := mux.NewRouter()
router.PathPrefix("/debug/").Handler(http.DefaultServeMux) router.PathPrefix("/debug/").Handler(http.DefaultServeMux)
@ -33,6 +33,9 @@ func newMetricsHandler(readyServer *ReadyServer) *mux.Router {
if readyServer != nil { if readyServer != nil {
router.Handle("/ready", readyServer) router.Handle("/ready", readyServer)
} }
router.HandleFunc("/quicktunnel", func(w http.ResponseWriter, r *http.Request) {
_, _ = fmt.Fprintf(w, `{"hostname":"%s"}`, quickTunnelHostname)
})
return router return router
} }
@ -41,6 +44,7 @@ func ServeMetrics(
l net.Listener, l net.Listener,
shutdownC <-chan struct{}, shutdownC <-chan struct{},
readyServer *ReadyServer, readyServer *ReadyServer,
quickTunnelHostname string,
log *zerolog.Logger, log *zerolog.Logger,
) (err error) { ) (err error) {
var wg sync.WaitGroup var wg sync.WaitGroup
@ -48,7 +52,7 @@ func ServeMetrics(
trace.AuthRequest = func(*http.Request) (bool, bool) { return true, true } trace.AuthRequest = func(*http.Request) (bool, bool) { return true, true }
// TODO: parameterize ReadTimeout and WriteTimeout. The maximum time we can // TODO: parameterize ReadTimeout and WriteTimeout. The maximum time we can
// profile CPU usage depends on WriteTimeout // profile CPU usage depends on WriteTimeout
h := newMetricsHandler(readyServer) h := newMetricsHandler(readyServer, quickTunnelHostname)
server := &http.Server{ server := &http.Server{
ReadTimeout: 10 * time.Second, ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second,