Skip to content

Commit

Permalink
[processor/resourcedetection] Add detection of os.description to the …
Browse files Browse the repository at this point in the history
…system detector (#24542)

**Description:** Add detection of `os.description` to the `system`
detector using `resource.WithOSDescription` from `opentelemetry-go`.

**Link to tracking Issue:** Fixes #24541

**Testing:** Manually tested on my Linux laptop, amended existing tests.

**Documentation:** Added to README.md
  • Loading branch information
mx-psi authored Aug 4, 2023
1 parent b9f9e7e commit 7a55bbd
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 40 deletions.
20 changes: 20 additions & 0 deletions .chloggen/mx-psi_os-description.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 os.description 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: [24541]

# (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:
28 changes: 18 additions & 10 deletions internal/metadataproviders/system/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/Showmax/go-fqdn"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders/internal"
Expand Down Expand Up @@ -45,6 +46,9 @@ type Provider interface {
// FQDN returns the fully qualified domain name
FQDN() (string, error)

// OSDescription returns a human readable description of the OS.
OSDescription(ctx context.Context) (string, error)

// OSType returns the host operating system
OSType() (string, error)

Expand Down Expand Up @@ -123,29 +127,33 @@ func (p systemMetadataProvider) reverseLookup(ipAddresses []string) (string, err
return "", fmt.Errorf("reverseLookup failed to convert IP addresses to name: %w", err)
}

func (p systemMetadataProvider) HostID(ctx context.Context) (string, error) {
res, err := p.newResource(ctx,
resource.WithHostID(),
)

func (p systemMetadataProvider) fromOption(ctx context.Context, opt resource.Option, semconv string) (string, error) {
res, err := p.newResource(ctx, opt)
if err != nil {
return "", fmt.Errorf("failed to obtain host id: %w", err)
return "", fmt.Errorf("failed to obtain %q: %w", semconv, err)
}

iter := res.Iter()

for iter.Next() {
if iter.Attribute().Key == conventions.AttributeHostID {
if iter.Attribute().Key == attribute.Key(semconv) {
v := iter.Attribute().Value.Emit()

if v == "" {
return "", fmt.Errorf("empty host id")
return "", fmt.Errorf("empty %q", semconv)
}
return v, nil
}
}

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

func (p systemMetadataProvider) HostID(ctx context.Context) (string, error) {
return p.fromOption(ctx, resource.WithHostID(), conventions.AttributeHostID)
}

func (p systemMetadataProvider) OSDescription(ctx context.Context) (string, error) {
return p.fromOption(ctx, resource.WithOSDescription(), conventions.AttributeOSDescription)
}

func (systemMetadataProvider) HostArch() (string, error) {
Expand Down
4 changes: 2 additions & 2 deletions internal/metadataproviders/system/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ func TestHostID(t *testing.T) {
name: "empty host.id",
resValue: "",
resError: nil,
err: "failed to obtain host id",
err: `failed to obtain "host.id"`,
expected: "",
},
{
name: "error",
resValue: "",
resError: fmt.Errorf("some error"),
err: "failed to obtain host id: some error",
err: `failed to obtain "host.id": some error`,
expected: "",
},
}
Expand Down
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
* os.description
* os.type
By default `host.name` is being set to FQDN if possible, and a hostname provided by OS used as fallback.
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 @@ -7,6 +7,8 @@ all_set:
enabled: true
host.name:
enabled: true
os.description:
enabled: true
os.type:
enabled: true
none_set:
Expand All @@ -17,5 +19,7 @@ none_set:
enabled: false
host.name:
enabled: false
os.description:
enabled: false
os.type:
enabled: false
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ resource_attributes:
description: The host.id
type: string
enabled: false
os.description:
description: Human readable OS version information.
type: string
enabled: false
os.type:
description: The os.type
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
return pcommon.NewResource(), "", fmt.Errorf("failed getting host architecture: %w", err)
}

osDescription, err := d.provider.OSDescription(ctx)
if err != nil {
return pcommon.NewResource(), "", fmt.Errorf("failed getting OS description: %w", err)
}

for _, source := range d.cfg.HostnameSources {
getHostFromSource := hostnameSourcesMap[source]
hostname, err = getHostFromSource(d)
Expand All @@ -83,6 +88,7 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
}
}
d.rb.SetHostArch(hostArch)
d.rb.SetOsDescription(osDescription)
return d.rb.Emit(), conventions.SchemaURL, nil
}
d.logger.Debug(err.Error())
Expand Down
48 changes: 33 additions & 15 deletions processor/resourcedetectionprocessor/internal/system/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ func (m *mockMetadata) FQDN() (string, error) {
return args.String(0), args.Error(1)
}

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

func (m *mockMetadata) OSType() (string, error) {
args := m.MethodCalled("OSType")
return args.String(0), args.Error(1)
Expand Down Expand Up @@ -92,12 +97,14 @@ func allEnabledConfig() metadata.ResourceAttributesConfig {
cfg := metadata.DefaultResourceAttributesConfig()
cfg.HostArch.Enabled = true
cfg.HostID.Enabled = true
cfg.OsDescription.Enabled = true
return cfg
}

func TestDetectFQDNAvailable(t *testing.T) {
md := &mockMetadata{}
md.On("FQDN").Return("fqdn", nil)
md.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
md.On("OSType").Return("darwin", nil)
md.On("HostID").Return("2", nil)
md.On("HostArch").Return("amd64", nil)
Expand All @@ -109,10 +116,11 @@ func TestDetectFQDNAvailable(t *testing.T) {
md.AssertExpectations(t)

expected := map[string]any{
conventions.AttributeHostName: "fqdn",
conventions.AttributeOSType: "darwin",
conventions.AttributeHostID: "2",
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
conventions.AttributeHostName: "fqdn",
conventions.AttributeOSDescription: "Ubuntu 22.04.2 LTS (Jammy Jellyfish)",
conventions.AttributeOSType: "darwin",
conventions.AttributeHostID: "2",
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
}

assert.Equal(t, expected, res.Attributes().AsRaw())
Expand All @@ -123,6 +131,7 @@ func TestFallbackHostname(t *testing.T) {
mdHostname := &mockMetadata{}
mdHostname.On("Hostname").Return("hostname", nil)
mdHostname.On("FQDN").Return("", errors.New("err"))
mdHostname.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
mdHostname.On("OSType").Return("darwin", nil)
mdHostname.On("HostArch").Return("amd64", nil)

Expand All @@ -145,6 +154,7 @@ func TestEnableHostID(t *testing.T) {
mdHostname := &mockMetadata{}
mdHostname.On("Hostname").Return("hostname", nil)
mdHostname.On("FQDN").Return("", errors.New("err"))
mdHostname.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
mdHostname.On("OSType").Return("darwin", nil)
mdHostname.On("HostID").Return("3", nil)
mdHostname.On("HostArch").Return("amd64", nil)
Expand All @@ -156,10 +166,11 @@ func TestEnableHostID(t *testing.T) {
mdHostname.AssertExpectations(t)

expected := map[string]any{
conventions.AttributeHostName: "hostname",
conventions.AttributeOSType: "darwin",
conventions.AttributeHostID: "3",
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
conventions.AttributeHostName: "hostname",
conventions.AttributeOSDescription: "Ubuntu 22.04.2 LTS (Jammy Jellyfish)",
conventions.AttributeOSType: "darwin",
conventions.AttributeHostID: "3",
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
}

assert.Equal(t, expected, res.Attributes().AsRaw())
Expand All @@ -168,6 +179,7 @@ func TestEnableHostID(t *testing.T) {
func TestUseHostname(t *testing.T) {
mdHostname := &mockMetadata{}
mdHostname.On("Hostname").Return("hostname", nil)
mdHostname.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
mdHostname.On("OSType").Return("darwin", nil)
mdHostname.On("HostID").Return("1", nil)
mdHostname.On("HostArch").Return("amd64", nil)
Expand All @@ -179,10 +191,11 @@ func TestUseHostname(t *testing.T) {
mdHostname.AssertExpectations(t)

expected := map[string]any{
conventions.AttributeHostName: "hostname",
conventions.AttributeOSType: "darwin",
conventions.AttributeHostID: "1",
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
conventions.AttributeHostName: "hostname",
conventions.AttributeOSDescription: "Ubuntu 22.04.2 LTS (Jammy Jellyfish)",
conventions.AttributeOSType: "darwin",
conventions.AttributeHostID: "1",
conventions.AttributeHostArch: conventions.AttributeHostArchAMD64,
}

assert.Equal(t, expected, res.Attributes().AsRaw())
Expand All @@ -191,6 +204,7 @@ func TestUseHostname(t *testing.T) {
func TestDetectError(t *testing.T) {
// FQDN and hostname fail with 'hostnameSources' set to 'dns'
mdFQDN := &mockMetadata{}
mdFQDN.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
mdFQDN.On("OSType").Return("windows", nil)
mdFQDN.On("FQDN").Return("", errors.New("err"))
mdFQDN.On("Hostname").Return("", errors.New("err"))
Expand All @@ -205,6 +219,7 @@ func TestDetectError(t *testing.T) {

// hostname fail with 'hostnameSources' set to 'os'
mdHostname := &mockMetadata{}
mdHostname.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
mdHostname.On("OSType").Return("windows", nil)
mdHostname.On("Hostname").Return("", errors.New("err"))
mdHostname.On("HostID").Return("", errors.New("err"))
Expand All @@ -219,6 +234,7 @@ func TestDetectError(t *testing.T) {
// OS type fails
mdOSType := &mockMetadata{}
mdOSType.On("FQDN").Return("fqdn", nil)
mdOSType.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
mdOSType.On("OSType").Return("", errors.New("err"))
mdOSType.On("HostID").Return("1", nil)
mdOSType.On("HostArch").Return("amd64", nil)
Expand All @@ -232,6 +248,7 @@ func TestDetectError(t *testing.T) {
// Host ID fails. All other attributes should be set.
mdHostID := &mockMetadata{}
mdHostID.On("Hostname").Return("hostname", nil)
mdHostID.On("OSDescription").Return("Ubuntu 22.04.2 LTS (Jammy Jellyfish)", nil)
mdHostID.On("OSType").Return("linux", nil)
mdHostID.On("HostID").Return("", errors.New("err"))
mdHostID.On("HostArch").Return("arm64", nil)
Expand All @@ -241,9 +258,10 @@ func TestDetectError(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, conventions.SchemaURL, schemaURL)
assert.Equal(t, map[string]any{
conventions.AttributeHostName: "hostname",
conventions.AttributeOSType: "linux",
conventions.AttributeHostArch: conventions.AttributeHostArchARM64,
conventions.AttributeHostName: "hostname",
conventions.AttributeOSDescription: "Ubuntu 22.04.2 LTS (Jammy Jellyfish)",
conventions.AttributeOSType: "linux",
conventions.AttributeHostArch: conventions.AttributeHostArchARM64,
}, res.Attributes().AsRaw())
}

Expand Down

0 comments on commit 7a55bbd

Please sign in to comment.