You can define custom metrics within a Yabeda.configure block using groups. Supported metric types include counter, gauge, histogram, and summary.
1. Declaration
Metrics are organized into groups. You can add comments, units, and tags during declaration.
2. Initialization
After declaring metrics, you must call Yabeda.configure! to apply the configuration. Note: If you are using Ruby on Rails, this step is handled automatically.
3. Usage
Access metrics via the group name on the Yabeda module. Use methods like .increment for counters or .measure for histograms.
4. Periodic Collection
Use the collect block to report metrics that represent the current state of your application (e.g., counting active records). This block is executed periodically by your adapter.
5. Default Tags
You can set global tags or group-specific tags. You can also temporarily override tags for a specific block of code using Yabeda.with_tags.
# 1. Declare metrics
Yabeda.configure do
group :your_app do
counter :bells_rang_count, comment: "Total number of bells being rang", tags: %i[bell_size]
gauge :whistles_active, comment: "Number of whistles ready to whistle"
histogram :whistle_runtime do
comment "How long whistles are being active"
unit :seconds
end
summary :bells_ringing_duration, unit: :seconds, comment: "How long bells are ringing"
end
end
# 2. Apply configuration (Automatic in Rails)
Yabeda.configure!
# 3. Use metrics
def ring_the_bell(id)
bell = Bell.find(id)
bell.ring!
Yabeda.your_app.bells_rang_count.increment({bell_size: bell.size}, by: 1)
end
def whistle!
Yabeda.your_app.whistle_runtime.measure do
# Run your code
end
end
# 4. Periodic collection
Yabeda.configure do
collect do
your_app.whistles_active.set({}, Whistle.where(state: :active).count)
end
end
# 5. Default tags and overrides
Yabeda.configure do
default_tag :rails_environment, 'production'
default_tag :tag_name, 'override', group: :your_app
end
Yabeda.with_tags(rails_environment: 'staging') do
Yabeda.your_app.bells_rang_count.increment({bell_size: bell.size}, by: 1)
end