Skip to content

[processor/resourcedetection] Add back host.name on GKE #12355

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

Merged
merged 2 commits into from
Jul 22, 2022
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
27 changes: 22 additions & 5 deletions processor/resourcedetectionprocessor/internal/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"go.uber.org/multierr"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal"
)
Expand All @@ -43,11 +44,15 @@ const (
// * Google App Engine (GAE).
// * Cloud Run.
// * Cloud Functions.
func NewDetector(_ component.ProcessorCreateSettings, _ internal.DetectorConfig) (internal.Detector, error) {
return &detector{detector: gcp.NewDetector()}, nil
func NewDetector(set component.ProcessorCreateSettings, _ internal.DetectorConfig) (internal.Detector, error) {
return &detector{
logger: set.Logger,
detector: gcp.NewDetector(),
}, nil
}

type detector struct {
logger *zap.Logger
detector gcpDetector
}

Expand All @@ -56,7 +61,7 @@ func (d *detector) Detect(context.Context) (resource pcommon.Resource, schemaURL
if !metadata.OnGCE() {
return res, "", nil
}
b := &resourceBuilder{attrs: res.Attributes()}
b := &resourceBuilder{logger: d.logger, attrs: res.Attributes()}
b.attrs.InsertString(conventions.AttributeCloudProvider, conventions.AttributeCloudProviderGCP)
b.add(conventions.AttributeCloudAccountID, d.detector.ProjectID)

Expand All @@ -66,6 +71,8 @@ func (d *detector) Detect(context.Context) (resource pcommon.Resource, schemaURL
b.addZoneOrRegion(d.detector.GKEAvailabilityZoneOrRegion)
b.add(conventions.AttributeK8SClusterName, d.detector.GKEClusterName)
b.add(conventions.AttributeHostID, d.detector.GKEHostID)
// GCEHostname is fallible on GKE, since it's not available when using workload identity.
b.addFallible(conventions.AttributeHostName, d.detector.GCEHostName)
case gcp.CloudRun:
b.attrs.InsertString(conventions.AttributeCloudPlatform, conventions.AttributeCloudPlatformGCPCloudRun)
b.add(conventions.AttributeFaaSName, d.detector.FaaSName)
Expand Down Expand Up @@ -106,8 +113,9 @@ func (d *detector) Detect(context.Context) (resource pcommon.Resource, schemaURL
// resourceBuilder simplifies constructing resources using GCP detection
// library functions.
type resourceBuilder struct {
errs []error
attrs pcommon.Map
logger *zap.Logger
errs []error
attrs pcommon.Map
}

func (r *resourceBuilder) add(key string, detect func() (string, error)) {
Expand All @@ -118,6 +126,15 @@ func (r *resourceBuilder) add(key string, detect func() (string, error)) {
}
}

// addFallible adds a detect function whose failures should be ignored
func (r *resourceBuilder) addFallible(key string, detect func() (string, error)) {
if v, err := detect(); err == nil {
r.attrs.InsertString(key, v)
} else {
r.logger.Info("Fallible detector failed. This attribute will not be available.", zap.String("key", key), zap.Error(err))
}
}

// zoneAndRegion functions are expected to return zone, region, err.
func (r *resourceBuilder) addZoneAndRegion(detect func() (string, string, error)) {
if zone, region, err := detect(); err == nil {
Expand Down
70 changes: 51 additions & 19 deletions processor/resourcedetectionprocessor/internal/gcp/gcp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/pdata/pcommon"
conventions "go.opentelemetry.io/collector/semconv/v1.6.1"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal"
)
Expand All @@ -40,31 +41,54 @@ func TestDetect(t *testing.T) {
}{
{
desc: "zonal GKE cluster",
detector: &detector{detector: &fakeGCPDetector{
detector: newTestDetector(&fakeGCPDetector{
projectID: "my-project",
cloudPlatform: gcp.GKE,
gceHostName: "my-gke-node-1234",
gkeHostID: "1472385723456792345",
gkeClusterName: "my-cluster",
gkeAvailabilityZone: "us-central1-c",
}},
}),
expectedResource: internal.NewResource(map[string]interface{}{
conventions.AttributeCloudProvider: conventions.AttributeCloudProviderGCP,
conventions.AttributeCloudAccountID: "my-project",
conventions.AttributeCloudPlatform: conventions.AttributeCloudPlatformGCPKubernetesEngine,
conventions.AttributeK8SClusterName: "my-cluster",
conventions.AttributeCloudAvailabilityZone: "us-central1-c",
conventions.AttributeHostID: "1472385723456792345",
conventions.AttributeHostName: "my-gke-node-1234",
}),
},
{
desc: "regional GKE cluster",
detector: &detector{detector: &fakeGCPDetector{
detector: newTestDetector(&fakeGCPDetector{
projectID: "my-project",
cloudPlatform: gcp.GKE,
gceHostName: "my-gke-node-1234",
gkeHostID: "1472385723456792345",
gkeClusterName: "my-cluster",
gkeRegion: "us-central1",
}),
expectedResource: internal.NewResource(map[string]interface{}{
conventions.AttributeCloudProvider: conventions.AttributeCloudProviderGCP,
conventions.AttributeCloudAccountID: "my-project",
conventions.AttributeCloudPlatform: conventions.AttributeCloudPlatformGCPKubernetesEngine,
conventions.AttributeK8SClusterName: "my-cluster",
conventions.AttributeCloudRegion: "us-central1",
conventions.AttributeHostID: "1472385723456792345",
conventions.AttributeHostName: "my-gke-node-1234",
}),
},
{
desc: "regional GKE cluster with workload identity",
detector: newTestDetector(&fakeGCPDetector{
projectID: "my-project",
cloudPlatform: gcp.GKE,
gceHostNameErr: fmt.Errorf("metadata endpoint is concealed"),
gkeHostID: "1472385723456792345",
gkeClusterName: "my-cluster",
gkeRegion: "us-central1",
}},
}),
expectedResource: internal.NewResource(map[string]interface{}{
conventions.AttributeCloudProvider: conventions.AttributeCloudProviderGCP,
conventions.AttributeCloudAccountID: "my-project",
Expand All @@ -76,15 +100,15 @@ func TestDetect(t *testing.T) {
},
{
desc: "GCE",
detector: &detector{detector: &fakeGCPDetector{
detector: newTestDetector(&fakeGCPDetector{
projectID: "my-project",
cloudPlatform: gcp.GCE,
gceHostID: "1472385723456792345",
gceHostName: "my-gke-node-1234",
gceHostType: "n1-standard1",
gceAvailabilityZone: "us-central1-c",
gceRegion: "us-central1",
}},
}),
expectedResource: internal.NewResource(map[string]interface{}{
conventions.AttributeCloudProvider: conventions.AttributeCloudProviderGCP,
conventions.AttributeCloudAccountID: "my-project",
Expand All @@ -98,14 +122,14 @@ func TestDetect(t *testing.T) {
},
{
desc: "Cloud Run",
detector: &detector{detector: &fakeGCPDetector{
detector: newTestDetector(&fakeGCPDetector{
projectID: "my-project",
cloudPlatform: gcp.CloudRun,
faaSID: "1472385723456792345",
faaSCloudRegion: "us-central1",
faaSName: "my-service",
faaSVersion: "123456",
}},
}),
expectedResource: internal.NewResource(map[string]interface{}{
conventions.AttributeCloudProvider: conventions.AttributeCloudProviderGCP,
conventions.AttributeCloudAccountID: "my-project",
Expand All @@ -118,14 +142,14 @@ func TestDetect(t *testing.T) {
},
{
desc: "Cloud Functions",
detector: &detector{detector: &fakeGCPDetector{
detector: newTestDetector(&fakeGCPDetector{
projectID: "my-project",
cloudPlatform: gcp.CloudFunctions,
faaSID: "1472385723456792345",
faaSCloudRegion: "us-central1",
faaSName: "my-service",
faaSVersion: "123456",
}},
}),
expectedResource: internal.NewResource(map[string]interface{}{
conventions.AttributeCloudProvider: conventions.AttributeCloudProviderGCP,
conventions.AttributeCloudAccountID: "my-project",
Expand All @@ -138,15 +162,15 @@ func TestDetect(t *testing.T) {
},
{
desc: "App Engine Standard",
detector: &detector{detector: &fakeGCPDetector{
detector: newTestDetector(&fakeGCPDetector{
projectID: "my-project",
cloudPlatform: gcp.AppEngineStandard,
appEngineServiceInstance: "1472385723456792345",
appEngineAvailabilityZone: "us-central1-c",
appEngineRegion: "us-central1",
appEngineServiceName: "my-service",
appEngineServiceVersion: "123456",
}},
}),
expectedResource: internal.NewResource(map[string]interface{}{
conventions.AttributeCloudProvider: conventions.AttributeCloudProviderGCP,
conventions.AttributeCloudAccountID: "my-project",
Expand All @@ -160,15 +184,15 @@ func TestDetect(t *testing.T) {
},
{
desc: "App Engine Flex",
detector: &detector{detector: &fakeGCPDetector{
detector: newTestDetector(&fakeGCPDetector{
projectID: "my-project",
cloudPlatform: gcp.AppEngineFlex,
appEngineServiceInstance: "1472385723456792345",
appEngineAvailabilityZone: "us-central1-c",
appEngineRegion: "us-central1",
appEngineServiceName: "my-service",
appEngineServiceVersion: "123456",
}},
}),
expectedResource: internal.NewResource(map[string]interface{}{
conventions.AttributeCloudProvider: conventions.AttributeCloudProviderGCP,
conventions.AttributeCloudAccountID: "my-project",
Expand All @@ -182,20 +206,20 @@ func TestDetect(t *testing.T) {
},
{
desc: "Unknown Platform",
detector: &detector{detector: &fakeGCPDetector{
detector: newTestDetector(&fakeGCPDetector{
projectID: "my-project",
cloudPlatform: gcp.UnknownPlatform,
}},
}),
expectedResource: internal.NewResource(map[string]interface{}{
conventions.AttributeCloudProvider: conventions.AttributeCloudProviderGCP,
conventions.AttributeCloudAccountID: "my-project",
}),
},
{
desc: "error",
detector: &detector{detector: &fakeGCPDetector{
detector: newTestDetector(&fakeGCPDetector{
err: fmt.Errorf("failed to get metadata"),
}},
}),
expectErr: true,
expectedResource: internal.NewResource(map[string]interface{}{
conventions.AttributeCloudProvider: conventions.AttributeCloudProviderGCP,
Expand All @@ -217,6 +241,13 @@ func TestDetect(t *testing.T) {
}
}

func newTestDetector(gcpDetector *fakeGCPDetector) *detector {
return &detector{
logger: zap.NewNop(),
detector: gcpDetector,
}
}

// fakeGCPDetector implements gcpDetector and uses fake values.
type fakeGCPDetector struct {
err error
Expand All @@ -240,6 +271,7 @@ type fakeGCPDetector struct {
gceHostType string
gceHostID string
gceHostName string
gceHostNameErr error
}

func (f *fakeGCPDetector) ProjectID() (string, error) {
Expand Down Expand Up @@ -372,7 +404,7 @@ func (f *fakeGCPDetector) GCEHostName() (string, error) {
if f.err != nil {
return "", f.err
}
return f.gceHostName, nil
return f.gceHostName, f.gceHostNameErr
}

func TestDeduplicateDetectors(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions unreleased/mx-psi_gke-detector.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: resourcedetectionprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add back `host.name` attribute when running on GKE

# One or more tracking issues related to the change
issues: [12354]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: