Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions pkg/app/piped/livestatestore/kubernetes/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,18 @@
package kubernetes

import (
"strconv"

"github.com/prometheus/client_golang/prometheus"
"k8s.io/client-go/tools/metrics"
)

const (
metricsLabelHost = "host"
metricsLabelMethod = "method"
metricsLabelCode = "code"
metricsLabelHost = "host"
metricsLabelMethod = "method"
metricsLabelCode = "code"
metricsLabelEvent = "event"
metricsLabelEventHandled = "handled"
)

var (
Expand All @@ -37,10 +41,23 @@ var (
metricsLabelCode,
},
)
metricsResourceEvents = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "livestatestore_kubernetes_resource_events_total",
Help: "Number of resource events received from kubernetes server.",
},
[]string{
metricsLabelEvent,
metricsLabelEventHandled,
},
)
)

func registerMetrics() {
prometheus.MustRegister(metricsAPIRequests)
prometheus.MustRegister(
metricsAPIRequests,
metricsResourceEvents,
)

opts := metrics.RegisterOpts{
RequestResult: requestResultCollector{},
Expand All @@ -58,3 +75,10 @@ func (c requestResultCollector) Increment(code string, method string, host strin
metricsLabelCode: code,
}).Inc()
}

func incrementResourceEventCounter(event string, handled bool) {
metricsResourceEvents.With(prometheus.Labels{
metricsLabelEvent: event,
metricsLabelEventHandled: strconv.FormatBool(handled),
}).Inc()
}
15 changes: 12 additions & 3 deletions pkg/app/piped/livestatestore/kubernetes/reflector.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,20 @@ func (r *reflector) onObjectAdd(obj interface{}) {

// Ignore all predefined ones.
if _, ok := ignoreResourceKeys[key.String()]; ok {
incrementResourceEventCounter("add", false)
return
}

// Ignore all objects that are not handled by this piped.
pipedID := u.GetAnnotations()[provider.LabelPiped]
if pipedID != "" && pipedID != r.pipedConfig.PipedID {
incrementResourceEventCounter("add", false)
return
}

r.logger.Info(fmt.Sprintf("received add event for %s", key.String()))
r.logger.Debug(fmt.Sprintf("received add event for %s", key.String()))
r.onAdd(u)
incrementResourceEventCounter("add", true)
}

func (r *reflector) onObjectUpdate(oldObj, obj interface{}) {
Expand All @@ -255,17 +258,20 @@ func (r *reflector) onObjectUpdate(oldObj, obj interface{}) {
// Ignore all predefined ones.
key := provider.MakeResourceKey(u)
if _, ok := ignoreResourceKeys[key.String()]; ok {
incrementResourceEventCounter("update", false)
return
}

// Ignore all objects that are not handled by this piped.
pipedID := u.GetAnnotations()[provider.LabelPiped]
if pipedID != "" && pipedID != r.pipedConfig.PipedID {
incrementResourceEventCounter("update", false)
return
}

r.logger.Info(fmt.Sprintf("received update event for %s", key.String()))
r.logger.Debug(fmt.Sprintf("received update event for %s", key.String()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

r.onUpdate(oldU, u)
incrementResourceEventCounter("update", true)
}

func (r *reflector) onObjectDelete(obj interface{}) {
Expand All @@ -274,17 +280,20 @@ func (r *reflector) onObjectDelete(obj interface{}) {

// Ignore all predefined ones.
if _, ok := ignoreResourceKeys[key.String()]; ok {
incrementResourceEventCounter("delete", false)
return
}

// Ignore all objects that are not handled by this piped.
pipedID := u.GetAnnotations()[provider.LabelPiped]
if pipedID != "" && pipedID != r.pipedConfig.PipedID {
incrementResourceEventCounter("delete", false)
return
}

r.logger.Info(fmt.Sprintf("received delete event for %s", key.String()))
r.logger.Debug(fmt.Sprintf("received delete event for %s", key.String()))
r.onDelete(u)
incrementResourceEventCounter("delete", true)
}

func isSupportedWatch(r metav1.APIResource) bool {
Expand Down