fozzie 0.0.15 → 0.0.16
Sign up to get free protection for your applications and to get access to all the features.
- data/Guardfile +3 -0
- data/doc/lib/core_ext/hash.html +79 -0
- data/doc/lib/fozzie.html +148 -0
- data/doc/lib/fozzie/configuration.html +174 -0
- data/doc/lib/fozzie/interface.html +288 -0
- data/doc/lib/fozzie/rack/middleware.html +97 -0
- data/doc/lib/fozzie/rails/middleware.html +97 -0
- data/doc/lib/fozzie/socket.html +123 -0
- data/doc/lib/fozzie/version.html +36 -0
- data/fozzie.gemspec +6 -0
- data/lib/fozzie.rb +52 -10
- data/lib/fozzie/configuration.rb +16 -32
- data/lib/fozzie/interface.rb +121 -0
- data/lib/fozzie/rack/middleware.rb +3 -0
- data/lib/fozzie/rails/middleware.rb +4 -0
- data/lib/fozzie/socket.rb +44 -0
- data/lib/fozzie/version.rb +1 -1
- data/spec/lib/fozzie/configuration_spec.rb +9 -42
- data/spec/lib/fozzie/interface_spec.rb +136 -0
- data/spec/lib/fozzie/rack/middleware_spec.rb +3 -5
- data/spec/lib/fozzie/rails/middleware_spec.rb +35 -29
- data/spec/lib/fozzie_spec.rb +6 -138
- metadata +69 -28
- data/lib/fozzie/classes.rb +0 -146
- data/lib/fozzie/config.rb +0 -19
- data/spec/lib/fozzie/config_spec.rb +0 -24
@@ -0,0 +1,97 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="content-type" content="text/html;charset=utf-8">
|
5
|
+
<title>middleware.rb</title>
|
6
|
+
<link rel="stylesheet" href="http://jashkenas.github.com/docco/resources/docco.css">
|
7
|
+
</head>
|
8
|
+
<body>
|
9
|
+
<div id='container'>
|
10
|
+
<div id="background"></div>
|
11
|
+
<div id="jump_to">
|
12
|
+
Jump To …
|
13
|
+
<div id="jump_wrapper">
|
14
|
+
<div id="jump_page">
|
15
|
+
<a class="source" href="../../core_ext/hash.html">hash.rb</a>
|
16
|
+
<a class="source" href="../../fozzie.html">fozzie.rb</a>
|
17
|
+
<a class="source" href="../configuration.html">configuration.rb</a>
|
18
|
+
<a class="source" href="../interface.html">interface.rb</a>
|
19
|
+
<a class="source" href="middleware.html">middleware.rb</a>
|
20
|
+
<a class="source" href="../rails/middleware.html">middleware.rb</a>
|
21
|
+
<a class="source" href="../socket.html">socket.rb</a>
|
22
|
+
<a class="source" href="../version.html">version.rb</a>
|
23
|
+
</div>
|
24
|
+
</div>
|
25
|
+
</div>
|
26
|
+
<table cellspacing=0 cellpadding=0>
|
27
|
+
<thead>
|
28
|
+
<tr>
|
29
|
+
<th class=docs><h1>middleware.rb</h1></th>
|
30
|
+
<th class=code></th>
|
31
|
+
</tr>
|
32
|
+
</thead>
|
33
|
+
<tbody>
|
34
|
+
<tr id='section-1'>
|
35
|
+
<td class=docs>
|
36
|
+
<div class="pilwrap">
|
37
|
+
<a class="pilcrow" href="#section-1">¶</a>
|
38
|
+
</div>
|
39
|
+
|
40
|
+
</td>
|
41
|
+
<td class=code>
|
42
|
+
<div class='highlight'><pre><span class="k">module</span> <span class="nn">Fozzie</span>
|
43
|
+
<span class="k">module</span> <span class="nn">Rack</span></pre></div>
|
44
|
+
</td>
|
45
|
+
</tr>
|
46
|
+
<tr id='section-2'>
|
47
|
+
<td class=docs>
|
48
|
+
<div class="pilwrap">
|
49
|
+
<a class="pilcrow" href="#section-2">¶</a>
|
50
|
+
</div>
|
51
|
+
<p>Time and record each request through a given Rack app
|
52
|
+
This middlewware times server processing for a resource, not view render.</p>
|
53
|
+
|
54
|
+
</td>
|
55
|
+
<td class=code>
|
56
|
+
<div class='highlight'><pre> <span class="k">class</span> <span class="nc">Middleware</span>
|
57
|
+
|
58
|
+
<span class="kp">attr_reader</span> <span class="ss">:app</span>
|
59
|
+
|
60
|
+
<span class="k">def</span> <span class="nf">initialize</span><span class="p">(</span><span class="n">app</span><span class="p">)</span>
|
61
|
+
<span class="vi">@app</span> <span class="o">=</span> <span class="n">app</span>
|
62
|
+
<span class="k">end</span>
|
63
|
+
|
64
|
+
<span class="k">def</span> <span class="nf">call</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
|
65
|
+
<span class="n">k</span> <span class="o">=</span> <span class="n">generate_key</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
|
66
|
+
<span class="k">if</span> <span class="n">k</span><span class="o">.</span><span class="n">nil?</span>
|
67
|
+
<span class="nb">self</span><span class="o">.</span><span class="n">call_without_timer</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
|
68
|
+
<span class="k">else</span>
|
69
|
+
<span class="nb">self</span><span class="o">.</span><span class="n">call_with_timer</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">env</span><span class="p">)</span>
|
70
|
+
<span class="k">end</span>
|
71
|
+
<span class="k">end</span>
|
72
|
+
|
73
|
+
<span class="k">def</span> <span class="nf">call_without_timer</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
|
74
|
+
<span class="vi">@app</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
|
75
|
+
<span class="k">end</span>
|
76
|
+
|
77
|
+
<span class="k">def</span> <span class="nf">call_with_timer</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="n">env</span><span class="p">)</span>
|
78
|
+
<span class="n">S</span><span class="o">.</span><span class="n">time_to_do</span> <span class="n">key</span> <span class="k">do</span>
|
79
|
+
<span class="vi">@app</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
|
80
|
+
<span class="k">end</span>
|
81
|
+
<span class="k">end</span>
|
82
|
+
|
83
|
+
<span class="k">def</span> <span class="nf">generate_key</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
|
84
|
+
<span class="n">s</span> <span class="o">=</span> <span class="n">env</span><span class="o">[</span><span class="s1">'PATH_INFO'</span><span class="o">]</span>
|
85
|
+
<span class="k">return</span> <span class="kp">nil</span> <span class="k">if</span> <span class="n">s</span><span class="o">.</span><span class="n">nil?</span>
|
86
|
+
<span class="n">s</span> <span class="o">=</span> <span class="p">(</span><span class="n">s</span> <span class="o">==</span> <span class="s1">'/'</span> <span class="p">?</span> <span class="s1">'index'</span> <span class="p">:</span> <span class="n">s</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="sr">/.(\/)./</span><span class="p">)</span> <span class="p">{</span><span class="o">|</span><span class="n">m</span><span class="o">|</span> <span class="n">m</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="s1">'/'</span><span class="p">,</span> <span class="s1">'.'</span><span class="p">)</span> <span class="p">}</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="sr">/\//</span><span class="p">,</span> <span class="s1">''</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">)</span>
|
87
|
+
<span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">nil?</span> <span class="o">||</span> <span class="n">s</span><span class="o">.</span><span class="n">empty?</span> <span class="p">?</span> <span class="kp">nil</span> <span class="p">:</span> <span class="s2">"</span><span class="si">#{</span><span class="n">s</span><span class="si">}</span><span class="s2">.render"</span><span class="p">)</span>
|
88
|
+
<span class="k">end</span>
|
89
|
+
|
90
|
+
<span class="k">end</span>
|
91
|
+
<span class="k">end</span>
|
92
|
+
<span class="k">end</span></pre></div>
|
93
|
+
</td>
|
94
|
+
</tr>
|
95
|
+
</table>
|
96
|
+
</div>
|
97
|
+
</body>
|
@@ -0,0 +1,97 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="content-type" content="text/html;charset=utf-8">
|
5
|
+
<title>middleware.rb</title>
|
6
|
+
<link rel="stylesheet" href="http://jashkenas.github.com/docco/resources/docco.css">
|
7
|
+
</head>
|
8
|
+
<body>
|
9
|
+
<div id='container'>
|
10
|
+
<div id="background"></div>
|
11
|
+
<div id="jump_to">
|
12
|
+
Jump To …
|
13
|
+
<div id="jump_wrapper">
|
14
|
+
<div id="jump_page">
|
15
|
+
<a class="source" href="../../core_ext/hash.html">hash.rb</a>
|
16
|
+
<a class="source" href="../../fozzie.html">fozzie.rb</a>
|
17
|
+
<a class="source" href="../configuration.html">configuration.rb</a>
|
18
|
+
<a class="source" href="../interface.html">interface.rb</a>
|
19
|
+
<a class="source" href="../rack/middleware.html">middleware.rb</a>
|
20
|
+
<a class="source" href="middleware.html">middleware.rb</a>
|
21
|
+
<a class="source" href="../socket.html">socket.rb</a>
|
22
|
+
<a class="source" href="../version.html">version.rb</a>
|
23
|
+
</div>
|
24
|
+
</div>
|
25
|
+
</div>
|
26
|
+
<table cellspacing=0 cellpadding=0>
|
27
|
+
<thead>
|
28
|
+
<tr>
|
29
|
+
<th class=docs><h1>middleware.rb</h1></th>
|
30
|
+
<th class=code></th>
|
31
|
+
</tr>
|
32
|
+
</thead>
|
33
|
+
<tbody>
|
34
|
+
<tr id='section-1'>
|
35
|
+
<td class=docs>
|
36
|
+
<div class="pilwrap">
|
37
|
+
<a class="pilcrow" href="#section-1">¶</a>
|
38
|
+
</div>
|
39
|
+
|
40
|
+
</td>
|
41
|
+
<td class=code>
|
42
|
+
<div class='highlight'><pre><span class="nb">require</span> <span class="s1">'fozzie/rack/middleware'</span>
|
43
|
+
|
44
|
+
<span class="k">module</span> <span class="nn">Fozzie</span>
|
45
|
+
<span class="k">module</span> <span class="nn">Rails</span></pre></div>
|
46
|
+
</td>
|
47
|
+
</tr>
|
48
|
+
<tr id='section-2'>
|
49
|
+
<td class=docs>
|
50
|
+
<div class="pilwrap">
|
51
|
+
<a class="pilcrow" href="#section-2">¶</a>
|
52
|
+
</div>
|
53
|
+
<p>Time and record each request through a given Rails app
|
54
|
+
This middlewware times server processing for a resource, not view render.</p>
|
55
|
+
</td>
|
56
|
+
<td class=code>
|
57
|
+
<div class='highlight'><pre> <span class="k">class</span> <span class="nc">Middleware</span> <span class="o"><</span> <span class="no">Fozzie</span><span class="o">::</span><span class="no">Rack</span><span class="o">::</span><span class="no">Middleware</span></pre></div>
|
58
|
+
</td>
|
59
|
+
</tr>
|
60
|
+
<tr id='section-3'>
|
61
|
+
<td class=docs>
|
62
|
+
<div class="pilwrap">
|
63
|
+
<a class="pilcrow" href="#section-3">¶</a>
|
64
|
+
</div>
|
65
|
+
<p>Generates the statistics key for the current path</p>
|
66
|
+
|
67
|
+
</td>
|
68
|
+
<td class=code>
|
69
|
+
<div class='highlight'><pre> <span class="k">def</span> <span class="nf">generate_key</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
|
70
|
+
<span class="n">path_str</span> <span class="o">=</span> <span class="n">env</span><span class="o">[</span><span class="s1">'PATH_INFO'</span><span class="o">]</span>
|
71
|
+
<span class="k">return</span> <span class="kp">nil</span> <span class="k">unless</span> <span class="n">path_str</span>
|
72
|
+
|
73
|
+
<span class="k">begin</span>
|
74
|
+
<span class="n">routing</span> <span class="o">=</span> <span class="p">(</span><span class="n">rails_version</span> <span class="o">==</span> <span class="mi">3</span> <span class="o">?</span> <span class="o">::</span><span class="no">Rails</span><span class="o">.</span><span class="n">application</span><span class="o">.</span><span class="n">routes</span> <span class="p">:</span> <span class="o">::</span><span class="no">ActionController</span><span class="o">::</span><span class="no">Routing</span><span class="o">::</span><span class="no">Routes</span><span class="p">)</span>
|
75
|
+
<span class="n">path</span> <span class="o">=</span> <span class="n">routing</span><span class="o">.</span><span class="n">recognize_path</span><span class="p">(</span><span class="n">path_str</span><span class="p">)</span>
|
76
|
+
<span class="n">stat</span> <span class="o">=</span> <span class="o">[</span><span class="n">path</span><span class="o">[</span><span class="ss">:controller</span><span class="o">]</span><span class="p">,</span> <span class="n">path</span><span class="o">[</span><span class="ss">:action</span><span class="o">]</span><span class="p">,</span> <span class="s2">"render"</span><span class="o">].</span><span class="n">join</span><span class="p">(</span><span class="s1">'.'</span><span class="p">)</span>
|
77
|
+
<span class="n">stat</span>
|
78
|
+
<span class="k">rescue</span> <span class="no">ActionController</span><span class="o">::</span><span class="no">RoutingError</span> <span class="o">=></span> <span class="n">exc</span>
|
79
|
+
<span class="n">S</span><span class="o">.</span><span class="n">increment</span> <span class="s2">"routing.error"</span>
|
80
|
+
<span class="kp">nil</span>
|
81
|
+
<span class="k">rescue</span> <span class="o">=></span> <span class="n">exc</span>
|
82
|
+
<span class="kp">nil</span>
|
83
|
+
<span class="k">end</span>
|
84
|
+
<span class="k">end</span>
|
85
|
+
|
86
|
+
<span class="k">def</span> <span class="nf">rails_version</span>
|
87
|
+
<span class="o">::</span><span class="no">Rails</span><span class="o">.</span><span class="n">version</span><span class="o">.</span><span class="n">to_i</span>
|
88
|
+
<span class="k">end</span>
|
89
|
+
|
90
|
+
<span class="k">end</span>
|
91
|
+
<span class="k">end</span>
|
92
|
+
<span class="k">end</span></pre></div>
|
93
|
+
</td>
|
94
|
+
</tr>
|
95
|
+
</table>
|
96
|
+
</div>
|
97
|
+
</body>
|
@@ -0,0 +1,123 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="content-type" content="text/html;charset=utf-8">
|
5
|
+
<title>socket.rb</title>
|
6
|
+
<link rel="stylesheet" href="http://jashkenas.github.com/docco/resources/docco.css">
|
7
|
+
</head>
|
8
|
+
<body>
|
9
|
+
<div id='container'>
|
10
|
+
<div id="background"></div>
|
11
|
+
<div id="jump_to">
|
12
|
+
Jump To …
|
13
|
+
<div id="jump_wrapper">
|
14
|
+
<div id="jump_page">
|
15
|
+
<a class="source" href="../core_ext/hash.html">hash.rb</a>
|
16
|
+
<a class="source" href="../fozzie.html">fozzie.rb</a>
|
17
|
+
<a class="source" href="configuration.html">configuration.rb</a>
|
18
|
+
<a class="source" href="interface.html">interface.rb</a>
|
19
|
+
<a class="source" href="rack/middleware.html">middleware.rb</a>
|
20
|
+
<a class="source" href="rails/middleware.html">middleware.rb</a>
|
21
|
+
<a class="source" href="socket.html">socket.rb</a>
|
22
|
+
<a class="source" href="version.html">version.rb</a>
|
23
|
+
</div>
|
24
|
+
</div>
|
25
|
+
</div>
|
26
|
+
<table cellspacing=0 cellpadding=0>
|
27
|
+
<thead>
|
28
|
+
<tr>
|
29
|
+
<th class=docs><h1>socket.rb</h1></th>
|
30
|
+
<th class=code></th>
|
31
|
+
</tr>
|
32
|
+
</thead>
|
33
|
+
<tbody>
|
34
|
+
<tr id='section-1'>
|
35
|
+
<td class=docs>
|
36
|
+
<div class="pilwrap">
|
37
|
+
<a class="pilcrow" href="#section-1">¶</a>
|
38
|
+
</div>
|
39
|
+
|
40
|
+
</td>
|
41
|
+
<td class=code>
|
42
|
+
<div class='highlight'><pre><span class="k">module</span> <span class="nn">Fozzie</span>
|
43
|
+
<span class="k">module</span> <span class="nn">Socket</span>
|
44
|
+
|
45
|
+
<span class="no">RESERVED_CHARS_REGEX</span> <span class="o">=</span> <span class="sr">/[\:\|\@]/</span>
|
46
|
+
|
47
|
+
<span class="kp">private</span></pre></div>
|
48
|
+
</td>
|
49
|
+
</tr>
|
50
|
+
<tr id='section-2'>
|
51
|
+
<td class=docs>
|
52
|
+
<div class="pilwrap">
|
53
|
+
<a class="pilcrow" href="#section-2">¶</a>
|
54
|
+
</div>
|
55
|
+
<p>Send the statistic to the server</p>
|
56
|
+
</td>
|
57
|
+
<td class=code>
|
58
|
+
<div class='highlight'><pre> <span class="k">def</span> <span class="nf">send</span><span class="p">(</span><span class="n">stat</span><span class="p">,</span> <span class="n">delta</span><span class="p">,</span> <span class="nb">type</span><span class="p">,</span> <span class="n">sample_rate</span><span class="p">)</span>
|
59
|
+
<span class="n">prefix</span> <span class="o">=</span> <span class="s2">"%s."</span> <span class="o">%</span> <span class="no">Fozzie</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">data_prefix</span> <span class="k">unless</span> <span class="no">Fozzie</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">data_prefix</span><span class="o">.</span><span class="n">nil?</span>
|
60
|
+
<span class="n">stat</span> <span class="o">=</span> <span class="n">stat</span><span class="o">.</span><span class="n">to_s</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="s1">'::'</span><span class="p">,</span> <span class="s1">'.'</span><span class="p">)</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="no">RESERVED_CHARS_REGEX</span><span class="p">,</span> <span class="s1">'_'</span><span class="p">)</span>
|
61
|
+
|
62
|
+
<span class="n">k</span> <span class="o">=</span> <span class="s2">"%s%s:%s|%s"</span> <span class="o">%</span> <span class="o">[</span><span class="n">prefix</span><span class="p">,</span> <span class="n">stat</span><span class="p">,</span> <span class="n">delta</span><span class="p">,</span> <span class="nb">type</span><span class="o">]</span>
|
63
|
+
<span class="n">k</span> <span class="o"><<</span> <span class="s1">'|@%s'</span> <span class="o">%</span> <span class="n">sample_rate</span><span class="o">.</span><span class="n">to_s</span> <span class="k">if</span> <span class="n">sample_rate</span> <span class="o"><</span> <span class="mi">1</span>
|
64
|
+
|
65
|
+
<span class="n">sampled</span><span class="p">(</span><span class="n">sample_rate</span><span class="p">)</span> <span class="p">{</span> <span class="n">send_to_socket</span><span class="p">(</span><span class="n">k</span><span class="o">.</span><span class="n">strip</span><span class="p">)</span> <span class="p">}</span>
|
66
|
+
<span class="k">end</span></pre></div>
|
67
|
+
</td>
|
68
|
+
</tr>
|
69
|
+
<tr id='section-3'>
|
70
|
+
<td class=docs>
|
71
|
+
<div class="pilwrap">
|
72
|
+
<a class="pilcrow" href="#section-3">¶</a>
|
73
|
+
</div>
|
74
|
+
<p>If the statistic is sampled, generate a condition to check if it’s good to send</p>
|
75
|
+
</td>
|
76
|
+
<td class=code>
|
77
|
+
<div class='highlight'><pre> <span class="k">def</span> <span class="nf">sampled</span><span class="p">(</span><span class="n">sample_rate</span><span class="p">)</span>
|
78
|
+
<span class="k">yield</span> <span class="k">unless</span> <span class="n">sample_rate</span> <span class="o"><</span> <span class="mi">1</span> <span class="ow">and</span> <span class="nb">rand</span> <span class="o">></span> <span class="n">sample_rate</span>
|
79
|
+
<span class="k">end</span></pre></div>
|
80
|
+
</td>
|
81
|
+
</tr>
|
82
|
+
<tr id='section-4'>
|
83
|
+
<td class=docs>
|
84
|
+
<div class="pilwrap">
|
85
|
+
<a class="pilcrow" href="#section-4">¶</a>
|
86
|
+
</div>
|
87
|
+
<p>Send data to the server via the socket</p>
|
88
|
+
</td>
|
89
|
+
<td class=code>
|
90
|
+
<div class='highlight'><pre> <span class="k">def</span> <span class="nf">send_to_socket</span><span class="p">(</span><span class="n">message</span><span class="p">)</span>
|
91
|
+
<span class="k">begin</span>
|
92
|
+
<span class="no">Fozzie</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span> <span class="p">{</span><span class="s2">"Statsd: </span><span class="si">#{</span><span class="n">message</span><span class="si">}</span><span class="s2">"</span><span class="p">}</span> <span class="k">if</span> <span class="no">Fozzie</span><span class="o">.</span><span class="n">logger</span>
|
93
|
+
<span class="no">Timeout</span><span class="o">.</span><span class="n">timeout</span><span class="p">(</span><span class="no">Fozzie</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">timeout</span><span class="p">)</span> <span class="p">{</span>
|
94
|
+
<span class="n">socket</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">message</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="no">Fozzie</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">host</span><span class="p">,</span> <span class="no">Fozzie</span><span class="o">.</span><span class="n">c</span><span class="o">.</span><span class="n">port</span><span class="p">)</span>
|
95
|
+
<span class="kp">true</span>
|
96
|
+
<span class="p">}</span>
|
97
|
+
<span class="k">rescue</span> <span class="o">=></span> <span class="n">exc</span>
|
98
|
+
<span class="no">Fozzie</span><span class="o">.</span><span class="n">ogger</span><span class="o">.</span><span class="n">debug</span> <span class="p">{</span><span class="s2">"Statsd Failure: </span><span class="si">#{</span><span class="n">exc</span><span class="o">.</span><span class="n">message</span><span class="si">}</span><span class="se">\n</span><span class="si">#{</span><span class="n">exc</span><span class="o">.</span><span class="n">backtrace</span><span class="si">}</span><span class="s2">"</span><span class="p">}</span> <span class="k">if</span> <span class="no">Fozzie</span><span class="o">.</span><span class="n">logger</span>
|
99
|
+
<span class="kp">false</span>
|
100
|
+
<span class="k">end</span>
|
101
|
+
<span class="k">end</span></pre></div>
|
102
|
+
</td>
|
103
|
+
</tr>
|
104
|
+
<tr id='section-5'>
|
105
|
+
<td class=docs>
|
106
|
+
<div class="pilwrap">
|
107
|
+
<a class="pilcrow" href="#section-5">¶</a>
|
108
|
+
</div>
|
109
|
+
<p>The Socket we want to use to send data</p>
|
110
|
+
|
111
|
+
</td>
|
112
|
+
<td class=code>
|
113
|
+
<div class='highlight'><pre> <span class="k">def</span> <span class="nf">socket</span>
|
114
|
+
<span class="vi">@socket</span> <span class="o">||=</span> <span class="no">UDPSocket</span><span class="o">.</span><span class="n">new</span>
|
115
|
+
<span class="k">end</span>
|
116
|
+
|
117
|
+
<span class="k">end</span>
|
118
|
+
<span class="k">end</span></pre></div>
|
119
|
+
</td>
|
120
|
+
</tr>
|
121
|
+
</table>
|
122
|
+
</div>
|
123
|
+
</body>
|
@@ -0,0 +1,36 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<html>
|
3
|
+
<head>
|
4
|
+
<meta http-equiv="content-type" content="text/html;charset=utf-8">
|
5
|
+
<title>version.rb</title>
|
6
|
+
<link rel="stylesheet" href="http://jashkenas.github.com/docco/resources/docco.css">
|
7
|
+
</head>
|
8
|
+
<body>
|
9
|
+
<div id='container'>
|
10
|
+
<div id="background"></div>
|
11
|
+
<div id="jump_to">
|
12
|
+
Jump To …
|
13
|
+
<div id="jump_wrapper">
|
14
|
+
<div id="jump_page">
|
15
|
+
<a class="source" href="../core_ext/hash.html">hash.rb</a>
|
16
|
+
<a class="source" href="../fozzie.html">fozzie.rb</a>
|
17
|
+
<a class="source" href="configuration.html">configuration.rb</a>
|
18
|
+
<a class="source" href="interface.html">interface.rb</a>
|
19
|
+
<a class="source" href="rack/middleware.html">middleware.rb</a>
|
20
|
+
<a class="source" href="rails/middleware.html">middleware.rb</a>
|
21
|
+
<a class="source" href="socket.html">socket.rb</a>
|
22
|
+
<a class="source" href="version.html">version.rb</a>
|
23
|
+
</div>
|
24
|
+
</div>
|
25
|
+
</div>
|
26
|
+
<table cellspacing=0 cellpadding=0>
|
27
|
+
<thead>
|
28
|
+
<tr>
|
29
|
+
<th class=docs><h1>version.rb</h1></th>
|
30
|
+
<th class=code></th>
|
31
|
+
</tr>
|
32
|
+
</thead>
|
33
|
+
<tbody>
|
34
|
+
</table>
|
35
|
+
</div>
|
36
|
+
</body>
|
data/fozzie.gemspec
CHANGED
@@ -16,15 +16,21 @@ Gem::Specification.new do |s|
|
|
16
16
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
17
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
18
|
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency 'sys-uname'
|
19
21
|
|
20
22
|
s.add_development_dependency 'rake'
|
21
23
|
s.add_development_dependency 'rspec'
|
22
24
|
s.add_development_dependency 'mocha'
|
23
25
|
s.add_development_dependency 'syntax'
|
26
|
+
|
24
27
|
s.add_development_dependency 'rack-test'
|
25
28
|
s.add_development_dependency 'simplecov'
|
26
29
|
s.add_development_dependency 'sinatra'
|
27
30
|
s.add_development_dependency 'actionpack'
|
31
|
+
|
28
32
|
s.add_development_dependency 'guard'
|
29
33
|
s.add_development_dependency 'guard-rspec'
|
34
|
+
s.add_development_dependency 'guard-rocco'
|
35
|
+
s.add_development_dependency 'fl-rocco'
|
30
36
|
end
|
data/lib/fozzie.rb
CHANGED
@@ -1,13 +1,55 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
# Fozzie is an implementation of the Statsd statistics gathering tool,
|
2
|
+
# designed to make gathering stastistics from applications easy, fast, and effective.
|
3
|
+
#
|
4
|
+
# Configuration can be applied through a block and/or configuration file ('config/fozzie.yml')
|
5
|
+
#
|
6
|
+
# Fozzie provides automatic namespacing for the current environment, and host.
|
7
|
+
#
|
8
|
+
# Rack and Rails middleware is avaliable to gather statistics on the processing time of Controller actions.
|
9
|
+
#
|
10
|
+
module Fozzie
|
5
11
|
|
6
|
-
|
7
|
-
|
8
|
-
|
12
|
+
require_relative 'fozzie/configuration'
|
13
|
+
require_relative "fozzie/interface"
|
14
|
+
require_relative "fozzie/version"
|
15
|
+
|
16
|
+
require_relative "fozzie/rack/middleware"
|
17
|
+
require_relative "fozzie/rails/middleware"
|
18
|
+
|
19
|
+
class << self
|
20
|
+
|
21
|
+
# Shortcut for `Fozzie.config`
|
22
|
+
def c
|
23
|
+
config
|
24
|
+
end
|
25
|
+
|
26
|
+
# Returns the current configuration. Creates configuration on first-time request
|
27
|
+
def config
|
28
|
+
@config ||= Configuration.new
|
29
|
+
end
|
30
|
+
|
31
|
+
# Allows the setting on valudes against the configuration
|
32
|
+
#
|
33
|
+
# `Fozzie.configure {|config| config.wat = :random }`
|
34
|
+
def configure
|
35
|
+
yield c if block_given?
|
36
|
+
end
|
37
|
+
|
38
|
+
# Set a logger
|
39
|
+
def logger=(logger)
|
40
|
+
@logger = logger
|
41
|
+
end
|
42
|
+
|
43
|
+
# Accessor for logger
|
44
|
+
def logger
|
45
|
+
@logger
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
# Loads each namespace for registering statistics
|
51
|
+
self.c.namespaces.each do |klas|
|
52
|
+
Kernel.const_set(klas, Interface.instance) unless const_defined?(klas)
|
53
|
+
end
|
9
54
|
|
10
|
-
module Fozzie
|
11
|
-
extend Fozzie::Config
|
12
|
-
include Fozzie::Classes
|
13
55
|
end
|
data/lib/fozzie/configuration.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require 'core_ext/hash'
|
2
|
-
require '
|
2
|
+
require 'sys/uname'
|
3
3
|
require 'timeout'
|
4
4
|
|
5
5
|
module Fozzie
|
@@ -7,44 +7,40 @@ module Fozzie
|
|
7
7
|
# Fozzie configuration allows assignment of global properties
|
8
8
|
# that will be used within the Fozzie codebase.
|
9
9
|
class Configuration
|
10
|
+
include Sys
|
10
11
|
|
11
12
|
attr_accessor :env, :config_path, :host, :port, :appname, :namespaces, :timeout
|
12
13
|
|
13
14
|
def initialize(args = {})
|
14
15
|
merge_and_assign_config(args)
|
15
|
-
self.ip_from_host
|
16
16
|
self.origin_name
|
17
|
-
self
|
18
17
|
end
|
19
18
|
|
19
|
+
# Returns the prefix for any stat requested to be registered
|
20
20
|
def data_prefix
|
21
|
-
s = [appname, origin_name, env].collect
|
21
|
+
s = [appname, origin_name, env].collect do |s|
|
22
|
+
s.empty? ? nil : s.gsub('.', '-')
|
23
|
+
end.compact.join('.').strip
|
22
24
|
(s.empty? ? nil : s)
|
23
25
|
end
|
24
26
|
|
25
|
-
|
26
|
-
@ip_from_host ||= host_to_ip
|
27
|
-
end
|
28
|
-
|
27
|
+
# Returns the origin name of the current machine to register the stat against
|
29
28
|
def origin_name
|
30
|
-
@origin_name ||=
|
29
|
+
@origin_name ||= Uname.uname.nodename
|
31
30
|
end
|
32
31
|
|
33
32
|
private
|
34
33
|
|
35
34
|
# Handle the merging of the given configuaration, and the default config.
|
36
|
-
# @return [Hash]
|
37
35
|
def merge_and_assign_config(args = {})
|
38
36
|
arg = self.class.default_configuration.merge(args.symbolize_keys)
|
39
|
-
arg.delete_if {|key, val| !self.respond_to?(key.to_sym) }
|
40
37
|
arg.merge!(config_from_yaml(arg))
|
41
|
-
arg.each {|a,v| self.send("#{a}=", v) }
|
38
|
+
arg.each {|a,v| self.send("#{a}=", v) if self.respond_to?(a.to_sym) }
|
42
39
|
|
43
40
|
arg
|
44
41
|
end
|
45
42
|
|
46
43
|
# Default configuration settings
|
47
|
-
# @return [Hash]
|
48
44
|
def self.default_configuration
|
49
45
|
{
|
50
46
|
:host => '127.0.0.1',
|
@@ -53,28 +49,11 @@ module Fozzie
|
|
53
49
|
:env => (ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'development'),
|
54
50
|
:appname => '',
|
55
51
|
:namespaces => %w{Stats S Statistics Warehouse},
|
56
|
-
:timeout => 5
|
52
|
+
:timeout => 0.5
|
57
53
|
}.dup
|
58
54
|
end
|
59
55
|
|
60
|
-
|
61
|
-
return self.host unless self.host.match(ip_address_regex).nil?
|
62
|
-
ips = begin
|
63
|
-
Timeout.timeout(self.timeout) { Resolv.getaddresses(self.host) }
|
64
|
-
rescue Timeout::Error => exc
|
65
|
-
[]
|
66
|
-
end
|
67
|
-
(ips.empty? ? "" : ips.compact.reject {|ip| ip.to_s.match(ip_address_regex).nil? }.first || "")
|
68
|
-
end
|
69
|
-
|
70
|
-
def ip_address_regex
|
71
|
-
/^(?:\d{1,3}\.){3}\d{1,3}$/
|
72
|
-
end
|
73
|
-
|
74
|
-
def full_config_path(path)
|
75
|
-
File.expand_path('config/fozzie.yml', path)
|
76
|
-
end
|
77
|
-
|
56
|
+
# Loads the configuration from YAML, if possible
|
78
57
|
def config_from_yaml(args)
|
79
58
|
fp = full_config_path(args[:config_path])
|
80
59
|
return {} unless File.exists?(fp)
|
@@ -82,6 +61,11 @@ module Fozzie
|
|
82
61
|
(cnf.kind_of?(Hash)) ? cnf.symbolize_keys : {}
|
83
62
|
end
|
84
63
|
|
64
|
+
# Returns the absolute file path for the Fozzie configuration, relative to the given path
|
65
|
+
def full_config_path(path)
|
66
|
+
File.expand_path('config/fozzie.yml', path)
|
67
|
+
end
|
68
|
+
|
85
69
|
end
|
86
70
|
|
87
71
|
end
|