Skip to content

Commit

Permalink
Fix /metrics, /debug/requests, /debug/events + additional flag for en…
Browse files Browse the repository at this point in the history
…abling those (#957)

* Debug endpoints restored (/debug/requests, /debug/events, /metrics); Command line parameter added to control the feature

* Update go/grpcwebproxy/main.go

Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>

* fix heading go/grpcwebproxy/main.go

Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>

Co-authored-by: Evgeny Mikerin <evgeny.mikerin@jpmchase.com>
Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
  • Loading branch information
3 people authored Jun 30, 2021
1 parent 2605d83 commit 189bea5
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions go/grpcwebproxy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"golang.org/x/net/context"
_ "golang.org/x/net/trace" // register in DefaultServerMux
"golang.org/x/net/trace" // register in DefaultServerMux
"google.golang.org/grpc"
"google.golang.org/grpc/grpclog"
"google.golang.org/grpc/metadata"
Expand All @@ -44,6 +44,8 @@ var (

flagHttpMaxWriteTimeout = pflag.Duration("server_http_max_write_timeout", 10*time.Second, "HTTP server config, max write duration.")
flagHttpMaxReadTimeout = pflag.Duration("server_http_max_read_timeout", 10*time.Second, "HTTP server config, max read duration.")

enableRequestDebug = pflag.Bool("enable_request_debug", false, "whether to enable (/debug/requests) and connection(/debug/events) monitoring; also controls prometheus monitoring (/metrics)")
)

func main() {
Expand Down Expand Up @@ -103,8 +105,12 @@ func main() {

if *runHttpServer {
// Debug server.
debugServer := buildServer(wrappedGrpc)
http.Handle("/metrics", promhttp.Handler())
var debugServer *http.Server
if *enableRequestDebug {
debugServer = buildDebugServer(wrappedGrpc, promhttp.Handler())
} else {
debugServer = buildServer(wrappedGrpc)
}
debugListener := buildListenerOrFail("http", *flagHttpPort)
serveServer(debugServer, debugListener, "http", errChan)
}
Expand All @@ -121,6 +127,25 @@ func main() {
// TODO(mwitkow): Add graceful shutdown.
}

func buildDebugServer(wrappedGrpc *grpcweb.WrappedGrpcServer, metricsHandler http.Handler) *http.Server {
return &http.Server{
WriteTimeout: *flagHttpMaxWriteTimeout,
ReadTimeout: *flagHttpMaxReadTimeout,
Handler: http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
switch req.URL.Path {
case "/metrics":
metricsHandler.ServeHTTP(resp, req)
case "/debug/requests":
trace.Traces(resp, req)
case "/debug/events":
trace.Events(resp, req)
default:
wrappedGrpc.ServeHTTP(resp, req)
}
}),
}
}

func buildServer(wrappedGrpc *grpcweb.WrappedGrpcServer) *http.Server {
return &http.Server{
WriteTimeout: *flagHttpMaxWriteTimeout,
Expand Down

0 comments on commit 189bea5

Please sign in to comment.