Revert "Revert "Avoid global by passing metricsGraphURL down""

This reverts commit 4e738016969d66bc7b1951f1d63f865f76a20bb8.
This commit is contained in:
Roland Schilter
2017-08-09 11:44:23 +01:00
parent 69c4430d58
commit c1562ef28f
13 changed files with 58 additions and 76 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) []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,
@@ -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()
@@ -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()
@@ -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),
}
}

View File

@@ -21,9 +21,6 @@ const (
)
var (
// As configured by the user
metricsGraphURL = ""
// Metadata for shown queries
shownQueries = []struct {
ID string
@@ -102,17 +99,11 @@ func formatMetricQueries(filter string, ids []string) map[string]string {
return queries
}
// 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
}
var metricsGraphURL = ""
// 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) NodeSummary {
func RenderMetricURLs(summary NodeSummary, n report.Node, metricsGraphURL string) NodeSummary {
if metricsGraphURL == "" {
return summary
}

View File

@@ -26,27 +26,23 @@ 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)
result := detailed.RenderMetricURLs(s, sampleUnknownNode, sampleMetricsGraphURL)
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)
result := detailed.RenderMetricURLs(s, samplePodNode, sampleMetricsGraphURL)
assert.Equal(t, 0, strings.Index(result.Metrics[0].URL, sampleMetricsGraphURL))
assert.Contains(t, result.Metrics[0].URL, "container_memory_usage_bytes%7Bpod_name%3D%5C%22foo%5C%22%7D")
@@ -55,9 +51,7 @@ func TestRenderMetricURLs(t *testing.T) {
}
func TestRenderMetricURLs_EmptyMetrics(t *testing.T) {
detailed.SetMetricsGraphURL(sampleMetricsGraphURL)
defer detailed.SetMetricsGraphURL("")
result := detailed.RenderMetricURLs(detailed.NodeSummary{}, samplePodNode)
result := detailed.RenderMetricURLs(detailed.NodeSummary{}, samplePodNode, sampleMetricsGraphURL)
m := result.Metrics[0]
assert.Equal(t, docker.CPUTotalUsage, m.ID)
@@ -73,13 +67,11 @@ 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)
result := detailed.RenderMetricURLs(s, samplePodNode, sampleMetricsGraphURL)
assert.NotEmpty(t, result.Metrics[0].URL)
assert.False(t, result.Metrics[0].ValueEmpty)
@@ -90,10 +82,8 @@ 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)
result := detailed.RenderMetricURLs(s, samplePodNode, "http://example.test/?q=:query")
assert.Contains(t, result.Metrics[0].URL, "http://example.test/?q=")
assert.Contains(t, result.Metrics[0].URL, "container_memory_usage_bytes%7Bpod_name%3D%22foo%22%7D")

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) 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),
},
}
}
@@ -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
}

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) (NodeSummary, bool) {
func MakeNodeSummary(r report.Report, n report.Node, metricsGraphURL string) (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), b
return RenderMetricURLs(summary, n, metricsGraphURL), 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) (NodeSummary, bool) {
}
if strings.HasPrefix(n.Topology, "group:") {
summary, b := groupNodeSummary(baseNodeSummary(r, n), r, n)
return RenderMetricURLs(summary, n), b
return RenderMetricURLs(summary, n, metricsGraphURL), 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)
return NodeSummary{
ns := NodeSummary{
ID: n.ID,
Shape: t.GetShape(),
Linkable: true,
@@ -144,6 +144,8 @@ 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) {
@@ -372,11 +374,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) NodeSummaries {
func Summaries(r report.Report, rns report.Nodes, metricsGraphURL string) NodeSummaries {
result := NodeSummaries{}
for id, node := range rns {
if summary, ok := MakeNodeSummary(r, node); ok {
if summary, ok := MakeNodeSummary(r, node, metricsGraphURL); 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