Skip to content

Commit

Permalink
Add support for controller-name use with StatefulSet (closes open-tel…
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeemster committed Aug 28, 2024
1 parent ee7be8b commit 3cec25f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
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
PrefContollerNameForStatefulSet 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
defaultPrefContollerNameForStatefulSet = 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,
PrefContollerNameForStatefulSet: defaultPrefContollerNameForStatefulSet,
}
}

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
4 changes: 2 additions & 2 deletions receiver/awscontainerinsightreceiver/internal/stores/store.go
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 @@ -63,7 +63,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

0 comments on commit 3cec25f

Please sign in to comment.