-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Kubernetes StatefulSet metricset (#6236)
metricbeat: add support for StatefulSet reporting in Kubernetes module
- Loading branch information
1 parent
5782e81
commit 4cbb1de
Showing
16 changed files
with
431 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
metricbeat/docs/modules/kubernetes/state_statefulset.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
//// | ||
This file is generated! See scripts/docs_collector.py | ||
//// | ||
|
||
[[metricbeat-metricset-kubernetes-state_statefulset]] | ||
=== Kubernetes state_statefulset metricset | ||
|
||
include::../../../module/kubernetes/state_statefulset/_meta/docs.asciidoc[] | ||
|
||
|
||
==== Fields | ||
|
||
For a description of each field in the metricset, see the | ||
<<exported-fields-kubernetes,exported fields>> section. | ||
|
||
Here is an example document generated by this metricset: | ||
|
||
[source,json] | ||
---- | ||
include::../../../module/kubernetes/state_statefulset/_meta/data.json[] | ||
---- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
metricbeat/module/kubernetes/state_statefulset/_meta/data.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"@timestamp": "2017-05-10T16:46:37.821Z", | ||
"beat": { | ||
"hostname": "X1", | ||
"name": "X1", | ||
"version": "6.0.0-alpha1" | ||
}, | ||
"kubernetes": { | ||
"namespace": "jenkins", | ||
"statefulset": { | ||
"name": "wise-lynx-jenkins-1616735317", | ||
"created": 123454, | ||
"replicas": { | ||
"desired": 1, | ||
"observed": 1, | ||
}, | ||
"generation": { | ||
"desired": 1, | ||
"observed": 1, | ||
} | ||
} | ||
}, | ||
"metricset": { | ||
"host": "192.168.99.100:18080", | ||
"module": "kubernetes", | ||
"name": "state_statefulset", | ||
"namespace": "statefulset", | ||
"rtt": 6719 | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
metricbeat/module/kubernetes/state_statefulset/_meta/docs.asciidoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is the `state_statefulset` metricset of the Kubernetes module. |
40 changes: 40 additions & 0 deletions
40
metricbeat/module/kubernetes/state_statefulset/_meta/fields.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
- name: statefulset | ||
type: group | ||
description: > | ||
kubernetes stateful set metrics | ||
release: ga | ||
fields: | ||
- name: name | ||
type: keyword | ||
description: > | ||
Kubernetes stateful set name | ||
- name: created | ||
type: long | ||
description: > | ||
The creation timestamp (epoch) for StatefulSet | ||
- name: replicas | ||
type: group | ||
description: > | ||
Kubernetes stateful set replicas status | ||
fields: | ||
- name: observed | ||
type: long | ||
description: > | ||
The number of observed replicas per StatefulSet | ||
- name: desired | ||
type: long | ||
description: > | ||
The number of desired replicas per StatefulSet | ||
- name: generation | ||
type: group | ||
description: > | ||
Kubernetes stateful set generation information | ||
fields: | ||
- name: observed | ||
type: long | ||
description: > | ||
The observed generation per StatefulSet | ||
- name: desired | ||
type: long | ||
description: > | ||
The desired generation per StatefulSet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package state_statefulset | ||
|
||
import ( | ||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/metricbeat/mb" | ||
"github.com/elastic/beats/metricbeat/module/kubernetes/util" | ||
|
||
dto "github.com/prometheus/client_model/go" | ||
) | ||
|
||
func eventMapping(families []*dto.MetricFamily) ([]common.MapStr, error) { | ||
eventsMap := map[string]common.MapStr{} | ||
for _, family := range families { | ||
for _, metric := range family.GetMetric() { | ||
statefulset := util.GetLabel(metric, "statefulset") | ||
if statefulset == "" { | ||
continue | ||
} | ||
namespace := util.GetLabel(metric, "namespace") | ||
statefulsetKey := namespace + "::" + statefulset | ||
event, ok := eventsMap[statefulsetKey] | ||
if !ok { | ||
event = common.MapStr{} | ||
eventsMap[statefulsetKey] = event | ||
} | ||
switch family.GetName() { | ||
case "kube_statefulset_metadata_generation": | ||
event.Put(mb.ModuleDataKey+".namespace", util.GetLabel(metric, "namespace")) | ||
event.Put(mb.NamespaceKey, "statefulset") | ||
event.Put("name", util.GetLabel(metric, "statefulset")) | ||
event.Put("generation.desired", metric.GetGauge().GetValue()) | ||
case "kube_statefulset_status_observed_generation": | ||
event.Put("generation.observed", metric.GetGauge().GetValue()) | ||
case "kube_statefulset_created": | ||
event.Put("created", metric.GetGauge().GetValue()) | ||
case "kube_statefulset_replicas": | ||
event.Put("replicas.desired", metric.GetGauge().GetValue()) | ||
case "kube_statefulset_status_replicas": | ||
event.Put("replicas.observed", metric.GetGauge().GetValue()) | ||
default: | ||
// Ignore unknown metric | ||
continue | ||
} | ||
} | ||
} | ||
|
||
// initialize, populate events array from values in eventsMap | ||
events := make([]common.MapStr, 0, len(eventsMap)) | ||
for _, event := range eventsMap { | ||
events = append(events, event) | ||
} | ||
return events, nil | ||
} |
63 changes: 63 additions & 0 deletions
63
metricbeat/module/kubernetes/state_statefulset/state_statefulset.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package state_statefulset | ||
|
||
import ( | ||
"github.com/elastic/beats/libbeat/common" | ||
"github.com/elastic/beats/metricbeat/helper" | ||
"github.com/elastic/beats/metricbeat/mb" | ||
"github.com/elastic/beats/metricbeat/mb/parse" | ||
) | ||
|
||
const ( | ||
defaultScheme = "http" | ||
defaultPath = "/metrics" | ||
) | ||
|
||
var ( | ||
hostParser = parse.URLHostParserBuilder{ | ||
DefaultScheme: defaultScheme, | ||
DefaultPath: defaultPath, | ||
}.Build() | ||
) | ||
|
||
// init registers the MetricSet with the central registry. | ||
// The New method will be called after the setup of the module and before starting to fetch data | ||
func init() { | ||
if err := mb.Registry.AddMetricSet("kubernetes", "state_statefulset", New, hostParser); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// MetricSet type defines all fields of the MetricSet | ||
// As a minimum it must inherit the mb.BaseMetricSet fields, but can be extended with | ||
// additional entries. These variables can be used to persist data or configuration between | ||
// multiple fetch calls. | ||
type MetricSet struct { | ||
mb.BaseMetricSet | ||
prometheus *helper.Prometheus | ||
} | ||
|
||
// New create a new instance of the MetricSet | ||
// Part of new is also setting up the configuration by processing additional | ||
// configuration entries if needed. | ||
func New(base mb.BaseMetricSet) (mb.MetricSet, error) { | ||
prometheus, err := helper.NewPrometheusClient(base) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &MetricSet{ | ||
BaseMetricSet: base, | ||
prometheus: prometheus, | ||
}, nil | ||
} | ||
|
||
// Fetch methods implements the data gathering and data conversion to the right format | ||
// It returns the event which is then forward to the output. In case of an error, a | ||
// descriptive error must be returned. | ||
func (m *MetricSet) Fetch() ([]common.MapStr, error) { | ||
families, err := m.prometheus.GetFamilies() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return eventMapping(families) | ||
} |
Oops, something went wrong.