-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processor/resourcedetection] Add GKE detector (#2821)
* [processor/resourcedetection] Add GKE detector This adds an gke detector for GKE. It sets cloud.provider, cloud.infrastructure_service, and k8s.cluster.name. It's intended to be used in conjunction with gce detector. The gke detector should come first so that the more specific cloud.infrastructure_service value wins (see README.md). Note that this code is taken from https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/detectors/gcp. There is a goal to prevent duplication with go-contrib in #1590 and I started down that path but it will require more time than currently available as it needs to transform from different data models, deal with any differences in metadata between existing detectors, etc. * add coverage for gce mock * review feedback * fix tests
- Loading branch information
Showing
10 changed files
with
345 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
processor/resourcedetectionprocessor/internal/gcp/gke/gke.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
119
processor/resourcedetectionprocessor/internal/gcp/gke/gke_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.