Skip to content

Commit

Permalink
[processor/resourcedetection] Only attempt to detect k8s node resourc…
Browse files Browse the repository at this point in the history
…e attributes when enabled (#32039)

**Description:** 
Detecting k8s node resource attributes requires specific permissions and
configuration. If they're disabled we shouldn't hit errors trying to
detect them. This change makes it so that these resource attributes are
only attempted to be detected when enabled.

**Link to tracking Issue:**
Resolves
#31941

**Testing:** 
Existing tests are passing. New test was failing before changes,
succeeds after.
  • Loading branch information
crobert-1 authored Mar 30, 2024
1 parent 144f640 commit 66df53c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
27 changes: 27 additions & 0 deletions .chloggen/resdetproc_check_enabled.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# 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: Only attempt to detect Kubernetes node resource attributes when they're enabled.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [31941]

# (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:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
22 changes: 14 additions & 8 deletions processor/resourcedetectionprocessor/internal/k8snode/k8snode.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var _ internal.Detector = (*detector)(nil)
type detector struct {
provider k8snode.Provider
logger *zap.Logger
ra *metadata.ResourceAttributesConfig
rb *metadata.ResourceBuilder
}

Expand All @@ -43,22 +44,27 @@ func NewDetector(set processor.CreateSettings, dcfg internal.DetectorConfig) (in
return &detector{
provider: k8snodeProvider,
logger: set.Logger,
ra: &cfg.ResourceAttributes,
rb: metadata.NewResourceBuilder(cfg.ResourceAttributes),
}, nil
}

func (d *detector) Detect(ctx context.Context) (resource pcommon.Resource, schemaURL string, err error) {
nodeUID, err := d.provider.NodeUID(ctx)
if err != nil {
return pcommon.NewResource(), "", fmt.Errorf("failed getting k8s node UID: %w", err)
if d.ra.K8sNodeUID.Enabled {
nodeUID, err := d.provider.NodeUID(ctx)
if err != nil {
return pcommon.NewResource(), "", fmt.Errorf("failed getting k8s node UID: %w", err)
}
d.rb.SetK8sNodeUID(nodeUID)
}
d.rb.SetK8sNodeUID(nodeUID)

nodeName, err := d.provider.NodeName(ctx)
if err != nil {
return pcommon.NewResource(), "", fmt.Errorf("failed getting k8s node name: %w", err)
if d.ra.K8sNodeName.Enabled {
nodeName, err := d.provider.NodeName(ctx)
if err != nil {
return pcommon.NewResource(), "", fmt.Errorf("failed getting k8s node name: %w", err)
}
d.rb.SetK8sNodeName(nodeName)
}
d.rb.SetK8sNodeName(nodeName)

return d.rb.Emit(), conventions.SchemaURL, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,27 @@ func TestDetect(t *testing.T) {

assert.Equal(t, expected, res.Attributes().AsRaw())
}

func TestDetectDisabledResourceAttributes(t *testing.T) {
md := &mockMetadata{}
cfg := CreateDefaultConfig()
cfg.ResourceAttributes.K8sNodeUID.Enabled = false
cfg.ResourceAttributes.K8sNodeName.Enabled = false
// set k8s cluster env variables and auth type to create a dummy API client
cfg.APIConfig.AuthType = k8sconfig.AuthTypeNone
t.Setenv("KUBERNETES_SERVICE_HOST", "127.0.0.1")
t.Setenv("KUBERNETES_SERVICE_PORT", "6443")
t.Setenv("K8S_NODE_NAME", "mainNode")

k8sDetector, err := NewDetector(processortest.NewNopCreateSettings(), cfg)
require.NoError(t, err)
k8sDetector.(*detector).provider = md
res, schemaURL, err := k8sDetector.Detect(context.Background())
require.NoError(t, err)
assert.Equal(t, conventions.SchemaURL, schemaURL)
md.AssertExpectations(t)

expected := map[string]any{}

assert.Equal(t, expected, res.Attributes().AsRaw())
}

0 comments on commit 66df53c

Please sign in to comment.