Skip to content

Commit 189bea5

Browse files
zolkinevgenymikerinjpmcjohanbrandhorst
authored
Fix /metrics, /debug/requests, /debug/events + additional flag for enabling 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 <[email protected]> * fix heading go/grpcwebproxy/main.go Co-authored-by: Johan Brandhorst-Satzkorn <[email protected]> Co-authored-by: Evgeny Mikerin <[email protected]> Co-authored-by: Johan Brandhorst-Satzkorn <[email protected]>
1 parent 2605d83 commit 189bea5

File tree

1 file changed

+28
-3
lines changed

1 file changed

+28
-3
lines changed

go/grpcwebproxy/main.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/sirupsen/logrus"
2222
"github.com/spf13/pflag"
2323
"golang.org/x/net/context"
24-
_ "golang.org/x/net/trace" // register in DefaultServerMux
24+
"golang.org/x/net/trace" // register in DefaultServerMux
2525
"google.golang.org/grpc"
2626
"google.golang.org/grpc/grpclog"
2727
"google.golang.org/grpc/metadata"
@@ -44,6 +44,8 @@ var (
4444

4545
flagHttpMaxWriteTimeout = pflag.Duration("server_http_max_write_timeout", 10*time.Second, "HTTP server config, max write duration.")
4646
flagHttpMaxReadTimeout = pflag.Duration("server_http_max_read_timeout", 10*time.Second, "HTTP server config, max read duration.")
47+
48+
enableRequestDebug = pflag.Bool("enable_request_debug", false, "whether to enable (/debug/requests) and connection(/debug/events) monitoring; also controls prometheus monitoring (/metrics)")
4749
)
4850

4951
func main() {
@@ -103,8 +105,12 @@ func main() {
103105

104106
if *runHttpServer {
105107
// Debug server.
106-
debugServer := buildServer(wrappedGrpc)
107-
http.Handle("/metrics", promhttp.Handler())
108+
var debugServer *http.Server
109+
if *enableRequestDebug {
110+
debugServer = buildDebugServer(wrappedGrpc, promhttp.Handler())
111+
} else {
112+
debugServer = buildServer(wrappedGrpc)
113+
}
108114
debugListener := buildListenerOrFail("http", *flagHttpPort)
109115
serveServer(debugServer, debugListener, "http", errChan)
110116
}
@@ -121,6 +127,25 @@ func main() {
121127
// TODO(mwitkow): Add graceful shutdown.
122128
}
123129

130+
func buildDebugServer(wrappedGrpc *grpcweb.WrappedGrpcServer, metricsHandler http.Handler) *http.Server {
131+
return &http.Server{
132+
WriteTimeout: *flagHttpMaxWriteTimeout,
133+
ReadTimeout: *flagHttpMaxReadTimeout,
134+
Handler: http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
135+
switch req.URL.Path {
136+
case "/metrics":
137+
metricsHandler.ServeHTTP(resp, req)
138+
case "/debug/requests":
139+
trace.Traces(resp, req)
140+
case "/debug/events":
141+
trace.Events(resp, req)
142+
default:
143+
wrappedGrpc.ServeHTTP(resp, req)
144+
}
145+
}),
146+
}
147+
}
148+
124149
func buildServer(wrappedGrpc *grpcweb.WrappedGrpcServer) *http.Server {
125150
return &http.Server{
126151
WriteTimeout: *flagHttpMaxWriteTimeout,

0 commit comments

Comments
 (0)