|
1 | | -stackify-api-ruby |
2 | | -================= |
| 1 | +# Stackify |
3 | 2 |
|
4 | | -Stackify API for Ruby |
| 3 | +Stackify Logs and Metrics API for Ruby |
| 4 | + |
| 5 | + |
| 6 | +Rails Installation |
| 7 | +------------------ |
| 8 | + |
| 9 | +### Rails 3.x/4.x |
| 10 | + |
| 11 | +Add the stackify gem to your Gemfile. In Gemfile: |
| 12 | + |
| 13 | + gem 'stackify-api-ruby' |
| 14 | + |
| 15 | +Run the bundle command to install it: |
| 16 | + |
| 17 | + $ bundle |
| 18 | + |
| 19 | +Or install it manually: |
| 20 | + |
| 21 | + $ gem install stackify-api-ruby |
| 22 | + |
| 23 | +After you install stackify-api-ruby you need to run the generator: |
| 24 | + |
| 25 | + $ rails g stackify --api_key=your_api_key |
| 26 | + |
| 27 | +The generator creates a file 'config/initializers/stackify.rb' configuring stackify-api-ruby with your API key. You can change default settings there. |
| 28 | + |
| 29 | +Usage: Logging |
| 30 | +------------------ |
| 31 | +### Rails Environment |
| 32 | + |
| 33 | +stackify-api-ruby starts with start of Rails. Every error, which occurs within your application, will be caught and sent to Stackify automatically. The same situation with logs - you just use the Rails logger as usual: |
| 34 | + |
| 35 | + Rails.logger.info "Some log message" |
| 36 | + |
| 37 | +### Other Environment |
| 38 | + |
| 39 | +For using stackify-api-ruby gem within any Ruby application add to top of your main file: |
| 40 | + |
| 41 | + require 'stackify-api-ruby' |
| 42 | + |
| 43 | +After that you need to make base configuration: |
| 44 | + |
| 45 | + Stackify.setup do |config| |
| 46 | + config.api_key = "your_api_key" |
| 47 | + config.env = :development |
| 48 | + config.app_name = "Your app name" |
| 49 | + config.app_location = "/somewhere/public" |
| 50 | + end |
| 51 | + |
| 52 | +"api_key" - it's you key for Stackify. "app-location" - it's location of your application for Nginx/Apache(for Nginx it's value of 'root', for Apache it's value of 'DocumentRoot' at config files). |
| 53 | + |
| 54 | +Gem has 3 modes of work - "both", "logging", "metrics". Mode "both" turns on both parts of gem - logging and metrics. |
| 55 | +If you need ONLY logging or metrics use "logging" or "metrics" mode accordingly. |
| 56 | + |
| 57 | + config.mode = :metrics |
| 58 | + |
| 59 | +You can set minimal level of logs, which should be caught by gem: |
| 60 | + |
| 61 | + config.log_level = :error |
| 62 | + |
| 63 | +If you want to use proxy for sending request, you can do it in such way: |
| 64 | + |
| 65 | + config.proxy = { uri: '127.0.0.1:8118', user: 'user_name', password: 'some_password' } |
| 66 | + |
| 67 | +For internal logging stackify-api-ruby uses such logger: |
| 68 | + |
| 69 | + config.logger = Logger.new(File.join(Rails.root, "log", "stackify.log")) |
| 70 | + |
| 71 | +After logs configuring you should wrap up your logger: |
| 72 | + |
| 73 | + logger = Logger.new('mylog.log') |
| 74 | + logger = Stackify::LoggerProxy.new(logger) |
| 75 | + |
| 76 | + |
| 77 | +And last thing you need to do - call method "run": |
| 78 | + |
| 79 | + Stackify.run #remember that this call is running in the background of your main script |
| 80 | + |
| 81 | +Usage: Metrics |
| 82 | +------------------ |
| 83 | + |
| 84 | +There are four different types of metrics: |
| 85 | + |
| 86 | +- **Gauge**: Keeps track of the last value that was set in the current minute |
| 87 | + |
| 88 | + Stackify::Metrics.set_gauge "MyCategory", "MyGauge", 100 |
| 89 | + |
| 90 | +- **Counter**: Calculates the rate per minute |
| 91 | + |
| 92 | + Stackify::Metrics.count "MyCategory", "MyCounter" |
| 93 | + |
| 94 | +- **Average**: Calculates the average of all values in the current minute |
| 95 | + |
| 96 | + Stackify::Metrics.average "MyCategory", "AverageSpeed", 200 |
| 97 | + |
| 98 | +- **Timer**: Calculates the average elapsed time for an operation in the current minute |
| 99 | + |
| 100 | + t = Time.now |
| 101 | + Stackify::Metrics.time "MyCategory", "ElapsedTime", t |
| 102 | + |
| 103 | +- **Counter and Timer**: Composite of the Counter and Timer metrics for convenience |
| 104 | + |
| 105 | + t = Time.now |
| 106 | + Stackify::Metrics.count_and_time "Metric", "CounterWithTime", t |
| 107 | + |
| 108 | +We can configure every metric with settings: |
| 109 | + |
| 110 | + settings = MetricSettings.new |
| 111 | + settings.autoreport_zero_if_nothing_reported = true |
| 112 | + # or |
| 113 | + settings.autoreport_last_value_if_nothing_reported = true |
| 114 | + Stackify::Metrics.set_gauge "MyCategory", "MyGauge", 100 , settings |
| 115 | + |
| 116 | +Note, "autoreport_last_value_if_nothing_reported" property has influence only on "average" metric. |
| 117 | + |
| 118 | +Also there are two methods for getting last values of metrics: |
| 119 | + |
| 120 | +- get_latest - return last value of certain metric |
| 121 | + |
| 122 | + ``` Stackify::Metrics.get_latest "MyCategory", "MyCounter" ``` |
| 123 | + |
| 124 | +- get_latest_all_metrics - return all values of existed metrics |
| 125 | + |
| 126 | + ``` Stackify::Metrics.get_latest_all_metrics``` |
| 127 | + |
| 128 | +## Requirements |
| 129 | +Ruby: 1.9/2.0/2.1 |
| 130 | + |
| 131 | +Rails: 3.x/4.x |
| 132 | + |
| 133 | +Contributing |
| 134 | +------------------ |
| 135 | + |
| 136 | +1. Fork it ( https://github.com/[my-github-username]/stackify/fork ) |
| 137 | +2. Create your feature branch (`git checkout -b my-new-feature`) |
| 138 | +3. Commit your changes (`git commit -am 'Add some feature'`) |
| 139 | +4. Push to the branch (`git push origin my-new-feature`) |
| 140 | +5. Create a new Pull Request |
0 commit comments