Module: Kotoshu::Metrics

Defined in:
lib/kotoshu/metrics_module.rb,
lib/kotoshu/metrics_collector.rb

Overview

Metrics and instrumentation for Kotoshu.

Provides thread-safe collection of performance metrics:

  • Lookup counts and timing

  • Cache hit/miss rates

  • Suggestion generation stats

  • Optional export to StatsD or Prometheus

Examples:

Enable metrics

Kotoshu::Metrics.enable
Kotoshu.correct?("hello")
Kotoshu::Metrics.stats
# => { lookups: 1, cache_hits: 0, cache_misses: 1, ... }

Defined Under Namespace

Classes: Collector

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.collectorCollector? (readonly)

Get the metrics collector.

Returns:

  • (Collector, nil)

    The collector instance



41
42
43
# File 'lib/kotoshu/metrics_module.rb', line 41

def collector
  @collector
end

Class Method Details

.disableObject

Disable metrics collection.



26
27
28
29
# File 'lib/kotoshu/metrics_module.rb', line 26

def disable
  @enabled = false
  @collector = nil
end

.enableObject

Enable metrics collection.



20
21
22
23
# File 'lib/kotoshu/metrics_module.rb', line 20

def enable
  @enabled = true
  @collector = Collector.new
end

.enabled?Boolean

Check if metrics are enabled.

Returns:

  • (Boolean)

    True if enabled



34
35
36
# File 'lib/kotoshu/metrics_module.rb', line 34

def enabled?
  @enabled ||= false
end

.record_cache(cache_type, hit:) ⇒ Object

Record a cache operation.

Parameters:

  • cache_type (String)

    Type of cache (lookup, suggestion)

  • hit (Boolean)

    True if cache hit



58
59
60
61
62
# File 'lib/kotoshu/metrics_module.rb', line 58

def record_cache(cache_type, hit:)
  return unless enabled?

  collector&.record_cache(cache_type, hit: hit)
end

.record_lookup(word, result:, time:) ⇒ Object

Record a lookup operation.

Parameters:

  • word (String)

    The word being looked up

  • result (Boolean)

    The lookup result

  • time (Float)

    Time taken in milliseconds



48
49
50
51
52
# File 'lib/kotoshu/metrics_module.rb', line 48

def record_lookup(word, result:, time:)
  return unless enabled?

  collector&.record_lookup(word, result: result, time: time)
end

.record_suggestions(word, count:, time:) ⇒ Object

Record suggestion generation.

Parameters:

  • word (String)

    The input word

  • count (Integer)

    Number of suggestions generated

  • time (Float)

    Time taken in milliseconds



69
70
71
72
73
# File 'lib/kotoshu/metrics_module.rb', line 69

def record_suggestions(word, count:, time:)
  return unless enabled?

  collector&.record_suggestions(word, count: count, time: time)
end

.resetObject

Reset all metrics.



85
86
87
88
89
# File 'lib/kotoshu/metrics_module.rb', line 85

def reset
  return unless enabled?

  collector&.reset
end

.statsHash

Get current metrics statistics.

Returns:

  • (Hash)

    Current statistics



78
79
80
81
82
# File 'lib/kotoshu/metrics_module.rb', line 78

def stats
  return {} unless enabled?

  collector&.stats || {}
end

.to_prometheusString

Get metrics as Prometheus format.

Returns:

  • (String)

    Prometheus exposition format



103
104
105
106
107
# File 'lib/kotoshu/metrics_module.rb', line 103

def to_prometheus
  return "" unless enabled?

  collector&.to_prometheus || ""
end

.to_statsdString

Get metrics as StatsD format.

Returns:

  • (String)

    StatsD protocol lines



94
95
96
97
98
# File 'lib/kotoshu/metrics_module.rb', line 94

def to_statsd
  return "" unless enabled?

  collector&.to_statsd || ""
end