Twingly::Metrics

GitHub Build Status

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:

Release workflow

  • Bump the version in lib/twingly/metrics/version.rb in a commit, no need to push (the release task does that).

  • Build and publish the gem. This will create the proper tag in git, push the commit and tag and upload to RubyGems.

    bundle exec rake release
    
    • If you are not logged in as twingly with ruby gems, the rake task will fail. Login using gem signin and run the release task again. It will be okay.
  • Update the changelog with GitHub Changelog Generator (gem install github_changelog_generator if you don't have it, set CHANGELOG_GITHUB_TOKEN to a personal access token to avoid rate limiting by GitHub). This command will update CHANGELOG.md, commit and push manually.

    github_changelog_generator -u twingly -p twingly-metrics