include Metric
def pay_user(user, amount)
pay(user.account, amount)
Metric.increment 'payment'
Metric.increment "payment-amount, #{amount.to_i}"
send_payment_notification(user.email)
end
def send_payment_notification(email)
send_email(payment, email)
Metric.increment 'email-payment'
end
source 'https://rubygems.org'
ruby '2.4.2'
gem 'rails', '5.1.5'
...
gem 'prometheus-client'
...
prometheus = Prometheus::Client.registry
test_counter = prometheus.count(:test_count, 'A test counter')
test_counter.increment
test_counter.get
1.0
test_counter = prometheus.counter(:test_counter, 'A test counter')
test_gauge = prometheus.gauge(:test_gauge, 'A test gauge')
test_histogram = prometheus.histogram(:test_histogram, 'A test histogram')
test_summary = prometheus.summary(:test_summary, 'A test summary')
touch lib/metrics.rb
module Metrics
def self.counter(name, docstring, base_labels = {})
provide_metric(name) || registry.counter(name, docstring, base_labels)
end
def self.summary(name, docstring, base_labels = {})
provide_metric(name) || registry.summary(name, docstring, base_labels)
end
def self.gauge(name, docstring, base_labels = {})
provide_metric(name) || registry.summary(name, docstring, base_labels)
end
def self.histogram(name, docstring, base_labels = {}, buckets = ::Prometheus::Clien::Histogram::DEFAULT_BUCKETS)
provide_metric(name) || registry.histogram(name, docstring, base_labels, buckets)
end
private
def self.provide_metrics(name)
registry.get(name)
end
def. self.registry
@registry || ::Prometheus::Client.registry
end
end
touch config/initializers/lib.rb
require 'metrics'
def destroy
user = User.find(params[:id])
user.destroy
Metrics.counter(:users_deletes_counter, "Deletes users counter").increment
redirect_to users_path, :notice => "User deleted."
end
.increment({service: 'foo'}, 2)
class User < ActiveRecord::Base
enum role: [:user, :vip, :admin]
after_initialize :set_default, :if => :new_record?
after_create do
Metrics.counter(:user_created_counter, "Users created counter").increment
end
end
require 'prometheus/middleware/collector'
require 'prometheus/middleware/exporter'
use Prometheus::Middleware::Collector
use Prometheus::Middleware::Exporter
- job_name: rails
file_sd_configs:
- files:
- targets/rails/*.json
refresh_interval: 5m