Skip to content

Commit

Permalink
Merge pull request #127 from weaveworks/metrics
Browse files Browse the repository at this point in the history
 Refactor Prometheus recorder
  • Loading branch information
stefanprodan authored Mar 28, 2019
2 parents fbdf38e + 9a5529a commit b945b37
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
1 change: 1 addition & 0 deletions cmd/flagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func main() {
logger,
slack,
meshProvider,
version.VERSION,
)

flaggerInformerFactory.Start(stopCh)
Expand Down
3 changes: 3 additions & 0 deletions docs/gitbook/usage/monitoring.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ Flagger exposes Prometheus metrics that can be used to determine the canary anal
the destination weight values:

```bash
# Flagger version and mesh provider gauge
flagger_info{version="0.10.0", mesh_provider="istio"} 1

# Canaries total gauge
flagger_canary_total{namespace="test"} 1

Expand Down
8 changes: 5 additions & 3 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"fmt"
"github.com/weaveworks/flagger/pkg/metrics"
"sync"
"time"

Expand Down Expand Up @@ -42,7 +43,7 @@ type Controller struct {
jobs map[string]CanaryJob
deployer CanaryDeployer
observer CanaryObserver
recorder CanaryRecorder
recorder metrics.CanaryRecorder
notifier *notifier.Slack
meshProvider string
}
Expand All @@ -57,7 +58,7 @@ func NewController(
logger *zap.SugaredLogger,
notifier *notifier.Slack,
meshProvider string,

version string,
) *Controller {
logger.Debug("Creating event broadcaster")
flaggerscheme.AddToScheme(scheme.Scheme)
Expand All @@ -84,7 +85,8 @@ func NewController(
metricsServer: metricServer,
}

recorder := NewCanaryRecorder(true)
recorder := metrics.NewCanaryRecorder(controllerAgentName, true)
recorder.SetInfo(version, meshProvider)

ctrl := &Controller{
kubeClient: kubeClient,
Expand Down
3 changes: 2 additions & 1 deletion pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
fakeFlagger "github.com/weaveworks/flagger/pkg/client/clientset/versioned/fake"
informers "github.com/weaveworks/flagger/pkg/client/informers/externalversions"
"github.com/weaveworks/flagger/pkg/logging"
"github.com/weaveworks/flagger/pkg/metrics"
"github.com/weaveworks/flagger/pkg/router"
"go.uber.org/zap"
appsv1 "k8s.io/api/apps/v1"
Expand Down Expand Up @@ -94,7 +95,7 @@ func SetupMocks(abtest bool) Mocks {
flaggerWindow: time.Second,
deployer: deployer,
observer: observer,
recorder: NewCanaryRecorder(false),
recorder: metrics.NewCanaryRecorder(controllerAgentName, false),
}
ctrl.flaggerSynced = alwaysReady

Expand Down
1 change: 1 addition & 0 deletions pkg/controller/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func (c *Controller) advanceCanary(name string, namespace string, skipLivenessCh
}

if !shouldAdvance {
c.recorder.SetStatus(cd, cd.Status.Phase)
return
}

Expand Down
26 changes: 20 additions & 6 deletions pkg/controller/recorder.go → pkg/metrics/recorder.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controller
package metrics

import (
"fmt"
Expand All @@ -10,55 +10,69 @@ import (

// CanaryRecorder records the canary analysis as Prometheus metrics
type CanaryRecorder struct {
info *prometheus.GaugeVec
duration *prometheus.HistogramVec
total *prometheus.GaugeVec
status *prometheus.GaugeVec
weight *prometheus.GaugeVec
}

// NewCanaryRecorder creates a new recorder and registers the Prometheus metrics
func NewCanaryRecorder(register bool) CanaryRecorder {
func NewCanaryRecorder(controller string, register bool) CanaryRecorder {
info := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Subsystem: controller,
Name: "info",
Help: "Flagger version and mesh provider information",
}, []string{"version", "mesh_provider"})

duration := prometheus.NewHistogramVec(prometheus.HistogramOpts{
Subsystem: controllerAgentName,
Subsystem: controller,
Name: "canary_duration_seconds",
Help: "Seconds spent performing canary analysis.",
Buckets: prometheus.DefBuckets,
}, []string{"name", "namespace"})

total := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Subsystem: controllerAgentName,
Subsystem: controller,
Name: "canary_total",
Help: "Total number of canary object",
}, []string{"namespace"})

// 0 - running, 1 - successful, 2 - failed
status := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Subsystem: controllerAgentName,
Subsystem: controller,
Name: "canary_status",
Help: "Last canary analysis result",
}, []string{"name", "namespace"})

weight := prometheus.NewGaugeVec(prometheus.GaugeOpts{
Subsystem: controllerAgentName,
Subsystem: controller,
Name: "canary_weight",
Help: "The virtual service destination weight current value",
}, []string{"workload", "namespace"})

if register {
prometheus.MustRegister(info)
prometheus.MustRegister(duration)
prometheus.MustRegister(total)
prometheus.MustRegister(status)
prometheus.MustRegister(weight)
}

return CanaryRecorder{
info: info,
duration: duration,
total: total,
status: status,
weight: weight,
}
}

// SetInfo sets the version and mesh provider labels
func (cr *CanaryRecorder) SetInfo(version string, meshProvider string) {
cr.info.WithLabelValues(version, meshProvider).Set(1)
}

// SetDuration sets the time spent in seconds performing canary analysis
func (cr *CanaryRecorder) SetDuration(cd *flaggerv1.Canary, duration time.Duration) {
cr.duration.WithLabelValues(cd.Spec.TargetRef.Name, cd.Namespace).Observe(duration.Seconds())
Expand Down

0 comments on commit b945b37

Please sign in to comment.