Skip to content

Commit

Permalink
Create internal/metadataproviders module (#10376)
Browse files Browse the repository at this point in the history
* [processor/resourcedetection] Move 'azure' metadata provider to internal package

* [processor/resourcedetection] Move 'docker' metadata provider to internal package

* [processor/resourcedetection] Move 'system' metadata provider to internal package

* [processor/resourcedetection] Move 'gcp' metadata provider to internal package

* [processor/resourcedetection] Move 'consul' metadata provider to internal package

* [processor/resourcedetection] Move 'ec2' metadata provider to internal package

* Add CODEOWNERS entry (same as resourcedetectionprocessor)

* add new module to versions.yaml

* Fix merge

* make gotidy
  • Loading branch information
mx-psi authored Jun 3, 2022
1 parent 39692fd commit c630f8f
Show file tree
Hide file tree
Showing 39 changed files with 463 additions and 147 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ internal/docker/ @open-telemetry/collector-c

internal/k8sconfig/ @open-telemetry/collector-contrib-approvers @pmcollins @dmitryax
internal/kubelet/ @open-telemetry/collector-contrib-approvers @dmitryax
internal/metadataproviders/ @open-telemetry/collector-contrib-approvers @jrcamp @pmm-sumo @Aneurysm9 @dashpole
internal/scrapertest/ @open-telemetry/collector-contrib-approvers @djaglowski
internal/splunk/ @open-telemetry/collector-contrib-approvers @pmcollins @dmitryax

Expand Down
9 changes: 6 additions & 3 deletions cmd/configschema/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ require (
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector v0.30.2-0.20220512190557-875976f87023 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v1.6.1 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.30.1 // indirect
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/ReneKroon/ttlcache/v2 v2.11.0 // indirect
github.com/SAP/go-hdb v0.105.5 // indirect
github.com/SermoDigital/jose v0.9.2-0.20161205224733-f6df55f235c2 // indirect
Expand Down Expand Up @@ -101,7 +101,7 @@ require (
github.com/dennwc/varint v1.0.0 // indirect
github.com/dgraph-io/ristretto v0.1.0 // indirect
github.com/digitalocean/godo v1.80.0 // indirect
github.com/docker/distribution v2.8.0-beta.1+incompatible // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/docker v20.10.16+incompatible // indirect
github.com/docker/go-connections v0.4.1-0.20210727194412-58542c764a11 // indirect
github.com/docker/go-units v0.4.0 // indirect
Expand Down Expand Up @@ -308,6 +308,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/internal/docker v0.52.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig v0.52.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/kubelet v0.52.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders v0.0.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/sharedcomponent v0.52.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk v0.52.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr v0.52.0 // indirect
Expand Down Expand Up @@ -497,7 +498,7 @@ require (
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
golang.org/x/time v0.0.0-20220411224347-583f2d630306 // indirect
golang.org/x/tools v0.1.10 // indirect
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
gonum.org/v1/gonum v0.11.0 // indirect
Expand Down Expand Up @@ -562,6 +563,8 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sco

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/kubelet => ../../internal/kubelet

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders => ../../internal/metadataproviders

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest => ../../internal/scrapertest

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/sharedcomponent => ../../internal/sharedcomponent
Expand Down
13 changes: 7 additions & 6 deletions cmd/configschema/go.sum

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

10 changes: 6 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ require (
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector v0.30.2-0.20220512190557-875976f87023 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v1.6.1 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.30.1 // indirect
github.com/Microsoft/go-winio v0.5.1 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/ReneKroon/ttlcache/v2 v2.11.0 // indirect
github.com/SAP/go-hdb v0.105.5 // indirect
github.com/SermoDigital/jose v0.9.2-0.20161205224733-f6df55f235c2 // indirect
Expand Down Expand Up @@ -233,7 +233,7 @@ require (
github.com/dennwc/varint v1.0.0 // indirect
github.com/dgraph-io/ristretto v0.1.0 // indirect
github.com/digitalocean/godo v1.80.0 // indirect
github.com/docker/distribution v2.8.0-beta.1+incompatible // indirect
github.com/docker/distribution v2.8.1+incompatible // indirect
github.com/docker/docker v20.10.16+incompatible // indirect
github.com/docker/go-connections v0.4.1-0.20210727194412-58542c764a11 // indirect
github.com/docker/go-units v0.4.0 // indirect
Expand Down Expand Up @@ -385,6 +385,7 @@ require (
github.com/open-telemetry/opentelemetry-collector-contrib/internal/docker v0.52.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig v0.52.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/kubelet v0.52.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders v0.0.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/sharedcomponent v0.52.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/splunk v0.52.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/batchperresourceattr v0.52.0 // indirect
Expand Down Expand Up @@ -500,7 +501,7 @@ require (
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
golang.org/x/time v0.0.0-20220411224347-583f2d630306 // indirect
golang.org/x/tools v0.1.10 // indirect
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
gonum.org/v1/gonum v0.11.0 // indirect
Expand All @@ -518,7 +519,6 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
gopkg.in/zorkian/go-datadog-api.v2 v2.30.0 // indirect
gotest.tools/v3 v3.1.0 // indirect
k8s.io/api v0.24.1 // indirect
k8s.io/apimachinery v0.24.1 // indirect
k8s.io/client-go v0.24.1 // indirect
Expand Down Expand Up @@ -564,6 +564,8 @@ replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sco

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/kubelet => ./internal/kubelet

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders => ./internal/metadataproviders

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest => ./internal/scrapertest

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/sharedcomponent => ./internal/sharedcomponent
Expand Down
15 changes: 8 additions & 7 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/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package ec2 // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/aws/ec2"
package ec2 // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders/aws/ec2"

import (
"context"
Expand All @@ -21,32 +21,32 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
)

type metadataProvider interface {
get(ctx context.Context) (ec2metadata.EC2InstanceIdentityDocument, error)
hostname(ctx context.Context) (string, error)
instanceID(ctx context.Context) (string, error)
type Provider interface {
Get(ctx context.Context) (ec2metadata.EC2InstanceIdentityDocument, error)
Hostname(ctx context.Context) (string, error)
InstanceID(ctx context.Context) (string, error)
}

type metadataClient struct {
metadata *ec2metadata.EC2Metadata
}

var _ metadataProvider = (*metadataClient)(nil)
var _ Provider = (*metadataClient)(nil)

func newMetadataClient(sess *session.Session) *metadataClient {
func NewProvider(sess *session.Session) Provider {
return &metadataClient{
metadata: ec2metadata.New(sess),
}
}

func (c *metadataClient) instanceID(ctx context.Context) (string, error) {
func (c *metadataClient) InstanceID(ctx context.Context) (string, error) {
return c.metadata.GetMetadataWithContext(ctx, "instance-id")
}

func (c *metadataClient) hostname(ctx context.Context) (string, error) {
func (c *metadataClient) Hostname(ctx context.Context) (string, error) {
return c.metadata.GetMetadataWithContext(ctx, "hostname")
}

func (c *metadataClient) get(ctx context.Context) (ec2metadata.EC2InstanceIdentityDocument, error) {
func (c *metadataClient) Get(ctx context.Context) (ec2metadata.EC2InstanceIdentityDocument, error) {
return c.metadata.GetInstanceIdentityDocumentWithContext(ctx)
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func TestMetadataProvider_get(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := newMetadataClient(tt.args.sess)
gotDoc, err := c.get(tt.args.ctx)
c := NewProvider(tt.args.sess)
gotDoc, err := c.Get(tt.args.ctx)
if (err != nil) != tt.wantErr {
t.Errorf("get() error = %v, wantErr %v", err, tt.wantErr)
return
Expand Down Expand Up @@ -82,8 +82,8 @@ func TestMetadataProvider_available(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := newMetadataClient(tt.args.sess)
if _, got := c.instanceID(tt.args.ctx); got != tt.want {
c := NewProvider(tt.args.sess)
if _, got := c.InstanceID(tt.args.ctx); got != tt.want {
t.Errorf("available() = %v, want %v", got, tt.want)
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// This file contains code based on the Azure IMDS samples, https://github.com/microsoft/azureimds
// under the Apache License 2.0

package azure // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/azure"
package azure // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders/azure"

import (
"context"
Expand All @@ -30,7 +30,7 @@ const (
metadataEndpoint = "http://169.254.169.254/metadata/instance/compute"
)

// Provider gets metadata from the Azure IMDS
// Provider gets metadata from the Azure IMDS.
type Provider interface {
Metadata(context.Context) (*ComputeMetadata, error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package azure // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/azure"
package azure // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/metadataproviders/azure"

import (
"context"
Expand Down
Loading

0 comments on commit c630f8f

Please sign in to comment.