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

Conversation

semistrict
Copy link
Contributor

@semistrict semistrict commented Jun 4, 2018

See: #772

@semistrict semistrict force-pushed the lazy-zpages branch 3 times, most recently from e1819e0 to d4a01b3 Compare June 4, 2018 19:31
@semistrict semistrict requested a review from rakyll June 4, 2018 19:31
@@ -31,6 +30,7 @@ import (
"go.opencensus.io/zpages"
"golang.org/x/net/context"
"google.golang.org/grpc"
"net/http"
Copy link
Contributor

Choose a reason for hiding this comment

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

Revert this change.

zpages/zpages.go Outdated
Handler = NewHandler("")
}

// GetHandler returns a handler that serves the z-pages.
Copy link
Contributor

Choose a reason for hiding this comment

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

NewHandler

zpages/zpages.go Outdated
}

// GetHandler 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).

@semistrict
Copy link
Contributor Author

Thanks @rakyll updated. I prefer the approach with taking a prefix as explained above and would like to keep it unless you feel strongly that it's wrong. After looking at what StripPrefix does, I don't think it's a good idea to recommend its use here if there's an straightforward way to avoid it.

Copy link
Contributor

@rakyll rakyll left a comment

Choose a reason for hiding this comment

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

I was just worried that user might be confused with that prefix is in this context.

Worrying about the route is the next step for them when they are registering the handler on the mux.

Otherwise LGTM.

@semistrict
Copy link
Contributor Author

So one way to fix this would be to not care what the prefix is and just match based on the last component of the URL. What do you think about that? Something like:

parts := path.Split(req.URL)
page := parts[len(parts-1)]
switch page {
case "rpcz": 
...
default:
return http.NotFoundHandler().Handle(...)
}

Then we wouldn't need to take a prefix parameter at all and it would work regardless of the path it is installed at?

@rakyll rakyll mentioned this pull request Jun 6, 2018
Copy link

@nfisher nfisher left a comment

Choose a reason for hiding this comment

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

Nice work and quick turnaround! 👍

zpages/zpages.go Outdated
}

// NewHandler returns a handler that serves the z-pages.
func NewHandler(prefix string) http.Handler {
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)

zpages/zpages.go Outdated
}

// AppendHandlers adds the z-pages to the given ServeMux rooted at pathPrefix.
func AppendHandlers(pathPrefix string, mux *http.ServeMux) *http.ServeMux {
Copy link

@nfisher nfisher Jun 7, 2018

Choose a reason for hiding this comment

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

Since it's returning a *ServeMux and there's no '/' specified on the mux it's probably ok to drop the mux parameter and create it in the method. /public is a fs folder so additional assets could be added at run-time and (famous last words) hopefully no one would want sub-paths under /tracez and /rpcz.

What are peoples thoughts on two functions?

  1. func zpages.NewPrefixed(pathPrefix string) *ServeMux
  2. func zpages.New() *ServeMux

The second basically calls the first with "" and would provide behaviour similar to the original zpages.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 think constructing our own ServeMux reduces the flexibility, in particular to add to the DefaultServeMux. Renamed the method to Handle and no longer return the ServeMux to avoid ambiguity.

I don't think we need a separate non-prefixed call. The user can just pass an empty string explicitly, I don't think it's worth adding a convenience method because I would think that the prefixed form would actually be more common in practice.

http.Handle("/debug/", http.StripPrefix("/debug", zpages.Handler))
log.Fatal(http.ListenAndServe(":8081", nil))
mux := zpages.AppendHandlers("/debug", http.NewServeMux())
log.Fatal(http.ListenAndServe("127.0.0.1:8081", mux))
Copy link

Choose a reason for hiding this comment

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

Like the change to 127.0.0.1 as it doesn't trigger FW notifications on OS X. 👍

@semistrict
Copy link
Contributor Author

semistrict commented Jun 7, 2018

@rakyll are you ok with the new function?

@@ -23,6 +23,6 @@ import (

func Example() {
// Both /debug/tracez and /debug/rpcz will be served.
Copy link
Contributor

Choose a reason for hiding this comment

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

served at the default mux.

zpages/zpages.go Outdated
@@ -32,15 +32,40 @@ package zpages // import "go.opencensus.io/zpages"

import (
"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.

@rakyll
Copy link
Contributor

rakyll commented Jun 7, 2018

LGTM

@semistrict semistrict merged commit 6edeb78 into census-instrumentation:master Jun 7, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants