Skip to content

Commit

Permalink
[resourcedetectionprocessor]: add host.id to system detector (#18618)
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Rosiek <drosiek@sumologic.com>
  • Loading branch information
sumo-drosiek authored Feb 16, 2023
1 parent cb0d0c8 commit 203d712
Show file tree
Hide file tree
Showing 17 changed files with 65 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .chloggen/drosiek-host-id.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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 host.id to system detector

# One or more tracking issues related to the change
issues: [18533]

# (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:
1 change: 1 addition & 0 deletions cmd/configschema/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsperfcountersreceiver v0.0.0-00010101000000-000000000000 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/zipkinreceiver v0.0.0-00010101000000-000000000000 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/zookeeperreceiver v0.0.0-00010101000000-000000000000 // indirect
github.com/panta/machineid v1.0.2 // indirect
github.com/signalfx/signalfx-agent/pkg/apm v0.0.0-20230214151822-6a6813cf5bf1 // indirect
)

Expand Down
2 changes: 2 additions & 0 deletions cmd/configschema/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cmd/otelcontribcol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ require (
github.com/openzipkin/zipkin-go v0.4.1 // indirect
github.com/outcaste-io/ristretto v0.2.0 // indirect
github.com/ovh/go-ovh v1.3.0 // indirect
github.com/panta/machineid v1.0.2 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/paulmach/orb v0.8.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
Expand Down
2 changes: 2 additions & 0 deletions cmd/otelcontribcol/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions exporter/datadogexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ require (
github.com/openshift/api v0.0.0-20210521075222-e273a339932a // indirect
github.com/openshift/client-go v0.0.0-20210521082421-73d9475a9142 // indirect
github.com/outcaste-io/ristretto v0.2.0 // indirect
github.com/panta/machineid v1.0.2 // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions exporter/datadogexporter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ require (
github.com/openzipkin/zipkin-go v0.4.1 // indirect
github.com/outcaste-io/ristretto v0.2.0 // indirect
github.com/ovh/go-ovh v1.3.0 // indirect
github.com/panta/machineid v1.0.2 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/paulmach/orb v0.8.0 // indirect
github.com/philhofer/fwd v1.1.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/metadataproviders/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/aws/aws-sdk-go v1.44.201
github.com/docker/docker v23.0.1+incompatible
github.com/hashicorp/consul/api v1.18.0
github.com/panta/machineid v1.0.2
github.com/stretchr/testify v1.8.1
)

Expand Down
2 changes: 2 additions & 0 deletions internal/metadataproviders/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions internal/metadataproviders/system/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"strings"

"github.com/Showmax/go-fqdn"
"github.com/panta/machineid"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders/internal"
)
Expand Down Expand Up @@ -61,6 +62,9 @@ type Provider interface {

// ReverseLookupHost does a reverse DNS query on the current host's IP address
ReverseLookupHost() (string, error)

// HostID returns Host Unique Identifier
HostID() (string, error)
}

type systemMetadataProvider struct {
Expand Down Expand Up @@ -123,3 +127,7 @@ 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() (string, error) {
return machineid.ID()
}
1 change: 1 addition & 0 deletions processor/resourcedetectionprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,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.name
* host.id
* os.type
By default `host.name` is being set to FQDN if possible, and a hostname provided by OS used as fallback.
Expand Down
1 change: 1 addition & 0 deletions processor/resourcedetectionprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.71.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/panta/machineid v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions processor/resourcedetectionprocessor/go.sum

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 @@ -70,12 +70,19 @@ func (d *Detector) Detect(_ context.Context) (resource pcommon.Resource, schemaU
if err != nil {
return res, "", fmt.Errorf("failed getting OS type: %w", err)
}

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

for _, source := range d.hostnameSources {
getHostFromSource := hostnameSourcesMap[source]
hostname, err = getHostFromSource(d)
if err == nil {
attrs.PutStr(conventions.AttributeHostName, hostname)
attrs.PutStr(conventions.AttributeOSType, osType)
attrs.PutStr(conventions.AttributeHostID, hostID)

return res, conventions.SchemaURL, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func (m *mockMetadata) OSType() (string, error) {
return args.String(0), args.Error(1)
}

func (m *mockMetadata) HostID() (string, error) {
args := m.MethodCalled("HostID")
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 @@ -92,6 +97,7 @@ 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)

detector := &Detector{provider: md, logger: zap.NewNop(), hostnameSources: []string{"dns"}}
res, schemaURL, err := detector.Detect(context.Background())
Expand All @@ -102,6 +108,7 @@ func TestDetectFQDNAvailable(t *testing.T) {
expected := map[string]any{
conventions.AttributeHostName: "fqdn",
conventions.AttributeOSType: "darwin",
conventions.AttributeHostID: "2",
}

assert.Equal(t, expected, res.Attributes().AsRaw())
Expand All @@ -113,6 +120,8 @@ func TestFallbackHostname(t *testing.T) {
mdHostname.On("Hostname").Return("hostname", nil)
mdHostname.On("FQDN").Return("", errors.New("err"))
mdHostname.On("OSType").Return("darwin", nil)
mdHostname.On("OSType").Return("darwin", nil)
mdHostname.On("HostID").Return("3", nil)

detector := &Detector{provider: mdHostname, logger: zap.NewNop(), hostnameSources: []string{"dns", "os"}}
res, schemaURL, err := detector.Detect(context.Background())
Expand All @@ -123,6 +132,7 @@ func TestFallbackHostname(t *testing.T) {
expected := map[string]any{
conventions.AttributeHostName: "hostname",
conventions.AttributeOSType: "darwin",
conventions.AttributeHostID: "3",
}

assert.Equal(t, expected, res.Attributes().AsRaw())
Expand All @@ -132,6 +142,7 @@ func TestUseHostname(t *testing.T) {
mdHostname := &mockMetadata{}
mdHostname.On("Hostname").Return("hostname", nil)
mdHostname.On("OSType").Return("darwin", nil)
mdHostname.On("HostID").Return("1", nil)

detector := &Detector{provider: mdHostname, logger: zap.NewNop(), hostnameSources: []string{"os"}}
res, schemaURL, err := detector.Detect(context.Background())
Expand All @@ -142,6 +153,7 @@ func TestUseHostname(t *testing.T) {
expected := map[string]any{
conventions.AttributeHostName: "hostname",
conventions.AttributeOSType: "darwin",
conventions.AttributeHostID: "1",
}

assert.Equal(t, expected, res.Attributes().AsRaw())
Expand All @@ -153,6 +165,7 @@ func TestDetectError(t *testing.T) {
mdFQDN.On("OSType").Return("windows", nil)
mdFQDN.On("FQDN").Return("", errors.New("err"))
mdFQDN.On("Hostname").Return("", errors.New("err"))
mdFQDN.On("HostID").Return("", errors.New("err"))

detector := &Detector{provider: mdFQDN, logger: zap.NewNop(), hostnameSources: []string{"dns"}}
res, schemaURL, err := detector.Detect(context.Background())
Expand All @@ -164,6 +177,7 @@ func TestDetectError(t *testing.T) {
mdHostname := &mockMetadata{}
mdHostname.On("OSType").Return("windows", nil)
mdHostname.On("Hostname").Return("", errors.New("err"))
mdHostname.On("HostID").Return("", errors.New("err"))

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

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

0 comments on commit 203d712

Please sign in to comment.