Merge pull request #3139 from weaveworks/3127-no-truncate

do not truncate tables

Fixes #3127.
This commit is contained in:
Matthias Radestock
2018-04-15 21:49:19 +01:00
committed by GitHub
3 changed files with 19 additions and 97 deletions

View File

@@ -281,7 +281,7 @@ func setupFlags(flags *flags) {
flag.StringVar(&flags.probe.pluginsRoot, "probe.plugins.root", "/var/run/scope/plugins", "Root directory to search for plugins")
flag.BoolVar(&flags.probe.noControls, "probe.no-controls", false, "Disable controls (e.g. start/stop containers, terminals, logs ...)")
flag.BoolVar(&flags.probe.noCommandLineArguments, "probe.omit.cmd-args", false, "Disable collection of command-line arguments")
flag.BoolVar(&flags.probe.noEnvironmentVariables, "probe.omit.env-vars", false, "Disable collection of environment variables")
flag.BoolVar(&flags.probe.noEnvironmentVariables, "probe.omit.env-vars", true, "Disable collection of environment variables")
flag.BoolVar(&flags.probe.insecure, "probe.insecure", false, "(SSL) explicitly allow \"insecure\" SSL connections and transfers")
flag.StringVar(&flags.probe.resolver, "probe.resolver", "", "IP address & port of resolver to use. Default is to use system resolver.")

View File

@@ -10,55 +10,37 @@ import (
"github.com/weaveworks/common/mtime"
)
// MaxTableRows sets the limit on the table size to render
// TODO: this won't be needed once we send reports incrementally
// Table types
const (
MaxTableRows = 20
TableEntryKeySeparator = "___"
TruncationCountPrefix = "table_truncation_count_"
MulticolumnTableType = "multicolumn-table"
PropertyListType = "property-list"
MulticolumnTableType = "multicolumn-table"
PropertyListType = "property-list"
)
// withTableTruncationInformation appends table truncation info to the node, returning the new node.
func (node Node) withTableTruncationInformation(prefix string, totalRowsCount int, now time.Time) Node {
if totalRowsCount > MaxTableRows {
truncationCount := fmt.Sprintf("%d", totalRowsCount-MaxTableRows)
node = node.WithLatest(TruncationCountPrefix+prefix, now, truncationCount)
}
return node
}
const (
tableEntryKeySeparator = "___"
truncationCountPrefix = "table_truncation_count_"
)
// AddPrefixMulticolumnTable appends arbitrary rows to the Node, returning a new node.
func (node Node) AddPrefixMulticolumnTable(prefix string, rows []Row) Node {
now := mtime.Now()
addedRowsCount := 0
for _, row := range rows {
if addedRowsCount >= MaxTableRows {
break
}
// Add all the row values as separate entries
for columnID, value := range row.Entries {
key := strings.Join([]string{row.ID, columnID}, TableEntryKeySeparator)
key := strings.Join([]string{row.ID, columnID}, tableEntryKeySeparator)
node = node.WithLatest(prefix+key, now, value)
}
addedRowsCount++
}
return node.withTableTruncationInformation(prefix, len(rows), now)
return node
}
// AddPrefixPropertyList appends arbitrary key-value pairs to the Node, returning a new node.
func (node Node) AddPrefixPropertyList(prefix string, propertyList map[string]string) Node {
now := mtime.Now()
addedPropertiesCount := 0
for label, value := range propertyList {
if addedPropertiesCount >= MaxTableRows {
break
}
node = node.WithLatest(prefix+label, now, value)
addedPropertiesCount++
}
return node.withTableTruncationInformation(prefix, len(propertyList), now)
return node
}
// WithoutPrefix returns the string with trimmed prefix and a
@@ -81,7 +63,7 @@ func (node Node) ExtractMulticolumnTable(template TableTemplate) (rows []Row) {
// methods should be enough to implement ForEachWithPrefix(prefix) straightforwardly.
node.Latest.ForEach(func(key string, _ time.Time, value string) {
if keyWithoutPrefix, ok := WithoutPrefix(key, template.Prefix); ok {
ids := strings.Split(keyWithoutPrefix, TableEntryKeySeparator)
ids := strings.Split(keyWithoutPrefix, tableEntryKeySeparator)
rowID, columnID := ids[0], ids[1]
// If the row with the given ID doesn't yet exist, we create an empty one.
if _, ok := rowsMapByID[rowID]; !ok {
@@ -137,7 +119,12 @@ func (node Node) ExtractPropertyList(template TableTemplate) (rows []Row) {
return rows
}
// ExtractTable returns the rows to build either a property list or a generic table from this node
// ExtractTable returns the rows to build either a property list or a
// generic table from this node. It also returns the number of rows,
// if any, that were truncated. The probes used to limit the number of
// labels, env vars and Weave Net connections they report, but this
// logic has since been removed. So the code here dealing with
// truncation is only retained in order to handle legacy reports.
func (node Node) ExtractTable(template TableTemplate) (rows []Row, truncationCount int) {
switch template.Type {
case MulticolumnTableType:
@@ -147,7 +134,7 @@ func (node Node) ExtractTable(template TableTemplate) (rows []Row, truncationCou
}
truncationCount = 0
if str, ok := node.Latest.Lookup(TruncationCountPrefix + template.Prefix); ok {
if str, ok := node.Latest.Lookup(truncationCountPrefix + template.Prefix); ok {
if n, err := fmt.Sscanf(str, "%d", &truncationCount); n != 1 || err != nil {
log.Warn("Unexpected truncation count format %q", str)
}

View File

@@ -1,7 +1,6 @@
package report_test
import (
"fmt"
"reflect"
"testing"
@@ -132,70 +131,6 @@ func TestFixedPropertyLists(t *testing.T) {
}
}
func TestPropertyListTruncation(t *testing.T) {
wantTruncationCount := 1
propertyList := map[string]string{}
for i := 0; i < report.MaxTableRows+wantTruncationCount; i++ {
key := fmt.Sprintf("key%d", i)
value := fmt.Sprintf("value%d", i)
propertyList[key] = value
}
nmd := report.MakeNode("foo1")
nmd = nmd.AddPrefixPropertyList("foo_", propertyList)
template := report.TableTemplate{
Type: report.PropertyListType,
Prefix: "foo_",
}
_, truncationCount := nmd.ExtractTable(template)
if truncationCount != wantTruncationCount {
t.Error(
"Property list should had been truncated by",
wantTruncationCount,
"and not",
truncationCount,
)
}
}
func TestMulticolumnTableTruncation(t *testing.T) {
wantTruncationCount := 1
rows := []report.Row{}
for i := 0; i < report.MaxTableRows+wantTruncationCount; i++ {
rowID := fmt.Sprintf("row%d", i)
colID := fmt.Sprintf("col%d", i)
value := fmt.Sprintf("value%d", i)
rows = append(rows, report.Row{
ID: rowID,
Entries: map[string]string{
colID: value,
},
})
}
nmd := report.MakeNode("foo1")
nmd = nmd.AddPrefixMulticolumnTable("foo_", rows)
template := report.TableTemplate{
Type: report.MulticolumnTableType,
Prefix: "foo_",
}
_, truncationCount := nmd.ExtractTable(template)
if truncationCount != wantTruncationCount {
t.Error(
"Property list should had been truncated by",
wantTruncationCount,
"and not",
truncationCount,
)
}
}
func TestTables(t *testing.T) {
want := []report.Table{
{