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

[receiver/awscontainerinsightreceiver] Add support for controller-name use with StatefulSet #34901

Closed
wants to merge 6 commits into from
Closed
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
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: 'enhancement'

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Allows metrics for StatefulSets to set the "PodName" to be the name of the controller instead of the pod's own name.

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

# (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: [user]
4 changes: 4 additions & 0 deletions receiver/awscontainerinsightreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ The "PodName" attribute is set based on the name of the relevant controllers lik

The "FullPodName" attribute is the pod name including suffix. If false FullPodName label is not added. The default value is false

**prefer_controller_name_for_stateful_set (optional)**

For StatefulSet deployments the default behaviour is to set the "PodName" attribute to the the pod's own name. However this can make aggregation of these metrics across a set difficult. If this is set to "true" the "PodName" will instead use the name of its controller which allows for metric aggregation. The default value is false.

## Sample configuration for Container Insights
This is a sample configuration for AWS Container Insights using the `awscontainerinsightreceiver` and `awsemfexporter` for an EKS cluster:
```
Expand Down
5 changes: 5 additions & 0 deletions receiver/awscontainerinsightreceiver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ type Config struct {
// If false FullPodName label is not added
// The default value is false
AddFullPodNameMetricLabel bool `mapstructure:"add_full_pod_name_metric_label"`

// By default a StatefulSet will always use its full "PodName" when storing metrics.
// If true this will instead use the name of the relevant controller instead to match Deployment, Daemonset, Job, ...
// The default value is false
PrefControllerNameForStatefulSet bool `mapstructure:"prefer_controller_name_for_stateful_set"`
}
14 changes: 9 additions & 5 deletions receiver/awscontainerinsightreceiver/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const (

// Don't tag pod full name by default
defaultAddFullPodNameMetricLabel = false

// Don't use controller name for StatefulSet metrics by default
defaultPrefControllerNameForStatefulSet = false
)

// NewFactory creates a factory for AWS container insight receiver
Expand All @@ -43,11 +46,12 @@ func NewFactory() receiver.Factory {
// createDefaultConfig returns a default config for the receiver.
func createDefaultConfig() component.Config {
return &Config{
CollectionInterval: defaultCollectionInterval,
ContainerOrchestrator: defaultContainerOrchestrator,
TagService: defaultTagService,
PrefFullPodName: defaultPrefFullPodName,
AddFullPodNameMetricLabel: defaultAddFullPodNameMetricLabel,
CollectionInterval: defaultCollectionInterval,
ContainerOrchestrator: defaultContainerOrchestrator,
TagService: defaultTagService,
PrefFullPodName: defaultPrefFullPodName,
AddFullPodNameMetricLabel: defaultAddFullPodNameMetricLabel,
PrefControllerNameForStatefulSet: defaultPrefControllerNameForStatefulSet,
}
}

Expand Down
28 changes: 17 additions & 11 deletions receiver/awscontainerinsightreceiver/internal/stores/podstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,11 @@ type PodStore struct {
prefFullPodName bool
logger *zap.Logger
sync.Mutex
addFullPodNameMetricLabel bool
addFullPodNameMetricLabel bool
prefControllerNameForStatefulSet bool
}

func NewPodStore(hostIP string, prefFullPodName bool, addFullPodNameMetricLabel bool, logger *zap.Logger) (*PodStore, error) {
func NewPodStore(hostIP string, prefFullPodName bool, addFullPodNameMetricLabel bool, prefControllerNameForStatefulSet bool, logger *zap.Logger) (*PodStore, error) {
podClient, err := kubeletutil.NewKubeletClient(hostIP, ci.KubeSecurePort, logger)
if err != nil {
return nil, err
Expand All @@ -122,14 +123,15 @@ func NewPodStore(hostIP string, prefFullPodName bool, addFullPodNameMetricLabel
}

podStore := &PodStore{
cache: newMapWithExpiry(podsExpiry),
prevMeasurements: make(map[string]*mapWithExpiry),
podClient: podClient,
nodeInfo: newNodeInfo(logger),
prefFullPodName: prefFullPodName,
k8sClient: k8sClient,
logger: logger,
addFullPodNameMetricLabel: addFullPodNameMetricLabel,
cache: newMapWithExpiry(podsExpiry),
prevMeasurements: make(map[string]*mapWithExpiry),
podClient: podClient,
nodeInfo: newNodeInfo(logger),
prefFullPodName: prefFullPodName,
k8sClient: k8sClient,
logger: logger,
addFullPodNameMetricLabel: addFullPodNameMetricLabel,
prefControllerNameForStatefulSet: prefControllerNameForStatefulSet,
}

return podStore, nil
Expand Down Expand Up @@ -589,7 +591,11 @@ func (p *PodStore) addPodOwnersAndPodName(metric CIMetric, pod *corev1.Pod, kube

if podName == "" {
if owner.Kind == ci.StatefulSet {
podName = pod.Name
if p.prefControllerNameForStatefulSet {
podName = name
} else {
podName = pod.Name
}
} else if owner.Kind == ci.DaemonSet || owner.Kind == ci.Job ||
owner.Kind == ci.ReplicaSet || owner.Kind == ci.ReplicationController {
podName = name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ func TestPodStore_addPodOwnersAndPodName(t *testing.T) {
assert.Equal(t, expectedOwner, kubernetesBlob)

// Test StatefulSet
podStore.prefControllerNameForStatefulSet = false
metric = generateMetric(fields, tags)
ssName := "StatefulSetTest"
pod.OwnerReferences[0].Kind = ci.StatefulSet
Expand All @@ -486,6 +487,14 @@ func TestPodStore_addPodOwnersAndPodName(t *testing.T) {
assert.Equal(t, expectedOwnerName, metric.GetTag(ci.PodNameKey))
assert.Equal(t, expectedOwner, kubernetesBlob)

podStore.prefControllerNameForStatefulSet = true
kubernetesBlob = map[string]any{}
podStore.addPodOwnersAndPodName(metric, pod, kubernetesBlob)
expectedOwner["pod_owners"] = []any{map[string]string{"owner_kind": ci.StatefulSet, "owner_name": ssName}}
expectedOwnerName = "StatefulSetTest"
assert.Equal(t, expectedOwnerName, metric.GetTag(ci.PodNameKey))
assert.Equal(t, expectedOwner, kubernetesBlob)

// Test ReplicationController
pod.Name = "this should not be in FullPodNameKey"
rcName := "ReplicationControllerTest"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type K8sDecorator struct {
podStore *PodStore
}

func NewK8sDecorator(ctx context.Context, tagService bool, prefFullPodName bool, addFullPodNameMetricLabel bool, logger *zap.Logger) (*K8sDecorator, error) {
func NewK8sDecorator(ctx context.Context, tagService bool, prefFullPodName bool, addFullPodNameMetricLabel bool, prefControllerNameForStatefulSet bool, logger *zap.Logger) (*K8sDecorator, error) {
hostIP := os.Getenv("HOST_IP")
if hostIP == "" {
return nil, errors.New("environment variable HOST_IP is not set in k8s deployment config")
Expand All @@ -54,7 +54,7 @@ func NewK8sDecorator(ctx context.Context, tagService bool, prefFullPodName bool,
ctx: ctx,
}

podstore, err := NewPodStore(hostIP, prefFullPodName, addFullPodNameMetricLabel, logger)
podstore, err := NewPodStore(hostIP, prefFullPodName, addFullPodNameMetricLabel, prefControllerNameForStatefulSet, logger)

if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion receiver/awscontainerinsightreceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (acir *awsContainerInsightReceiver) Start(ctx context.Context, host compone
}

if acir.config.ContainerOrchestrator == ci.EKS {
k8sDecorator, err := stores.NewK8sDecorator(ctx, acir.config.TagService, acir.config.PrefFullPodName, acir.config.AddFullPodNameMetricLabel, acir.settings.Logger)
k8sDecorator, err := stores.NewK8sDecorator(ctx, acir.config.TagService, acir.config.PrefFullPodName, acir.config.AddFullPodNameMetricLabel, acir.config.PrefControllerNameForStatefulSet, acir.settings.Logger)
if err != nil {
return err
}
Expand Down