From e493f2b52886d5c6f320b2d3f41209cbfe1ee41a Mon Sep 17 00:00:00 2001 From: Igor Zolotarev Date: Tue, 16 Mar 2021 19:14:56 +0300 Subject: [PATCH 1/3] ipairs instead of pairs in histogram --- CHANGELOG.md | 3 +++ metrics/collectors/histogram.lua | 11 +++++------ test/collectors/histogram_test.lua | 8 ++++++++ 3 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 test/collectors/histogram_test.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index c3a28d34..8a7e68f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - New default metrics: cpu_user_time, cpu_system_time - Vinyl metrics +### Fixed +- `ipairs` instead of `pairs` while iteration in `histogram` + ## [0.7.1] - 2021-03-18 ### Added - zone label support for Tarantool Cartridge >= '2.4.0' diff --git a/metrics/collectors/histogram.lua b/metrics/collectors/histogram.lua index e575a3c4..6e7bf30a 100644 --- a/metrics/collectors/histogram.lua +++ b/metrics/collectors/histogram.lua @@ -9,8 +9,7 @@ local Histogram = Shared:new_class('histogram', {'observe_latency'}) function Histogram.check_buckets(buckets) local prev = -math.huge - for k, v in pairs(buckets) do - if type(k) ~= 'number' then return false end + for _, v in ipairs(buckets) do if type(v) ~= 'number' then return false end if v <= 0 then return false end if prev > v then return false end @@ -48,7 +47,7 @@ function Histogram:observe(num, label_pairs) self.count_collector:inc(1, label_pairs) self.sum_collector:inc(num, label_pairs) - for _, bucket in pairs(self.buckets) do + for _, bucket in ipairs(self.buckets) do local bkt_label_pairs = table.deepcopy(label_pairs) bkt_label_pairs.le = bucket @@ -64,13 +63,13 @@ end function Histogram:collect() local result = {} - for _, obs in pairs(self.count_collector:collect()) do + for _, obs in ipairs(self.count_collector:collect()) do table.insert(result, obs) end - for _, obs in pairs(self.sum_collector:collect()) do + for _, obs in ipairs(self.sum_collector:collect()) do table.insert(result, obs) end - for _, obs in pairs(self.bucket_collector:collect()) do + for _, obs in ipairs(self.bucket_collector:collect()) do table.insert(result, obs) end return result diff --git a/test/collectors/histogram_test.lua b/test/collectors/histogram_test.lua new file mode 100644 index 00000000..2505c9b1 --- /dev/null +++ b/test/collectors/histogram_test.lua @@ -0,0 +1,8 @@ +local t = require('luatest') +local g = t.group() + +local metrics = require('metrics') + +g.test_unsorted_buckets_error = function() + t.assert_error_msg_contains('Invalid value for objectives', metrics.summary, 'latency', nil, {0.9, 0.5}) +end From 4b3576dfe06bad78f20375bd183dc00f2558fbd4 Mon Sep 17 00:00:00 2001 From: Igor Zolotarev Date: Wed, 26 May 2021 17:26:51 +0300 Subject: [PATCH 2/3] updated changelog and test --- CHANGELOG.md | 4 +--- test/collectors/histogram_test.lua | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a7e68f4..99227c28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - metrics registry refactoring to search with `O(1)` [#188](https://github.com/tarantool/metrics/issues/188) +- `ipairs` instead of `pairs` while iteration in `histogram` [#196](https://github.com/tarantool/metrics/issues/196) ### Fixed - be gentle to http routes, don't leave gaps in the array @@ -30,9 +31,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - New default metrics: cpu_user_time, cpu_system_time - Vinyl metrics -### Fixed -- `ipairs` instead of `pairs` while iteration in `histogram` - ## [0.7.1] - 2021-03-18 ### Added - zone label support for Tarantool Cartridge >= '2.4.0' diff --git a/test/collectors/histogram_test.lua b/test/collectors/histogram_test.lua index 2505c9b1..d6237919 100644 --- a/test/collectors/histogram_test.lua +++ b/test/collectors/histogram_test.lua @@ -4,5 +4,5 @@ local g = t.group() local metrics = require('metrics') g.test_unsorted_buckets_error = function() - t.assert_error_msg_contains('Invalid value for objectives', metrics.summary, 'latency', nil, {0.9, 0.5}) + t.assert_error_msg_contains('Invalid value for objectives', metrics.histogram, 'latency', nil, {0.9, 0.5}) end From 428ac3039529b6227927d5d0631581513dc0a24f Mon Sep 17 00:00:00 2001 From: Igor Zolotarev Date: Wed, 26 May 2021 17:38:36 +0300 Subject: [PATCH 3/3] Fix error message in test --- test/collectors/histogram_test.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/collectors/histogram_test.lua b/test/collectors/histogram_test.lua index d6237919..6b9e92c8 100644 --- a/test/collectors/histogram_test.lua +++ b/test/collectors/histogram_test.lua @@ -4,5 +4,5 @@ local g = t.group() local metrics = require('metrics') g.test_unsorted_buckets_error = function() - t.assert_error_msg_contains('Invalid value for objectives', metrics.histogram, 'latency', nil, {0.9, 0.5}) + t.assert_error_msg_contains('Invalid value for buckets', metrics.histogram, 'latency', nil, {0.9, 0.5}) end