Class: Attio::Observability::Metrics::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/attio/observability.rb

Overview

In-memory metrics for testing

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMemory

Returns a new instance of Memory.

Since:

  • 1.0.0



185
186
187
188
189
# File 'lib/attio/observability.rb', line 185

def initialize
  @counters = Hash.new(0)
  @gauges = {}
  @histograms = Hash.new { |h, k| h[k] = [] }
end

Instance Attribute Details

#countersObject (readonly)

Since:

  • 1.0.0



183
184
185
# File 'lib/attio/observability.rb', line 183

def counters
  @counters
end

#gaugesObject (readonly)

Since:

  • 1.0.0



183
184
185
# File 'lib/attio/observability.rb', line 183

def gauges
  @gauges
end

#histogramsObject (readonly)

Since:

  • 1.0.0



183
184
185
# File 'lib/attio/observability.rb', line 183

def histograms
  @histograms
end

Instance Method Details

#format_tags(tags) ⇒ Object (private)

Since:

  • 1.0.0



212
213
214
215
216
217
# File 'lib/attio/observability.rb', line 212

private def format_tags(tags)
  # Format tags in a consistent way that works across Ruby versions
  sorted = tags.sort.to_h
  content = sorted.map { |k, v| ":#{k}=>#{v.inspect}" }.join(", ")
  "{#{content}}"
end

#gauge(metric, value, tags: {}) ⇒ Object

Since:

  • 1.0.0



196
197
198
199
# File 'lib/attio/observability.rb', line 196

def gauge(metric, value, tags: {})
  key = tags.empty? ? "#{metric}:" : "#{metric}:#{format_tags(tags)}"
  @gauges[key] = value
end

#histogram(metric, value, tags: {}) ⇒ Object

Since:

  • 1.0.0



201
202
203
204
# File 'lib/attio/observability.rb', line 201

def histogram(metric, value, tags: {})
  key = tags.empty? ? "#{metric}:" : "#{metric}:#{format_tags(tags)}"
  @histograms[key] << value
end

#increment(metric, tags: {}) ⇒ Object

Since:

  • 1.0.0



191
192
193
194
# File 'lib/attio/observability.rb', line 191

def increment(metric, tags: {})
  key = tags.empty? ? "#{metric}:" : "#{metric}:#{format_tags(tags)}"
  @counters[key] += 1
end

#reset!Object

Since:

  • 1.0.0



206
207
208
209
210
# File 'lib/attio/observability.rb', line 206

def reset!
  @counters.clear
  @gauges.clear
  @histograms.clear
end