Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

librato-rails

> 编程语言
Open source

Report key app statistics to the Librato Metrics service and easily track your own custom metrics

159 stars0 likes1 views
WebsiteGitHub

About

Report key app statistics to the Librato Metrics service and easily track your own custom metrics

librato-rails


Important note on breaking change

NOTE: Starting with version 2.0.0 librato-rails requires a Librato account that supports tagged metrics.

If your Librato account doesn't yet support tagged metrics or you are using a heroku addon, please use the 1.x.x version.


librato-rails will report key statistics for your Rails app to Librato and allow you to easily track your own custom metrics. Metrics are delivered asynchronously behind the scenes so they won't affect performance of your requests.

Rails versions 3.0 or greater are supported on Ruby 1.9.3 and above.

Verified combinations of Ruby/Rails are available in our build matrix.

Upgrading

Upgrading from version 1.x to 2.x introduces breaking changes for legacy sources. Please contact [email protected] to migrate an existing Librato account.

Quick Start

Note: If you have not yet enabled Rails on the Librato integrations page within your account, do this first. This will automatically set up Rails and Rack Spaces, displaying many useful performance metrics.

Installing librato-rails and relaunching your application will automatically start the reporting of built-in performance metrics to your Librato account.

Once installed, custom metrics can also be added easily:

ruby
# keep counts of key events
Librato.increment 'user.signup'

# easily benchmark sections of code to verify production performance
Librato.timing 'my.complicated.work' do
  # do work
end

# track averages across requests
Librato.measure 'user.social_graph.nodes', user.social_graph.size

Installation

In your Gemfile add:

gem 'librato-rails'

Then run bundle install.

Configuration

If you don't have a Librato account already, sign up. In order to send measurements to Librato you need to provide your account credentials to librato-rails. You can provide these one of two ways:

Use a config file

Create a config/librato.yml like the following:

yaml
production:
  user: 
  token: 

The librato.yml file is parsed via ERB in case you need to add some host or environment-specific magic.

Note that using a configuration file allows you to specify different configurations per-environment. Submission will be disabled in any environment without credentials.

Use environment variables

Alternately you can provide LIBRATO_USER and LIBRATO_TOKEN environment variables. Unlike config file settings, environment variables will be used in all non-test environments (development, production, etc).

Note that if a config file is present, all environment variables will be ignored.

To see all config options or for information on combining config files and environment variables see the full configuration docs.

Running on Heroku

If you are using the Librato Heroku addon, your user and token environment variables will already be set in your Heroku environment. If you are running without the addon you will need to provide them yourself.

If Heroku idles your application, measurements will not be sent until it receives another request and is restarted. If you see intermittent gaps in your measurements during periods of low traffic, this is the most likely cause.

If you are using Librato as a Heroku addon, a paid plan is required for reporting custom metrics with librato-rails. You can view more about available addon levels here.

Default Tags

Librato Metrics supports tagged measurements that are associated with a metric, one or more tag pairs, and a point in time. For more information on tagged measurements, visit our API documentation.

Detected Tags

By default, service, environment and host are detected and applied as default tags for submitted measurements. Optionally, you can override the detected values in your configuration file:

yaml
production:
  user: 
  token: 
  tags:
    service: 'myapp'
    environment: 'production'
    host: 'myapp-prod-1'
Custom Tags

In addition to the default tags, you can also provide custom tags:

yaml
production:
  user: 
  token: 
  tags:
    region: 'us-east-1'

Full information on configuration options is available on the configuration wiki page.

Automatic Measurements

After installing librato-rails and restarting your app you will see a number of new metrics appear in your Librato account. These track request performance, sql queries, mail handling, and other key stats.

Built-in performance metrics will start with either rails or rack, depending on the level they are being sampled from. For example: rails.request.total is the total number of requests rails has received each minute.

The metrics automatically recorded by librato-rails are organized into named metric suites that can be selectively enabled/disabled:

Rails Suites

Request Metrics
  • rails_controller: Metrics which provide a high level overview of request performance including rails.request.total, rails.request.time, rails.request.time.db, rails.request.time.view, and rails.request.slow
  • rails_method: rails.request.method metric with method tag name and HTTP method tag value, e.g. method=POST
  • rails_status: rails.request.status metric with status tag name and HTTP status code tag value, e.g. status=200
System-Specific Metrics
  • rails_cache: rails.cache.* metrics including reads, writes, hits & deletes
  • rails_job: rails.job.* metrics including jobs queued, started & performed (Rails 4.2+)
  • rails_mail: rails.mail.* metrics including mail sent / received
  • rails_render: rails.view.* metrics including time to render individual templates & partials
  • rails_sql: rails.sql.* metrics, including SELECT / INSERT / UPDATE / DELETEs as well as total query operations

Rack Suites

Rack measurements are taken from the very beginning of your rack middleware stack. They include all time spent in your ruby process (not just in Rails proper). They will also show requests that are handled entirely in middleware and don't appear in the rails suites above.

  • rack: The rack.request.total, rack.request.time, rack.request.slow, and rack.request.queue.time metrics
  • rack_method: rack.request.method metric with method tag name and HTTP method tag value, e.g. method=POST
  • rack_status: rack.request.status metric with status tag name and HTTP status code tag value, e.g. status=200
Queue Time

The rack.request.queue.time metric will show you queuing time (the time between your load balancer receiving a request & your application process starting to work on it) if your load balancer sets HTTP_X_REQUEST_START or HTTP_X_QUEUE_START.

Default Suites

All of the rails & rack suites listed above are enabled by default.

Suite Configuration

Suites can be configured via either the LIBRATO_SUITES environment variable or the suites setting in a config/librato.yml configuration file. The syntax is the same regardless of configuration method.

…

Note that you should specify either an explicit list of suites to enable or add/subtract individual suites from the default list (using the +/- prefixes). If you try to mix these two forms a Librato::Rack::InvalidSuiteConfiguration error will be raised.

Configuring the metric suites via the config/librato.yml file would look like this:

yaml
production:
  user: [email protected]
  token: abc123
  suites: 'rails_controller,rails_status,rails_sql,rack'

Custom Measurements

Tracking anything that interests you is easy with Librato. There are four primary helpers available to use anywhere in your application:

increment

Use for tracking a running total of something across requests, examples:

…

Other things you might track this way: user signups, requests of a certain type or to a certain route, total jobs queued or processed, emails sent or received

Sporadic Increment Reporting

Note that increment is primarily used for tracking the rate of occurrence of some event. Given this increment metrics are continuous by default: after being called on a metric once they will report on every interval, reporting zeros for any interval when increment was not called on the metric.

Especially with custom sources you may want the opposite behavior - reporting a measurement only during intervals where increment was called on the metric:

ruby
# report a value for 'user.uploaded_file' only during non-zero intervals
Librato.increment 'user.uploaded_file', tags: { user: user.id, bucket: bucket.name }, sporadic: true

measure

Use when you want to track an average value per-request. Examples:

ruby
Librato.measure 'user.social_graph.nodes', 212

# report from custom per-measurement tags
Librato.measure 'jobs.queued', 3, tags: { priority: 'high', worker: 'worker.12' }

timing

Like Librato.measure this is per-request, but specialized for timing information:

ruby
Librato.timing 'twitter.lookup.time', 21.2

# report from custom per-measurement tags
Librato.measure 'api.response.time', time, tags: { node: node_name, db: 'rr1' }

The block form auto-submits the time it took for its contents to execute as the measurement value:

ruby
Librato.timing 'twitter.lookup.time' do
  @twitter = Twitter.lookup(user)
end

Block form timings are measured in milliseconds(ms).

Percentiles

By defaults timings will send the average, sum, max and min for every minute. If you want to send percentiles as well you can specify them inline while instrumenting:

ruby
# track a single percentile
Librato.timing 'api.request.time', time, percentile: 95

# track multiple percentiles
Librato.timing 'api.request.time', time, percentile: [95, 99]

You can also use percentiles with the block form of timings:

ruby
Librato.timing 'my.important.event', percentile: 95 do
  # do work
end

group

There is also a grouping helper, to make managing nested metrics easier. So this:

ruby
Librato.measure 'memcached.gets', 20
Librato.measure 'memcached.sets', 2
Librato.measure 'memcached.hits', 18

Can also be written as:

ruby
Librato.group 'memcached' do |g|
  g.measure 'gets', 20
  g.measure 'sets', 2
  g.measure 'hits', 18
end

Symbols can be used interchangably with strings for metric names.

Use with ActiveSupport::Notifications

librato-rails and ActiveSupport::Notifications work great together. In fact, many of the Rails metrics provided are produced by subscribing to the instrumentation events built into Rails.

Assume you have a custom event:

ruby
ActiveSupport::Notifications.instrument 'my.event', user: user do
  # do work..
end

Writing a subscriber to capture that event and its outcomes is easy:

…

These are just a few examples. Combining ActiveSupport::Notifications instrumentation with Librato can be extremely powerful. As an added benefit, using the instrument/subscribers model allows you to isolate complex i

Issues· 15 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言