From 64cef4c54cfbe5ddc75ca33cd7308cc52b91d0bd Mon Sep 17 00:00:00 2001 From: Oliver Sand Date: Fri, 25 Sep 2020 19:20:03 +0200 Subject: [PATCH] fix: counter.inc should only allow incrementing by values greather than zero --- prometheus_client/CHANGELOG.md | 6 +++++- prometheus_client/lib/src/prometheus_client/counter.dart | 4 ++-- prometheus_client/pubspec.yaml | 2 +- prometheus_client/test/prometheus_client_counter_test.dart | 6 ++++++ prometheus_client_shelf/CHANGELOG.md | 4 ++++ prometheus_client_shelf/pubspec.yaml | 2 +- 6 files changed, 19 insertions(+), 5 deletions(-) diff --git a/prometheus_client/CHANGELOG.md b/prometheus_client/CHANGELOG.md index f837968..0984f51 100644 --- a/prometheus_client/CHANGELOG.md +++ b/prometheus_client/CHANGELOG.md @@ -1,6 +1,10 @@ +## 0.4.1 + +- `counter.inc()` should only allow to increment by values greather than zero. + ## 0.4.0+4 -- Moved to new org [tentaclelabs](https://github.com/tentaclelabs) +- Moved to new org [tentaclelabs](https://github.com/tentaclelabs). ## 0.4.0+3 diff --git a/prometheus_client/lib/src/prometheus_client/counter.dart b/prometheus_client/lib/src/prometheus_client/counter.dart index a5fd45e..e5ca1d5 100644 --- a/prometheus_client/lib/src/prometheus_client/counter.dart +++ b/prometheus_client/lib/src/prometheus_client/counter.dart @@ -39,9 +39,9 @@ class CounterChild { /// Increment the [value] of the counter with labels by [amount]. /// Increments by one, if no amount is provided. void inc([double amount = 1]) { - if (amount < 0) { + if (amount <= 0) { throw ArgumentError.value( - amount, 'amount', 'Must be greater or equal to zero.'); + amount, 'amount', 'Must be greater than zero.'); } _value += amount; diff --git a/prometheus_client/pubspec.yaml b/prometheus_client/pubspec.yaml index 65f82d2..84f47f0 100644 --- a/prometheus_client/pubspec.yaml +++ b/prometheus_client/pubspec.yaml @@ -1,6 +1,6 @@ name: prometheus_client description: Dart implementation of the Prometheus client library providing metrics. -version: 0.4.0+4 +version: 0.4.1 homepage: https://github.com/tentaclelabs/prometheus_client environment: diff --git a/prometheus_client/test/prometheus_client_counter_test.dart b/prometheus_client/test/prometheus_client_counter_test.dart index 0d86dae..fc95713 100644 --- a/prometheus_client/test/prometheus_client_counter_test.dart +++ b/prometheus_client/test/prometheus_client_counter_test.dart @@ -41,6 +41,12 @@ void main() { expect(() => counter.inc(-42.0), throwsArgumentError); }); + test('Should not increment by zero', () { + final counter = Counter('my_metric', 'Help!'); + + expect(() => counter.inc(0.0), throwsArgumentError); + }); + test('Should not allow to set label values if no labels were specified', () { final counter = Counter('my_metric', 'Help!'); diff --git a/prometheus_client_shelf/CHANGELOG.md b/prometheus_client_shelf/CHANGELOG.md index 19ad0d7..7a7b2a5 100644 --- a/prometheus_client_shelf/CHANGELOG.md +++ b/prometheus_client_shelf/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.4.1 + +No changes + ## 0.4.0+4 - Moved to new org [tentaclelabs](https://github.com/tentaclelabs) diff --git a/prometheus_client_shelf/pubspec.yaml b/prometheus_client_shelf/pubspec.yaml index f55f6ac..fe4343c 100644 --- a/prometheus_client_shelf/pubspec.yaml +++ b/prometheus_client_shelf/pubspec.yaml @@ -1,6 +1,6 @@ name: prometheus_client_shelf description: Dart implementation of the Prometheus client library providing a shelf integration. -version: 0.4.0+4 +version: 0.4.1 homepage: https://github.com/tentaclelabs/prometheus_client environment: