Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[processor/resourcedetection] Add host.ip addresses detection #24450

Merged
merged 19 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .chloggen/mx-psi_host.ip-ref-impl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: resourcedetectionprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add detection of host.ip to system detector.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [24450]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
37 changes: 37 additions & 0 deletions internal/metadataproviders/system/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ type Provider interface {

// HostArch returns the host architecture
HostArch() (string, error)

// HostIPs returns the host's IP interfaces
HostIPs() ([]net.IP, error)
}

type systemMetadataProvider struct {
Expand Down Expand Up @@ -159,3 +162,37 @@ func (p systemMetadataProvider) OSDescription(ctx context.Context) (string, erro
func (systemMetadataProvider) HostArch() (string, error) {
return internal.GOARCHtoHostArch(runtime.GOARCH), nil
}

func (p systemMetadataProvider) HostIPs() (ips []net.IP, err error) {
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}

for _, iface := range ifaces {
// skip if the interface is down or is a loopback interface
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
continue
}

addrs, errAddr := iface.Addrs()
if errAddr != nil {
return nil, fmt.Errorf("failed to get addresses for interface %v: %w", iface, errAddr)
}
for _, addr := range addrs {
ip, _, parseErr := net.ParseCIDR(addr.String())
if parseErr != nil {
return nil, fmt.Errorf("failed to parse address %q from interface %v: %w", addr, iface, parseErr)
}

if ip.IsLoopback() {
// skip loopback IPs
continue
}
mx-psi marked this conversation as resolved.
Show resolved Hide resolved

ips = append(ips, ip)
}

}
return ips, err
}
1 change: 1 addition & 0 deletions processor/resourcedetectionprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Queries the host machine to retrieve the following resource attributes:
* host.arch
* host.name
* host.id
* host.ip
* host.cpu.vendor.id
* host.cpu.family
* host.cpu.model.id
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ all_set:
enabled: true
host.id:
enabled: true
host.ip:
enabled: true
host.name:
enabled: true
os.description:
Expand All @@ -41,6 +43,8 @@ none_set:
enabled: false
host.id:
enabled: false
host.ip:
enabled: false
host.name:
enabled: false
os.description:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ resource_attributes:
description: The host.arch
type: string
enabled: false
host.ip:
description: IP addresses for the host
type: slice
enabled: false
host.cpu.vendor.id:
description: The host.cpu.vendor.id
type: string
Expand Down
12 changes: 12 additions & 0 deletions processor/resourcedetectionprocessor/internal/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
return pcommon.NewResource(), "", fmt.Errorf("failed getting host architecture: %w", err)
}

var hostIPAttribute []any
if d.cfg.ResourceAttributes.HostIP.Enabled {
hostIPs, errIPs := d.provider.HostIPs()
if errIPs != nil {
return pcommon.NewResource(), "", fmt.Errorf("failed getting host IP addresses: %w", errIPs)
}
for _, ip := range hostIPs {
hostIPAttribute = append(hostIPAttribute, ip.String())
}
}

osDescription, err := d.provider.OSDescription(ctx)
if err != nil {
return pcommon.NewResource(), "", fmt.Errorf("failed getting OS description: %w", err)
Expand All @@ -107,6 +118,7 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
}
}
d.rb.SetHostArch(hostArch)
d.rb.SetHostIP(hostIPAttribute)
d.rb.SetOsDescription(osDescription)
if len(cpuInfo) > 0 {
err = setHostCPUInfo(d, cpuInfo[0])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package system
import (
"context"
"errors"
"net"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -66,6 +67,16 @@ func (m *mockMetadata) ReverseLookupHost() (string, error) {
return args.String(0), args.Error(1)
}

func (m *mockMetadata) HostIPs() ([]net.IP, error) {
args := m.MethodCalled("HostIPs")
return args.Get(0).([]net.IP), args.Error(1)
}

var (
testIPsAttribute = []any{"192.168.1.140", "fe80::abc2:4a28:737a:609e"}
testIPsAddresses = []net.IP{net.ParseIP(testIPsAttribute[0].(string)), net.ParseIP(testIPsAttribute[1].(string))}
)

func TestNewDetector(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -97,6 +108,7 @@ func allEnabledConfig() metadata.ResourceAttributesConfig {
cfg := metadata.DefaultResourceAttributesConfig()
cfg.HostArch.Enabled = true
cfg.HostID.Enabled = true
cfg.HostIP.Enabled = true
cfg.OsDescription.Enabled = true
return cfg
}
Expand All @@ -108,6 +120,7 @@ func TestDetectFQDNAvailable(t *testing.T) {
md.On("OSType").Return("darwin", nil)
md.On("HostID").Return("2", nil)
md.On("HostArch").Return("amd64", nil)
md.On("HostIPs").Return(testIPsAddresses, nil)

detector := newTestDetector(md, []string{"dns"}, allEnabledConfig())
res, schemaURL, err := detector.Detect(context.Background())
Expand All @@ -121,6 +134,7 @@ func TestDetectFQDNAvailable(t *testing.T) {
conventions.AttributeOSType: "darwin",
conventions.AttributeHostID: "2",
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
"host.ip": testIPsAttribute,
}

assert.Equal(t, expected, res.Attributes().AsRaw())
Expand All @@ -141,6 +155,7 @@ func TestFallbackHostname(t *testing.T) {
assert.Equal(t, conventions.SchemaURL, schemaURL)
mdHostname.AssertExpectations(t)
mdHostname.AssertNotCalled(t, "HostID")
mdHostname.AssertNotCalled(t, "HostIPs")

expected := map[string]any{
conventions.AttributeHostName: "hostname",
Expand All @@ -158,6 +173,7 @@ func TestEnableHostID(t *testing.T) {
mdHostname.On("OSType").Return("darwin", nil)
mdHostname.On("HostID").Return("3", nil)
mdHostname.On("HostArch").Return("amd64", nil)
mdHostname.On("HostIPs").Return(testIPsAddresses, nil)

detector := newTestDetector(mdHostname, []string{"dns", "os"}, allEnabledConfig())
res, schemaURL, err := detector.Detect(context.Background())
Expand All @@ -171,6 +187,7 @@ func TestEnableHostID(t *testing.T) {
conventions.AttributeOSType: "darwin",
conventions.AttributeHostID: "3",
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
"host.ip": testIPsAttribute,
}

assert.Equal(t, expected, res.Attributes().AsRaw())
Expand All @@ -183,6 +200,7 @@ func TestUseHostname(t *testing.T) {
mdHostname.On("OSType").Return("darwin", nil)
mdHostname.On("HostID").Return("1", nil)
mdHostname.On("HostArch").Return("amd64", nil)
mdHostname.On("HostIPs").Return(testIPsAddresses, nil)

detector := newTestDetector(mdHostname, []string{"os"}, allEnabledConfig())
res, schemaURL, err := detector.Detect(context.Background())
Expand All @@ -196,6 +214,7 @@ func TestUseHostname(t *testing.T) {
conventions.AttributeOSType: "darwin",
conventions.AttributeHostID: "1",
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
"host.ip": testIPsAttribute,
}

assert.Equal(t, expected, res.Attributes().AsRaw())
Expand All @@ -210,6 +229,7 @@ func TestDetectError(t *testing.T) {
mdFQDN.On("Hostname").Return("", errors.New("err"))
mdFQDN.On("HostID").Return("", errors.New("err"))
mdFQDN.On("HostArch").Return("amd64", nil)
mdFQDN.On("HostIPs").Return(testIPsAddresses, nil)

detector := newTestDetector(mdFQDN, []string{"dns"}, allEnabledConfig())
res, schemaURL, err := detector.Detect(context.Background())
Expand All @@ -224,6 +244,7 @@ func TestDetectError(t *testing.T) {
mdHostname.On("Hostname").Return("", errors.New("err"))
mdHostname.On("HostID").Return("", errors.New("err"))
mdHostname.On("HostArch").Return("amd64", nil)
mdHostname.On("HostIPs").Return(testIPsAddresses, nil)

detector = newTestDetector(mdHostname, []string{"os"}, allEnabledConfig())
res, schemaURL, err = detector.Detect(context.Background())
Expand All @@ -238,6 +259,7 @@ func TestDetectError(t *testing.T) {
mdOSType.On("OSType").Return("", errors.New("err"))
mdOSType.On("HostID").Return("1", nil)
mdOSType.On("HostArch").Return("amd64", nil)
mdOSType.On("HostIPs").Return(testIPsAddresses, nil)

detector = newTestDetector(mdOSType, []string{"os"}, allEnabledConfig())
res, schemaURL, err = detector.Detect(context.Background())
Expand All @@ -252,6 +274,7 @@ func TestDetectError(t *testing.T) {
mdHostID.On("OSType").Return("linux", nil)
mdHostID.On("HostID").Return("", errors.New("err"))
mdHostID.On("HostArch").Return("arm64", nil)
mdHostID.On("HostIPs").Return(testIPsAddresses, nil)

detector = newTestDetector(mdHostID, []string{"os"}, allEnabledConfig())
res, schemaURL, err = detector.Detect(context.Background())
Expand All @@ -262,6 +285,7 @@ func TestDetectError(t *testing.T) {
conventions.AttributeOSDescription: "Ubuntu 22.04.2 LTS (Jammy Jellyfish)",
conventions.AttributeOSType: "linux",
conventions.AttributeHostArch: conventions.AttributeHostArchARM64,
"host.ip": testIPsAttribute,
}, res.Attributes().AsRaw())
}

Expand Down