Skip to content

Commit

Permalink
resourcedetectionprocessor: use opentelemetry-go for host id detection (
Browse files Browse the repository at this point in the history
open-telemetry#18740)

* resourcedetectionprocessor: use gopsutil for host id detection

Signed-off-by: Dominik Rosiek <[email protected]>

* feat: use opentelemetry sdk

Signed-off-by: Dominik Rosiek <[email protected]>

---------

Signed-off-by: Dominik Rosiek <[email protected]>
  • Loading branch information
sumo-drosiek authored Jun 12, 2023
1 parent 820510e commit 6249ae8
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 30 deletions.
16 changes: 16 additions & 0 deletions .chloggen/drosiek-host-id-2.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: "use opentelemetry-go library for `host.id` detection in the `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: 0 additions & 1 deletion cmd/configschema/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/windowsperfcountersreceiver v0.79.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/zipkinreceiver v0.79.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/receiver/zookeeperreceiver v0.79.0 // indirect
github.com/panta/machineid v1.0.2 // indirect
github.com/samber/lo v1.37.0 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/signalfx/signalfx-agent/pkg/apm v0.0.0-20230214151822-6a6813cf5bf1 // indirect
Expand Down
2 changes: 0 additions & 2 deletions cmd/configschema/go.sum

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

1 change: 0 additions & 1 deletion cmd/otelcontribcol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,6 @@ require (
github.com/openzipkin/zipkin-go v0.4.1 // indirect
github.com/outcaste-io/ristretto v0.2.1 // 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.9.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
Expand Down
2 changes: 0 additions & 2 deletions cmd/otelcontribcol/go.sum

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

1 change: 0 additions & 1 deletion exporter/datadogexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ require (
github.com/openshift/api v3.9.0+incompatible // indirect
github.com/openshift/client-go v0.0.0-20210521082421-73d9475a9142 // indirect
github.com/outcaste-io/ristretto v0.2.1 // indirect
github.com/panta/machineid v1.0.2 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/philhofer/fwd v1.1.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
Expand Down
2 changes: 0 additions & 2 deletions exporter/datadogexporter/go.sum

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

1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,6 @@ require (
github.com/openzipkin/zipkin-go v0.4.1 // indirect
github.com/outcaste-io/ristretto v0.2.1 // 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.9.0 // indirect
github.com/philhofer/fwd v1.1.2 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum

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

8 changes: 7 additions & 1 deletion internal/metadataproviders/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ require (
github.com/aws/aws-sdk-go v1.44.277
github.com/docker/docker v24.0.2+incompatible
github.com/hashicorp/consul/api v1.21.0
github.com/panta/machineid v1.0.2
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/collector/semconv v0.79.0
go.opentelemetry.io/otel/sdk v1.16.0
)

require (
Expand All @@ -19,6 +20,8 @@ require (
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
github.com/hashicorp/go-hclog v1.5.0 // indirect
Expand All @@ -38,6 +41,9 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
golang.org/x/net v0.10.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/time v0.3.0 // indirect
Expand Down
27 changes: 20 additions & 7 deletions internal/metadataproviders/go.sum

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

26 changes: 22 additions & 4 deletions internal/metadataproviders/system/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
package system // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders/system"

import (
"context"
"fmt"
"net"
"os"
"runtime"
"strings"

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

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders/internal"
)
Expand Down Expand Up @@ -53,7 +55,7 @@ type Provider interface {
ReverseLookupHost() (string, error)

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

type systemMetadataProvider struct {
Expand Down Expand Up @@ -117,6 +119,22 @@ 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()
func (p systemMetadataProvider) HostID(ctx context.Context) (string, error) {
res, err := resource.New(ctx,
resource.WithHostID(),
)

if err != nil {
return "", fmt.Errorf("failed to obtain host id: %w", err)
}

iter := res.Iter()

for iter.Next() {
if iter.Attribute().Key == conventions.AttributeHostID {
return iter.Attribute().Value.Emit(), nil
}
}

return "", fmt.Errorf("failed to obtain host id")
}
2 changes: 1 addition & 1 deletion processor/resourcedetectionprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.79.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/rs/cors v1.9.0 // indirect
Expand All @@ -84,6 +83,7 @@ require (
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.42.0 // indirect
go.opentelemetry.io/otel v1.16.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/sdk v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/net v0.10.0 // indirect
Expand Down
4 changes: 2 additions & 2 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 @@ -49,7 +49,7 @@ func NewDetector(p processor.CreateSettings, dcfg internal.DetectorConfig) (inte
}

// Detect detects system metadata and returns a resource with the available ones
func (d *Detector) Detect(_ context.Context) (resource pcommon.Resource, schemaURL string, err error) {
func (d *Detector) Detect(ctx context.Context) (resource pcommon.Resource, schemaURL string, err error) {
var hostname string

res := pcommon.NewResource()
Expand All @@ -60,7 +60,7 @@ func (d *Detector) Detect(_ context.Context) (resource pcommon.Resource, schemaU
return res, "", fmt.Errorf("failed getting OS type: %w", err)
}

hostID, err := d.provider.HostID()
hostID, err := d.provider.HostID(ctx)
if err != nil {
return res, "", fmt.Errorf("failed getting host ID: %w", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (m *mockMetadata) OSType() (string, error) {
return args.String(0), args.Error(1)
}

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

0 comments on commit 6249ae8

Please sign in to comment.