fozzie 0.0.26 → 0.0.27

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -12,101 +12,106 @@ Ruby gem for registering statistics to a [Statsd](https://github.com/etsy/statsd
12
12
  Send through statistics depending on the type you want to provide:
13
13
 
14
14
  ### Increment counter
15
-
16
- Stats.increment 'wat' # increments the value in a Statsd bucket called 'some.prefix.wat' -
17
- # the exact bucket name depends on the bucket name prefix (see below)
18
-
15
+ ``` ruby
16
+ Stats.increment 'wat' # increments the value in a Statsd bucket called 'some.prefix.wat' -
17
+ # the exact bucket name depends on the bucket name prefix (see below)
18
+ ```
19
19
  ### Decrement counter
20
-
21
- Stats.decrement 'wat'
22
-
20
+ ``` ruby
21
+ Stats.decrement 'wat'
22
+ ```
23
23
  ### Decrement counter - provide a value as integer
24
-
25
- Stats.count 'wat', 5
26
-
24
+ ``` ruby
25
+ Stats.count 'wat', 5
26
+ ```
27
27
  ### Basic timing - provide a value in milliseconds
28
-
29
- Stats.timing 'wat', 500
30
-
28
+ ``` ruby
29
+ Stats.timing 'wat', 500
30
+ ```
31
31
  ### Timings - provide a block to time against (inline and do syntax supported)
32
+ ``` ruby
33
+ Stats.time 'wat' { sleep 5 }
32
34
 
33
- Stats.time 'wat' { sleep 5 }
34
-
35
- Stats.time_to_do 'wat' do
36
- sleep 5
37
- end
38
-
39
- Stats.time_for 'wat' { sleep 5 }
35
+ Stats.time_to_do 'wat' do
36
+ sleep 5
37
+ end
40
38
 
39
+ Stats.time_for 'wat' { sleep 5 }
40
+ ```
41
41
  ### Gauges - register arbitrary values
42
-
43
- Stats.gauge 'wat', 99
44
-
42
+ ``` ruby
43
+ Stats.gauge 'wat', 99
44
+ ```
45
45
  ### Events - register different events
46
46
 
47
47
 
48
48
  #### Commits
49
+ ``` ruby
50
+ Stats.commit
49
51
 
50
- Stats.commit
51
-
52
- Stats.committed
53
-
52
+ Stats.committed
53
+ ```
54
54
  #### Builds
55
+ ``` ruby
56
+ Stats.built
55
57
 
56
- Stats.built
57
-
58
- Stats.build
59
-
58
+ Stats.build
59
+ ```
60
60
  #### Deployments
61
-
62
- Stats.deployed
61
+ ``` ruby
62
+ Stats.deployed
63
+ ```
63
64
 
64
65
  With a custom app:
66
+ ``` ruby
67
+ Stats.deployed 'watapp'
65
68
 
66
- Stats.deployed 'watapp'
67
-
68
- Stats.deploy
69
+ Stats.deploy
70
+ ```
69
71
 
70
72
  With a custom app:
71
-
72
- Stats.deploy 'watapp'
73
+ ``` ruby
74
+ Stats.deploy 'watapp'
75
+ ```
73
76
 
74
77
  #### Custom
75
-
76
- Stats.event 'pull'
77
-
78
- With a custom app:
79
-
80
- Stats.event 'pull', 'watapp'
81
-
78
+ ``` ruby
79
+ Stats.event 'pull'
80
+ ```
81
+ With a custom app:
82
+
83
+ ``` ruby
84
+ Stats.event 'pull', 'watapp'
85
+ ```
82
86
  ### Boolean result - pass a value to be true or false, and increment on true
83
-
84
- Stats.increment_on 'wat', duck.valid?
85
-
87
+ ``` ruby
88
+ Stats.increment_on 'wat', duck.valid?
89
+ ```
86
90
  ## Sampling
87
91
 
88
92
  Each of the above methods accepts a sample rate as the last argument (before any applicable blocks), e.g:
89
93
 
90
- Stats.increment 'wat', 10
94
+ ``` ruby
95
+ Stats.increment 'wat', 10
91
96
 
92
- Stats.decrement 'wat', 10
97
+ Stats.decrement 'wat', 10
93
98
 
94
- Stats.count 'wat', 5, 10
95
-
99
+ Stats.count 'wat', 5, 10
100
+ ```
96
101
  ## Monitor
97
102
 
98
103
  You can monitor methods with the following:
104
+ ``` ruby
105
+ class FooBar
99
106
 
100
- class FooBar
101
-
102
- _monitor
103
- def zar
104
- # my code here...
105
- end
106
-
107
- end
107
+ _monitor
108
+ def zar
109
+ # my code here...
110
+ end
108
111
 
109
- This will register the processing time for this method, everytime it is called, under the Graphite bucket `foo_bar.zar`.
112
+ end
113
+ ```
114
+ This will register the processing time for this method, everytime it is called, under the Graphite bucket `foo_bar.zar`.
110
115
 
111
116
  This will work on both Class and Instance methods.
112
117
 
@@ -114,10 +119,12 @@ This will work on both Class and Instance methods.
114
119
 
115
120
  Fozzie supports the following namespaces as default
116
121
 
117
- Stats.increment 'wat'
118
- S.increment 'wat'
119
- Statistics.increment 'wat'
120
- Warehouse.increment 'wat'
122
+ ``` ruby
123
+ Stats.increment 'wat'
124
+ S.increment 'wat'
125
+ Statistics.increment 'wat'
126
+ Warehouse.increment 'wat'
127
+ ```
121
128
 
122
129
  You can customise this via the YAML configuration (see instructions below)
123
130
 
@@ -129,29 +136,52 @@ Fozzie is configured via a YAML or by setting a block against the Fozzie namespa
129
136
 
130
137
  Create a `fozzie.yml` within a `config` folder on the root of your app, which contains your settings for each env. Simple, verbose example below.
131
138
 
132
- development:
133
- appname: wat
134
- host: '127.0.0.1'
135
- port: 8125
136
- namespaces: %w{Foo Bar Wat}
137
- test:
138
- appname: wat
139
- host: 'localhost'
140
- port: 8125
141
- namespaces: %w{Foo Bar Wat}
142
- production:
143
- appname: wat
144
- host: 'stats.wat.com'
145
- port: 8125
146
- namespaces: %w{Foo Bar Wat}
139
+ ``` yaml
140
+ development:
141
+ appname: wat
142
+ host: '127.0.0.1'
143
+ port: 8125
144
+ namespaces: %w{Foo Bar Wat}
145
+ prefix: %{foo bar car}
146
+ test:
147
+ appname: wat
148
+ host: 'localhost'
149
+ port: 8125
150
+ namespaces: %w{Foo Bar Wat}
151
+ production:
152
+ appname: wat
153
+ host: 'stats.wat.com'
154
+ port: 8125
155
+ namespaces: %w{Foo Bar Wat}
156
+ ```
147
157
 
148
158
  ### Configure block
149
159
 
150
- Fozzie.configure do |config|
151
- config.appname = "wat"
152
- config.host = "127.0.0.1"
153
- config.port = 8125
154
- end
160
+ ``` ruby
161
+ Fozzie.configure do |config|
162
+ config.appname = "wat"
163
+ config.host = "127.0.0.1"
164
+ config.port = 8125
165
+ config.prefix = []
166
+ end
167
+ ```
168
+ ### Prefixes
169
+
170
+ You can inject or set the prefix value for your application.
171
+
172
+ ``` ruby
173
+ Fozzie.configure do |config|
174
+ config.prefix = ['foo', 'wat', 'bar']
175
+ end
176
+ ```
177
+
178
+ ``` ruby
179
+ Fozzie.configure do |config|
180
+ config.prefix << 'dynamic-value'
181
+ end
182
+ ```
183
+
184
+ Prefixes are cached on first use, therefore any changes to the Fozzie configure prefix after first metric is sent in your application will be ignored.
155
185
 
156
186
  ## Middleware
157
187
 
@@ -159,13 +189,15 @@ To time and register the controller actions within your Rails application, Fozzi
159
189
 
160
190
  ### Rack
161
191
 
162
- require 'rack'
163
- require 'fozzie'
192
+ ``` ruby
193
+ require 'rack'
194
+ require 'fozzie'
164
195
 
165
- app = Rack::Builder.new {
166
- use Fozzie::Rack::Middleware
167
- lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'OK'] }
168
- }
196
+ app = Rack::Builder.new {
197
+ use Fozzie::Rack::Middleware
198
+ lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'OK'] }
199
+ }
200
+ ```
169
201
 
170
202
  ### Rails
171
203
 
@@ -178,11 +210,13 @@ Fozzie::Rails::Middleware will automatically be invoked on Rails initialization.
178
210
  Fozzie automatically constructs bucket name prefixes from app name,
179
211
  hostname, and environment. For example:
180
212
 
181
- Stats.increment 'wat'
182
-
213
+ ``` ruby
214
+ Stats.increment 'wat'
215
+ ```
183
216
  increments the bucket named
184
-
185
- app-name.your-computer-name.development.wat
217
+ ``` text
218
+ app-name.your-computer-name.development.wat
219
+ ```
186
220
 
187
221
  When working on your development machine. This allows multiple
188
222
  application instances, in different environments, to be distinguished
@@ -195,12 +229,16 @@ The app name can be configured via the YAML configuration.
195
229
  The current implementation of Fozzie wraps the sending of the statistic in a timeout and rescue block, which prevent long host lookups (i.e. if your stats server disappears) and minimises impact on your code or application if something is erroring at a low level.
196
230
 
197
231
  Fozzie will try to log these errors, but only if a logger has been applied (which by default it does not). Examples:
198
-
199
- require 'logger'
200
- Fozzie.logger = Logger.new(STDOUT)
201
232
 
202
- require 'logger'
203
- Fozzie.logger = Logger.new 'log/fozzie.log'
233
+ ``` ruby
234
+ require 'logger'
235
+ Fozzie.logger = Logger.new(STDOUT)
236
+ ```
237
+
238
+ ``` ruby
239
+ require 'logger'
240
+ Fozzie.logger = Logger.new 'log/fozzie.log'
241
+ ```
204
242
 
205
243
  This may change, depending on feedback and more production experience.
206
244
 
@@ -10,7 +10,7 @@ module Fozzie
10
10
  class Configuration
11
11
  include Sys
12
12
 
13
- attr_accessor :env, :config_path, :host, :port, :appname, :namespaces, :timeout, :monitor_classes, :sniff_envs, :ignore_prefix
13
+ attr_accessor :env, :config_path, :host, :port, :appname, :namespaces, :timeout, :monitor_classes, :sniff_envs, :ignore_prefix, :prefix
14
14
 
15
15
  def initialize(args = {})
16
16
  merge_and_assign_config(args)
@@ -24,11 +24,17 @@ module Fozzie
24
24
  # Returns the prefix for any stat requested to be registered
25
25
  def data_prefix
26
26
  return nil if @ignore_prefix
27
+ return @data_prefix if @data_prefix
27
28
 
28
- @data_prefix ||= [appname, origin_name, env].collect do |s|
29
- s.empty? ? nil : s.gsub('.', '-')
30
- end.compact.join('.').strip
31
- (@data_prefix.empty? ? nil : @data_prefix)
29
+ data_prefix = @prefix.collect do |me|
30
+ (me.kind_of?(Symbol) && self.respond_to?(me.to_sym) ? self.send(me) : me.to_s)
31
+ end
32
+
33
+ data_prefix = data_prefix.collect do |s|
34
+ s.empty? ? nil : s.gsub(Socket::DELIMETER, '-')
35
+ end.compact.join(Socket::DELIMETER).strip
36
+
37
+ @data_prefix ||= (data_prefix.empty? ? nil : data_prefix)
32
38
  end
33
39
 
34
40
  # Returns the origin name of the current machine to register the stat against
@@ -55,6 +61,7 @@ module Fozzie
55
61
  def self.default_configuration
56
62
  {
57
63
  :host => '127.0.0.1',
64
+ :prefix => [:appname, :origin_name, :env],
58
65
  :port => 8125,
59
66
  :config_path => '',
60
67
  :env => (ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development'),
data/lib/fozzie/socket.rb CHANGED
@@ -4,6 +4,7 @@ module Fozzie
4
4
  module Socket
5
5
 
6
6
  RESERVED_CHARS_REGEX = /[\:\|\@\s]/
7
+ DELIMETER = '.'
7
8
 
8
9
  private
9
10
 
@@ -12,10 +13,10 @@ module Fozzie
12
13
  # Creates the Statsd key from the given values, and sends to socket (depending on sample rate)
13
14
  #
14
15
  def send(stat, delta, type, sample_rate)
15
- stat = [stat].flatten.compact.collect(&:to_s).join('.').downcase
16
- stat = stat.gsub('::', '.').gsub(RESERVED_CHARS_REGEX, '_')
16
+ stat = [stat].flatten.compact.collect(&:to_s).join(DELIMETER).downcase
17
+ stat = stat.gsub('::', DELIMETER).gsub(RESERVED_CHARS_REGEX, '_')
17
18
 
18
- k = [Fozzie.c.data_prefix, stat].compact.join('.')
19
+ k = [Fozzie.c.data_prefix, stat].compact.join(DELIMETER)
19
20
  k << ":"
20
21
  k << [delta, type].join('|')
21
22
  k << '@%s' % sample_rate.to_s if sample_rate < 1
@@ -1,3 +1,3 @@
1
1
  module Fozzie
2
- VERSION = "0.0.26"
2
+ VERSION = "0.0.27"
3
3
  end
@@ -24,20 +24,35 @@ describe Fozzie::Configuration do
24
24
  subject.env.should eq 'test'
25
25
  end
26
26
 
27
- it "creates a data prefix" do
28
- subject.stubs(:origin_name).returns("")
29
- subject.data_prefix.should eq 'test'
30
- end
27
+ describe "#prefix and #data_prefix" do
31
28
 
32
- it "creates a data prefix with appname when set" do
33
- subject.stubs(:origin_name).returns("")
34
- subject.appname = 'astoria'
35
- subject.data_prefix.should eq 'astoria.test'
36
- end
29
+ it "creates a #data_prefix" do
30
+ subject.stubs(:origin_name).returns("")
31
+ subject.data_prefix.should eq 'test'
32
+ end
33
+
34
+ it "creates a #data_prefix with appname when set" do
35
+ subject.stubs(:origin_name).returns("")
36
+ subject.appname = 'astoria'
37
+ subject.data_prefix.should eq 'astoria.test'
38
+ end
39
+
40
+ it "creates a #data_prefix with origin" do
41
+ subject.appname = 'astoria'
42
+ subject.data_prefix.should match /^astoria\.(\S+)\.test$/
43
+ end
44
+
45
+ it "allows dynamic assignment of #prefix to derive #data_prefix" do
46
+ subject.prefix = [:foo, :bar, :car]
47
+ subject.data_prefix.should eq 'foo.bar.car'
48
+ end
49
+
50
+ it "allows dynamic injection of value to prefix" do
51
+ subject.stubs(:origin_name).returns("")
52
+ subject.prefix << 'git-sha-1234'
53
+ subject.data_prefix.should eq 'test.git-sha-1234'
54
+ end
37
55
 
38
- it "creates a prefix with origin" do
39
- subject.appname = 'astoria'
40
- subject.data_prefix.should match /^astoria\.(\S+)\.test$/
41
56
  end
42
57
 
43
58
  it "handles missing configuration namespace" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fozzie
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.26
4
+ version: 0.0.27
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-07-29 00:00:00.000000000 Z
13
+ date: 2012-08-22 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: sys-uname
@@ -262,7 +262,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
262
262
  version: '0'
263
263
  segments:
264
264
  - 0
265
- hash: -2413055484564325442
265
+ hash: 1975054614990968952
266
266
  required_rubygems_version: !ruby/object:Gem::Requirement
267
267
  none: false
268
268
  requirements:
@@ -271,7 +271,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
271
271
  version: '0'
272
272
  segments:
273
273
  - 0
274
- hash: -2413055484564325442
274
+ hash: 1975054614990968952
275
275
  requirements: []
276
276
  rubyforge_project: fozzie
277
277
  rubygems_version: 1.8.24