Skip to content

Commit

Permalink
Avoid global by passing metricsGraphURL down
Browse files Browse the repository at this point in the history
  • Loading branch information
rndstr committed Jul 21, 2017
1 parent 0332e3b commit 1a00288
Show file tree
Hide file tree
Showing 13 changed files with 73 additions and 96 deletions.
2 changes: 1 addition & 1 deletion app/api_report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func topologyServer() *httptest.Server {
router := mux.NewRouter().SkipClean(true)
app.RegisterTopologyRoutes(router, app.StaticCollector(fixture.Report), map[string]bool{"foo_capability": true})
app.RegisterTopologyRoutes(router, app.StaticCollector(fixture.Report), map[string]bool{"foo_capability": true}, "")
return httptest.NewServer(router)
}

Expand Down
12 changes: 6 additions & 6 deletions app/api_topologies.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,17 +548,17 @@ func (r *Registry) RendererForTopology(topologyID string, values url.Values, rpt
return topology.renderer, nil, nil
}

type reporterHandler func(context.Context, Reporter, http.ResponseWriter, *http.Request)
type reporterHandler func(context.Context, Reporter, string, http.ResponseWriter, *http.Request)

func captureReporter(rep Reporter, f reporterHandler) CtxHandlerFunc {
func captureReporter(rep Reporter, metricsGraphURL string, f reporterHandler) CtxHandlerFunc {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
f(ctx, rep, w, r)
f(ctx, rep, metricsGraphURL, w, r)
}
}

type rendererHandler func(context.Context, render.Renderer, render.Decorator, report.Report, http.ResponseWriter, *http.Request)
type rendererHandler func(context.Context, render.Renderer, render.Decorator, report.Report, string, http.ResponseWriter, *http.Request)

func (r *Registry) captureRenderer(rep Reporter, f rendererHandler) CtxHandlerFunc {
func (r *Registry) captureRenderer(rep Reporter, metricGraphsURL string, f rendererHandler) CtxHandlerFunc {
return func(ctx context.Context, w http.ResponseWriter, req *http.Request) {
var (
topologyID = mux.Vars(req)["topology"]
Expand All @@ -579,6 +579,6 @@ func (r *Registry) captureRenderer(rep Reporter, f rendererHandler) CtxHandlerFu
respondWith(w, http.StatusInternalServerError, err)
return
}
f(ctx, renderer, decorator, rpt, w, req)
f(ctx, renderer, decorator, rpt, metricGraphsURL, w, req)
}
}
4 changes: 2 additions & 2 deletions app/api_topologies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,14 @@ func getTestContainerLabelFilterTopologySummary(t *testing.T, exclude bool) (det
return nil, err
}

return detailed.Summaries(fixture.Report, renderer.Render(fixture.Report, decorator)), nil
return detailed.Summaries(fixture.Report, renderer.Render(fixture.Report, decorator), ""), nil
}

func TestAPITopologyAddsKubernetes(t *testing.T) {
router := mux.NewRouter()
c := app.NewCollector(1 * time.Minute)
app.RegisterReportPostHandler(c, router)
app.RegisterTopologyRoutes(router, c, map[string]bool{"foo_capability": true})
app.RegisterTopologyRoutes(router, c, map[string]bool{"foo_capability": true}, "")
ts := httptest.NewServer(router)
defer ts.Close()

Expand Down
11 changes: 6 additions & 5 deletions app/api_topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ type APINode struct {
}

// Full topology.
func handleTopology(ctx context.Context, renderer render.Renderer, decorator render.Decorator, report report.Report, w http.ResponseWriter, r *http.Request) {
func handleTopology(ctx context.Context, renderer render.Renderer, decorator render.Decorator, report report.Report, metricsGraphURL string, w http.ResponseWriter, r *http.Request) {
respondWith(w, http.StatusOK, APITopology{
Nodes: detailed.Summaries(report, renderer.Render(report, decorator)),
Nodes: detailed.Summaries(report, renderer.Render(report, decorator), metricsGraphURL),
})
}

// Individual nodes.
func handleNode(ctx context.Context, renderer render.Renderer, decorator render.Decorator, report report.Report, w http.ResponseWriter, r *http.Request) {
func handleNode(ctx context.Context, renderer render.Renderer, decorator render.Decorator, report report.Report, metricsGraphURL string, w http.ResponseWriter, r *http.Request) {
var (
vars = mux.Vars(r)
topologyID = vars["topology"]
Expand All @@ -49,13 +49,14 @@ func handleNode(ctx context.Context, renderer render.Renderer, decorator render.
http.NotFound(w, r)
return
}
respondWith(w, http.StatusOK, APINode{Node: detailed.MakeNode(topologyID, report, rendered, node)})
respondWith(w, http.StatusOK, APINode{Node: detailed.MakeNode(topologyID, report, rendered, node, metricsGraphURL)})
}

// Websocket for the full topology.
func handleWebsocket(
ctx context.Context,
rep Reporter,
metricsGraphURL string,
w http.ResponseWriter,
r *http.Request,
) {
Expand Down Expand Up @@ -123,7 +124,7 @@ func handleWebsocket(
log.Errorf("Error generating report: %v", err)
return
}
newTopo := detailed.Summaries(report, renderer.Render(report, decorator))
newTopo := detailed.Summaries(report, renderer.Render(report, decorator), metricsGraphURL)
diff := detailed.TopoDiff(previousTopo, newTopo)
previousTopo = newTopo

Expand Down
8 changes: 4 additions & 4 deletions app/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,23 @@ func gzipHandler(h http.HandlerFunc) http.HandlerFunc {
}

// RegisterTopologyRoutes registers the various topology routes with a http mux.
func RegisterTopologyRoutes(router *mux.Router, r Reporter, capabilities map[string]bool) {
func RegisterTopologyRoutes(router *mux.Router, r Reporter, capabilities map[string]bool, metricsGraphURL string) {
get := router.Methods("GET").Subrouter()
get.HandleFunc("/api",
gzipHandler(requestContextDecorator(apiHandler(r, capabilities))))
get.HandleFunc("/api/topology",
gzipHandler(requestContextDecorator(topologyRegistry.makeTopologyList(r))))
get.
HandleFunc("/api/topology/{topology}",
gzipHandler(requestContextDecorator(topologyRegistry.captureRenderer(r, handleTopology)))).
gzipHandler(requestContextDecorator(topologyRegistry.captureRenderer(r, metricsGraphURL, handleTopology)))).
Name("api_topology_topology")
get.
HandleFunc("/api/topology/{topology}/ws",
requestContextDecorator(captureReporter(r, handleWebsocket))). // NB not gzip!
requestContextDecorator(captureReporter(r, metricsGraphURL, handleWebsocket))). // NB not gzip!
Name("api_topology_topology_ws")
get.
MatcherFunc(URLMatcher("/api/topology/{topology}/{id}")).HandlerFunc(
gzipHandler(requestContextDecorator(topologyRegistry.captureRenderer(r, handleNode)))).
gzipHandler(requestContextDecorator(topologyRegistry.captureRenderer(r, metricsGraphURL, handleNode)))).
Name("api_topology_topology_id")
get.HandleFunc("/api/report",
gzipHandler(requestContextDecorator(makeRawReportHandler(r))))
Expand Down
8 changes: 3 additions & 5 deletions prog/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"github.com/weaveworks/scope/common/weave"
"github.com/weaveworks/scope/common/xfer"
"github.com/weaveworks/scope/probe/docker"
"github.com/weaveworks/scope/render/detailed"
"github.com/weaveworks/weave/common"
)

Expand All @@ -52,7 +51,7 @@ func init() {
}

// Router creates the mux for all the various app components.
func router(collector app.Collector, controlRouter app.ControlRouter, pipeRouter app.PipeRouter, externalUI bool, capabilities map[string]bool) http.Handler {
func router(collector app.Collector, controlRouter app.ControlRouter, pipeRouter app.PipeRouter, externalUI bool, capabilities map[string]bool, metricsGraphURL string) http.Handler {
router := mux.NewRouter().SkipClean(true)

// We pull in the http.DefaultServeMux to get the pprof routes
Expand All @@ -62,7 +61,7 @@ func router(collector app.Collector, controlRouter app.ControlRouter, pipeRouter
app.RegisterReportPostHandler(collector, router)
app.RegisterControlRoutes(router, controlRouter)
app.RegisterPipeRoutes(router, pipeRouter)
app.RegisterTopologyRoutes(router, collector, capabilities)
app.RegisterTopologyRoutes(router, collector, capabilities, metricsGraphURL)

uiHandler := http.FileServer(GetFS(externalUI))
router.PathPrefix("/ui").Name("static").Handler(
Expand Down Expand Up @@ -225,7 +224,6 @@ func appMain(flags appFlags) {
setLogLevel(flags.logLevel)
setLogFormatter(flags.logPrefix)
runtime.SetBlockProfileRate(flags.blockProfileRate)
detailed.SetMetricsGraphURL(flags.metricsGraphURL)

defer log.Info("app exiting")
rand.Seed(time.Now().UnixNano())
Expand Down Expand Up @@ -300,7 +298,7 @@ func appMain(flags appFlags) {
capabilities := map[string]bool{
xfer.ReportPersistenceCapability: flags.s3URL != "local",
}
handler := router(collector, controlRouter, pipeRouter, flags.externalUI, capabilities)
handler := router(collector, controlRouter, pipeRouter, flags.externalUI, capabilities, flags.metricsGraphURL)
if flags.logHTTP {
handler = middleware.Log{
LogRequestHeaders: flags.logHTTPHeaders,
Expand Down
12 changes: 6 additions & 6 deletions render/detailed/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ func internetAddr(node report.Node, ep report.Node) (string, bool) {
return addr, true
}

func (c *connectionCounters) rows(r report.Report, ns report.Nodes, includeLocal bool) []Connection {
func (c *connectionCounters) rows(r report.Report, ns report.Nodes, includeLocal bool, metricsGraphURL string) []Connection {
output := []Connection{}
for row, count := range c.counts {
// Use MakeNodeSummary to render the id and label of this node
// TODO(paulbellamy): Would be cleaner if we hade just a
// MakeNodeID(ns[row.remoteNodeID]). As we don't need the whole summary.
summary, _ := MakeNodeSummary(r, ns[row.remoteNodeID])
summary, _ := MakeNodeSummary(r, ns[row.remoteNodeID], metricsGraphURL)
connection := Connection{
ID: fmt.Sprintf("%s-%s-%s-%s", row.remoteNodeID, row.remoteAddr, row.localAddr, row.port),
NodeID: summary.ID,
Expand Down Expand Up @@ -162,7 +162,7 @@ func (c *connectionCounters) rows(r report.Report, ns report.Nodes, includeLocal
return output
}

func incomingConnectionsSummary(topologyID string, r report.Report, n report.Node, ns report.Nodes) ConnectionsSummary {
func incomingConnectionsSummary(topologyID string, r report.Report, n report.Node, ns report.Nodes, metricsGraphURL string) ConnectionsSummary {
localEndpointIDs, localEndpointIDCopies := endpointChildIDsAndCopyMapOf(n)
counts := newConnectionCounters()

Expand All @@ -188,11 +188,11 @@ func incomingConnectionsSummary(topologyID string, r report.Report, n report.Nod
TopologyID: topologyID,
Label: "Inbound",
Columns: columnHeaders,
Connections: counts.rows(r, ns, isInternetNode(n)),
Connections: counts.rows(r, ns, isInternetNode(n), metricsGraphURL),
}
}

func outgoingConnectionsSummary(topologyID string, r report.Report, n report.Node, ns report.Nodes) ConnectionsSummary {
func outgoingConnectionsSummary(topologyID string, r report.Report, n report.Node, ns report.Nodes, metricsGraphURL string) ConnectionsSummary {
localEndpoints := endpointChildrenOf(n)
counts := newConnectionCounters()

Expand Down Expand Up @@ -220,7 +220,7 @@ func outgoingConnectionsSummary(topologyID string, r report.Report, n report.Nod
TopologyID: topologyID,
Label: "Outbound",
Columns: columnHeaders,
Connections: counts.rows(r, ns, isInternetNode(n)),
Connections: counts.rows(r, ns, isInternetNode(n), metricsGraphURL),
}
}

Expand Down
25 changes: 6 additions & 19 deletions render/detailed/links.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// MetricLink describes a URL referencing a metric.
type MetricLink struct {
// References the metric id
ID string `json:"id,omitempty"`
ID string `json:"id"`
Label string `json:"label"`
URL string `json:"url"`
Priority int `json:"priority"`
Expand All @@ -25,9 +25,6 @@ type MetricLink struct {
const urlQueryVarName = ":query"

var (
// As configured by the user
metricsGraphURL = ""

// Available metric links
linkTemplates = []MetricLink{
{ID: docker.CPUTotalUsage, Label: "CPU", Priority: 1},
Expand Down Expand Up @@ -105,22 +102,10 @@ var (
}
)

// SetMetricsGraphURL sets the URL we deduce our eventual metric link from.
// Supports placeholders such as `:orgID` and `:query`. An empty url disables
// this feature. If the `:query` part is missing, a JSON version will be
// appended, see `queryParamsAsJSON()` for more info.
func SetMetricsGraphURL(url string) {
metricsGraphURL = url
}

// NodeMetricLinks returns the links of a node. The links are collected
// by a predefined set but filtered depending on whether a query
// is configured or not for the particular topology.
func NodeMetricLinks(_ report.Report, n report.Node) []MetricLink {
if metricsGraphURL == "" {
return nil
}

queries := topologyQueries[n.Topology]
if len(queries) == 0 {
return nil
Expand All @@ -137,8 +122,10 @@ func NodeMetricLinks(_ report.Report, n report.Node) []MetricLink {
}

// RenderMetricLinks executes the templated links by supplying the node summary as data.
// `metricsGraphURL` supports placeholders such as `:orgID` and `:query`. If the `:query`
// part is missing, a JSON version will be appended, see `queryParamsAsJSON()` for more info.
// It returns the modified summary.
func RenderMetricLinks(summary NodeSummary, n report.Node) NodeSummary {
func RenderMetricLinks(summary NodeSummary, n report.Node, metricsGraphURL string) NodeSummary {
queries := topologyQueries[n.Topology]
if len(queries) == 0 || len(summary.MetricLinks) == 0 {
return summary
Expand All @@ -157,7 +144,7 @@ func RenderMetricLinks(summary NodeSummary, n report.Node) NodeSummary {
continue
}

link.URL = buildURL(bs.String())
link.URL = buildURL(bs.String(), metricsGraphURL)
links = append(links, link)
}
summary.MetricLinks = links
Expand All @@ -167,7 +154,7 @@ func RenderMetricLinks(summary NodeSummary, n report.Node) NodeSummary {

// buildURL puts together the URL by looking at the configured
// `metricsGraphURL`.
func buildURL(query string) string {
func buildURL(query, metricsGraphURL string) string {
if strings.Contains(metricsGraphURL, urlQueryVarName) {
return strings.Replace(metricsGraphURL, urlQueryVarName, url.PathEscape(query), -1)
}
Expand Down
19 changes: 3 additions & 16 deletions render/detailed/links_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,12 @@ var (
sampleUnknownNode = report.MakeNode("???").WithTopology("foo")
)

func TestNodeMetricLinks_DefaultDisabled(t *testing.T) {
links := detailed.NodeMetricLinks(sampleReport, samplePodNode)
assert.Nil(t, links)
}

func TestNodeMetricLinks_UnknownTopology(t *testing.T) {
detailed.SetMetricsGraphURL("/foo")

links := detailed.NodeMetricLinks(sampleReport, sampleUnknownNode)
assert.Nil(t, links)
}

func TestNodeMetricLinks(t *testing.T) {
detailed.SetMetricsGraphURL("/foo")
defer detailed.SetMetricsGraphURL("")
expected := []detailed.MetricLink{
{ID: docker.CPUTotalUsage, Label: "CPU", Priority: 1, URL: ""},
{ID: docker.MemoryUsage, Label: "Memory", Priority: 2, URL: ""},
Expand All @@ -43,16 +34,14 @@ func TestNodeMetricLinks(t *testing.T) {
func TestRenderMetricLinks_UnknownTopology(t *testing.T) {
summary := detailed.NodeSummary{}

result := detailed.RenderMetricLinks(summary, sampleUnknownNode)
result := detailed.RenderMetricLinks(summary, sampleUnknownNode, "")
assert.Equal(t, summary, result)
}

func TestRenderMetricLinks_Pod(t *testing.T) {
detailed.SetMetricsGraphURL("/prom/:orgID/notebook/new")
defer detailed.SetMetricsGraphURL("")
summary := detailed.NodeSummary{Label: "woo", MetricLinks: detailed.NodeMetricLinks(sampleReport, samplePodNode)}

result := detailed.RenderMetricLinks(summary, samplePodNode)
result := detailed.RenderMetricLinks(summary, samplePodNode, "/prom/:orgID/notebook/new")
assert.Equal(t,
"/prom/:orgID/notebook/new/%7B%22cells%22:%5B%7B%22queries%22:%5B%22sum%28rate%28container_cpu_usage_seconds_total%7Bpod_name=%5C%22woo%5C%22%7D%5B1m%5D%29%29%22%5D%7D%5D%7D",
result.MetricLinks[0].URL)
Expand All @@ -62,11 +51,9 @@ func TestRenderMetricLinks_Pod(t *testing.T) {
}

func TestRenderMetricLinks_QueryReplacement(t *testing.T) {
detailed.SetMetricsGraphURL("/foo/:orgID/bar?q=:query")
defer detailed.SetMetricsGraphURL("")
summary := detailed.NodeSummary{Label: "boo", MetricLinks: detailed.NodeMetricLinks(sampleReport, samplePodNode)}

result := detailed.RenderMetricLinks(summary, samplePodNode)
result := detailed.RenderMetricLinks(summary, samplePodNode, "/foo/:orgID/bar?q=:query")
assert.Equal(t,
"/foo/:orgID/bar?q=sum%28rate%28container_cpu_usage_seconds_total%7Bpod_name=%22boo%22%7D%5B1m%5D%29%29",
result.MetricLinks[0].URL)
Expand Down
14 changes: 7 additions & 7 deletions render/detailed/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ func (c *ControlInstance) CodecDecodeSelf(decoder *codec.Decoder) {

// MakeNode transforms a renderable node to a detailed node. It uses
// aggregate metadata, plus the set of origin node IDs, to produce tables.
func MakeNode(topologyID string, r report.Report, ns report.Nodes, n report.Node) Node {
summary, _ := MakeNodeSummary(r, n)
func MakeNode(topologyID string, r report.Report, ns report.Nodes, n report.Node, metricsGraphURL string) Node {
summary, _ := MakeNodeSummary(r, n, metricsGraphURL)
return Node{
NodeSummary: summary,
Controls: controls(r, n),
Children: children(r, n),
Children: children(r, n, metricsGraphURL),
Connections: []ConnectionsSummary{
incomingConnectionsSummary(topologyID, r, n, ns),
outgoingConnectionsSummary(topologyID, r, n, ns),
incomingConnectionsSummary(topologyID, r, n, ns, metricsGraphURL),
outgoingConnectionsSummary(topologyID, r, n, ns, metricsGraphURL),
},
}
}
Expand Down Expand Up @@ -181,13 +181,13 @@ var nodeSummaryGroupSpecs = []struct {
},
}

func children(r report.Report, n report.Node) []NodeSummaryGroup {
func children(r report.Report, n report.Node, metricsGraphURL string) []NodeSummaryGroup {
summaries := map[string][]NodeSummary{}
n.Children.ForEach(func(child report.Node) {
if child.ID == n.ID {
return
}
summary, ok := MakeNodeSummary(r, child)
summary, ok := MakeNodeSummary(r, child, metricsGraphURL)
if !ok {
return
}
Expand Down
Loading

0 comments on commit 1a00288

Please sign in to comment.