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

Making kube-state-metrics a library #575

Merged
merged 9 commits into from
Nov 2, 2018
40 changes: 20 additions & 20 deletions pkg/collectors/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,70 +124,70 @@ func (b *Builder) buildPodCollector() *Collector {
store := metricsstore.NewMetricsStore(genFunc)
reflectorPerNamespace(b.ctx, b.kubeClient, &v1.Pod{}, store, b.namespaces, createPodListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildCronJobCollector() *Collector {
store := metricsstore.NewMetricsStore(generateCronJobMetrics)
reflectorPerNamespace(b.ctx, b.kubeClient, &batchv1beta1.CronJob{}, store, b.namespaces, createCronJobListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildConfigMapCollector() *Collector {
store := metricsstore.NewMetricsStore(generateConfigMapMetrics)
reflectorPerNamespace(b.ctx, b.kubeClient, &v1.ConfigMap{}, store, b.namespaces, createConfigMapListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildDaemonSetCollector() *Collector {
store := metricsstore.NewMetricsStore(generateDaemonSetMetrics)
reflectorPerNamespace(b.ctx, b.kubeClient, &extensions.DaemonSet{}, store, b.namespaces, createDaemonSetListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildDeploymentCollector() *Collector {
store := metricsstore.NewMetricsStore(generateDeploymentMetrics)
reflectorPerNamespace(b.ctx, b.kubeClient, &extensions.Deployment{}, store, b.namespaces, createDeploymentListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildEndpointsCollector() *Collector {
store := metricsstore.NewMetricsStore(generateEndpointsMetrics)
reflectorPerNamespace(b.ctx, b.kubeClient, &v1.Endpoints{}, store, b.namespaces, createEndpointsListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildHPACollector() *Collector {
store := metricsstore.NewMetricsStore(generateHPAMetrics)
reflectorPerNamespace(b.ctx, b.kubeClient, &autoscaling.HorizontalPodAutoscaler{}, store, b.namespaces, createHPAListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildJobCollector() *Collector {
store := metricsstore.NewMetricsStore(generateJobMetrics)
reflectorPerNamespace(b.ctx, b.kubeClient, &batchv1.Job{}, store, b.namespaces, createJobListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildLimitRangeCollector() *Collector {
store := metricsstore.NewMetricsStore(generateLimitRangeMetrics)
reflectorPerNamespace(b.ctx, b.kubeClient, &v1.LimitRange{}, store, b.namespaces, createLimitRangeListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildNamespaceCollector() *Collector {
store := metricsstore.NewMetricsStore(generateNamespaceMetrics)
reflectorPerNamespace(b.ctx, b.kubeClient, &v1.Namespace{}, store, b.namespaces, createNamespaceListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildNodeCollector() *Collector {
Expand All @@ -197,70 +197,70 @@ func (b *Builder) buildNodeCollector() *Collector {
store := metricsstore.NewMetricsStore(genFunc)
reflectorPerNamespace(b.ctx, b.kubeClient, &v1.Node{}, store, b.namespaces, createNodeListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildPersistentVolumeCollector() *Collector {
store := metricsstore.NewMetricsStore(generatePersistentVolumeMetrics)
reflectorPerNamespace(b.ctx, b.kubeClient, &v1.PersistentVolume{}, store, b.namespaces, createPersistentVolumeListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildPersistentVolumeClaimCollector() *Collector {
store := metricsstore.NewMetricsStore(generatePersistentVolumeClaimMetrics)
reflectorPerNamespace(b.ctx, b.kubeClient, &v1.PersistentVolumeClaim{}, store, b.namespaces, createPersistentVolumeClaimListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildPodDisruptionBudgetCollector() *Collector {
store := metricsstore.NewMetricsStore(generatePodDisruptionBudgetMetrics)
reflectorPerNamespace(b.ctx, b.kubeClient, &v1beta1.PodDisruptionBudget{}, store, b.namespaces, createPodDisruptionBudgetListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildReplicaSetCollector() *Collector {
store := metricsstore.NewMetricsStore(generateReplicaSetMetrics)
reflectorPerNamespace(b.ctx, b.kubeClient, &extensions.ReplicaSet{}, store, b.namespaces, createReplicaSetListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildReplicationControllerCollector() *Collector {
store := metricsstore.NewMetricsStore(generateReplicationControllerMetrics)
reflectorPerNamespace(b.ctx, b.kubeClient, &v1.ReplicationController{}, store, b.namespaces, createReplicationControllerListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildResourceQuotaCollector() *Collector {
store := metricsstore.NewMetricsStore(generateResourceQuotaMetrics)
reflectorPerNamespace(b.ctx, b.kubeClient, &v1.ResourceQuota{}, store, b.namespaces, createResourceQuotaListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildSecretCollector() *Collector {
store := metricsstore.NewMetricsStore(generateSecretMetrics)
reflectorPerNamespace(b.ctx, b.kubeClient, &v1.Secret{}, store, b.namespaces, createSecretListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildServiceCollector() *Collector {
store := metricsstore.NewMetricsStore(generateServiceMetrics)
reflectorPerNamespace(b.ctx, b.kubeClient, &v1.Service{}, store, b.namespaces, createServiceListWatch)

return newCollector(store)
return NewCollector(store)
}

func (b *Builder) buildStatefulSetCollector() *Collector {
store := metricsstore.NewMetricsStore(generateStatefulSetMetrics)
reflectorPerNamespace(b.ctx, b.kubeClient, &apps.StatefulSet{}, store, b.namespaces, createStatefulSetListWatch)

return newCollector(store)
return NewCollector(store)
}

func reflectorPerNamespace(
Expand Down
107 changes: 4 additions & 103 deletions pkg/collectors/collectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,123 +17,24 @@ limitations under the License.
package collectors

import (
"time"

"regexp"

"github.com/prometheus/client_golang/prometheus"
"k8s.io/api/core/v1"
"k8s.io/kube-state-metrics/pkg/metrics"
)

var (
resyncPeriod = 5 * time.Minute

ScrapeErrorTotalMetric = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "ksm_scrape_error_total",
Help: "Total scrape errors encountered when scraping a resource",
},
[]string{"resource"},
)

ResourcesPerScrapeMetric = prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Name: "ksm_resources_per_scrape",
Help: "Number of resources returned per scrape",
},
[]string{"resource"},
)

invalidLabelCharRE = regexp.MustCompile(`[^a-zA-Z0-9_]`)
)

type store interface {
type Store interface {
GetAll() []*metrics.Metric
}

// Collector represents a kube-state-metrics metric collector. It is stripped
// down version of the Prometheus client_golang collector.
type Collector struct {
store store
Store Store
}

func newCollector(s store) *Collector {
func NewCollector(s Store) *Collector {
return &Collector{s}
}

// Collect returns all metrics of the underlying store of the collector.
func (c *Collector) Collect() []*metrics.Metric {
return c.store.GetAll()
}

func newMetricFamilyDef(name, help string, labelKeys []string, constLabels prometheus.Labels) *metricFamilyDef {
return &metricFamilyDef{name, help, labelKeys, constLabels}
}

// metricFamilyDef represents a metric family definition
type metricFamilyDef struct {
Name string
Help string
LabelKeys []string
ConstLabels prometheus.Labels
}

func boolFloat64(b bool) float64 {
if b {
return 1
}
return 0
}

// addConditionMetrics generates one metric for each possible node condition
// status. For this function to work properly, the last label in the metric
// description must be the condition.
func addConditionMetrics(desc *metricFamilyDef, cs v1.ConditionStatus, lv ...string) []*metrics.Metric {
ms := []*metrics.Metric{}
m, err := metrics.NewMetric(desc.Name, desc.LabelKeys, append(lv, "true"), boolFloat64(cs == v1.ConditionTrue))
if err != nil {
panic(err)
}
ms = append(ms, m)
m, err = metrics.NewMetric(desc.Name, desc.LabelKeys, append(lv, "false"), boolFloat64(cs == v1.ConditionFalse))
if err != nil {
panic(err)
}
ms = append(ms, m)
m, err = metrics.NewMetric(desc.Name, desc.LabelKeys, append(lv, "unknown"), boolFloat64(cs == v1.ConditionUnknown))
if err != nil {
panic(err)
}
ms = append(ms, m)

return ms
}

func kubeLabelsToPrometheusLabels(labels map[string]string) ([]string, []string) {
labelKeys := make([]string, len(labels))
labelValues := make([]string, len(labels))
i := 0
for k, v := range labels {
labelKeys[i] = "label_" + sanitizeLabelName(k)
labelValues[i] = v
i++
}
return labelKeys, labelValues
}

func kubeAnnotationsToPrometheusAnnotations(annotations map[string]string) ([]string, []string) {
annotationKeys := make([]string, len(annotations))
annotationValues := make([]string, len(annotations))
i := 0
for k, v := range annotations {
annotationKeys[i] = "annotation_" + sanitizeLabelName(k)
annotationValues[i] = v
i++
}
return annotationKeys, annotationValues
}

func sanitizeLabelName(s string) string {
return invalidLabelCharRE.ReplaceAllString(s, "_")
return c.Store.GetAll()
}
10 changes: 5 additions & 5 deletions pkg/collectors/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ import (
var (
descConfigMapLabelsDefaultLabels = []string{"namespace", "configmap"}

descConfigMapInfo = newMetricFamilyDef(
descConfigMapInfo = metrics.NewMetricFamilyDef(
"kube_configmap_info",
"Information about configmap.",
descConfigMapLabelsDefaultLabels,
nil,
)

descConfigMapCreated = newMetricFamilyDef(
descConfigMapCreated = metrics.NewMetricFamilyDef(
"kube_configmap_created",
"Unix creation timestamp",
descConfigMapLabelsDefaultLabels,
nil,
)

descConfigMapMetadataResourceVersion = newMetricFamilyDef(
descConfigMapMetadataResourceVersion = metrics.NewMetricFamilyDef(
"kube_configmap_metadata_resource_version",
"Resource version representing a specific version of the configmap.",
append(descConfigMapLabelsDefaultLabels, "resource_version"),
Expand All @@ -70,7 +70,7 @@ func generateConfigMapMetrics(obj interface{}) []*metrics.Metric {
mPointer := obj.(*v1.ConfigMap)
m := *mPointer

addConstMetric := func(desc *metricFamilyDef, v float64, lv ...string) {
addConstMetric := func(desc *metrics.MetricFamilyDef, v float64, lv ...string) {
lv = append([]string{m.Namespace, m.Name}, lv...)

m, err := metrics.NewMetric(desc.Name, desc.LabelKeys, lv, v)
Expand All @@ -80,7 +80,7 @@ func generateConfigMapMetrics(obj interface{}) []*metrics.Metric {

ms = append(ms, m)
}
addGauge := func(desc *metricFamilyDef, v float64, lv ...string) {
addGauge := func(desc *metrics.MetricFamilyDef, v float64, lv ...string) {
addConstMetric(desc, v, lv...)
}
addGauge(descConfigMapInfo, 1)
Expand Down
Loading