sequra-style 1.16.0 → 1.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a71149d266a3203c44ae7001af44810e2bce7c2d9ab32ec4fc7457a1cee5b7bf
4
- data.tar.gz: 68420dfeac0bb98f32b021b6571b801f0f5f62ee70cd88e8f984a5356079039b
3
+ metadata.gz: c3cb23d52c4f8f874a5aad9ee232ab2b3de754fa4c993fe9a24575072f134d99
4
+ data.tar.gz: 0014151bbc9f819fdedf44f7604f2a47fa7e3e320873e501138f5c75886750fb
5
5
  SHA512:
6
- metadata.gz: 0e4b82aa044144103b651085477c4965e1c69ffe4fc7b550a5141400b0744ba852dc99b07f8b17a3fb3543e6afcfcbe87ebfc36a9a8a82e02bc7f5b841fd422e
7
- data.tar.gz: aed0d7b670c14f6efde676abe0e202a05542ee8bf195053e1abd761ffd10ea6aeda7a3f331be7d70e58732bf8e2cafc170a6cd39b0df43df73bed31a7098212a
6
+ metadata.gz: d9ef0de4f838e0be3c4f2413ab7496aa8fa96baf5eb10026cc01a822112d1e51bd0428032b7505de0ef6f3922f77d840c0c9eda081098dfea559d01d3b6641cb
7
+ data.tar.gz: cf6170ea43e2aa6f3a03da557bb43927ec72f3ac48bbdefa55f7059426803b96f8439b603f3246df45347505ceb7311e0e0091dc3ea120d9b0c64b06469c8aca
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.18.0](https://github.com/sequra/sequra-style/compare/v1.17.0...v1.18.0) (2026-07-28)
4
+
5
+
6
+ ### Features
7
+
8
+ * **rubocop:** add PrometheusMetricLabels cop ([#79](https://github.com/sequra/sequra-style/issues/79)) ([a8f3ec1](https://github.com/sequra/sequra-style/commit/a8f3ec16cdf9779ad391c7032eca2b109892569d))
9
+
10
+ ## [1.17.0](https://github.com/sequra/sequra-style/compare/v1.16.0...v1.17.0) (2026-07-20)
11
+
12
+
13
+ ### Features
14
+
15
+ * **rubocop:** enable Style/OpenStructUse ([#77](https://github.com/sequra/sequra-style/issues/77)) ([713e0a5](https://github.com/sequra/sequra-style/commit/713e0a5e8b45ab3ccfd68a5aab2618a7c9a2bd4e))
16
+
3
17
  ## [1.16.0](https://github.com/sequra/sequra-style/compare/v1.15.0...v1.16.0) (2026-05-20)
4
18
 
5
19
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- sequra-style (1.16.0)
4
+ sequra-style (1.18.0)
5
5
  rubocop (~> 1.75)
6
6
  rubocop-performance (~> 1.25)
7
7
  rubocop-rails (~> 2.31)
@@ -10,7 +10,7 @@ PATH
10
10
  GEM
11
11
  remote: https://rubygems.org/
12
12
  specs:
13
- activesupport (8.1.2)
13
+ activesupport (8.1.3)
14
14
  base64
15
15
  bigdecimal
16
16
  concurrent-ruby (~> 1.0, >= 1.3.1)
@@ -32,7 +32,7 @@ GEM
32
32
  drb (2.2.3)
33
33
  i18n (1.14.8)
34
34
  concurrent-ruby (~> 1.0)
35
- json (2.18.1)
35
+ json (2.19.8)
36
36
  language_server-protocol (3.17.0.5)
37
37
  lint_roller (1.1.0)
38
38
  logger (1.7.0)
@@ -44,7 +44,7 @@ GEM
44
44
  racc
45
45
  prism (1.9.0)
46
46
  racc (1.8.1)
47
- rack (3.2.4)
47
+ rack (3.2.6)
48
48
  rainbow (3.1.1)
49
49
  rake (13.0.6)
50
50
  regexp_parser (2.11.3)
@@ -101,10 +101,10 @@ PLATFORMS
101
101
  ruby
102
102
 
103
103
  DEPENDENCIES
104
- bundler (~> 2.1.4)
104
+ bundler (>= 2.2.33, < 3)
105
105
  rake (~> 13.0.1)
106
106
  rspec (~> 3.13)
107
107
  sequra-style!
108
108
 
109
109
  BUNDLED WITH
110
- 2.1.4
110
+ 2.5.22
data/default.yml CHANGED
@@ -716,6 +716,12 @@ Style/NegatedIf:
716
716
  Style/NestedModifier:
717
717
  Enabled: true
718
718
 
719
+ # Ban OpenStruct usage (allocation-heavy: a singleton class + accessor methods
720
+ # per instance). Prefer Data.define, Struct, a plain class, or test doubles.
721
+ # Grandfather existing usages in your project's .rubocop_todo.yml.
722
+ Style/OpenStructUse:
723
+ Enabled: true
724
+
719
725
  # Define optional arguments at end of parameter list
720
726
  # https://rubystyle.guide/#optional-arguments
721
727
  Style/OptionalArguments:
@@ -851,3 +857,14 @@ Sequra/NoSidekiqPerformStubs:
851
857
  - "**/*_spec.rb"
852
858
  - "**/spec/support/**/*.rb"
853
859
  - "**/spec/shared_examples/**/*.rb"
860
+
861
+ # Catch malformed labels on prometheus_exporter metric calls. `increment`,
862
+ # `decrement` and `observe` take labels as a positional hash, so a `labels:`
863
+ # keyword silently produces a series labelled `labels="{...}"` that no
864
+ # by-label query can match. Also catches the value/labels argument order,
865
+ # which is reversed between `observe` and `increment`. Autocorrectable.
866
+ # `warning` severity: these are silently broken metrics, not style, so they
867
+ # fail the build rather than waiting for adoption.
868
+ Sequra/PrometheusMetricLabels:
869
+ Enabled: true
870
+ Severity: warning
@@ -0,0 +1,156 @@
1
+ module RuboCop
2
+ module Cop
3
+ module Sequra
4
+ # Detects malformed label arguments on `prometheus_exporter` metric calls.
5
+ #
6
+ # `PrometheusExporter::Client::RemoteMetric` takes labels as a positional
7
+ # hash, and the position differs per method:
8
+ #
9
+ # increment(keys = nil, value = 1)
10
+ # decrement(keys = nil, value = 1)
11
+ # observe(value = 1, keys = nil)
12
+ #
13
+ # None of them accept keyword arguments, so `increment(labels: {...})`
14
+ # does not raise. Ruby folds the keyword into the positional `keys` hash
15
+ # and the exporter emits a series carrying a label literally named
16
+ # `labels`, whose value is the stringified inner hash. The result is
17
+ # valid Prometheus exposition text, so the scrape succeeds and the
18
+ # target stays healthy — `sum by (outcome)` simply never matches.
19
+ #
20
+ # Nothing downstream can catch it: the exporter keys its series by the
21
+ # raw hash, `labels_text` interpolates label names without validating
22
+ # them, and `opts: { labels: [...] }` at registration is discarded
23
+ # outright for counters and gauges. There is no label schema anywhere,
24
+ # so a wrong label name is indistinguishable from a new one, and the
25
+ # mistake only surfaces when a query silently returns nothing.
26
+ #
27
+ # @example
28
+ # # bad - folded into a label named "labels"
29
+ # counter.increment(labels: { outcome: :bound })
30
+ #
31
+ # # good
32
+ # counter.increment({ outcome: :bound })
33
+ #
34
+ # # bad - observe takes the value first
35
+ # histogram.observe({ outcome: :ok }, duration)
36
+ #
37
+ # # good
38
+ # histogram.observe(duration, { outcome: :ok })
39
+ #
40
+ # # bad - increment takes the labels first
41
+ # counter.increment(1, { outcome: :bound })
42
+ #
43
+ # # good
44
+ # counter.increment({ outcome: :bound }, 1)
45
+ #
46
+ # # bad - freezes the mistake into a passing spec
47
+ # expect(counter).to receive(:increment).with(labels: { outcome: :bound })
48
+ #
49
+ # # good
50
+ # expect(counter).to receive(:increment).with({ outcome: :bound })
51
+ #
52
+ class PrometheusMetricLabels < Base
53
+ extend AutoCorrector
54
+
55
+ MSG_LABELS_KEYWORD = "Pass Prometheus labels positionally, not as a `labels:` keyword. " \
56
+ "`increment`/`observe`/`decrement` take no keyword arguments, so this " \
57
+ "becomes a single series labelled `labels=\"{...}\"` and by-label " \
58
+ "queries never match. Use `increment({ outcome: :bound })`.".freeze
59
+
60
+ MSG_VALUE_FIRST = "`observe` takes the value first and the labels second: " \
61
+ "`observe(value, { outcome: :ok })`. A labels hash in the first position " \
62
+ "is recorded as the observed value.".freeze
63
+
64
+ MSG_LABELS_FIRST = "`%<method>s` takes the labels first and the value second: " \
65
+ "`%<method>s({ outcome: :ok }, 1)`. This is the reverse of `observe`.".freeze
66
+
67
+ MSG_STUBBED_LABELS = "Stubbing `increment` with a `labels:` keyword asserts a label shape " \
68
+ "prometheus_exporter never produces, so the spec stays green while " \
69
+ "the metric is broken. Match the positional hash instead: " \
70
+ "`.with({ outcome: :bound })`.".freeze
71
+
72
+ LABELS_FIRST_METHODS = [:increment, :decrement].freeze
73
+ VALUE_FIRST_METHODS = [:observe].freeze
74
+ METRIC_METHODS = (LABELS_FIRST_METHODS + VALUE_FIRST_METHODS).freeze
75
+
76
+ # `expect(counter).to receive(:increment).with(...)` and the
77
+ # `have_received` spy equivalent.
78
+ def_node_matcher :stubbed_metric_expectation?, <<~PATTERN
79
+ (send
80
+ (send nil? {:receive :have_received} (sym {:increment :decrement :observe}))
81
+ :with ...)
82
+ PATTERN
83
+
84
+ def on_send(node)
85
+ check_stubbed_labels_keyword(node)
86
+
87
+ return unless node.receiver && METRIC_METHODS.include?(node.method_name)
88
+
89
+ # A `labels:` key is the more specific diagnosis, so when it is
90
+ # present it wins and the positional checks stay quiet.
91
+ return if check_labels_keyword(node)
92
+
93
+ check_argument_order(node)
94
+ end
95
+
96
+ private
97
+
98
+ def check_labels_keyword(node)
99
+ pair = labels_pair(trailing_hash(node))
100
+ return false unless pair
101
+
102
+ register_labels_keyword_offense(node, pair, MSG_LABELS_KEYWORD)
103
+ true
104
+ end
105
+
106
+ def check_stubbed_labels_keyword(node)
107
+ return unless stubbed_metric_expectation?(node)
108
+
109
+ pair = labels_pair(trailing_hash(node))
110
+ return unless pair
111
+
112
+ register_labels_keyword_offense(node, pair, MSG_STUBBED_LABELS)
113
+ end
114
+
115
+ def check_argument_order(node)
116
+ arguments = node.arguments
117
+ return if arguments.empty?
118
+
119
+ if VALUE_FIRST_METHODS.include?(node.method_name)
120
+ add_offense(arguments.first, message: MSG_VALUE_FIRST) if arguments.first.hash_type?
121
+ elsif value_before_labels?(arguments)
122
+ add_offense(node, message: format(MSG_LABELS_FIRST, method: node.method_name))
123
+ end
124
+ end
125
+
126
+ # Only the unambiguous flip: a bare number where the labels belong,
127
+ # followed by the labels hash. Anything less literal than that could
128
+ # be a non-metric `increment` (`Rails.cache`, atomics) and is left
129
+ # alone.
130
+ def value_before_labels?(arguments)
131
+ arguments.size == 2 && arguments.first.numeric_type? && arguments.last.hash_type?
132
+ end
133
+
134
+ def trailing_hash(node)
135
+ last_argument = node.arguments.last
136
+ last_argument if last_argument&.hash_type?
137
+ end
138
+
139
+ def labels_pair(hash)
140
+ return unless hash
141
+
142
+ hash.pairs.find { |pair| pair.key.sym_type? && pair.key.value == :labels }
143
+ end
144
+
145
+ def register_labels_keyword_offense(node, pair, message)
146
+ add_offense(pair, message:) do |corrector|
147
+ # Unwrapping is only safe when `labels:` is the whole hash;
148
+ # with siblings present there is no single value to hoist.
149
+ hash = trailing_hash(node)
150
+ corrector.replace(hash, pair.value.source) if hash.pairs.one?
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
156
+ end
@@ -1,5 +1,5 @@
1
1
  module Sequra
2
2
  module Style
3
- VERSION = "1.16.0"
3
+ VERSION = "1.18.0"
4
4
  end
5
5
  end
data/lib/sequra_style.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require "rubocop"
2
2
  require_relative "rubocop/cop/sequra/async_job_pattern"
3
3
  require_relative "rubocop/cop/sequra/no_sidekiq_perform_stubs"
4
+ require_relative "rubocop/cop/sequra/prometheus_metric_labels"
data/sequra-style.gemspec CHANGED
@@ -26,7 +26,7 @@ Gem::Specification.new do |spec|
26
26
  spec.add_dependency "rubocop-rails", "~> 2.31"
27
27
  spec.add_dependency "rubocop-rspec", "~> 3.5"
28
28
 
29
- spec.add_development_dependency "bundler", "~> 2.1.4"
29
+ spec.add_development_dependency "bundler", ">= 2.2.33", "< 3"
30
30
  spec.add_development_dependency "rake", "~> 13.0.1"
31
31
  spec.add_development_dependency "rspec", "~> 3.13"
32
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequra-style
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.16.0
4
+ version: 1.18.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sequra engineering
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-05-20 00:00:00.000000000 Z
11
+ date: 2026-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -70,16 +70,22 @@ dependencies:
70
70
  name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 2.2.33
76
+ - - "<"
74
77
  - !ruby/object:Gem::Version
75
- version: 2.1.4
78
+ version: '3'
76
79
  type: :development
77
80
  prerelease: false
78
81
  version_requirements: !ruby/object:Gem::Requirement
79
82
  requirements:
80
- - - "~>"
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 2.2.33
86
+ - - "<"
81
87
  - !ruby/object:Gem::Version
82
- version: 2.1.4
88
+ version: '3'
83
89
  - !ruby/object:Gem::Dependency
84
90
  name: rake
85
91
  requirement: !ruby/object:Gem::Requirement
@@ -136,6 +142,7 @@ files:
136
142
  - docs/decisions/template.md
137
143
  - lib/rubocop/cop/sequra/async_job_pattern.rb
138
144
  - lib/rubocop/cop/sequra/no_sidekiq_perform_stubs.rb
145
+ - lib/rubocop/cop/sequra/prometheus_metric_labels.rb
139
146
  - lib/sequra/style.rb
140
147
  - lib/sequra/style/version.rb
141
148
  - lib/sequra_style.rb