Skip to content

Commit bbef70f

Browse files
committed
Merge pull request #2 from v-dolgishev/master
Logs and metrics
2 parents 0204fd2 + a1e58bf commit bbef70f

46 files changed

Lines changed: 2047 additions & 3 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.ruby-gemset

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
stackify-api-ruby

.ruby-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ruby-2.1.2

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in stackify.gemspec
4+
gemspec

Gemfile.lock

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
PATH
2+
remote: .
3+
specs:
4+
stackify-api-ruby (1.0.3)
5+
faraday (>= 0.8)
6+
7+
GEM
8+
remote: https://rubygems.org/
9+
specs:
10+
faraday (0.9.0)
11+
multipart-post (>= 1.2, < 3)
12+
multipart-post (2.0.0)
13+
rake (0.9.6)
14+
15+
PLATFORMS
16+
ruby
17+
18+
DEPENDENCIES
19+
bundler (~> 1.6)
20+
rake (~> 0)
21+
stackify-api-ruby!

README.md

Lines changed: 139 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,140 @@
1-
stackify-api-ruby
2-
=================
1+
# Stackify
32

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

Rakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require 'bundler/gem_tasks'
2+

lib/core_ext/core_ext.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
require 'json'
2+
require 'core_ext/object'
3+
require 'core_ext/fixnum'

lib/core_ext/fixnum.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Fixnum
2+
SECONDS_IN_MINUTE = 60
3+
SECONDS_IN_HOUR = 60 * SECONDS_IN_MINUTE
4+
SECONDS_IN_DAY = 24 * SECONDS_IN_HOUR
5+
6+
def days
7+
self * SECONDS_IN_DAY
8+
end
9+
10+
def minutes
11+
self * SECONDS_IN_MINUTE
12+
end
13+
14+
def ago
15+
Time.now - self
16+
end
17+
end

lib/core_ext/object.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Object
2+
def try(*a, &b)
3+
if a.empty? && block_given?
4+
yield self
5+
else
6+
public_send(*a, &b) if respond_to?(a.first)
7+
end
8+
end
9+
end
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require 'rails/generators/base'
2+
3+
class StackifyGenerator < Rails::Generators::Base
4+
source_root File.expand_path('../../stackify/templates', __FILE__)
5+
6+
class_option :api_key, type: :string, required: true
7+
8+
desc 'Creates a Stackify initializer'
9+
def copy_initializer
10+
template 'stackify.rb', 'config/initializers/stackify.rb'
11+
end
12+
13+
end

0 commit comments

Comments
 (0)