Twingly::Metrics
A gem for collecting and reporting metrics from our Ruby applications.
This gem currently relies on the metriks gem for collecting metrics. The reason for this is that we're already using that gem in our projects. What the twingly-metrics gem currently adds in functionality, is the ability to report metrics to Grafana cloud, using their Graphite HTTP API.
The code here was inspired by the reporter in the metriks-librato_metrics gem.
Installation
Add this line to your application's Gemfile:
gem "twingly-metrics"
Or install it yourself as:
$ gem install twingly-metrics
Usage
The following example shows how to use the Twingly::Metrics::GrafanaCloudReporter to report metrics to Grafana Cloud using their Graphite HTTP API.
For more information on the API endpoint and credentials, see the Grafana Cloud documentation.
require "twingly/metrics"
reporter = Twingly::Metrics::GrafanaCloudReporter.new(
endpoint: # The API endpoint of your Grafana Cloud Graphite HTTP API
# For example: https://<something>.grafana.net/graphite/metrics
user: # Your Grafana Cloud username for the Graphite HTTP API
token: # Your Grafana Cloud token for the Graphite HTTP API
prefix: "my_app", # Optional, can be used to add a prefix to all reported metrics,
# for example the name of your application
source: "some-example-host", # Optional, can be used for adding a "source" tag to all reported metrics,
# useful for separating metrics by host
logger: Logger.new(STDOUT), # Optional, a logger to log any errors raised in the reporter
# See the reporter class for more options
)
reporter.start # Starts the reporter thread in the background
# Create some metrics for the reporter to have something to report
Metriks.meter("test.meter").mark(1)
at_exit do
reporter.flush # Ensure all unreported metrics are sent before exiting
end
Supported metric types
The reporter currently supports the following metric types from the metriks gem: