Skip to content

Commit

Permalink
feat: use opentelemetry sdk
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Rosiek <[email protected]>
  • Loading branch information
Dominik Rosiek committed Jun 12, 2023
1 parent a8b184e commit 0ef2f5c
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .chloggen/drosiek-host-id-2.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ change_type: enhancement
component: resourcedetectionprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: use gopsutil for host id detection
note: "use opentelemetry-go library for `host.id` detection in the `system` detector"

# One or more tracking issues related to the change
issues: [18533]
Expand Down
15 changes: 7 additions & 8 deletions 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/shirou/gopsutil/v3 v3.23.4
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,7 +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-ole/go-ole v1.2.6 // 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 @@ -28,7 +30,6 @@ require (
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/serf v0.10.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
Expand All @@ -39,12 +40,10 @@ require (
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/shoenig/go-m1cpu v0.1.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // 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
47 changes: 19 additions & 28 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/shirou/gopsutil/v3/host"
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 host.HostID()
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")
}
9 changes: 1 addition & 8 deletions processor/resourcedetectionprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.1 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
Expand All @@ -62,7 +61,6 @@ require (
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.16.5 // indirect
github.com/knadh/koanf v1.5.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
Expand All @@ -78,19 +76,14 @@ require (
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/rs/cors v1.9.0 // indirect
github.com/shirou/gopsutil/v3 v3.23.4 // indirect
github.com/shoenig/go-m1cpu v0.1.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/collector/featuregate v1.0.0-rcv0012 // indirect
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
Loading

0 comments on commit 0ef2f5c

Please sign in to comment.