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

resourcedetectionprocessor: use opentelemetry-go for host id detection #18740

Merged
merged 2 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
feat: use opentelemetry sdk
Signed-off-by: Dominik Rosiek <drosiek@sumologic.com>
  • Loading branch information
Dominik Rosiek committed Jun 12, 2023
commit 0ef2f5c6c717460569724d8fe5d0d703171b0494
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"
sumo-drosiek marked this conversation as resolved.
Show resolved Hide resolved
"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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this method tested anywhere? i don't see any specific tests around these changes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure how to test it properly as extracting resource to interface for me looks like unnecessary complication. I am open for suggestions

res, err := resource.New(ctx,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit "funny" how you need create a resource that includes the "host.id" attribute here, only to "peel" the resource to get just the "host.id" attribute value, and in the end you put that value in an attribute of another resource. This is due to the structure of the contrib's system.Provider interface 🤷

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, it isn't ideal but I I feel like it is a side affect of the collector needing features before the go-sdk could implement.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I second that the ergonomics could be better here. The SDK exposes a Resource API, but not APIs for the underlying attributes, which is what you need here. This is probably the best approach given the circumstances.

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