Skip to content

Commit

Permalink
[exporter/datadogexporter] Do not pick up localhost-like names from a…
Browse files Browse the repository at this point in the history
…ttributes (#6477)

We need to do this validation to exclude localhost-like names which are not really useful.
  • Loading branch information
mx-psi authored Dec 1, 2021
1 parent 9499ab7 commit 8cf5373
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
20 changes: 20 additions & 0 deletions exporter/datadogexporter/internal/attributes/hostname.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ func getClusterName(attrs pdata.AttributeMap) (string, bool) {
//
// It returns a boolean value indicated if any name was found
func HostnameFromAttributes(attrs pdata.AttributeMap) (string, bool) {
// Check if the host is localhost or 0.0.0.0, if so discard it.
// We don't do the more strict validation done for metadata,
// to avoid breaking users existing invalid-but-accepted hostnames.
var invalidHosts = map[string]struct{}{
"0.0.0.0": {},
"127.0.0.1": {},
"localhost": {},
"localhost.localdomain": {},
"localhost6.localdomain6": {},
"ip6-localhost": {},
}

candidateHost, ok := unsanitizedHostnameFromAttributes(attrs)
if _, invalid := invalidHosts[candidateHost]; invalid {
return "", false
}
return candidateHost, ok
}

func unsanitizedHostnameFromAttributes(attrs pdata.AttributeMap) (string, bool) {
// Custom hostname: useful for overriding in k8s/cloud envs
if customHostname, ok := attrs.Get(AttributeDatadogHostname); ok {
return customHostname.StringVal(), true
Expand Down
7 changes: 7 additions & 0 deletions exporter/datadogexporter/internal/attributes/hostname_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ func TestHostnameFromAttributes(t *testing.T) {
hostname, ok = HostnameFromAttributes(attrs)
assert.False(t, ok)
assert.Empty(t, hostname)

attrs = testutils.NewAttributeMap(map[string]string{
AttributeDatadogHostname: "127.0.0.1",
})
hostname, ok = HostnameFromAttributes(attrs)
assert.False(t, ok)
assert.Empty(t, hostname)
}

func TestGetClusterName(t *testing.T) {
Expand Down

0 comments on commit 8cf5373

Please sign in to comment.