Merge branch 'k8s_sd_metrics' of https://github.com/dominikschulz/prometheus into dominikschulz-k8s_sd_metrics

This commit is contained in:
Fabian Reinartz 2016-11-21 11:44:48 +01:00
commit 06555bde93
5 changed files with 58 additions and 3 deletions

View File

@ -83,6 +83,8 @@ func (e *Endpoints) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
e.endpointsInf.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(o interface{}) {
eventCount.WithLabelValues("endpoints", "add").Inc()
eps, err := convertToEndpoints(o)
if err != nil {
e.logger.With("err", err).Errorln("converting to Endpoints object failed")
@ -91,6 +93,8 @@ func (e *Endpoints) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
send(e.buildEndpoints(eps))
},
UpdateFunc: func(_, o interface{}) {
eventCount.WithLabelValues("endpoints", "update").Inc()
eps, err := convertToEndpoints(o)
if err != nil {
e.logger.With("err", err).Errorln("converting to Endpoints object failed")
@ -99,6 +103,8 @@ func (e *Endpoints) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
send(e.buildEndpoints(eps))
},
DeleteFunc: func(o interface{}) {
eventCount.WithLabelValues("endpoints", "delete").Inc()
eps, err := convertToEndpoints(o)
if err != nil {
e.logger.With("err", err).Errorln("converting to Endpoints object failed")
@ -129,9 +135,18 @@ func (e *Endpoints) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
e.serviceInf.AddEventHandler(cache.ResourceEventHandlerFuncs{
// TODO(fabxc): potentially remove add and delete event handlers. Those should
// be triggered via the endpoint handlers already.
AddFunc: func(o interface{}) { serviceUpdate(o) },
UpdateFunc: func(_, o interface{}) { serviceUpdate(o) },
DeleteFunc: func(o interface{}) { serviceUpdate(o) },
AddFunc: func(o interface{}) {
eventCount.WithLabelValues("service", "add").Inc()
serviceUpdate(o)
},
UpdateFunc: func(_, o interface{}) {
eventCount.WithLabelValues("service", "update").Inc()
serviceUpdate(o)
},
DeleteFunc: func(o interface{}) {
eventCount.WithLabelValues("service", "delete").Inc()
serviceUpdate(o)
},
})
// Block until the target provider is explicitly canceled.

View File

@ -17,6 +17,7 @@ import (
"io/ioutil"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/prometheus/config"
"github.com/prometheus/common/log"
@ -37,6 +38,27 @@ const (
namespaceLabel = metaLabelPrefix + "namespace"
)
var (
eventCount = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "prometheus_sd_kubernetes_events_total",
Help: "The number of Kubernetes events handled.",
},
[]string{"role", "event"},
)
)
func init() {
prometheus.MustRegister(eventCount)
// Initialize metric vectors.
for _, role := range []string{"endpoints", "node", "pod", "service"} {
for _, evt := range []string{"add", "delete", "update"} {
eventCount.WithLabelValues(role, evt)
}
}
}
// Kubernetes implements the TargetProvider interface for discovering
// targets from Kubernetes.
type Kubernetes struct {

View File

@ -63,6 +63,8 @@ func (n *Node) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
}
n.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(o interface{}) {
eventCount.WithLabelValues("node", "add").Inc()
node, err := convertToNode(o)
if err != nil {
n.logger.With("err", err).Errorln("converting to Node object failed")
@ -71,6 +73,8 @@ func (n *Node) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
send(n.buildNode(node))
},
DeleteFunc: func(o interface{}) {
eventCount.WithLabelValues("node", "delete").Inc()
node, err := convertToNode(o)
if err != nil {
n.logger.With("err", err).Errorln("converting to Node object failed")
@ -79,6 +83,8 @@ func (n *Node) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
send(&config.TargetGroup{Source: nodeSource(node)})
},
UpdateFunc: func(_, o interface{}) {
eventCount.WithLabelValues("node", "update").Inc()
node, err := convertToNode(o)
if err != nil {
n.logger.With("err", err).Errorln("converting to Node object failed")

View File

@ -71,6 +71,8 @@ func (p *Pod) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
}
p.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(o interface{}) {
eventCount.WithLabelValues("pod", "add").Inc()
pod, err := convertToPod(o)
if err != nil {
p.logger.With("err", err).Errorln("converting to Pod object failed")
@ -79,6 +81,8 @@ func (p *Pod) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
send(p.buildPod(pod))
},
DeleteFunc: func(o interface{}) {
eventCount.WithLabelValues("pod", "delete").Inc()
pod, err := convertToPod(o)
if err != nil {
p.logger.With("err", err).Errorln("converting to Pod object failed")
@ -87,6 +91,8 @@ func (p *Pod) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
send(&config.TargetGroup{Source: podSource(pod)})
},
UpdateFunc: func(_, o interface{}) {
eventCount.WithLabelValues("pod", "update").Inc()
pod, err := convertToPod(o)
if err != nil {
p.logger.With("err", err).Errorln("converting to Pod object failed")

View File

@ -62,6 +62,8 @@ func (s *Service) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
}
s.informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(o interface{}) {
eventCount.WithLabelValues("service", "add").Inc()
svc, err := convertToService(o)
if err != nil {
s.logger.With("err", err).Errorln("converting to Service object failed")
@ -70,6 +72,8 @@ func (s *Service) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
send(s.buildService(svc))
},
DeleteFunc: func(o interface{}) {
eventCount.WithLabelValues("service", "delete").Inc()
svc, err := convertToService(o)
if err != nil {
s.logger.With("err", err).Errorln("converting to Service object failed")
@ -78,6 +82,8 @@ func (s *Service) Run(ctx context.Context, ch chan<- []*config.TargetGroup) {
send(&config.TargetGroup{Source: serviceSource(svc)})
},
UpdateFunc: func(_, o interface{}) {
eventCount.WithLabelValues("service", "update").Inc()
svc, err := convertToService(o)
if err != nil {
s.logger.With("err", err).Errorln("converting to Service object failed")