-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new prometheus metrics to follow flag changes in the server (#1204)
* WIP Signed-off-by: Thomas Poignant <[email protected]> * add new metrics to check the lifecycle of the configuration file of your flag Signed-off-by: Thomas Poignant <[email protected]> * Add test for notifier Signed-off-by: Thomas Poignant <[email protected]> * lint fix Signed-off-by: Thomas Poignant <[email protected]> --------- Signed-off-by: Thomas Poignant <[email protected]>
- Loading branch information
1 parent
da42931
commit 3eb9eef
Showing
8 changed files
with
276 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package metric | ||
|
||
import ( | ||
"github.com/thomaspoignant/go-feature-flag/notifier" | ||
) | ||
|
||
type notifierPrometheus struct { | ||
metricsService Metrics | ||
} | ||
|
||
func NewPrometheusNotifier(metricsService Metrics) notifier.Notifier { | ||
return ¬ifierPrometheus{ | ||
metricsService: metricsService, | ||
} | ||
} | ||
|
||
func (n *notifierPrometheus) Notify(diff notifier.DiffCache) error { | ||
if !diff.HasDiff() { | ||
return nil | ||
} | ||
n.metricsService.IncFlagChange() | ||
for flagName := range diff.Deleted { | ||
n.metricsService.IncFlagDeleted(flagName) | ||
} | ||
|
||
for flagName := range diff.Added { | ||
n.metricsService.IncFlagCreated(flagName) | ||
} | ||
|
||
for flagName := range diff.Updated { | ||
n.metricsService.IncFlagUpdated(flagName) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package metric | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/thomaspoignant/go-feature-flag/internal/flag" | ||
"github.com/thomaspoignant/go-feature-flag/notifier" | ||
"testing" | ||
) | ||
|
||
func TestPrometheusNotifier_with_diff(t *testing.T) { | ||
m, err := NewMetrics() | ||
assert.NoError(t, err) | ||
|
||
diff := notifier.DiffCache{ | ||
Deleted: map[string]flag.Flag{ | ||
"test-flag": &flag.InternalFlag{}, | ||
}, | ||
Updated: map[string]notifier.DiffUpdated{ | ||
"test-flag2": { | ||
Before: &flag.InternalFlag{}, | ||
After: &flag.InternalFlag{}, | ||
}, | ||
}, | ||
Added: map[string]flag.Flag{ | ||
"test-flagAdd1": &flag.InternalFlag{}, | ||
"test-flagAdd2": &flag.InternalFlag{}, | ||
"test-flagAdd3": &flag.InternalFlag{}, | ||
}, | ||
} | ||
|
||
n := NewPrometheusNotifier(m) | ||
err = n.Notify(diff) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, 1.0, testutil.ToFloat64(m.flagDeleteCounter)) | ||
assert.Equal(t, 1.0, testutil.ToFloat64(m.flagUpdateCounter)) | ||
assert.Equal(t, 3.0, testutil.ToFloat64(m.flagCreateCounter)) | ||
assert.Equal(t, 1.0, testutil.ToFloat64(m.flagCreateCounterVec.WithLabelValues("test-flagAdd1"))) | ||
assert.Equal(t, 1.0, testutil.ToFloat64(m.flagCreateCounterVec.WithLabelValues("test-flagAdd2"))) | ||
assert.Equal(t, 1.0, testutil.ToFloat64(m.flagCreateCounterVec.WithLabelValues("test-flagAdd3"))) | ||
assert.Equal(t, 1.0, testutil.ToFloat64(m.flagUpdateCounterVec.WithLabelValues("test-flag2"))) | ||
assert.Equal(t, 1.0, testutil.ToFloat64(m.flagDeleteCounterVec.WithLabelValues("test-flag"))) | ||
assert.Equal(t, 1.0, testutil.ToFloat64(m.flagChange)) | ||
} | ||
|
||
func TestPrometheusNotifier_no_diff(t *testing.T) { | ||
m, err := NewMetrics() | ||
assert.NoError(t, err) | ||
|
||
diff := notifier.DiffCache{ | ||
Deleted: map[string]flag.Flag{}, | ||
Updated: map[string]notifier.DiffUpdated{}, | ||
Added: map[string]flag.Flag{}, | ||
} | ||
|
||
n := NewPrometheusNotifier(m) | ||
err = n.Notify(diff) | ||
assert.NoError(t, err) | ||
assert.Equal(t, 0.0, testutil.ToFloat64(m.flagDeleteCounter)) | ||
assert.Equal(t, 0.0, testutil.ToFloat64(m.flagUpdateCounter)) | ||
assert.Equal(t, 0.0, testutil.ToFloat64(m.flagCreateCounter)) | ||
assert.Equal(t, 0.0, testutil.ToFloat64(m.flagCreateCounterVec.WithLabelValues("test-flagAdd1"))) | ||
assert.Equal(t, 0.0, testutil.ToFloat64(m.flagCreateCounterVec.WithLabelValues("test-flagAdd2"))) | ||
assert.Equal(t, 0.0, testutil.ToFloat64(m.flagCreateCounterVec.WithLabelValues("test-flagAdd3"))) | ||
assert.Equal(t, 0.0, testutil.ToFloat64(m.flagUpdateCounterVec.WithLabelValues("test-flag2"))) | ||
assert.Equal(t, 0.0, testutil.ToFloat64(m.flagDeleteCounterVec.WithLabelValues("test-flag"))) | ||
assert.Equal(t, 0.0, testutil.ToFloat64(m.flagChange)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters