librato-rails 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/LICENSE +24 -0
  2. data/README.md +105 -0
  3. data/Rakefile +41 -0
  4. data/lib/librato-rails.rb +1 -0
  5. data/lib/librato/rack.rb +4 -0
  6. data/lib/librato/rack/middleware.rb +47 -0
  7. data/lib/librato/rails.rb +209 -0
  8. data/lib/librato/rails/aggregator.rb +48 -0
  9. data/lib/librato/rails/counter_cache.rb +41 -0
  10. data/lib/librato/rails/group.rb +27 -0
  11. data/lib/librato/rails/helpers.rb +19 -0
  12. data/lib/librato/rails/railtie.rb +18 -0
  13. data/lib/librato/rails/subscribers.rb +77 -0
  14. data/lib/librato/rails/version.rb +5 -0
  15. data/lib/librato/rails/worker.rb +58 -0
  16. data/lib/tasks/metrics-rails_tasks.rake +4 -0
  17. data/test/dummy/README.rdoc +261 -0
  18. data/test/dummy/Rakefile +7 -0
  19. data/test/dummy/app/assets/javascripts/application.js +15 -0
  20. data/test/dummy/app/assets/javascripts/home.js +2 -0
  21. data/test/dummy/app/assets/javascripts/status.js +2 -0
  22. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  23. data/test/dummy/app/assets/stylesheets/home.css +4 -0
  24. data/test/dummy/app/assets/stylesheets/status.css +4 -0
  25. data/test/dummy/app/controllers/application_controller.rb +10 -0
  26. data/test/dummy/app/controllers/home_controller.rb +33 -0
  27. data/test/dummy/app/controllers/status_controller.rb +5 -0
  28. data/test/dummy/app/controllers/user_controller.rb +13 -0
  29. data/test/dummy/app/helpers/application_helper.rb +2 -0
  30. data/test/dummy/app/helpers/home_helper.rb +2 -0
  31. data/test/dummy/app/helpers/status_helper.rb +2 -0
  32. data/test/dummy/app/mailers/user_mailer.rb +8 -0
  33. data/test/dummy/app/models/user.rb +18 -0
  34. data/test/dummy/app/views/home/index.html.erb +2 -0
  35. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  36. data/test/dummy/app/views/status/code.html.erb +2 -0
  37. data/test/dummy/app/views/user_mailer/welcome_email.text.erb +1 -0
  38. data/test/dummy/config.ru +4 -0
  39. data/test/dummy/config/application.rb +63 -0
  40. data/test/dummy/config/boot.rb +10 -0
  41. data/test/dummy/config/database.yml +25 -0
  42. data/test/dummy/config/environment.rb +5 -0
  43. data/test/dummy/config/environments/development.rb +37 -0
  44. data/test/dummy/config/environments/production.rb +67 -0
  45. data/test/dummy/config/environments/test.rb +37 -0
  46. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  47. data/test/dummy/config/initializers/inflections.rb +15 -0
  48. data/test/dummy/config/initializers/mime_types.rb +5 -0
  49. data/test/dummy/config/initializers/secret_token.rb +7 -0
  50. data/test/dummy/config/initializers/session_store.rb +8 -0
  51. data/test/dummy/config/initializers/silence_assets.rb +16 -0
  52. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  53. data/test/dummy/config/locales/en.yml +5 -0
  54. data/test/dummy/config/routes.rb +62 -0
  55. data/test/dummy/config/unicorn.rb +16 -0
  56. data/test/dummy/db/development.sqlite3 +0 -0
  57. data/test/dummy/db/migrate/20120719231810_create_users.rb +10 -0
  58. data/test/dummy/db/schema.rb +23 -0
  59. data/test/dummy/db/test.sqlite3 +0 -0
  60. data/test/dummy/log/development.log +23936 -0
  61. data/test/dummy/log/test.log +17942 -0
  62. data/test/dummy/public/404.html +26 -0
  63. data/test/dummy/public/422.html +26 -0
  64. data/test/dummy/public/500.html +25 -0
  65. data/test/dummy/public/favicon.ico +0 -0
  66. data/test/dummy/script/rails +6 -0
  67. data/test/dummy/test_env.sh +2 -0
  68. data/test/dummy/tmp/cache/assets/C94/D50/sprockets%2F1a49716f234a1b88976e3f09954f8f14 +0 -0
  69. data/test/dummy/tmp/cache/assets/CD8/370/sprockets%2F357970feca3ac29060c1e3861e2c0953 +0 -0
  70. data/test/dummy/tmp/cache/assets/CDF/870/sprockets%2Fb878faf942403e313a5b103e5d80488e +0 -0
  71. data/test/dummy/tmp/cache/assets/CE8/7E0/sprockets%2F178e2a1f9aa891d473009c7f3095df28 +0 -0
  72. data/test/dummy/tmp/cache/assets/CF9/7C0/sprockets%2F40fc2f3d2a468a00e463f1d313cb1683 +0 -0
  73. data/test/dummy/tmp/cache/assets/D04/890/sprockets%2F587335c079eef8d5a63784fc8f99905a +0 -0
  74. data/test/dummy/tmp/cache/assets/D05/D40/sprockets%2F1c9faaf28d05409b88ad3113374d613c +0 -0
  75. data/test/dummy/tmp/cache/assets/D32/A10/sprockets%2F13fe41fee1fe35b49d145bcc06610705 +0 -0
  76. data/test/dummy/tmp/cache/assets/D4E/1B0/sprockets%2Ff7cbd26ba1d28d48de824f0e94586655 +0 -0
  77. data/test/dummy/tmp/cache/assets/D4F/000/sprockets%2F25e44896aac12384727e9dab827ebef9 +0 -0
  78. data/test/dummy/tmp/cache/assets/D51/0D0/sprockets%2Fe895d60a653d8b87f7c5717a4d4cf1f3 +0 -0
  79. data/test/dummy/tmp/cache/assets/D5A/EA0/sprockets%2Fd771ace226fc8215a3572e0aa35bb0d6 +0 -0
  80. data/test/dummy/tmp/cache/assets/D8B/F90/sprockets%2Ffe6ce696e9141eb755d8eed79128e17c +0 -0
  81. data/test/dummy/tmp/cache/assets/D98/8B0/sprockets%2Fedbef6e0d0a4742346cf479f2c522eb0 +0 -0
  82. data/test/dummy/tmp/cache/assets/DA1/8B0/sprockets%2F65acae5ede4e92f105c5e8631407c7fc +0 -0
  83. data/test/dummy/tmp/cache/assets/DA5/7B0/sprockets%2Fa0fc1785d4dc1bde68dd7d5652d27a95 +0 -0
  84. data/test/dummy/tmp/cache/assets/DDC/400/sprockets%2Fcffd775d018f68ce5dba1ee0d951a994 +0 -0
  85. data/test/dummy/tmp/cache/assets/E04/890/sprockets%2F2f5173deea6c795b8fdde723bb4b63af +0 -0
  86. data/test/fixtures/config/librato.yml +10 -0
  87. data/test/integration/helper_test.rb +29 -0
  88. data/test/integration/mail_test.rb +15 -0
  89. data/test/integration/request_test.rb +53 -0
  90. data/test/integration/sql_test.rb +30 -0
  91. data/test/metrics-rails_test.rb +39 -0
  92. data/test/remote/rails_remote_test.rb +105 -0
  93. data/test/support/integration_case.rb +19 -0
  94. data/test/test_helper.rb +23 -0
  95. data/test/unit/aggregator_test.rb +16 -0
  96. data/test/unit/configuration_test.rb +49 -0
  97. data/test/unit/counter_cache_test.rb +19 -0
  98. data/test/unit/group_test.rb +49 -0
  99. data/test/unit/middleware_test.rb +82 -0
  100. data/test/unit/worker_test.rb +31 -0
  101. metadata +300 -0
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ </div>
24
+ </body>
25
+ </html>
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,2 @@
1
+ export LIBRATO_METRICS_USER='matt@librato.com'
2
+ export LIBRATO_METRICS_TOKEN='02673bee476e872a5c40d4529d98c2cfd2d741882c302c1b7f10ccb1ee9eb45e'
@@ -0,0 +1,10 @@
1
+ test:
2
+ email: 'test@bar.com'
3
+ api_key: 'test api key'
4
+ prefix: 'rails-test'
5
+ flush_interval: 30
6
+ source: 'custom-1'
7
+
8
+ production:
9
+ email: live@bar.com
10
+ api_key: 'live api key'
@@ -0,0 +1,29 @@
1
+ require 'test_helper'
2
+
3
+ class HelperTest < ActiveSupport::IntegrationCase
4
+
5
+ test 'controller helpers' do
6
+ visit custom_path
7
+
8
+ assert_equal 1, counters['custom.visits']
9
+ assert_equal 3, counters['custom.events']
10
+
11
+ assert_equal 12, aggregate['custom.timing'][:sum]
12
+ assert_equal 2, aggregate['custom.timing'][:count]
13
+ end
14
+
15
+ test 'model class helpers' do
16
+ visit custom_path
17
+
18
+ assert_equal 3, counters['custom.model.lookups']
19
+ assert_equal 19.0, aggregate['custom.model.search'][:sum]
20
+ assert_equal 2, aggregate['custom.model.search'][:count]
21
+ end
22
+
23
+ test 'model instance helpers' do
24
+ visit custom_path
25
+
26
+ assert_equal 1, counters['custom.model.touch']
27
+ end
28
+
29
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ class MailTest < ActiveSupport::IntegrationCase
4
+
5
+ # Query tests - the numbers specified assume running against SQLite
6
+
7
+ test 'mail sent' do
8
+ prefix = Librato::Rails.prefix
9
+
10
+ user = User.create!(:email => 'foo@foo.com', :password => 'wow')
11
+ UserMailer.welcome_email(user).deliver
12
+ assert_equal 1, counters["#{prefix}.mail.sent"]
13
+ end
14
+
15
+ end
@@ -0,0 +1,53 @@
1
+ require 'test_helper'
2
+
3
+ class RequestTest < ActiveSupport::IntegrationCase
4
+
5
+ # Each request
6
+
7
+ test 'increment total and status' do
8
+ prefix = Librato::Rails.prefix
9
+ visit root_path
10
+
11
+ assert_equal 1, counters["#{prefix}.request.total"]
12
+ assert_equal 1, counters["#{prefix}.request.status.200"]
13
+ assert_equal 1, counters["#{prefix}.request.status.2xx"]
14
+
15
+ visit '/status/204'
16
+
17
+ assert_equal 2, counters["#{prefix}.request.total"]
18
+ assert_equal 1, counters["#{prefix}.request.status.200"]
19
+ assert_equal 1, counters["#{prefix}.request.status.204"]
20
+ assert_equal 2, counters["#{prefix}.request.status.2xx"]
21
+ end
22
+
23
+ test 'request times' do
24
+ prefix = Librato::Rails.prefix
25
+ visit root_path
26
+
27
+ # common for all paths
28
+ assert_equal 1, aggregate["#{prefix}.request.time"][:count], 'should record total time'
29
+ assert_equal 1, aggregate["#{prefix}.request.time.db"][:count], 'should record db time'
30
+ assert_equal 1, aggregate["#{prefix}.request.time.view"][:count], 'should record view time'
31
+
32
+ # status specific
33
+ assert_equal 1, aggregate["#{prefix}.request.status.200.time"][:count]
34
+ assert_equal 1, aggregate["#{prefix}.request.status.2xx.time"][:count]
35
+ end
36
+
37
+ test 'track exceptions' do
38
+ prefix = Librato::Rails.prefix
39
+ begin
40
+ visit exception_path #rescue nil
41
+ rescue RuntimeError => e
42
+ raise unless e.message == 'test exception!'
43
+ end
44
+ assert_equal 1, counters["#{prefix}.request.exceptions"]
45
+ end
46
+
47
+ test 'track slow requests' do
48
+ prefix = Librato::Rails.prefix
49
+ visit slow_path
50
+ assert_equal 1, counters["#{prefix}.request.slow"]
51
+ end
52
+
53
+ end
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+
3
+ class SQLTest < ActiveSupport::IntegrationCase
4
+
5
+ # Query tests - the numbers specified assume running against SQLite
6
+
7
+ test 'total queries and query types' do
8
+ prefix = Librato::Rails.prefix
9
+
10
+ # note that modifying queries are wrapped in a transaction which
11
+ # adds 2 to total queries per operation.
12
+ user = User.create!(:email => 'foo@foo.com', :password => 'wow')
13
+ assert_equal 3, counters["#{prefix}.sql.queries"]
14
+ assert_equal 1, counters["#{prefix}.sql.inserts"]
15
+
16
+ foo = User.find_by_email('foo@foo.com')
17
+ assert_equal 4, counters["#{prefix}.sql.queries"]
18
+ assert_equal 1, counters["#{prefix}.sql.selects"]
19
+
20
+ foo.password = 'new password'
21
+ foo.save
22
+ assert_equal 7, counters["#{prefix}.sql.queries"]
23
+ assert_equal 1, counters["#{prefix}.sql.updates"]
24
+
25
+ foo.destroy
26
+ assert_equal 10, counters["#{prefix}.sql.queries"]
27
+ assert_equal 1, counters["#{prefix}.sql.deletes"]
28
+ end
29
+
30
+ end
@@ -0,0 +1,39 @@
1
+ require 'test_helper'
2
+
3
+ class LibratoRailsTest < ActiveSupport::TestCase
4
+
5
+ test 'is a module' do
6
+ assert_kind_of Module, Librato::Rails
7
+ end
8
+
9
+ test 'client is available' do
10
+ assert_kind_of Librato::Metrics::Client, Librato::Rails.client
11
+ end
12
+
13
+ test '#increment exists' do
14
+ assert Librato::Rails.respond_to?(:increment)
15
+ Librato::Rails.increment :baz, 5
16
+ end
17
+
18
+ test '#measure exists' do
19
+ assert Librato::Rails.respond_to?(:measure)
20
+ Librato::Rails.timing 'queries', 10
21
+ end
22
+
23
+ test '#timing exists' do
24
+ assert Librato::Rails.respond_to?(:timing)
25
+ Librato::Rails.timing 'request.time.total', 121.2
26
+ end
27
+
28
+ test 'source is assignable' do
29
+ original = Librato::Rails.source
30
+ Librato::Rails.source = 'foobar'
31
+ assert_equal 'foobar', Librato::Rails.source
32
+ Librato::Rails.source = original
33
+ end
34
+
35
+ test 'qualified source includes pid' do
36
+ assert_match /\.\d{2,6}$/, Librato::Rails.qualified_source
37
+ end
38
+
39
+ end
@@ -0,0 +1,105 @@
1
+ require 'test_helper'
2
+
3
+ class LibratoRailsRemoteTest < ActiveSupport::TestCase
4
+
5
+ # These tests connect to the Metrics server with an account and verify remote
6
+ # functions. They will only run if the below environment variables are set.
7
+ #
8
+ # BE CAREFUL, running these tests will DELETE ALL metrics currently in the
9
+ # test account.
10
+ #
11
+ if ENV['LIBRATO_RAILS_TEST_EMAIL'] && ENV['LIBRATO_RAILS_TEST_API_KEY']
12
+
13
+ setup do
14
+ # delete any generated Librato::Rails
15
+ Librato::Rails.email = ENV['LIBRATO_RAILS_TEST_EMAIL']
16
+ Librato::Rails.api_key = ENV['LIBRATO_RAILS_TEST_API_KEY']
17
+ if ENV['LIBRATO_RAILS_TEST_API_ENDPOINT']
18
+ Librato::Rails.api_endpoint = ENV['LIBRATO_RAILS_TEST_API_ENDPOINT']
19
+ end
20
+ Librato::Rails.delete_all
21
+ end
22
+
23
+ test 'flush sends counters' do
24
+ delete_all_metrics
25
+ source = Librato::Rails.qualified_source
26
+
27
+ Librato::Rails.increment :foo
28
+ Librato::Rails.increment :bar, 2
29
+ Librato::Rails.increment :foo
30
+ Librato::Rails.flush
31
+
32
+ client = Librato::Rails.client
33
+ metric_names = client.list.map { |m| m['name'] }
34
+ assert metric_names.include?('foo'), 'foo should be present'
35
+ assert metric_names.include?('bar'), 'bar should be present'
36
+
37
+ foo = client.fetch 'foo', :count => 10
38
+ assert_equal 1, foo[source].length
39
+ assert_equal 2, foo[source][0]['value']
40
+
41
+ bar = client.fetch 'bar', :count => 10
42
+ assert_equal 1, bar[source].length
43
+ assert_equal 2, bar[source][0]['value']
44
+ end
45
+
46
+ test 'counters should persist through flush' do
47
+ Librato::Rails.increment 'knightrider'
48
+ Librato::Rails.flush
49
+ assert_equal 1, Librato::Rails.counters['knightrider']
50
+ end
51
+
52
+ test 'flush sends measures/timings' do
53
+ delete_all_metrics
54
+ source = Librato::Rails.qualified_source
55
+
56
+ Librato::Rails.timing 'request.time.total', 122.1
57
+ Librato::Rails.measure 'items_bought', 20
58
+ Librato::Rails.timing 'request.time.total', 81.3
59
+ Librato::Rails.flush
60
+
61
+ client = Librato::Rails.client
62
+ metric_names = client.list.map { |m| m['name'] }
63
+ assert metric_names.include?('request.time.total'), 'request.time.total should be present'
64
+ assert metric_names.include?('items_bought'), 'request.time.db should be present'
65
+
66
+ total = client.fetch 'request.time.total', :count => 10
67
+ assert_equal 2, total[source][0]['count']
68
+ assert_in_delta 203.4, total[source][0]['sum'], 0.1
69
+
70
+ items = client.fetch 'items_bought', :count => 10
71
+ assert_equal 1, items[source][0]['count']
72
+ assert_in_delta 20, items[source][0]['sum'], 0.1
73
+ end
74
+
75
+ test 'flush should purge measures/timings' do
76
+ delete_all_metrics
77
+
78
+ Librato::Rails.timing 'request.time.total', 122.1
79
+ Librato::Rails.measure 'items_bought', 20
80
+ Librato::Rails.flush
81
+
82
+ assert Librato::Rails.aggregate.empty?, 'measures and timings should be cleared with flush'
83
+ end
84
+
85
+ test 'empty flush should not be sent' do
86
+ delete_all_metrics
87
+ Librato::Rails.flush
88
+
89
+ assert_equal [], Librato::Rails.client.list
90
+ end
91
+
92
+ private
93
+
94
+ def delete_all_metrics
95
+ client = Librato::Rails.client
96
+ client.list.each do |metric|
97
+ client.connection.delete("metrics/#{metric['name']}")
98
+ end
99
+ end
100
+
101
+ else
102
+ puts "Skipping remote tests..."
103
+ end
104
+
105
+ end
@@ -0,0 +1,19 @@
1
+ class ActiveSupport::IntegrationCase < ActiveSupport::TestCase
2
+ include Capybara::DSL
3
+ include Rails.application.routes.url_helpers
4
+
5
+ setup do
6
+ # remove any accumulated metrics
7
+ Librato::Rails.delete_all
8
+ end
9
+
10
+ private
11
+
12
+ def aggregate
13
+ Librato::Rails.aggregate
14
+ end
15
+
16
+ def counters
17
+ Librato::Rails.counters
18
+ end
19
+ end