StatsCloud

StatsCloud module

Installation

Add this line to your application's Gemfile:

gem 'statscloud'

And then execute:

$ bundle

Or install it yourself as:

$ gem install statscloud

Usage

Rails

  1. Run generator for creatinig initializer file, statscloud.io configuration file .statscloud.yml and directory for saving your metrics configurations. ruby rails g stats_cloud:install
  2. Set up your configuration files (you can read more about StatsCloud configuration here).

StatsCloud auth token is a required parameter for getting access to StatsCloud service. The best way to configure this auth token in your application is using environment variable STATSCLOUD_AUTH_TOKEN. StatsCloud add-on at Heroku configures this environment variable automatically during adding this add-on to your application. If you want to use statscloud service in your development process (or somewhere else) you can copy Heroku config vars to your local .env file.

  1. You can change statscloud.rb intializer file if you want to start StatsCloud service with a different environment, create several instances of StatsCloud::Client with different configurations or add some tags to your StatsmeterClient.

    with_environment('env')

    This method allows you to record metrics from different environments (like test, development, production) separately, so

    StatsCloud::Client.new.with_environment('production')
    

    would start StatsCloud service at production environment. Without this method, the environment would be chosen from the configuration file. If you don't configure it service would try to find env in the RAILS_ENV and if it is also missing it would set the value as the default.

    with_tags(array[string])

    You can start service with sending some tags to the cluster. Tags are used to group metrics. By default, the host name is used as a tag, which allows you to track metrics for both the application as a whole and for each host individually. In the cloud, an array of tags ['region', 'server_name'] can be used, which allows you to track metrics for the application as a whole, separate regions or separate servers.

    StatsCloud::Client.new.with_tags(['region', 'server_name'])
    

    with_config_file(file)

    Also, it's available to set up several StatsCloud clients with different configuration files via with_config_file method.

    StatsCloud::Client.new.with_config_file('.production_statscloud.yml')
    

    You can combine these methods calling them as a chain.

  2. StatsCloud initializer defines StatsCloudClient constant which you can use to record metrics with record_event method for recording one event or record_events to send multiple events to cluster from any place of your application:

    StatsCloudClient.meter.record_event('event', 1)
    StatsCloudClient.meter.record_events({name: 'gauge', measurement: 123}, {name: 'counter'})
    

    Also, you can use these methods directly from StatsCloudClient:

    StatsCloudClient.record_event('event', 1)
    StatsCloudClient.record_events({name: 'gauge', measurement: 123}, {name: 'counter'})
    

    Ruby

  3. Install statscloud gem:

    gem install statscloud
    
  4. Require statscloud in the project and set up your application structure, with .statscloud.yml configuration file (at the root of the project) and metrics configs (read more about StatsCloud configuration here).

  5. Customize statscloud run with with_environment, with_tags and with_config_file methods and start it where you need it.

    statscloud_client = StatsCloud::Client.new
    statscloud_client.with_environment('test').with_tags(['usa', 'server_1']).start
    
  6. Send metrics via statscloud_client.meter or directly via StatsCloud::Client instance:

    meter = statscloud_client.meter
    meter.record_event('event', 1)
    meter.record_events({name: 'gauge', measurement: 123}, {name: 'counter'})
    
    # also available and preferable
    
    statscloud_client.record_event('some_event', 4)
    statscloud_client.record_events({name: 'some_gauge', measurement: 321})
    
    
  7. Stop work of StatsCloud service when 'time is over':

    statscloud_client.stop
    

The full version of pure ruby example takes the form:

require 'statscloud' # import gem

# start the module, returns a thread
statscloud_client = StatsCloud::Client.new
statscloud_client.start

meter = statscloud_client.meter
# track events (remember that you have to wait for the socket connection to the cluster before record any metrics)
meter.record_event('numeric', 123)
meter.record_events({name: 'gauge', measurement: 123}, {name: 'counter'})

statscloud_client.record_event('some_event', 123)

# stop cluster work
statscloud_client.stop

License

(c) Copyright 2018 Agilium Labs LLC, all rights reserved.