Skip to content

Commit

Permalink
Merge pull request #2921 from weaveworks/optimise-dnsnames
Browse files Browse the repository at this point in the history
Avoid object creation when scanning DNS names
  • Loading branch information
bboreham authored Nov 6, 2017
2 parents 6aab6ce + 800979a commit c52b5f6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
4 changes: 2 additions & 2 deletions render/detailed/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ func internetAddr(node report.Node, ep report.Node) (string, bool) {
if !ok {
return "", false
}
if names := render.DNSNames(ep); len(names) > 0 {
if name, found := render.DNSFirstMatch(ep, func(string) bool { return true }); found {
// we show the "most important" name only, since we don't have
// space for more
addr = fmt.Sprintf("%s (%s)", names[0], addr)
addr = fmt.Sprintf("%s (%s)", name, addr)
}
return addr, true
}
Expand Down
34 changes: 20 additions & 14 deletions render/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package render

import (
"net"
"sort"
"strings"

"github.com/weaveworks/scope/probe/endpoint"
Expand Down Expand Up @@ -42,10 +41,8 @@ func NewDerivedExternalNode(n report.Node, addr string, local report.Networks) (
// First, check if it's a known service and emit a a specific node if it
// is. This needs to be done before checking IPs since known services can
// live in the same network, see https://github.com/weaveworks/scope/issues/2163
for _, hostname := range DNSNames(n) {
if isKnownService(hostname) {
return NewDerivedPseudoNode(ServiceNodeIDPrefix+hostname, n), true
}
if hostname, found := DNSFirstMatch(n, isKnownService); found {
return NewDerivedPseudoNode(ServiceNodeIDPrefix+hostname, n), true
}

// If the dstNodeAddr is not in a network local to this report, we emit an
Expand All @@ -62,15 +59,24 @@ func NewDerivedExternalNode(n report.Node, addr string, local report.Networks) (
return report.Node{}, false
}

// DNSNames returns a prioritized list of snooped and reverse-resolved
// DNS names associated with node n.
func DNSNames(n report.Node) []string {
snoopedNames, _ := n.Sets.Lookup(endpoint.SnoopedDNSNames)
reverseNames, _ := n.Sets.Lookup(endpoint.ReverseDNSNames)
// sort the names, to make selection for display more
// DNSFirstMatch returns the first DNS name where match() returns
// true, from a prioritized list of snooped and reverse-resolved DNS
// names associated with node n.
func DNSFirstMatch(n report.Node, match func(name string) bool) (string, bool) {
// we rely on Sets being sorted, to make selection for display more
// deterministic
sort.StringSlice(snoopedNames).Sort()
sort.StringSlice(reverseNames).Sort()
// prioritize snooped names
return append(snoopedNames, reverseNames...)
snoopedNames, _ := n.Sets.Lookup(endpoint.SnoopedDNSNames)
for _, hostname := range snoopedNames {
if match(hostname) {
return hostname, true
}
}
reverseNames, _ := n.Sets.Lookup(endpoint.ReverseDNSNames)
for _, hostname := range reverseNames {
if match(hostname) {
return hostname, true
}
}
return "", false
}

0 comments on commit c52b5f6

Please sign in to comment.