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 detection of os.description to the system detector #24542

Merged
merged 5 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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_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 @@ -74,6 +74,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.hostnameSources {
getHostFromSource := hostnameSourcesMap[source]
hostname, err = getHostFromSource(d)
Expand All @@ -82,6 +87,7 @@ func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schem
d.rb.SetOsType(osType)
d.rb.SetHostID(hostID)
d.rb.SetHostArch(hostArch)
d.rb.SetOsDescription(osDescription)
return d.rb.Emit(), conventions.SchemaURL, nil
}
d.logger.Debug(err.Error())
Expand Down
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 @@ -110,10 +117,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 @@ -124,6 +132,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("HostID").Return("3", nil)
mdHostname.On("HostArch").Return("amd64", nil)
Expand All @@ -147,6 +156,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 @@ -159,10 +169,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 @@ -171,6 +182,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 @@ -183,10 +195,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 @@ -195,6 +208,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 @@ -210,6 +224,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 @@ -225,6 +240,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("", errors.New("err"))
mdOSType.On("HostArch").Return("amd64", nil)
Expand Down