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

[processor/resourcedetection] Add GKE detector #2821

Merged
merged 7 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 7 additions & 1 deletion processor/resourcedetectionprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ to read resource information from the [GCE metadata server](https://cloud.google
* host.image.id
* host.type

* GKE: Google Kubernetes Engine

* cloud.provider ("gcp")
* cloud.infrastructure_service ("gcp_gke")
* k8s.cluster.name (name of the GKE cluster)

* AWS EC2: Uses [AWS SDK for Go](https://docs.aws.amazon.com/sdk-for-go/api/aws/ec2metadata/) to read resource information from the [EC2 instance metadata API](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html) to retrieve the following resource attributes:

* cloud.provider ("aws")
Expand Down Expand Up @@ -94,7 +100,7 @@ ec2:
## Configuration

```yaml
# a list of resource detectors to run, valid options are: "env", "system", "gce", "ec2", "ecs", "elastic_beanstalk", "azure"
# a list of resource detectors to run, valid options are: "env", "system", "gce", "gke", "ec2", "ecs", "elastic_beanstalk", "azure"
detectors: [ <string> ]
# determines if existing resource attributes should be overridden or preserved, defaults to true
override: <bool>
Expand Down
10 changes: 6 additions & 4 deletions processor/resourcedetectionprocessor/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/azure"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/env"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/gcp/gce"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/gcp/gke"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/system"
)

Expand All @@ -54,13 +55,14 @@ type factory struct {
// NewFactory creates a new factory for ResourceDetection processor.
func NewFactory() component.ProcessorFactory {
resourceProviderFactory := internal.NewProviderFactory(map[internal.DetectorType]internal.DetectorFactory{
env.TypeStr: env.NewDetector,
system.TypeStr: system.NewDetector,
gce.TypeStr: gce.NewDetector,
azure.TypeStr: azure.NewDetector,
ec2.TypeStr: ec2.NewDetector,
ecs.TypeStr: ecs.NewDetector,
elasticbeanstalk.TypeStr: elasticbeanstalk.NewDetector,
azure.TypeStr: azure.NewDetector,
env.TypeStr: env.NewDetector,
gce.TypeStr: gce.NewDetector,
gke.TypeStr: gke.NewDetector,
system.TypeStr: system.NewDetector,
})

f := &factory{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"go.opentelemetry.io/collector/translator/conventions"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/gcp"
)

const (
Expand All @@ -34,11 +35,11 @@ const (
var _ internal.Detector = (*Detector)(nil)

type Detector struct {
metadata gceMetadata
metadata gcp.Metadata
}

func NewDetector(component.ProcessorCreateParams, internal.DetectorConfig) (internal.Detector, error) {
return &Detector{metadata: &gceMetadataImpl{}}, nil
return &Detector{metadata: &gcp.MetadataImpl{}}, nil
}

func (d *Detector) Detect(context.Context) (pdata.Resource, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,68 +20,30 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/translator/conventions"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/gcp"
)

type mockMetadata struct {
mock.Mock
}

func (m *mockMetadata) OnGCE() bool {
return m.MethodCalled("OnGCE").Bool(0)
}

func (m *mockMetadata) ProjectID() (string, error) {
args := m.MethodCalled("ProjectID")
return args.String(0), args.Error(1)
}

func (m *mockMetadata) Zone() (string, error) {
args := m.MethodCalled("Zone")
return args.String(0), args.Error(1)
}

func (m *mockMetadata) Hostname() (string, error) {
args := m.MethodCalled("Hostname")
return args.String(0), args.Error(1)
}

func (m *mockMetadata) InstanceID() (string, error) {
args := m.MethodCalled("InstanceID")
return args.String(0), args.Error(1)
}

func (m *mockMetadata) InstanceName() (string, error) {
args := m.MethodCalled("InstanceName")
return args.String(0), args.Error(1)
}

func (m *mockMetadata) Get(suffix string) (string, error) {
args := m.MethodCalled("Get")
return args.String(0), args.Error(1)
}

func TestNewDetector(t *testing.T) {
d, err := NewDetector(component.ProcessorCreateParams{Logger: zap.NewNop()}, nil)
assert.NotNil(t, d)
assert.NoError(t, err)
}

func TestDetectTrue(t *testing.T) {
md := &mockMetadata{}
md := &gcp.MockMetadata{}
md.On("OnGCE").Return(true)
md.On("ProjectID").Return("1", nil)
md.On("Zone").Return("zone", nil)
md.On("Hostname").Return("hostname", nil)
md.On("InstanceID").Return("2", nil)
md.On("InstanceName").Return("name", nil)
md.On("Get").Return("machine-type", nil)
md.On("Get", "instance/machine-type").Return("machine-type", nil)

detector := &Detector{metadata: md}
res, err := detector.Detect(context.Background())
Expand All @@ -105,7 +67,7 @@ func TestDetectTrue(t *testing.T) {
}

func TestDetectFalse(t *testing.T) {
md := &mockMetadata{}
md := &gcp.MockMetadata{}
md.On("OnGCE").Return(false)

detector := &Detector{metadata: md}
Expand All @@ -116,14 +78,14 @@ func TestDetectFalse(t *testing.T) {
}

func TestDetectError(t *testing.T) {
md := &mockMetadata{}
md := &gcp.MockMetadata{}
md.On("OnGCE").Return(true)
md.On("ProjectID").Return("", errors.New("err1"))
md.On("Zone").Return("", errors.New("err2"))
md.On("Hostname").Return("", errors.New("err3"))
md.On("InstanceID").Return("", errors.New("err4"))
md.On("InstanceName").Return("", errors.New("err5"))
md.On("Get").Return("", errors.New("err6"))
md.On("Get", "instance/machine-type").Return("", errors.New("err6"))

detector := &Detector{metadata: md}
res, err := detector.Detect(context.Background())
Expand Down
78 changes: 78 additions & 0 deletions processor/resourcedetectionprocessor/internal/gcp/gke/gke.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package gke

import (
"context"
"os"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/translator/conventions"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/gcp"
)

const (
// TypeStr is type of detector.
TypeStr = "gke"

// GCE metadata attribute containing the GKE cluster name.
clusterNameAttribute = "cluster-name"

// Environment variable that is set when running on Kubernetes.
kubernetesServiceHostEnvVar = "KUBERNETES_SERVICE_HOST"
)

var _ internal.Detector = (*Detector)(nil)

type Detector struct {
log *zap.Logger
metadata gcp.Metadata
}

func NewDetector(params component.ProcessorCreateParams, _ internal.DetectorConfig) (internal.Detector, error) {
return &Detector{log: params.Logger, metadata: &gcp.MetadataImpl{}}, nil
}

// Detect detects associated resources when running in GKE environment.
func (gke *Detector) Detect(ctx context.Context) (pdata.Resource, error) {
res := pdata.NewResource()

// Check if on GCP.
if !gke.metadata.OnGCE() {
return res, nil
}

attr := res.Attributes()
attr.InsertString(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderGCP)

// Check if running on k8s.
if os.Getenv(kubernetesServiceHostEnvVar) == "" {
return res, nil
}

attr.InsertString(conventions.AttributeCloudInfrastructureService, conventions.AttributeCloudProviderGCPGKE)

if clusterName, err := gke.metadata.InstanceAttributeValue(clusterNameAttribute); err != nil {
gke.log.Warn("Unable to determine GKE cluster name", zap.Error(err))
} else if clusterName != "" {
attr.InsertString(conventions.AttributeK8sCluster, clusterName)
}

return res, nil
}
119 changes: 119 additions & 0 deletions processor/resourcedetectionprocessor/internal/gcp/gke/gke_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package gke

import (
"context"
"errors"
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/gcp"
)

func TestNotGCE(t *testing.T) {
metadata := &gcp.MockMetadata{}
detector := &Detector{
log: zap.NewNop(),
metadata: metadata,
}

metadata.On("OnGCE").Return(false)
res, err := detector.Detect(context.Background())
require.NoError(t, err)
assert.Equal(t, 0, res.Attributes().Len())

metadata.AssertExpectations(t)
}

func TestDetectWithoutCluster(t *testing.T) {
metadata := &gcp.MockMetadata{}
detector := &Detector{
log: zap.NewNop(),
metadata: metadata,
}

metadata.On("OnGCE").Return(true)
metadata.On("InstanceAttributeValue", "cluster-name").Return("", errors.New("no cluster"))

require.NoError(t, os.Setenv("KUBERNETES_SERVICE_HOST", "localhost"))

res, err := detector.Detect(context.Background())
require.NoError(t, err)

assert.Equal(t, map[string]interface{}{
"cloud.provider": "gcp",
"cloud.infrastructure_service": "gcp_gke",
}, internal.AttributesToMap(res.Attributes()))

metadata.AssertExpectations(t)
}

func TestDetectWithoutK8s(t *testing.T) {
metadata := &gcp.MockMetadata{}
detector := &Detector{
log: zap.NewNop(),
metadata: metadata,
}

metadata.On("OnGCE").Return(true)

require.NoError(t, os.Unsetenv("KUBERNETES_SERVICE_HOST"))

res, err := detector.Detect(context.Background())
require.NoError(t, err)

assert.Equal(t, map[string]interface{}{
"cloud.provider": "gcp",
}, internal.AttributesToMap(res.Attributes()))

metadata.AssertExpectations(t)
}

func TestDetector_Detect(t *testing.T) {
metadata := &gcp.MockMetadata{}
detector := &Detector{
log: zap.NewNop(),
metadata: metadata,
}

metadata.On("OnGCE").Return(true)
metadata.On("InstanceAttributeValue", "cluster-name").Return("cluster-a", nil)

require.NoError(t, os.Setenv("KUBERNETES_SERVICE_HOST", "localhost"))

res, err := detector.Detect(context.Background())
require.NoError(t, err)

assert.Equal(t, map[string]interface{}{
"cloud.provider": "gcp",
"cloud.infrastructure_service": "gcp_gke",
"k8s.cluster.name": "cluster-a",
}, internal.AttributesToMap(res.Attributes()))

metadata.AssertExpectations(t)
}

func TestNewDetector(t *testing.T) {
detector, err := NewDetector(component.ProcessorCreateParams{Logger: zap.NewNop()}, nil)
assert.NoError(t, err)
assert.NotNil(t, detector)
}
Loading