Skip to content

Commit

Permalink
[processor/resourcedetection] Add host.arch to system detector (ope…
Browse files Browse the repository at this point in the history
…n-telemetry#22940)

**Description:** 

Add `host.arch` to system detector. It is detected via `runtime.GOARCH`.

This way of detecting may be wrong if running under an emulator or
binary translator; the only common case I know of this happening in
practice is on ARM64 macOS when using Rosetta. There is a way to detect
this (see DataDog/gohai/pull/77) but since we provide arm64 builds for
macOS, I think this is a non-issue

**Link to tracking Issue:** Fixes open-telemetry#22939

**Testing:** Added unit tests. Tested on my Linux AMD64 laptop

**Documentation:** Documented new attribute on README
  • Loading branch information
mx-psi authored Jul 18, 2023
1 parent 3f2821e commit 02f688d
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 23 deletions.
20 changes: 20 additions & 0 deletions .chloggen/mx-psi_system-host-arch.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: The system detector now detects the `host.arch` semantic convention

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

# (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: The GOARCH value is used on architectures that are not well-known
18 changes: 0 additions & 18 deletions internal/metadataproviders/internal/goos_test.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,18 @@ func GOOSToOSType(goos string) string {
}
return goos
}

func GOARCHtoHostArch(goarch string) string {
// These cases differ from the spec well-known values
switch goarch {
case "arm":
return "arm32"
case "ppc64le":
return "ppc64"
case "386":
return "x86"
}

// Other cases either match the spec or are not well-known (so we use a custom value)
return goarch
}
48 changes: 48 additions & 0 deletions internal/metadataproviders/internal/internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package internal

import (
"testing"

"github.com/stretchr/testify/assert"
conventions "go.opentelemetry.io/collector/semconv/v1.18.0"
)

func TestGOOSToOsType(t *testing.T) {
assert.Equal(t, "darwin", GOOSToOSType("darwin"))
assert.Equal(t, "linux", GOOSToOSType("linux"))
assert.Equal(t, "windows", GOOSToOSType("windows"))
assert.Equal(t, "dragonflybsd", GOOSToOSType("dragonfly"))
assert.Equal(t, "z_os", GOOSToOSType("zos"))
}

func TestGOARCHToHostArch(t *testing.T) {
tests := []struct {
goarch string
hostArch string
}{
// well-known values that are supported by Go
{goarch: "386", hostArch: conventions.AttributeHostArchX86},
{goarch: "amd64", hostArch: conventions.AttributeHostArchAMD64},
{goarch: "arm", hostArch: conventions.AttributeHostArchARM32},
{goarch: "arm64", hostArch: conventions.AttributeHostArchARM64},
{goarch: "ppc64", hostArch: conventions.AttributeHostArchPPC64},
{goarch: "ppc64le", hostArch: conventions.AttributeHostArchPPC64},
{goarch: "s390x", hostArch: conventions.AttributeHostArchS390x},

// not well-known values
{goarch: "mips", hostArch: "mips"},
{goarch: "mips64", hostArch: "mips64"},
{goarch: "mips64le", hostArch: "mips64le"},
{goarch: "mipsle", hostArch: "mipsle"},
{goarch: "riscv64", hostArch: "riscv64"},
}

for _, tt := range tests {
t.Run(tt.goarch, func(t *testing.T) {
assert.Equal(t, tt.hostArch, GOARCHtoHostArch(tt.goarch))
})
}
}
7 changes: 7 additions & 0 deletions internal/metadataproviders/system/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ type Provider interface {

// HostID returns Host Unique Identifier
HostID(ctx context.Context) (string, error)

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

type systemMetadataProvider struct {
Expand Down Expand Up @@ -144,3 +147,7 @@ func (p systemMetadataProvider) HostID(ctx context.Context) (string, error) {

return "", fmt.Errorf("failed to obtain host id")
}

func (systemMetadataProvider) HostArch() (string, error) {
return internal.GOARCHtoHostArch(runtime.GOARCH), nil
}
1 change: 1 addition & 0 deletions processor/resourcedetectionprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Note: use the Docker detector (see below) if running the Collector as a Docker c
Queries the host machine to retrieve the following resource attributes:
* host.arch
* host.name
* host.id
* os.type
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.

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
default:
all_set:
resource_attributes:
host.arch:
enabled: true
host.id:
enabled: true
host.name:
Expand All @@ -9,6 +11,8 @@ all_set:
enabled: true
none_set:
resource_attributes:
host.arch:
enabled: false
host.id:
enabled: false
host.name:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ resource_attributes:
description: The os.type
type: string
enabled: true
host.arch:
description: The host.arch
type: string
enabled: false
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
return res, "", fmt.Errorf("failed getting host ID: %w", err)
}

hostArch, err := d.provider.HostArch()
if err != nil {
return res, "", fmt.Errorf("failed getting host architecture: %w", err)
}

for _, source := range d.hostnameSources {
getHostFromSource := hostnameSourcesMap[source]
hostname, err = getHostFromSource(d)
Expand All @@ -80,6 +85,9 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
if d.resourceAttributes.HostID.Enabled {
attrs.PutStr(conventions.AttributeHostID, hostID)
}
if d.resourceAttributes.HostArch.Enabled {
attrs.PutStr(conventions.AttributeHostArch, hostArch)
}

return res, conventions.SchemaURL, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders/system"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/system/internal/metadata"
)

var _ system.Provider = (*mockMetadata)(nil)
Expand Down Expand Up @@ -45,6 +46,11 @@ func (m *mockMetadata) HostID(_ context.Context) (string, error) {
return args.String(0), args.Error(1)
}

func (m *mockMetadata) HostArch() (string, error) {
args := m.MethodCalled("HostArch")
return args.String(0), args.Error(1)
}

func (m *mockMetadata) LookupCNAME() (string, error) {
args := m.MethodCalled("LookupCNAME")
return args.String(0), args.Error(1)
Expand Down Expand Up @@ -82,13 +88,21 @@ func TestNewDetector(t *testing.T) {
}
}

func allEnabledConfig() metadata.ResourceAttributesConfig {
cfg := metadata.DefaultResourceAttributesConfig()
cfg.HostArch.Enabled = true
cfg.HostID.Enabled = true
return cfg
}

func TestDetectFQDNAvailable(t *testing.T) {
md := &mockMetadata{}
md.On("FQDN").Return("fqdn", nil)
md.On("OSType").Return("darwin", nil)
md.On("HostID").Return("2", nil)
md.On("HostArch").Return("amd64", nil)

resourceAttributes := CreateDefaultConfig().ResourceAttributes
resourceAttributes := allEnabledConfig()
detector := &Detector{provider: md, logger: zap.NewNop(), hostnameSources: []string{"dns"}, resourceAttributes: resourceAttributes}
res, schemaURL, err := detector.Detect(context.Background())
require.NoError(t, err)
Expand All @@ -98,6 +112,8 @@ func TestDetectFQDNAvailable(t *testing.T) {
expected := map[string]any{
conventions.AttributeHostName: "fqdn",
conventions.AttributeOSType: "darwin",
conventions.AttributeHostID: "2",
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
}

assert.Equal(t, expected, res.Attributes().AsRaw())
Expand All @@ -110,6 +126,7 @@ func TestFallbackHostname(t *testing.T) {
mdHostname.On("FQDN").Return("", errors.New("err"))
mdHostname.On("OSType").Return("darwin", nil)
mdHostname.On("HostID").Return("3", nil)
mdHostname.On("HostArch").Return("amd64", nil)

resourceAttributes := CreateDefaultConfig().ResourceAttributes
detector := &Detector{provider: mdHostname, logger: zap.NewNop(), hostnameSources: []string{"dns", "os"}, resourceAttributes: resourceAttributes}
Expand All @@ -132,9 +149,9 @@ func TestEnableHostID(t *testing.T) {
mdHostname.On("FQDN").Return("", errors.New("err"))
mdHostname.On("OSType").Return("darwin", nil)
mdHostname.On("HostID").Return("3", nil)
mdHostname.On("HostArch").Return("amd64", nil)

resourceAttributes := CreateDefaultConfig().ResourceAttributes
resourceAttributes.HostID.Enabled = true
resourceAttributes := allEnabledConfig()
detector := &Detector{provider: mdHostname, logger: zap.NewNop(), hostnameSources: []string{"dns", "os"}, resourceAttributes: resourceAttributes}
res, schemaURL, err := detector.Detect(context.Background())
require.NoError(t, err)
Expand All @@ -145,6 +162,7 @@ func TestEnableHostID(t *testing.T) {
conventions.AttributeHostName: "hostname",
conventions.AttributeOSType: "darwin",
conventions.AttributeHostID: "3",
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
}

assert.Equal(t, expected, res.Attributes().AsRaw())
Expand All @@ -155,8 +173,9 @@ func TestUseHostname(t *testing.T) {
mdHostname.On("Hostname").Return("hostname", nil)
mdHostname.On("OSType").Return("darwin", nil)
mdHostname.On("HostID").Return("1", nil)
mdHostname.On("HostArch").Return("amd64", nil)

resourceAttributes := CreateDefaultConfig().ResourceAttributes
resourceAttributes := allEnabledConfig()
detector := &Detector{provider: mdHostname, logger: zap.NewNop(), hostnameSources: []string{"os"}, resourceAttributes: resourceAttributes}
res, schemaURL, err := detector.Detect(context.Background())
require.NoError(t, err)
Expand All @@ -166,6 +185,8 @@ func TestUseHostname(t *testing.T) {
expected := map[string]any{
conventions.AttributeHostName: "hostname",
conventions.AttributeOSType: "darwin",
conventions.AttributeHostID: "1",
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
}

assert.Equal(t, expected, res.Attributes().AsRaw())
Expand All @@ -178,8 +199,9 @@ func TestDetectError(t *testing.T) {
mdFQDN.On("FQDN").Return("", errors.New("err"))
mdFQDN.On("Hostname").Return("", errors.New("err"))
mdFQDN.On("HostID").Return("", errors.New("err"))
mdFQDN.On("HostArch").Return("amd64", nil)

resourceAttributes := CreateDefaultConfig().ResourceAttributes
resourceAttributes := allEnabledConfig()
detector := &Detector{provider: mdFQDN, logger: zap.NewNop(), hostnameSources: []string{"dns"}, resourceAttributes: resourceAttributes}
res, schemaURL, err := detector.Detect(context.Background())
assert.Error(t, err)
Expand All @@ -191,6 +213,7 @@ func TestDetectError(t *testing.T) {
mdHostname.On("OSType").Return("windows", nil)
mdHostname.On("Hostname").Return("", errors.New("err"))
mdHostname.On("HostID").Return("", errors.New("err"))
mdHostname.On("HostArch").Return("amd64", nil)

detector = &Detector{provider: mdHostname, logger: zap.NewNop(), hostnameSources: []string{"os"}, resourceAttributes: resourceAttributes}
res, schemaURL, err = detector.Detect(context.Background())
Expand All @@ -203,6 +226,7 @@ func TestDetectError(t *testing.T) {
mdOSType.On("FQDN").Return("fqdn", nil)
mdOSType.On("OSType").Return("", errors.New("err"))
mdOSType.On("HostID").Return("", errors.New("err"))
mdOSType.On("HostArch").Return("amd64", nil)

detector = &Detector{provider: mdOSType, logger: zap.NewNop(), hostnameSources: []string{"dns"}, resourceAttributes: resourceAttributes}
res, schemaURL, err = detector.Detect(context.Background())
Expand Down

0 comments on commit 02f688d

Please sign in to comment.