Browse Source

Fold release.Observer interface into release.Changes

Sam Broughton 3 years ago
parent
commit
043a755676
4 changed files with 26 additions and 17 deletions
  1. 9
    10
      release/releaser.go
  2. 4
    0
      update/automated.go
  3. 11
    0
      update/metrics.go
  4. 2
    7
      update/release.go

+ 9
- 10
release/releaser.go View File

@@ -11,19 +11,19 @@ import (
11 11
 type Changes interface {
12 12
 	CalculateRelease(update.ReleaseContext, log.Logger) ([]*update.ServiceUpdate, update.Result, error)
13 13
 	ReleaseKind() update.ReleaseKind
14
+	ReleaseType() update.ReleaseType
14 15
 	CommitMessage() string
15 16
 }
16 17
 
17
-type Observer interface {
18
-	Observe(time.Time, error)
19
-}
20
-
21 18
 func Release(rc *ReleaseContext, changes Changes, logger log.Logger) (results update.Result, err error) {
22
-	if o, ok := changes.(Observer); ok {
23
-		defer func(start time.Time) {
24
-			o.Observe(start, err)
25
-		}(time.Now())
26
-	}
19
+	defer func(start time.Time) {
20
+		update.ObserveRelease(
21
+			start,
22
+			err == nil,
23
+			changes.ReleaseType(),
24
+			changes.ReleaseKind(),
25
+		)
26
+	}(time.Now())
27 27
 
28 28
 	logger = log.NewContext(logger).With("type", "release")
29 29
 
@@ -34,7 +34,6 @@ func Release(rc *ReleaseContext, changes Changes, logger log.Logger) (results up
34 34
 
35 35
 	err = ApplyChanges(rc, updates, logger)
36 36
 	return results, err
37
-
38 37
 }
39 38
 
40 39
 func ApplyChanges(rc *ReleaseContext, updates []*update.ServiceUpdate, logger log.Logger) error {

+ 4
- 0
update/automated.go View File

@@ -42,6 +42,10 @@ func (a *Automated) CalculateRelease(rc ReleaseContext, logger log.Logger) ([]*S
42 42
 	return updates, result, err
43 43
 }
44 44
 
45
+func (a *Automated) ReleaseType() ReleaseType {
46
+	return "automated"
47
+}
48
+
45 49
 func (a *Automated) ReleaseKind() ReleaseKind {
46 50
 	return ReleaseKindExecute
47 51
 }

+ 11
- 0
update/metrics.go View File

@@ -1,6 +1,9 @@
1 1
 package update
2 2
 
3 3
 import (
4
+	"fmt"
5
+	"time"
6
+
4 7
 	"github.com/go-kit/kit/metrics"
5 8
 	"github.com/go-kit/kit/metrics/prometheus"
6 9
 	stdprometheus "github.com/prometheus/client_golang/prometheus"
@@ -27,3 +30,11 @@ var (
27 30
 func NewStageTimer(stage string) *metrics.Timer {
28 31
 	return metrics.NewTimer(stageDuration.With(fluxmetrics.LabelStage, stage))
29 32
 }
33
+
34
+func ObserveRelease(start time.Time, success bool, releaseType ReleaseType, releaseKind ReleaseKind) {
35
+	releaseDuration.With(
36
+		fluxmetrics.LabelSuccess, fmt.Sprint(success),
37
+		fluxmetrics.LabelReleaseType, string(releaseType),
38
+		fluxmetrics.LabelReleaseKind, string(releaseKind),
39
+	).Observe(time.Since(start).Seconds())
40
+}

+ 2
- 7
update/release.go View File

@@ -10,7 +10,6 @@ import (
10 10
 	"github.com/go-kit/kit/log"
11 11
 	"github.com/weaveworks/flux"
12 12
 	"github.com/weaveworks/flux/cluster"
13
-	fluxmetrics "github.com/weaveworks/flux/metrics"
14 13
 	"github.com/weaveworks/flux/policy"
15 14
 	"github.com/weaveworks/flux/registry"
16 15
 )
@@ -27,6 +26,7 @@ var (
27 26
 
28 27
 // ReleaseKind says whether a release is to be planned only, or planned then executed
29 28
 type ReleaseKind string
29
+type ReleaseType string
30 30
 
31 31
 const (
32 32
 	ReleaseKindPlan    ReleaseKind = "plan"
@@ -64,7 +64,7 @@ type ReleaseSpec struct {
64 64
 
65 65
 // ReleaseType gives a one-word description of the release, mainly
66 66
 // useful for labelling metrics or log messages.
67
-func (s ReleaseSpec) ReleaseType() string {
67
+func (s ReleaseSpec) ReleaseType() ReleaseType {
68 68
 	switch {
69 69
 	case s.ImageSpec == ImageSpecLatest:
70 70
 		return "latest_images"
@@ -305,11 +305,6 @@ func (s ReleaseSpec) calculateImageUpdates(rc ReleaseContext, candidates []*Serv
305 305
 }
306 306
 
307 307
 func (s ReleaseSpec) Observe(start time.Time, err error) {
308
-	releaseDuration.With(
309
-		fluxmetrics.LabelSuccess, fmt.Sprint(err == nil),
310
-		fluxmetrics.LabelReleaseType, s.ReleaseType(),
311
-		fluxmetrics.LabelReleaseKind, string(s.Kind),
312
-	).Observe(time.Since(start).Seconds())
313 308
 }
314 309
 
315 310
 type ServiceSpec string // ServiceID or "<all>"

Loading…
Cancel
Save