Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Lazily enable zpages at runtime #774

Merged
merged 4 commits into from
Jun 7, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions examples/grpc/helloworld_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloRe
}

func main() {
// Start z-Pages server.
go func() {
http.Handle("/debug/", http.StripPrefix("/debug", zpages.Handler))
log.Fatal(http.ListenAndServe(":8081", nil))
log.Fatal(http.ListenAndServe("127.0.0.1:8081", zpages.NewHandler("/debug")))
}()

// Register stats and trace exporters to export
// the collected data.
view.RegisterExporter(&exporter.PrintExporter{})
Expand Down
5 changes: 4 additions & 1 deletion examples/http/helloworld_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ import (
)

func main() {
go func() { log.Fatal(http.ListenAndServe(":8081", zpages.Handler)) }()
// Start z-Pages server.
go func() {
log.Fatal(http.ListenAndServe("127.0.0.1:8081", zpages.NewHandler("/debug")))
}()

// Register stats and trace exporters to export the collected data.
exporter := &exporter.PrintExporter{}
Expand Down
2 changes: 1 addition & 1 deletion zpages/rpcz.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var (
}
)

func init() {
func registerRPCViews() {
views := make([]*view.View, 0, len(viewType))
for v := range viewType {
views = append(views, v)
Expand Down
4 changes: 0 additions & 4 deletions zpages/tracez.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ var (
}
)

func init() {
internal.LocalSpanStoreEnabled = true
}

func canonicalCodeString(code int32) string {
if code < 0 || int(code) >= len(canonicalCodes) {
return "error code " + strconv.FormatInt(int64(code), 10)
Expand Down
25 changes: 20 additions & 5 deletions zpages/zpages.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,31 @@
package zpages // import "go.opencensus.io/zpages"

import (
"fmt"
"net/http"
"sync"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the line.

"go.opencensus.io/internal"
)

// Handler is an http.Handler that serves the zpages.
// Handler is deprecated: Use NewHandler.
var Handler http.Handler

var enableOnce sync.Once

func init() {
Handler = NewHandler("")
}

// NewHandler returns a handler that serves the z-pages.
func NewHandler(prefix string) http.Handler {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't have the prefix here.

It is coupling the responsibilities of a mux and a handler into a handler.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I this this is preferable to using StripPrefix. It is much more readable, does not allocate a new request object, and does not try to fake the URL (like StripPrefix does).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rakyll If this were changed to http.ServerMux I think that would work for what I referenced in #783. The http.Handler strips the .Handle* functions and while I could simply cast the return to a ServerMux it feels dirty as I'm relying on knowledge of the implementation detail rather than a public API contract. (Moved this comment from the exported var as I didn’t notice the deprecation comment)

enableOnce.Do(func() {
internal.LocalSpanStoreEnabled = true
registerRPCViews()
})
zpagesMux := http.NewServeMux()
zpagesMux.HandleFunc("/rpcz", rpczHandler)
zpagesMux.HandleFunc("/tracez", tracezHandler)
zpagesMux.Handle("/public/", http.FileServer(fs))
Handler = zpagesMux
zpagesMux.HandleFunc(fmt.Sprintf("%s/rpcz", prefix), rpczHandler)
zpagesMux.HandleFunc(fmt.Sprintf("%s/tracez", prefix), tracezHandler)
zpagesMux.Handle(fmt.Sprintf("%s/public/", prefix), http.FileServer(fs))
return zpagesMux
}