Skip to content

Commit 554dee1

Browse files
authored
[processor/resourcedetection] Add host.ip addresses detection (#24450)
**Description:** Adds detection for `host.ip`. Relates to open-telemetry/semantic-conventions/pull/203 Fixes #24765
1 parent 73747ae commit 554dee1

File tree

11 files changed

+122
-1
lines changed

11 files changed

+122
-1
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Use this changelog template to create an entry for release notes.
2+
# If your change doesn't affect end users, such as a test fix or a tooling change,
3+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
4+
5+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
6+
change_type: enhancement
7+
8+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
9+
component: resourcedetectionprocessor
10+
11+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
12+
note: Add detection of host.ip to system detector.
13+
14+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
15+
issues: [24450]
16+
17+
# (Optional) One or more lines of additional information to render under the primary note.
18+
# These lines will be padded with 2 spaces and then inserted directly into the document.
19+
# Use pipe (|) for multiline entries.
20+
subtext:

internal/metadataproviders/system/metadata.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ type Provider interface {
6363

6464
// HostArch returns the host architecture
6565
HostArch() (string, error)
66+
67+
// HostIPs returns the host's IP interfaces
68+
HostIPs() ([]net.IP, error)
6669
}
6770

6871
type systemMetadataProvider struct {
@@ -159,3 +162,37 @@ func (p systemMetadataProvider) OSDescription(ctx context.Context) (string, erro
159162
func (systemMetadataProvider) HostArch() (string, error) {
160163
return internal.GOARCHtoHostArch(runtime.GOARCH), nil
161164
}
165+
166+
func (p systemMetadataProvider) HostIPs() (ips []net.IP, err error) {
167+
ifaces, err := net.Interfaces()
168+
if err != nil {
169+
return nil, err
170+
}
171+
172+
for _, iface := range ifaces {
173+
// skip if the interface is down or is a loopback interface
174+
if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 {
175+
continue
176+
}
177+
178+
addrs, errAddr := iface.Addrs()
179+
if errAddr != nil {
180+
return nil, fmt.Errorf("failed to get addresses for interface %v: %w", iface, errAddr)
181+
}
182+
for _, addr := range addrs {
183+
ip, _, parseErr := net.ParseCIDR(addr.String())
184+
if parseErr != nil {
185+
return nil, fmt.Errorf("failed to parse address %q from interface %v: %w", addr, iface, parseErr)
186+
}
187+
188+
if ip.IsLoopback() {
189+
// skip loopback IPs
190+
continue
191+
}
192+
193+
ips = append(ips, ip)
194+
}
195+
196+
}
197+
return ips, err
198+
}

processor/resourcedetectionprocessor/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Queries the host machine to retrieve the following resource attributes:
4848
* host.arch
4949
* host.name
5050
* host.id
51+
* host.ip
5152
* host.cpu.vendor.id
5253
* host.cpu.family
5354
* host.cpu.model.id

processor/resourcedetectionprocessor/internal/system/internal/metadata/generated_config.go

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

processor/resourcedetectionprocessor/internal/system/internal/metadata/generated_config_test.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

processor/resourcedetectionprocessor/internal/system/internal/metadata/generated_resource.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

processor/resourcedetectionprocessor/internal/system/internal/metadata/generated_resource_test.go

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

processor/resourcedetectionprocessor/internal/system/internal/metadata/testdata/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ all_set:
1717
enabled: true
1818
host.id:
1919
enabled: true
20+
host.ip:
21+
enabled: true
2022
host.name:
2123
enabled: true
2224
os.description:
@@ -41,6 +43,8 @@ none_set:
4143
enabled: false
4244
host.id:
4345
enabled: false
46+
host.ip:
47+
enabled: false
4448
host.name:
4549
enabled: false
4650
os.description:

processor/resourcedetectionprocessor/internal/system/metadata.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ resource_attributes:
2323
description: The host.arch
2424
type: string
2525
enabled: false
26+
host.ip:
27+
description: IP addresses for the host
28+
type: slice
29+
enabled: false
2630
host.cpu.vendor.id:
2731
description: The host.cpu.vendor.id
2832
type: string

processor/resourcedetectionprocessor/internal/system/system.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,17 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
8383
return pcommon.NewResource(), "", fmt.Errorf("failed getting host architecture: %w", err)
8484
}
8585

86+
var hostIPAttribute []any
87+
if d.cfg.ResourceAttributes.HostIP.Enabled {
88+
hostIPs, errIPs := d.provider.HostIPs()
89+
if errIPs != nil {
90+
return pcommon.NewResource(), "", fmt.Errorf("failed getting host IP addresses: %w", errIPs)
91+
}
92+
for _, ip := range hostIPs {
93+
hostIPAttribute = append(hostIPAttribute, ip.String())
94+
}
95+
}
96+
8697
osDescription, err := d.provider.OSDescription(ctx)
8798
if err != nil {
8899
return pcommon.NewResource(), "", fmt.Errorf("failed getting OS description: %w", err)
@@ -107,6 +118,7 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
107118
}
108119
}
109120
d.rb.SetHostArch(hostArch)
121+
d.rb.SetHostIP(hostIPAttribute)
110122
d.rb.SetOsDescription(osDescription)
111123
if len(cpuInfo) > 0 {
112124
err = setHostCPUInfo(d, cpuInfo[0])

0 commit comments

Comments
 (0)