Revert "Avoid global by passing metricsGraphURL down"

This reverts commit 1a002880ed38cbb8da73e41954513d7144b362c4.
This commit is contained in:
Roland Schilter
2017-07-24 17:54:21 +02:00
parent 29a3070338
commit b2f4eeb4e6
13 changed files with 80 additions and 60 deletions

View File

@@ -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, metricsGraphURL string) []Connection {
func (c *connectionCounters) rows(r report.Report, ns report.Nodes, includeLocal bool) []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], metricsGraphURL)
summary, _ := MakeNodeSummary(r, ns[row.remoteNodeID])
connection := Connection{
ID: fmt.Sprintf("%s-%s-%s-%s", row.remoteNodeID, row.remoteAddr, row.localAddr, row.port),
NodeID: summary.ID,
@@ -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, metricsGraphURL string) ConnectionsSummary {
func incomingConnectionsSummary(topologyID string, r report.Report, n report.Node, ns report.Nodes) ConnectionsSummary {
localEndpointIDs, localEndpointIDCopies := endpointChildIDsAndCopyMapOf(n)
counts := newConnectionCounters()
@@ -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), metricsGraphURL),
Connections: counts.rows(r, ns, isInternetNode(n)),
}
}
func outgoingConnectionsSummary(topologyID string, r report.Report, n report.Node, ns report.Nodes, metricsGraphURL string) ConnectionsSummary {
func outgoingConnectionsSummary(topologyID string, r report.Report, n report.Node, ns report.Nodes) ConnectionsSummary {
localEndpoints := endpointChildrenOf(n)
counts := newConnectionCounters()
@@ -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), metricsGraphURL),
Connections: counts.rows(r, ns, isInternetNode(n)),
}
}

View File

@@ -16,6 +16,9 @@ import (
const urlQueryVarName = ":query"
var (
// As configured by the user
metricsGraphURL = ""
// Metadata for shown queries
shownQueries = []struct {
ID string
@@ -94,9 +97,17 @@ 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
}
// RenderMetricURLs sets respective URLs for metrics in a node summary. Missing metrics
// where we have a query for will be appended as an empty metric (no values or samples).
func RenderMetricURLs(summary NodeSummary, n report.Node, metricsGraphURL string) NodeSummary {
func RenderMetricURLs(summary NodeSummary, n report.Node) NodeSummary {
if metricsGraphURL == "" {
return summary
}
@@ -127,7 +138,7 @@ func RenderMetricURLs(summary NodeSummary, n report.Node, metricsGraphURL string
}
ms = append(ms, metric)
ms[len(ms)-1].URL = buildURL(bs.String(), metricsGraphURL)
ms[len(ms)-1].URL = buildURL(bs.String())
found[metric.ID] = struct{}{}
}
@@ -151,7 +162,7 @@ func RenderMetricURLs(summary NodeSummary, n report.Node, metricsGraphURL string
ms = append(ms, report.MetricRow{
ID: metadata.ID,
Label: metadata.Label,
URL: buildURL(bs.String(), metricsGraphURL),
URL: buildURL(bs.String()),
Metric: &report.Metric{},
Priority: maxprio,
ValueEmpty: true,
@@ -164,7 +175,7 @@ func RenderMetricURLs(summary NodeSummary, n report.Node, metricsGraphURL string
}
// buildURL puts together the URL by looking at the configured `metricsGraphURL`.
func buildURL(query, metricsGraphURL string) string {
func buildURL(query string) string {
if strings.Contains(metricsGraphURL, urlQueryVarName) {
return strings.Replace(metricsGraphURL, urlQueryVarName, url.QueryEscape(query), -1)
}

View File

@@ -25,23 +25,27 @@ var (
func TestRenderMetricURLs_Disabled(t *testing.T) {
s := detailed.NodeSummary{Label: "foo", Metrics: sampleMetrics}
result := detailed.RenderMetricURLs(s, samplePodNode, "")
result := detailed.RenderMetricURLs(s, samplePodNode)
assert.Empty(t, result.Metrics[0].URL)
assert.Empty(t, result.Metrics[1].URL)
}
func TestRenderMetricURLs_UnknownTopology(t *testing.T) {
detailed.SetMetricsGraphURL(sampleMetricsGraphURL)
defer detailed.SetMetricsGraphURL("")
s := detailed.NodeSummary{Label: "foo", Metrics: sampleMetrics}
result := detailed.RenderMetricURLs(s, sampleUnknownNode, sampleMetricsGraphURL)
result := detailed.RenderMetricURLs(s, sampleUnknownNode)
assert.Empty(t, result.Metrics[0].URL)
assert.Empty(t, result.Metrics[1].URL)
}
func TestRenderMetricURLs(t *testing.T) {
detailed.SetMetricsGraphURL(sampleMetricsGraphURL)
defer detailed.SetMetricsGraphURL("")
s := detailed.NodeSummary{Label: "foo", Metrics: sampleMetrics}
result := detailed.RenderMetricURLs(s, samplePodNode, sampleMetricsGraphURL)
result := detailed.RenderMetricURLs(s, samplePodNode)
u := "/prom/:orgID/notebook/new/%7B%22cells%22%3A%5B%7B%22queries%22%3A%5B%22sum%28container_memory_usage_bytes%7Bpod_name%3D%5C%22foo%5C%22%7D%29%22%5D%7D%5D%7D"
assert.Equal(t, u, result.Metrics[0].URL)
@@ -50,7 +54,9 @@ func TestRenderMetricURLs(t *testing.T) {
}
func TestRenderMetricURLs_EmptyMetrics(t *testing.T) {
result := detailed.RenderMetricURLs(detailed.NodeSummary{}, samplePodNode, sampleMetricsGraphURL)
detailed.SetMetricsGraphURL(sampleMetricsGraphURL)
defer detailed.SetMetricsGraphURL("")
result := detailed.RenderMetricURLs(detailed.NodeSummary{}, samplePodNode)
m := result.Metrics[0]
assert.Equal(t, docker.CPUTotalUsage, m.ID)
@@ -66,11 +72,13 @@ func TestRenderMetricURLs_EmptyMetrics(t *testing.T) {
}
func TestRenderMetricURLs_CombinedEmptyMetrics(t *testing.T) {
detailed.SetMetricsGraphURL(sampleMetricsGraphURL)
defer detailed.SetMetricsGraphURL("")
s := detailed.NodeSummary{
Label: "foo",
Metrics: []report.MetricRow{{ID: docker.MemoryUsage, Priority: 1}},
}
result := detailed.RenderMetricURLs(s, samplePodNode, sampleMetricsGraphURL)
result := detailed.RenderMetricURLs(s, samplePodNode)
assert.NotEmpty(t, result.Metrics[0].URL)
assert.False(t, result.Metrics[0].ValueEmpty)
@@ -81,8 +89,10 @@ func TestRenderMetricURLs_CombinedEmptyMetrics(t *testing.T) {
}
func TestRenderMetricURLs_QueryReplacement(t *testing.T) {
detailed.SetMetricsGraphURL("http://example.test/?q=:query")
defer detailed.SetMetricsGraphURL("")
s := detailed.NodeSummary{Label: "foo", Metrics: sampleMetrics}
result := detailed.RenderMetricURLs(s, samplePodNode, "http://example.test/?q=:query")
result := detailed.RenderMetricURLs(s, samplePodNode)
u := "http://example.test/?q=sum%28container_memory_usage_bytes%7Bpod_name%3D%22foo%22%7D%29"
assert.Equal(t, u, result.Metrics[0].URL)

View File

@@ -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, metricsGraphURL string) Node {
summary, _ := MakeNodeSummary(r, n, metricsGraphURL)
func MakeNode(topologyID string, r report.Report, ns report.Nodes, n report.Node) Node {
summary, _ := MakeNodeSummary(r, n)
return Node{
NodeSummary: summary,
Controls: controls(r, n),
Children: children(r, n, metricsGraphURL),
Children: children(r, n),
Connections: []ConnectionsSummary{
incomingConnectionsSummary(topologyID, r, n, ns, metricsGraphURL),
outgoingConnectionsSummary(topologyID, r, n, ns, metricsGraphURL),
incomingConnectionsSummary(topologyID, r, n, ns),
outgoingConnectionsSummary(topologyID, r, n, ns),
},
}
}
@@ -181,13 +181,13 @@ var nodeSummaryGroupSpecs = []struct {
},
}
func children(r report.Report, n report.Node, metricsGraphURL string) []NodeSummaryGroup {
func children(r report.Report, n report.Node) []NodeSummaryGroup {
summaries := map[string][]NodeSummary{}
n.Children.ForEach(func(child report.Node) {
if child.ID == n.ID {
return
}
summary, ok := MakeNodeSummary(r, child, metricsGraphURL)
summary, ok := MakeNodeSummary(r, child)
if !ok {
return
}

View File

@@ -18,7 +18,7 @@ import (
)
func child(t *testing.T, r render.Renderer, id string) detailed.NodeSummary {
s, ok := detailed.MakeNodeSummary(fixture.Report, r.Render(fixture.Report, nil)[id], "")
s, ok := detailed.MakeNodeSummary(fixture.Report, r.Render(fixture.Report, nil)[id])
if !ok {
t.Fatalf("Expected node %s to be summarizable, but wasn't", id)
}
@@ -32,7 +32,7 @@ func connectionID(nodeID string, addr string) string {
func TestMakeDetailedHostNode(t *testing.T) {
renderableNodes := render.HostRenderer.Render(fixture.Report, nil)
renderableNode := renderableNodes[fixture.ClientHostNodeID]
have := detailed.MakeNode("hosts", fixture.Report, renderableNodes, renderableNode, "")
have := detailed.MakeNode("hosts", fixture.Report, renderableNodes, renderableNode)
containerImageNodeSummary := child(t, render.ContainerImageRenderer, expected.ClientContainerImageNodeID)
containerNodeSummary := child(t, render.ContainerRenderer, fixture.ClientContainerNodeID)
@@ -183,7 +183,7 @@ func TestMakeDetailedContainerNode(t *testing.T) {
if !ok {
t.Fatalf("Node not found: %s", id)
}
have := detailed.MakeNode("containers", fixture.Report, renderableNodes, renderableNode, "")
have := detailed.MakeNode("containers", fixture.Report, renderableNodes, renderableNode)
serverProcessNodeSummary := child(t, render.ProcessRenderer, fixture.ServerProcessNodeID)
serverProcessNodeSummary.Linkable = true
@@ -313,7 +313,7 @@ func TestMakeDetailedPodNode(t *testing.T) {
if !ok {
t.Fatalf("Node not found: %s", id)
}
have := detailed.MakeNode("pods", fixture.Report, renderableNodes, renderableNode, "")
have := detailed.MakeNode("pods", fixture.Report, renderableNodes, renderableNode)
containerNodeSummary := child(t, render.ContainerWithImageNameRenderer, fixture.ServerContainerNodeID)
serverProcessNodeSummary := child(t, render.ProcessRenderer, fixture.ServerProcessNodeID)

View File

@@ -102,12 +102,12 @@ var primaryAPITopology = map[string]string{
}
// MakeNodeSummary summarizes a node, if possible.
func MakeNodeSummary(r report.Report, n report.Node, metricsGraphURL string) (NodeSummary, bool) {
func MakeNodeSummary(r report.Report, n report.Node) (NodeSummary, bool) {
if renderer, ok := renderers[n.Topology]; ok {
// Skip (and don't fall through to fallback) if renderer maps to nil
if renderer != nil {
summary, b := renderer(baseNodeSummary(r, n), n)
return RenderMetricURLs(summary, n, metricsGraphURL), b
return RenderMetricURLs(summary, n), b
}
} else if _, ok := r.Topology(n.Topology); ok {
summary := baseNodeSummary(r, n)
@@ -116,7 +116,7 @@ func MakeNodeSummary(r report.Report, n report.Node, metricsGraphURL string) (No
}
if strings.HasPrefix(n.Topology, "group:") {
summary, b := groupNodeSummary(baseNodeSummary(r, n), r, n)
return RenderMetricURLs(summary, n, metricsGraphURL), b
return RenderMetricURLs(summary, n), b
}
return NodeSummary{}, false
}
@@ -134,7 +134,7 @@ func (n NodeSummary) SummarizeMetrics() NodeSummary {
func baseNodeSummary(r report.Report, n report.Node) NodeSummary {
t, _ := r.Topology(n.Topology)
ns := NodeSummary{
return NodeSummary{
ID: n.ID,
Shape: t.GetShape(),
Linkable: true,
@@ -144,8 +144,6 @@ func baseNodeSummary(r report.Report, n report.Node) NodeSummary {
Tables: NodeTables(r, n),
Adjacency: n.Adjacency,
}
return ns
}
func pseudoNodeSummary(base NodeSummary, n report.Node) (NodeSummary, bool) {
@@ -374,11 +372,11 @@ func (s nodeSummariesByID) Less(i, j int) bool { return s[i].ID < s[j].ID }
type NodeSummaries map[string]NodeSummary
// Summaries converts RenderableNodes into a set of NodeSummaries
func Summaries(r report.Report, rns report.Nodes, metricsGraphURL string) NodeSummaries {
func Summaries(r report.Report, rns report.Nodes) NodeSummaries {
result := NodeSummaries{}
for id, node := range rns {
if summary, ok := MakeNodeSummary(r, node, metricsGraphURL); ok {
if summary, ok := MakeNodeSummary(r, node); ok {
for i, m := range summary.Metrics {
summary.Metrics[i] = m.Summary()
}

View File

@@ -21,7 +21,7 @@ import (
func TestSummaries(t *testing.T) {
{
// Just a convenient source of some rendered nodes
have := detailed.Summaries(fixture.Report, render.ProcessRenderer.Render(fixture.Report, nil), "")
have := detailed.Summaries(fixture.Report, render.ProcessRenderer.Render(fixture.Report, nil))
// The ids of the processes rendered above
expectedIDs := []string{
fixture.ClientProcess1NodeID,
@@ -51,7 +51,7 @@ func TestSummaries(t *testing.T) {
input := fixture.Report.Copy()
input.Process.Nodes[fixture.ClientProcess1NodeID].Metrics[process.CPUUsage] = metric
have := detailed.Summaries(input, render.ProcessRenderer.Render(input, nil), "")
have := detailed.Summaries(input, render.ProcessRenderer.Render(input, nil))
node, ok := have[fixture.ClientProcess1NodeID]
if !ok {
@@ -184,7 +184,7 @@ func TestMakeNodeSummary(t *testing.T) {
},
}
for _, testcase := range testcases {
have, ok := detailed.MakeNodeSummary(fixture.Report, testcase.input, "")
have, ok := detailed.MakeNodeSummary(fixture.Report, testcase.input)
if ok != testcase.ok {
t.Errorf("%s: MakeNodeSummary failed: expected ok value to be: %v", testcase.name, testcase.ok)
continue