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

Add prometheus metrics #102

Closed
wants to merge 5 commits into from
Closed
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
6 changes: 3 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

[[constraint]]
name = "github.com/prometheus/client_golang"
version = "0.8.0"
revision = "3653aff4d509dd87a6ba41b82d57c0c662733e14"

[[override]]
name = "github.com/golang/protobuf"
Expand Down
16 changes: 12 additions & 4 deletions chaoskube/chaoskube.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ import (
"math/rand"
"time"

"github.com/linki/chaoskube/metrics"
"github.com/linki/chaoskube/util"
log "github.com/sirupsen/logrus"

"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/client-go/kubernetes"

"github.com/linki/chaoskube/util"
)

// Chaoskube represents an instance of chaoskube
Expand Down Expand Up @@ -87,8 +86,13 @@ func New(client kubernetes.Interface, labels, annotations, namespaces labels.Sel
// described by channel next. It returns when the given context is canceled.
func (c *Chaoskube) Run(ctx context.Context, next <-chan time.Time) {
for {
metrics.RunCounter.Inc()

if err := c.TerminateVictim(); err != nil {
metrics.ErrorCounter.Inc()
c.Logger.WithField("err", err).Error("failed to terminate victim")
} else {
metrics.PodsDeletedCounter.Inc()
}

c.Logger.Debug("sleeping...")
Expand Down Expand Up @@ -191,7 +195,11 @@ func (c *Chaoskube) DeletePod(victim v1.Pod) error {
return nil
}

return c.Client.CoreV1().Pods(victim.Namespace).Delete(victim.Name, nil)
start := time.Now()
e := c.Client.CoreV1().Pods(victim.Namespace).Delete(victim.Name, nil)
metrics.TerminationHistogram.Observe(time.Since(start).Seconds())

return e
}

// filterByNamespaces filters a list of pods by a given namespace selector.
Expand Down
29 changes: 29 additions & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package metrics

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

var (
// PodsDeletedCounter is the pods deleted counter
PodsDeletedCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "pods_deleted",
Help: "The total number of pods deleted",
})
// RunCounter is the run function executions counter
RunCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "run_counts",
Help: "The total number of pod termination logic runs",
})
// ErrorCounter is the run function executions counter
ErrorCounter = promauto.NewCounter(prometheus.CounterOpts{
Name: "termination_errors",
Help: "The total number of errors on terminate victim operation",
})
// errorCounter is the run function executions counter
TerminationHistogram = promauto.NewHistogram(prometheus.HistogramOpts{
Name: "termination_time_seconds",
Help: "The time took single pod termination to finish",
})
)