Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit e4e63ab

Browse files
committed
etcd-backup-operator: add prometheus-metrics.
Added a few metricts and an exporter to the backup operator. Related to: #2095 Removed gorilla mux
1 parent 8347d27 commit e4e63ab

4 files changed

Lines changed: 70 additions & 12 deletions

File tree

Gopkg.lock

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/backup-operator/main.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,12 @@ package main
1717
import (
1818
"context"
1919
"flag"
20-
"os"
21-
"runtime"
22-
"time"
23-
20+
"fmt"
2421
controller "github.com/coreos/etcd-operator/pkg/controller/backup-operator"
2522
"github.com/coreos/etcd-operator/pkg/util/constants"
2623
"github.com/coreos/etcd-operator/pkg/util/k8sutil"
2724
version "github.com/coreos/etcd-operator/version"
28-
25+
"github.com/prometheus/client_golang/prometheus/promhttp"
2926
"github.com/sirupsen/logrus"
3027
v1 "k8s.io/api/core/v1"
3128
"k8s.io/client-go/kubernetes"
@@ -34,6 +31,10 @@ import (
3431
"k8s.io/client-go/tools/leaderelection"
3532
"k8s.io/client-go/tools/leaderelection/resourcelock"
3633
"k8s.io/client-go/tools/record"
34+
"net/http"
35+
"os"
36+
"runtime"
37+
"time"
3738
)
3839

3940
var (
@@ -81,7 +82,8 @@ func main() {
8182

8283
ctx, cancel := context.WithCancel(context.Background())
8384
defer cancel()
84-
85+
http.Handle("/metrics", promhttp.Handler())
86+
go http.ListenAndServe(fmt.Sprintf(":%d", 9091), router)
8587
leaderelection.RunOrDie(ctx, leaderelection.LeaderElectionConfig{
8688
Lock: rl,
8789
LeaseDuration: 15 * time.Second,

pkg/backup/metrics/metrics.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package metrics
2+
3+
import "github.com/prometheus/client_golang/prometheus"
4+
5+
var (
6+
BackupsAttemptedTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
7+
Namespace: "etcd_operator",
8+
Name: "backups_attempt_total",
9+
Help: "Backups attempt by name and namespace",
10+
},
11+
[]string{"name", "namespace"},
12+
)
13+
14+
BackupsSuccessTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
15+
Namespace: "etcd_operator",
16+
Name: "backups_success_total",
17+
Help: "Backups success by name and namespace",
18+
},
19+
[]string{"name", "namespace"},
20+
)
21+
22+
BackupsLastSuccess = prometheus.NewGaugeVec(prometheus.GaugeOpts{
23+
Namespace: "etcd_operator",
24+
Name: "backup_last_success",
25+
Help: "Timestamp of last successfull backup, by name and namespace",
26+
},
27+
[]string{"name", "namespace"},
28+
)
29+
)
30+
31+
func init() {
32+
prometheus.MustRegister(BackupsAttemptedTotal)
33+
prometheus.MustRegister(BackupsSuccessTotal)
34+
prometheus.MustRegister(BackupsLastSuccess)
35+
}

pkg/controller/backup-operator/sync.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ import (
2121
"time"
2222

2323
api "github.com/coreos/etcd-operator/pkg/apis/etcd/v1beta2"
24+
"github.com/coreos/etcd-operator/pkg/backup/metrics"
2425
"github.com/coreos/etcd-operator/pkg/util/constants"
25-
26+
"github.com/prometheus/client_golang/prometheus"
2627
"github.com/sirupsen/logrus"
2728
apierrors "k8s.io/apimachinery/pkg/api/errors"
2829
"k8s.io/apimachinery/pkg/api/meta"
@@ -101,14 +102,17 @@ func (b *Backup) processItem(key string) error {
101102
ctx := context.Background()
102103
ctx, cancel := context.WithCancel(ctx)
103104
go b.periodicRunnerFunc(ctx, ticker, eb)
104-
105105
// Store cancel function for periodic
106106
b.backupRunnerStore.Store(eb.ObjectMeta.UID, BackupRunner{eb.Spec, cancel})
107107

108108
} else if !isPeriodic {
109109
// Perform backup
110+
metrics.BackupsAttemptedTotal.With(prometheus.Labels(prometheus.Labels{
111+
"namespace": eb.ObjectMeta.Namespace,
112+
"name": eb.ObjectMeta.Name,
113+
})).Inc()
110114
bs, err := b.handleBackup(nil, &eb.Spec, false)
111-
// Report backup status
115+
//status of backup
112116
b.reportBackupStatus(bs, err, eb)
113117
}
114118
return err
@@ -195,9 +199,13 @@ func (b *Backup) periodicRunnerFunc(ctx context.Context, t *time.Ticker, eb *api
195199
}
196200
if err == nil {
197201
// Perform backup
202+
metrics.BackupsAttemptedTotal.With(prometheus.Labels(prometheus.Labels{
203+
"namespace": eb.ObjectMeta.Namespace,
204+
"name": eb.ObjectMeta.Name,
205+
})).Inc()
198206
bs, err = b.handleBackup(&ctx, &latestEb.Spec, true)
199207
}
200-
// Report backup status
208+
//BackupStatus here
201209
b.reportBackupStatus(bs, err, latestEb)
202210
}
203211
}
@@ -213,6 +221,15 @@ func (b *Backup) reportBackupStatus(bs *api.BackupStatus, berr error, eb *api.Et
213221
eb.Status.EtcdRevision = bs.EtcdRevision
214222
eb.Status.EtcdVersion = bs.EtcdVersion
215223
eb.Status.LastSuccessDate = bs.LastSuccessDate
224+
metrics.BackupsSuccessTotal.With(prometheus.Labels(prometheus.Labels{
225+
"namespace": eb.ObjectMeta.Namespace,
226+
"name": eb.ObjectMeta.Name,
227+
})).Inc()
228+
metrics.BackupsLastSuccess.With(prometheus.Labels(prometheus.Labels{
229+
"namespace": eb.ObjectMeta.Namespace,
230+
"name": eb.ObjectMeta.Name,
231+
})).Set(float64(time.Now().Unix()))
232+
216233
}
217234
_, err := b.backupCRCli.EtcdV1beta2().EtcdBackups(b.namespace).Update(eb)
218235
if err != nil {
@@ -249,7 +266,6 @@ func (b *Backup) handleBackup(parentContext *context.Context, spec *api.BackupSp
249266
if err != nil {
250267
return nil, err
251268
}
252-
253269
// When BackupPolicy.TimeoutInSecond <= 0, use default DefaultBackupTimeout.
254270
backupTimeout := time.Duration(constants.DefaultBackupTimeout)
255271
if spec.BackupPolicy != nil && spec.BackupPolicy.TimeoutInSecond > 0 {

0 commit comments

Comments
 (0)