Skip to content

Commit

Permalink
Add metadata only on demand
Browse files Browse the repository at this point in the history
Signed-off-by: chrismark <[email protected]>
  • Loading branch information
ChrsMark committed Jan 10, 2022
1 parent 0767797 commit 9e7177c
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 12 deletions.
3 changes: 2 additions & 1 deletion deploy/kubernetes/elastic-agent-managed-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ rules:
- apiGroups: [ "batch" ]
resources:
- jobs
- cronjobs
# Uncomment if need metadata for cronjob objects in versions >= v1.21
#- cronjobs
verbs: [ "get", "list", "watch" ]
# required for apiserver
- nonResourceURLs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ rules:
- apiGroups: [ "batch" ]
resources:
- jobs
- cronjobs
# Uncomment if need metadata for cronjob objects in versions >= v1.21
#- cronjobs
verbs: [ "get", "list", "watch" ]
# required for apiserver
- nonResourceURLs:
Expand Down
3 changes: 2 additions & 1 deletion deploy/kubernetes/elastic-agent-standalone-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,8 @@ rules:
- apiGroups: ["batch"]
resources:
- jobs
- cronjobs
# Uncomment if need metadata for cronjob objects in versions >= v1.21
#- cronjobs
verbs: ["get", "list", "watch"]
- apiGroups:
- ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ rules:
- apiGroups: ["batch"]
resources:
- jobs
- cronjobs
# Uncomment if need metadata for cronjob objects in versions >= v1.21
#- cronjobs
verbs: ["get", "list", "watch"]
- apiGroups:
- ""
Expand Down
3 changes: 2 additions & 1 deletion deploy/kubernetes/metricbeat-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,8 @@ rules:
- apiGroups: ["batch"]
resources:
- jobs
- cronjobs
# Uncomment if need metadata for cronjob objects in versions >= v1.21
#- cronjobs
verbs: ["get", "list", "watch"]
- apiGroups:
- ""
Expand Down
3 changes: 2 additions & 1 deletion deploy/kubernetes/metricbeat/metricbeat-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ rules:
- apiGroups: ["batch"]
resources:
- jobs
- cronjobs
# Uncomment if need metadata for cronjob objects in versions >= v1.21
#- cronjobs
verbs: ["get", "list", "watch"]
- apiGroups:
- ""
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
This is the `state_cronjob` metricset of the Kubernetes module.

This metricset does not add metadata by default and hence in order to
add metadata for this one need to configure the metricset with `add_metadata: true`
and uncomment the proper `apiGroup` in the `ClusterRole`. Metadata are only available
for versions of k8s >= v1.21.
27 changes: 21 additions & 6 deletions metricbeat/module/kubernetes/state_cronjob/state_cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ func NewCronJobMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) {
return nil, fmt.Errorf("must be child of kubernetes module")
}

return &CronJobMetricSet{
config := util.GetDefaultDisabledMetaConfig()
if err := base.Module().UnpackConfig(&config); err != nil {
return nil, fmt.Errorf("error loading config of kubernetes module")
}

ms := CronJobMetricSet{
BaseMetricSet: base,
prometheus: prometheus,
mod: mod,
enricher: util.NewResourceMetadataEnricher(base, &kubernetes.CronJob{}, false),
mapping: &p.MetricsMapping{
Metrics: map[string]p.MetricMap{
"kube_cronjob_info": p.InfoMetric(),
Expand All @@ -81,15 +85,22 @@ func NewCronJobMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) {
"concurrency_policy": p.KeyLabel("concurrency"),
},
},
}, nil
}
if config.AddMetadata {
ms.enricher = util.NewResourceMetadataEnricher(
base, &kubernetes.CronJob{}, false)
}
return &ms, nil
}

// Fetch prometheus metrics and treats those prefixed by mb.ModuleDataKey as
// module rooted fields at the event that gets reported
//
// Copied from other kube state metrics.
func (m *CronJobMetricSet) Fetch(reporter mb.ReporterV2) {
m.enricher.Start()
if m.enricher != nil {
m.enricher.Start()
}

families, err := m.mod.GetStateMetricsFamilies(m.prometheus)
if err != nil {
Expand All @@ -104,7 +115,9 @@ func (m *CronJobMetricSet) Fetch(reporter mb.ReporterV2) {
return
}

m.enricher.Enrich(events)
if m.enricher != nil {
m.enricher.Enrich(events)
}
for _, event := range events {
e, err := util.CreateEvent(event, "kubernetes.cronjob")
if err != nil {
Expand All @@ -122,6 +135,8 @@ func (m *CronJobMetricSet) Fetch(reporter mb.ReporterV2) {

// Close stops this metricset
func (m *CronJobMetricSet) Close() error {
m.enricher.Stop()
if m.enricher != nil {
m.enricher.Stop()
}
return nil
}
6 changes: 6 additions & 0 deletions metricbeat/module/kubernetes/util/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,12 @@ func getResourceMetadataWatchers(config *kubernetesConfig, resource kubernetes.R
return watcher, nodeWatcher, namespaceWatcher
}

func GetDefaultDisabledMetaConfig() *kubernetesConfig {
return &kubernetesConfig{
AddMetadata: false,
}
}

func validatedConfig(base mb.BaseMetricSet) *kubernetesConfig {
config := kubernetesConfig{
AddMetadata: true,
Expand Down

0 comments on commit 9e7177c

Please sign in to comment.