Skip to content

Commit

Permalink
[processor/resourcedetection] Add host.mac to system detector (open-t…
Browse files Browse the repository at this point in the history
…elemetry#29588)

**Description:**

Adds support for `host.mac` detection to the `system` detector on the
resource detection processor.

This convention is defined in the specification [on the host
document](https://github.com/open-telemetry/semantic-conventions/blob/v1.23.1/docs/resource/host.md).

**Link to tracking Issue:** Fixes open-telemetry#29587 and therefore fixes open-telemetry#22045

**Testing:** 

Unit tests; manually Tested on my laptop with the following
configuration:

```
receivers:
  hostmetrics:
    collection_interval: 10s
    scrapers:
      load:

processors:
  resourcedetection:
    detectors: ["system"]
    system:
      resource_attributes:
        host.mac:
          enabled: true

exporters:
  debug:
    verbosity: detailed

service:
  pipelines:
    metrics:
      receivers: [hostmetrics]
      processors: [resourcedetection]
      exporters: [debug]
```

---------

Co-authored-by: Curtis Robert <crobert@splunk.com>
  • Loading branch information
2 people authored and jayasai470 committed Dec 8, 2023
1 parent eb435d0 commit 4a89d29
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 1 deletion.
27 changes: 27 additions & 0 deletions .chloggen/mx-psi_host.mac.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# 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.mac 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: [29587]

# (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:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
20 changes: 20 additions & 0 deletions internal/metadataproviders/system/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ type Provider interface {

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

// HostMACs returns the host's MAC addresses
HostMACs() ([]net.HardwareAddr, error)
}

type systemMetadataProvider struct {
Expand Down Expand Up @@ -196,3 +199,20 @@ func (p systemMetadataProvider) HostIPs() (ips []net.IP, err error) {
}
return ips, err
}

func (p systemMetadataProvider) HostMACs() (macs []net.HardwareAddr, 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
}

macs = append(macs, iface.HardwareAddr)
}
return macs, err
}
1 change: 1 addition & 0 deletions processor/resourcedetectionprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ Queries the host machine to retrieve the following resource attributes:
* host.name
* host.id
* host.ip
* host.mac
* 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 @@ -19,6 +19,8 @@ all_set:
enabled: true
host.ip:
enabled: true
host.mac:
enabled: true
host.name:
enabled: true
os.description:
Expand All @@ -45,6 +47,8 @@ none_set:
enabled: false
host.ip:
enabled: false
host.mac:
enabled: false
host.name:
enabled: false
os.description:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ resource_attributes:
description: IP addresses for the host
type: slice
enabled: false
host.mac:
description: MAC addresses for the host
type: slice
enabled: false
host.cpu.vendor.id:
description: The host.cpu.vendor.id
type: string
Expand Down
25 changes: 25 additions & 0 deletions processor/resourcedetectionprocessor/internal/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"context"
"errors"
"fmt"
"net"
"strconv"
"strings"

"github.com/shirou/gopsutil/v3/cpu"
"go.opentelemetry.io/collector/featuregate"
Expand Down Expand Up @@ -69,6 +71,17 @@ func NewDetector(p processor.CreateSettings, dcfg internal.DetectorConfig) (inte
}, nil
}

// toIEEERA converts a MAC address to IEEE RA format.
// Per the spec: "MAC Addresses MUST be represented in IEEE RA hexadecimal form: as hyphen-separated
// octets in uppercase hexadecimal form from most to least significant."
// Golang returns MAC addresses as colon-separated octets in lowercase hexadecimal form from most
// to least significant, so we need to:
// - Replace colons with hyphens
// - Convert to uppercase
func toIEEERA(mac net.HardwareAddr) string {
return strings.ToUpper(strings.ReplaceAll(mac.String(), ":", "-"))
}

// Detect detects system metadata and returns a resource with the available ones
func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schemaURL string, err error) {
var hostname string
Expand All @@ -94,6 +107,17 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
}
}

var hostMACAttribute []any
if d.cfg.ResourceAttributes.HostMac.Enabled {
hostMACs, errMACs := d.provider.HostMACs()
if errMACs != nil {
return pcommon.NewResource(), "", fmt.Errorf("failed to get host MAC addresses: %w", errMACs)
}
for _, mac := range hostMACs {
hostMACAttribute = append(hostMACAttribute, toIEEERA(mac))
}
}

osDescription, err := d.provider.OSDescription(ctx)
if err != nil {
return pcommon.NewResource(), "", fmt.Errorf("failed getting OS description: %w", err)
Expand All @@ -119,6 +143,7 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
}
d.rb.SetHostArch(hostArch)
d.rb.SetHostIP(hostIPAttribute)
d.rb.SetHostMac(hostMACAttribute)
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 @@ -72,9 +72,17 @@ func (m *mockMetadata) HostIPs() ([]net.IP, error) {
return args.Get(0).([]net.IP), args.Error(1)
}

func (m *mockMetadata) HostMACs() ([]net.HardwareAddr, error) {
args := m.MethodCalled("HostMACs")
return args.Get(0).([]net.HardwareAddr), 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))}

testMACsAttribute = []any{"00-00-00-00-00-01", "DE-AD-BE-EF-00-00"}
testMACsAddresses = []net.HardwareAddr{{0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, {0xDE, 0xAD, 0xBE, 0xEF, 0x00, 0x00}}
)

func TestNewDetector(t *testing.T) {
Expand Down Expand Up @@ -104,11 +112,34 @@ func TestNewDetector(t *testing.T) {
}
}

func TestToIEEERA(t *testing.T) {
tests := []struct {
addr net.HardwareAddr
expected string
}{
{
addr: testMACsAddresses[0],
expected: testMACsAttribute[0].(string),
},
{
addr: testMACsAddresses[1],
expected: testMACsAttribute[1].(string),
},
}

for _, tt := range tests {
t.Run(tt.expected, func(t *testing.T) {
assert.Equal(t, tt.expected, toIEEERA(tt.addr))
})
}
}

func allEnabledConfig() metadata.ResourceAttributesConfig {
cfg := metadata.DefaultResourceAttributesConfig()
cfg.HostArch.Enabled = true
cfg.HostID.Enabled = true
cfg.HostIP.Enabled = true
cfg.HostMac.Enabled = true
cfg.OsDescription.Enabled = true
return cfg
}
Expand All @@ -121,6 +152,7 @@ func TestDetectFQDNAvailable(t *testing.T) {
md.On("HostID").Return("2", nil)
md.On("HostArch").Return("amd64", nil)
md.On("HostIPs").Return(testIPsAddresses, nil)
md.On("HostMACs").Return(testMACsAddresses, nil)

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

assert.Equal(t, expected, res.Attributes().AsRaw())
Expand Down Expand Up @@ -174,6 +207,7 @@ func TestEnableHostID(t *testing.T) {
mdHostname.On("HostID").Return("3", nil)
mdHostname.On("HostArch").Return("amd64", nil)
mdHostname.On("HostIPs").Return(testIPsAddresses, nil)
mdHostname.On("HostMACs").Return(testMACsAddresses, nil)

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

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

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

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

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

detector = newTestDetector(mdHostname, []string{"os"}, allEnabledConfig())
res, schemaURL, err = detector.Detect(context.Background())
Expand Down Expand Up @@ -275,6 +314,7 @@ func TestDetectError(t *testing.T) {
mdHostID.On("HostID").Return("", errors.New("err"))
mdHostID.On("HostArch").Return("arm64", nil)
mdHostID.On("HostIPs").Return(testIPsAddresses, nil)
mdHostID.On("HostMACs").Return(testMACsAddresses, nil)

detector = newTestDetector(mdHostID, []string{"os"}, allEnabledConfig())
res, schemaURL, err = detector.Detect(context.Background())
Expand All @@ -286,6 +326,7 @@ func TestDetectError(t *testing.T) {
conventions.AttributeOSType: "linux",
conventions.AttributeHostArch: conventions.AttributeHostArchARM64,
"host.ip": testIPsAttribute,
"host.mac": testMACsAttribute,
}, res.Attributes().AsRaw())
}

Expand Down

0 comments on commit 4a89d29

Please sign in to comment.