Class: Labkit::RateLimit::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/labkit/rate_limit/matcher.rb

Overview

Matcher is the internal representation of a single key/value predicate in a Rule#match hash. Rule.new normalizes every match value through Matcher.build; the Evaluator calls Matcher#match? per identifier value.

Accepted input shapes (everything else raises ArgumentError):

- any plain value (String, Symbol, Integer, ...) -> :eq matcher
- a Regexp instance                              -> :re matcher (Ruby convenience)
- { eq: <value> }                                -> :eq matcher (canonical, YAML-compatible)
- { re: <String|Regexp> }                        -> :re matcher (canonical, YAML-compatible)

Hash-key naming follows the metrics-catalog selector pattern. Glob, prefix, and other matcher kinds are intentionally out of scope here; see gitlab-com/gl-infra/production-engineering#28853 for that follow-up.

An :re matcher coerces the identifier value via #to_s before applying the regex, so callers can match non-String identifier values such as Integer status codes (e.g. { re: “^5” } against status: 503).

Constant Summary collapse

KNOWN_HASH_KEYS =
%i[eq re].freeze
MAX_REGEX_SOURCE_LENGTH =
200
ERROR_INSPECT_LIMIT =
80

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(input) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/labkit/rate_limit/matcher.rb', line 27

def self.build(input)
  case input
  when Regexp
    new(type: :re, value: input)
  when Hash
    from_hash(input)
  when Array
    raise ArgumentError,
      "rate-limit match value must be a single-key Hash like {re: \"...\"} or {eq: ...}, got #{truncate_for_error(input)}"
  else
    new(type: :eq, value: input)
  end
end

Instance Method Details

#match?(identifier_value) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
94
# File 'lib/labkit/rate_limit/matcher.rb', line 85

def match?(identifier_value)
  case type
  when :eq
    value == identifier_value
  when :re
    value.match?(identifier_value.to_s)
  else
    raise ArgumentError, "unknown matcher type: #{type.inspect}"
  end
end