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.arch to system detector #22940

Merged
merged 7 commits into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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_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 @@ -138,3 +141,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 @@ -43,6 +43,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 @@ -14,4 +14,8 @@ resource_attributes:
os.type:
description: The os.type
type: string
enabled: true
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,20 @@ func TestNewDetector(t *testing.T) {
}
}

func allEnabledConfig() metadata.ResourceAttributesConfig {
cfg := metadata.DefaultResourceAttributesConfig()
cfg.HostArch.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 @@ -99,6 +112,7 @@ func TestDetectFQDNAvailable(t *testing.T) {
conventions.AttributeHostName: "fqdn",
conventions.AttributeOSType: "darwin",
conventions.AttributeHostID: "2",
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
}

assert.Equal(t, expected, res.Attributes().AsRaw())
Expand All @@ -112,8 +126,9 @@ func TestFallbackHostname(t *testing.T) {
mdHostname.On("OSType").Return("darwin", nil)
mdHostname.On("OSType").Return("darwin", nil)
mdHostname.On("HostID").Return("3", nil)
mdHostname.On("HostArch").Return("amd64", nil)

resourceAttributes := CreateDefaultConfig().ResourceAttributes
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 @@ -124,6 +139,7 @@ func TestFallbackHostname(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 @@ -134,8 +150,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 @@ -146,6 +163,7 @@ func TestUseHostname(t *testing.T) {
conventions.AttributeHostName: "hostname",
conventions.AttributeOSType: "darwin",
conventions.AttributeHostID: "1",
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
}

assert.Equal(t, expected, res.Attributes().AsRaw())
Expand All @@ -158,8 +176,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 @@ -171,6 +190,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 @@ -183,6 +203,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