From 5452f063935185d32c2fffcdbcf1f3decf1db3da Mon Sep 17 00:00:00 2001 From: zouyee Date: Mon, 4 Dec 2017 20:57:19 +0800 Subject: [PATCH] add componentstatus metrics --- Documentation/README.md | 1 + Documentation/componentstatus-metrics.md | 5 ++ collectors/componentstatus.go | 103 +++++++++++++++++++++++ collectors/componentstatus_test.go | 83 ++++++++++++++++++ main.go | 2 + 5 files changed, 194 insertions(+) create mode 100644 Documentation/componentstatus-metrics.md create mode 100644 collectors/componentstatus.go create mode 100644 collectors/componentstatus_test.go diff --git a/Documentation/README.md b/Documentation/README.md index 17c1b6c9..17a18e96 100644 --- a/Documentation/README.md +++ b/Documentation/README.md @@ -6,6 +6,7 @@ Any contribution to improving this documentation or adding sample usages will be Per group of metrics there is one file for each metrics. See each file for specific documentation about the exposed metrics: +* [ComponentStatus Metrics](componentstatus-metrics.md) * [CronJob Metrics](cronjob-metrics.md) * [DaemonSet Metrics](daemonset-metrics.md) * [Deployment Metrics](deployment-metrics.md) diff --git a/Documentation/componentstatus-metrics.md b/Documentation/componentstatus-metrics.md new file mode 100644 index 00000000..90f18e65 --- /dev/null +++ b/Documentation/componentstatus-metrics.md @@ -0,0 +1,5 @@ +# Service Metrics + +| Metric name| Metric type | Labels/tags | +| ---------- | ----------- | ----------- | +| kube_componentstatus_status_healthy | Gauge | `name`=<component-name>
`status`=<True\|False\|Unknow> | diff --git a/collectors/componentstatus.go b/collectors/componentstatus.go new file mode 100644 index 00000000..444e9d39 --- /dev/null +++ b/collectors/componentstatus.go @@ -0,0 +1,103 @@ +/* +Copyright 2017 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package collectors + +import ( + "github.com/golang/glog" + "github.com/prometheus/client_golang/prometheus" + "golang.org/x/net/context" + "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/fields" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/tools/cache" +) + +var ( + descComponentStatusStatusHealthy = prometheus.NewDesc( + "kube_componentstatus_status_healthy", + "kube component status healthy status.", + []string{"name", "status"}, nil, + ) +) + +type ComponentStatusLister func() (v1.ComponentStatusList, error) + +func (csl ComponentStatusLister) List() (v1.ComponentStatusList, error) { + return csl() +} + +func RegisterComponentStatusCollector(registry prometheus.Registerer, kubeClient kubernetes.Interface, namespace string) { + client := kubeClient.CoreV1().RESTClient() + glog.Infof("collect componentstatuses with %s", client.APIVersion()) + slw := cache.NewListWatchFromClient(client, "componentstatuses", v1.NamespaceAll, fields.Everything()) + sinf := cache.NewSharedInformer(slw, &v1.ComponentStatus{}, resyncPeriod) + + componentStatusLister := ComponentStatusLister(func() (componentStatuses v1.ComponentStatusList, err error) { + for _, m := range sinf.GetStore().List() { + componentStatuses.Items = append(componentStatuses.Items, *m.(*v1.ComponentStatus)) + } + return componentStatuses, nil + }) + + registry.MustRegister(&componentStatusCollector{store: componentStatusLister}) + go sinf.Run(context.Background().Done()) +} + +type componentStatusStore interface { + List() (componentStatuses v1.ComponentStatusList, err error) +} + +// componentStatusCollector collects metrics about all components in the cluster. +type componentStatusCollector struct { + store componentStatusStore +} + +// Describe implements the prometheus.Collector interface. +func (csc *componentStatusCollector) Describe(ch chan<- *prometheus.Desc) { + ch <- descComponentStatusStatusHealthy +} + +// Collect implements the prometheus.Collector interface. +func (csc *componentStatusCollector) Collect(ch chan<- prometheus.Metric) { + csl, err := csc.store.List() + if err != nil { + glog.Errorf("listing component status failed: %s", err) + return + } + for _, s := range csl.Items { + csc.collectComponentStatus(ch, s) + } + glog.Infof("collected %d componentstatuses", len(csl.Items)) +} + +func (csc *componentStatusCollector) collectComponentStatus(ch chan<- prometheus.Metric, s v1.ComponentStatus) { + addConstMetric := func(desc *prometheus.Desc, t prometheus.ValueType, v float64, lv ...string) { + lv = append([]string{s.Name}, lv...) + ch <- prometheus.MustNewConstMetric(desc, t, v, lv...) + } + addGauge := func(desc *prometheus.Desc, v float64, lv ...string) { + addConstMetric(desc, prometheus.GaugeValue, v, lv...) + } + for _, p := range s.Conditions { + if p.Type == v1.ComponentHealthy { + addGauge(descComponentStatusStatusHealthy, boolFloat64(p.Status == v1.ConditionTrue), string(v1.ConditionTrue)) + addGauge(descComponentStatusStatusHealthy, boolFloat64(p.Status == v1.ConditionFalse), string(v1.ConditionFalse)) + addGauge(descComponentStatusStatusHealthy, boolFloat64(p.Status == v1.ConditionUnknown), string(v1.ConditionUnknown)) + break + } + } +} diff --git a/collectors/componentstatus_test.go b/collectors/componentstatus_test.go new file mode 100644 index 00000000..e5dc36f1 --- /dev/null +++ b/collectors/componentstatus_test.go @@ -0,0 +1,83 @@ +/* +Copyright 2017 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package collectors + +import ( + "testing" + + "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +type mockcomponentStatusStore struct { + list func() (v1.ComponentStatusList, error) +} + +func (mcs mockcomponentStatusStore) List() (v1.ComponentStatusList, error) { + return mcs.list() +} + +func TestComponentStatusCollector(t *testing.T) { + // Fixed metadata on type and help text. We prepend this to every expected + // output so we only have to modify a single place when doing adjustments. + const metadata = ` + # HELP kube_componentstatus_status_healthy kube component status healthy status. + # TYPE kube_componentstatus_status_healthy gauge + ` + cases := []struct { + cms v1.ComponentStatusList + metrics []string // which metrics should be checked + want string + }{ + // Verify phase enumerations. + { + cms: v1.ComponentStatusList{ + Items: []v1.ComponentStatus{ + { + ObjectMeta: metav1.ObjectMeta{ + Name: "etcd1", + }, + Conditions: []v1.ComponentCondition{ + { + Type: v1.ComponentHealthy, + Status: v1.ConditionTrue, + }, + }, + }, + }, + }, + want: metadata + ` + kube_componentstatus_status_healthy{name="etcd1",status="False"} 0 + kube_componentstatus_status_healthy{name="etcd1",status="True"} 1 + kube_componentstatus_status_healthy{name="etcd1",status="Unknown"} 0 + `, + metrics: []string{"kube_componentstatus_status_healthy"}, + }, + } + for _, c := range cases { + dc := &componentStatusCollector{ + store: &mockcomponentStatusStore{ + list: func() (v1.ComponentStatusList, error) { + return c.cms, nil + }, + }, + } + if err := gatherAndCompare(dc, c.want, c.metrics); err != nil { + t.Errorf("unexpected collecting result:\n%s", err) + } + } +} diff --git a/main.go b/main.go index 528a9351..bf9fb6fb 100644 --- a/main.go +++ b/main.go @@ -47,6 +47,7 @@ const ( var ( defaultCollectors = collectorSet{ + "componentstatuses": struct{}{}, "daemonsets": struct{}{}, "deployments": struct{}{}, "limitranges": struct{}{}, @@ -65,6 +66,7 @@ var ( "horizontalpodautoscalers": struct{}{}, } availableCollectors = map[string]func(registry prometheus.Registerer, kubeClient clientset.Interface, namespace string){ + "componentstatuses": collectors.RegisterComponentStatusCollector, "cronjobs": collectors.RegisterCronJobCollector, "daemonsets": collectors.RegisterDaemonSetCollector, "deployments": collectors.RegisterDeploymentCollector,