Skip to content

Commit

Permalink
Add enricher to storageclass metricset (#33032)
Browse files Browse the repository at this point in the history
* add enricher for storageclass

Signed-off-by: Tetiana Kravchenko <tetiana.kravchenko@elastic.co>

* add pr number

Signed-off-by: Tetiana Kravchenko <tetiana.kravchenko@elastic.co>

* fix check-no-changes] Error 1

Signed-off-by: Tetiana Kravchenko <tetiana.kravchenko@elastic.co>

* generate notice file

Signed-off-by: Tetiana Kravchenko <tetiana.kravchenko@elastic.co>

* readd removed metric from test file

Signed-off-by: Tetiana Kravchenko <tetiana.kravchenko@elastic.co>

Signed-off-by: Tetiana Kravchenko <tetiana.kravchenko@elastic.co>
  • Loading branch information
tetianakravchenko authored Sep 12, 2022
1 parent aa8ea6d commit d20fff4
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 21 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ https://github.com/elastic/beats/compare/v8.2.0\...main[Check the HEAD diff]
- Fix GCP storage field naming {pull}32806[32806]
- in module/windows/perfmon, changed collection method of the second counter value required to create a displayable value {pull}32305[32305]
- Fix and improve AWS metric period calculation to avoid zero-length intervals {pull}32724[32724]
- Add network name to data.json {pull}32986[32986]
- Add missing cluster metadata to k8s module metricsets {pull}32979[32979]
- Add missing cluster metadata to k8s module metricsets {pull}32979[32979] {pull}33032[33032]

*Packetbeat*

Expand Down
4 changes: 2 additions & 2 deletions NOTICE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9444,11 +9444,11 @@ SOFTWARE.

--------------------------------------------------------------------------------
Dependency : github.com/elastic/elastic-agent-autodiscover
Version: v0.2.1
Version: v0.3.0
Licence type (autodetected): Apache-2.0
--------------------------------------------------------------------------------

Contents of probable licence file $GOMODCACHE/github.com/elastic/elastic-agent-autodiscover@v0.2.1/LICENSE:
Contents of probable licence file $GOMODCACHE/github.com/elastic/elastic-agent-autodiscover@v0.3.0/LICENSE:

Apache License
Version 2.0, January 2004
Expand Down
7 changes: 7 additions & 0 deletions deploy/kubernetes/metricbeat-kubernetes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ data:
- state_resourcequota
- state_statefulset
- state_service
- state_persistentvolume
- state_persistentvolumeclaim
- state_storageclass
# If `https` is used to access `kube-state-metrics`, uncomment following settings:
# bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
# ssl.certificate_authorities:
Expand Down Expand Up @@ -303,6 +306,10 @@ rules:
- jobs
- cronjobs
verbs: ["get", "list", "watch"]
- apiGroups: ["storage.k8s.io"]
resources:
- storageclasses
verbs: ["get", "list", "watch"]
- apiGroups:
- ""
resources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ data:
- state_resourcequota
- state_statefulset
- state_service
- state_persistentvolume
- state_persistentvolumeclaim
- state_storageclass
# If `https` is used to access `kube-state-metrics`, uncomment following settings:
# bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
# ssl.certificate_authorities:
Expand Down
4 changes: 4 additions & 0 deletions deploy/kubernetes/metricbeat/metricbeat-role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ rules:
- jobs
- cronjobs
verbs: ["get", "list", "watch"]
- apiGroups: ["storage.k8s.io"]
resources:
- storageclasses
verbs: ["get", "list", "watch"]
- apiGroups:
- ""
resources:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ require (
github.com/aws/smithy-go v1.12.0
github.com/awslabs/kinesis-aggregation/go/v2 v2.0.0-20220623125934-28468a6701b5
github.com/elastic/bayeux v1.0.5
github.com/elastic/elastic-agent-autodiscover v0.2.1
github.com/elastic/elastic-agent-autodiscover v0.3.0
github.com/elastic/elastic-agent-libs v0.2.11
github.com/elastic/elastic-agent-shipper-client v0.4.0
github.com/elastic/elastic-agent-system-metrics v0.4.4
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -602,11 +602,10 @@ github.com/elastic/bayeux v1.0.5 h1:UceFq01ipmT3S8DzFK+uVAkbCdiPR0Bqei8qIGmUeY0=
github.com/elastic/bayeux v1.0.5/go.mod h1:CSI4iP7qeo5MMlkznGvYKftp8M7qqP/3nzmVZoXHY68=
github.com/elastic/dhcp v0.0.0-20200227161230-57ec251c7eb3 h1:lnDkqiRFKm0rxdljqrj3lotWinO9+jFmeDXIC4gvIQs=
github.com/elastic/dhcp v0.0.0-20200227161230-57ec251c7eb3/go.mod h1:aPqzac6AYkipvp4hufTyMj5PDIphF3+At8zr7r51xjY=
github.com/elastic/elastic-agent-autodiscover v0.2.1 h1:Nbeayh3vq2FNm6xaFo34mhUdOu0EVlpj53CqCsbU0E4=
github.com/elastic/elastic-agent-autodiscover v0.2.1/go.mod h1:gPnzzfdYNdgznAb+iG9eyyXaQXBbAMHa+Y6Z8hXfcGY=
github.com/elastic/elastic-agent-autodiscover v0.3.0 h1:kdpNnIDnVk7gvQxxR6PzZY7aM8LyMTRkwI/p+FNS17s=
github.com/elastic/elastic-agent-autodiscover v0.3.0/go.mod h1:p3MSf9813JEnolCTD0GyVAr3+Eptg2zQ9aZVFjl4tJ4=
github.com/elastic/elastic-agent-client/v7 v7.0.0-20210727140539-f0905d9377f6 h1:nFvXHBjYK3e9+xF0WKDeAKK4aOO51uC28s+L9rBmilo=
github.com/elastic/elastic-agent-client/v7 v7.0.0-20210727140539-f0905d9377f6/go.mod h1:uh/Gj9a0XEbYoM4NYz4LvaBVARz3QXLmlNjsrKY9fTc=
github.com/elastic/elastic-agent-libs v0.2.5/go.mod h1:chO3rtcLyGlKi9S0iGVZhYCzDfdDsAQYBc+ui588AFE=
github.com/elastic/elastic-agent-libs v0.2.11 h1:ZeYn35Kxt+IdtMPmE01TaDeaahCg/z7MkGPVWUo6Lp4=
github.com/elastic/elastic-agent-libs v0.2.11/go.mod h1:chO3rtcLyGlKi9S0iGVZhYCzDfdDsAQYBc+ui588AFE=
github.com/elastic/elastic-agent-shipper-client v0.4.0 h1:nsTJF9oo4RHLl+zxFUZqNHaE86C6Ba5aImfegcEf6Sk=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ data:
- state_resourcequota
- state_statefulset
- state_service
- state_persistentvolume
- state_persistentvolumeclaim
- state_storageclass
- module: kubernetes
metricsets:
- apiserver
Expand Down Expand Up @@ -293,6 +296,10 @@ rules:
- jobs
- cronjobs
verbs: ["get", "list", "watch"]
- apiGroups: ["storage.k8s.io"]
resources:
- storageclasses
verbs: ["get", "list", "watch"]
- apiGroups:
- ""
resources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,3 @@ kube_storageclass_created{storageclass="test_storageclass"} 1.501569018e+09
# HELP kube_storageclass_labels Kubernetes labels converted to Prometheus labels.
# TYPE kube_storageclass_labels gauge
kube_storageclass_labels{storageclass="test_storageclass",label_foo="bar"} 1


Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"RootFields": {},
"RootFields": null,
"ModuleFields": {
"labels": {
"foo": "bar"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"RootFields": {},
"RootFields": null,
"ModuleFields": {
"labels": {
"addonmanager_kubernetes_io_mode": "EnsureExists"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[
{
"RootFields": {},
"RootFields": null,
"ModuleFields": null,
"MetricSetFields": {
"created": "2021-08-12T14:24:52.000Z",
"name": "standard",
"provisioner": "rancher.io/local-path"
"provisioner": "rancher.io/local-path",
"reclaim_policy": "Delete",
"volume_binding_mode": "WaitForFirstConsumer"
},
"Index": "",
"ID": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
p "github.com/elastic/beats/v7/metricbeat/helper/prometheus"
"github.com/elastic/beats/v7/metricbeat/mb"
k8smod "github.com/elastic/beats/v7/metricbeat/module/kubernetes"
"github.com/elastic/beats/v7/metricbeat/module/kubernetes/util"
"github.com/elastic/elastic-agent-autodiscover/kubernetes"
)

func init() {
Expand All @@ -38,6 +40,7 @@ type StorageClassMetricSet struct {
prometheus p.Prometheus
mapping *p.MetricsMapping
mod k8smod.Module
enricher util.Enricher
}

// NewStorageClassMetricSet returns a prometheus based metricset for Storage classes
Expand All @@ -54,6 +57,7 @@ func NewStorageClassMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) {
BaseMetricSet: base,
prometheus: prometheus,
mod: mod,
enricher: util.NewResourceMetadataEnricher(base, &kubernetes.StorageClass{}, mod.GetMetricsRepo(), false),
mapping: &p.MetricsMapping{
Metrics: map[string]p.MetricMap{
"kube_storageclass_info": p.InfoMetric(),
Expand All @@ -66,10 +70,12 @@ func NewStorageClassMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) {
"kube_storageclass_created": p.Metric("created", p.OpUnixTimestampValue()),
},
Labels: map[string]p.LabelMap{
"storageclass": p.KeyLabel("name"),
"provisioner": p.Label("provisioner"),
"reclaimPolicy": p.Label("reclaim_policy"),
"volumeBindingMode": p.Label("volume_binding_mode"),
"storageclass": p.KeyLabel("name"),
"provisioner": p.Label("provisioner"),
"reclaimPolicy": p.Label("reclaim_policy"),
"reclaim_policy": p.Label("reclaim_policy"),
"volumeBindingMode": p.Label("volume_binding_mode"),
"volume_binding_mode": p.Label("volume_binding_mode"),
},
},
}, nil
Expand All @@ -78,6 +84,8 @@ func NewStorageClassMetricSet(base mb.BaseMetricSet) (mb.MetricSet, error) {
// Fetch prometheus metrics and treats those prefixed by mb.ModuleDataKey as
// module rooted fields at the event that gets reported
func (m *StorageClassMetricSet) Fetch(reporter mb.ReporterV2) {
m.enricher.Start()

families, err := m.mod.GetStateMetricsFamilies(m.prometheus)
if err != nil {
m.Logger().Error(err)
Expand All @@ -91,12 +99,24 @@ func (m *StorageClassMetricSet) Fetch(reporter mb.ReporterV2) {
return
}

m.enricher.Enrich(events)

for _, event := range events {
event[mb.NamespaceKey] = "storageclass"
reported := reporter.Event(mb.TransformMapStrToEvent("kubernetes", event, nil))
if !reported {

e, err := util.CreateEvent(event, "kubernetes.storageclass")
if err != nil {
m.Logger().Error(err)
}

if reported := reporter.Event(e); !reported {
m.Logger().Debug("error trying to emit event")
return
}
}
}

// Close stops this metricset
func (m *StorageClassMetricSet) Close() error {
m.enricher.Stop()
return nil
}
2 changes: 2 additions & 0 deletions metricbeat/module/kubernetes/util/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ func NewResourceMetadataEnricher(
m[id] = metaGen.Generate("persistentvolume", r)
case *kubernetes.PersistentVolumeClaim:
m[id] = metaGen.Generate("persistentvolumeclaim", r)
case *kubernetes.StorageClass:
m[id] = metaGen.Generate("storageclass", r)
default:
m[id] = metaGen.Generate(r.GetObjectKind().GroupVersionKind().Kind, r)
}
Expand Down

0 comments on commit d20fff4

Please sign in to comment.