appsignal 2.5.0.alpha.1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (211) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +33 -0
  3. data/.rspec +4 -0
  4. data/.rubocop.yml +66 -0
  5. data/.rubocop_todo.yml +124 -0
  6. data/.travis.yml +72 -0
  7. data/.yardopts +8 -0
  8. data/CHANGELOG.md +639 -0
  9. data/Gemfile +3 -0
  10. data/LICENSE +20 -0
  11. data/README.md +264 -0
  12. data/Rakefile +214 -0
  13. data/appsignal.gemspec +42 -0
  14. data/benchmark.rake +77 -0
  15. data/bin/appsignal +13 -0
  16. data/ext/Rakefile +27 -0
  17. data/ext/agent.yml +64 -0
  18. data/ext/appsignal_extension.c +692 -0
  19. data/ext/base.rb +79 -0
  20. data/ext/extconf.rb +35 -0
  21. data/gemfiles/capistrano2.gemfile +7 -0
  22. data/gemfiles/capistrano3.gemfile +7 -0
  23. data/gemfiles/grape.gemfile +7 -0
  24. data/gemfiles/no_dependencies.gemfile +5 -0
  25. data/gemfiles/padrino.gemfile +7 -0
  26. data/gemfiles/que.gemfile +5 -0
  27. data/gemfiles/rails-3.2.gemfile +6 -0
  28. data/gemfiles/rails-4.0.gemfile +6 -0
  29. data/gemfiles/rails-4.1.gemfile +6 -0
  30. data/gemfiles/rails-4.2.gemfile +10 -0
  31. data/gemfiles/rails-5.0.gemfile +5 -0
  32. data/gemfiles/rails-5.1.gemfile +5 -0
  33. data/gemfiles/resque.gemfile +12 -0
  34. data/gemfiles/sequel-435.gemfile +11 -0
  35. data/gemfiles/sequel.gemfile +11 -0
  36. data/gemfiles/sinatra.gemfile +6 -0
  37. data/gemfiles/webmachine.gemfile +5 -0
  38. data/lib/appsignal.rb +804 -0
  39. data/lib/appsignal/auth_check.rb +65 -0
  40. data/lib/appsignal/capistrano.rb +10 -0
  41. data/lib/appsignal/cli.rb +108 -0
  42. data/lib/appsignal/cli/demo.rb +63 -0
  43. data/lib/appsignal/cli/diagnose.rb +500 -0
  44. data/lib/appsignal/cli/helpers.rb +72 -0
  45. data/lib/appsignal/cli/install.rb +277 -0
  46. data/lib/appsignal/cli/notify_of_deploy.rb +113 -0
  47. data/lib/appsignal/config.rb +287 -0
  48. data/lib/appsignal/demo.rb +107 -0
  49. data/lib/appsignal/event_formatter.rb +74 -0
  50. data/lib/appsignal/event_formatter/action_view/render_formatter.rb +24 -0
  51. data/lib/appsignal/event_formatter/active_record/instantiation_formatter.rb +14 -0
  52. data/lib/appsignal/event_formatter/active_record/sql_formatter.rb +14 -0
  53. data/lib/appsignal/event_formatter/elastic_search/search_formatter.rb +32 -0
  54. data/lib/appsignal/event_formatter/faraday/request_formatter.rb +19 -0
  55. data/lib/appsignal/event_formatter/mongo_ruby_driver/query_formatter.rb +89 -0
  56. data/lib/appsignal/event_formatter/moped/query_formatter.rb +80 -0
  57. data/lib/appsignal/extension.rb +63 -0
  58. data/lib/appsignal/extension/jruby.rb +460 -0
  59. data/lib/appsignal/garbage_collection_profiler.rb +48 -0
  60. data/lib/appsignal/hooks.rb +105 -0
  61. data/lib/appsignal/hooks/action_cable.rb +113 -0
  62. data/lib/appsignal/hooks/active_support_notifications.rb +52 -0
  63. data/lib/appsignal/hooks/celluloid.rb +30 -0
  64. data/lib/appsignal/hooks/data_mapper.rb +18 -0
  65. data/lib/appsignal/hooks/delayed_job.rb +19 -0
  66. data/lib/appsignal/hooks/mongo_ruby_driver.rb +21 -0
  67. data/lib/appsignal/hooks/net_http.rb +29 -0
  68. data/lib/appsignal/hooks/passenger.rb +22 -0
  69. data/lib/appsignal/hooks/puma.rb +35 -0
  70. data/lib/appsignal/hooks/que.rb +21 -0
  71. data/lib/appsignal/hooks/rake.rb +39 -0
  72. data/lib/appsignal/hooks/redis.rb +30 -0
  73. data/lib/appsignal/hooks/sequel.rb +60 -0
  74. data/lib/appsignal/hooks/shoryuken.rb +43 -0
  75. data/lib/appsignal/hooks/sidekiq.rb +144 -0
  76. data/lib/appsignal/hooks/unicorn.rb +40 -0
  77. data/lib/appsignal/hooks/webmachine.rb +23 -0
  78. data/lib/appsignal/integrations/capistrano/appsignal.cap +39 -0
  79. data/lib/appsignal/integrations/capistrano/capistrano_2_tasks.rb +52 -0
  80. data/lib/appsignal/integrations/data_mapper.rb +33 -0
  81. data/lib/appsignal/integrations/delayed_job_plugin.rb +54 -0
  82. data/lib/appsignal/integrations/grape.rb +53 -0
  83. data/lib/appsignal/integrations/mongo_ruby_driver.rb +55 -0
  84. data/lib/appsignal/integrations/object.rb +35 -0
  85. data/lib/appsignal/integrations/padrino.rb +84 -0
  86. data/lib/appsignal/integrations/que.rb +43 -0
  87. data/lib/appsignal/integrations/railtie.rb +41 -0
  88. data/lib/appsignal/integrations/rake.rb +2 -0
  89. data/lib/appsignal/integrations/resque.rb +20 -0
  90. data/lib/appsignal/integrations/resque_active_job.rb +30 -0
  91. data/lib/appsignal/integrations/sinatra.rb +17 -0
  92. data/lib/appsignal/integrations/webmachine.rb +38 -0
  93. data/lib/appsignal/js_exception_transaction.rb +54 -0
  94. data/lib/appsignal/marker.rb +63 -0
  95. data/lib/appsignal/minutely.rb +42 -0
  96. data/lib/appsignal/rack/generic_instrumentation.rb +49 -0
  97. data/lib/appsignal/rack/js_exception_catcher.rb +70 -0
  98. data/lib/appsignal/rack/rails_instrumentation.rb +51 -0
  99. data/lib/appsignal/rack/sinatra_instrumentation.rb +99 -0
  100. data/lib/appsignal/rack/streaming_listener.rb +73 -0
  101. data/lib/appsignal/system.rb +81 -0
  102. data/lib/appsignal/transaction.rb +498 -0
  103. data/lib/appsignal/transmitter.rb +107 -0
  104. data/lib/appsignal/utils.rb +127 -0
  105. data/lib/appsignal/utils/params_sanitizer.rb +59 -0
  106. data/lib/appsignal/utils/query_params_sanitizer.rb +55 -0
  107. data/lib/appsignal/version.rb +3 -0
  108. data/lib/sequel/extensions/appsignal_integration.rb +3 -0
  109. data/resources/appsignal.yml.erb +39 -0
  110. data/resources/cacert.pem +3866 -0
  111. data/spec/.rubocop.yml +7 -0
  112. data/spec/lib/appsignal/auth_check_spec.rb +80 -0
  113. data/spec/lib/appsignal/capistrano2_spec.rb +224 -0
  114. data/spec/lib/appsignal/capistrano3_spec.rb +237 -0
  115. data/spec/lib/appsignal/cli/demo_spec.rb +67 -0
  116. data/spec/lib/appsignal/cli/diagnose_spec.rb +988 -0
  117. data/spec/lib/appsignal/cli/helpers_spec.rb +171 -0
  118. data/spec/lib/appsignal/cli/install_spec.rb +632 -0
  119. data/spec/lib/appsignal/cli/notify_of_deploy_spec.rb +168 -0
  120. data/spec/lib/appsignal/cli_spec.rb +56 -0
  121. data/spec/lib/appsignal/config_spec.rb +637 -0
  122. data/spec/lib/appsignal/demo_spec.rb +87 -0
  123. data/spec/lib/appsignal/event_formatter/action_view/render_formatter_spec.rb +44 -0
  124. data/spec/lib/appsignal/event_formatter/active_record/instantiation_formatter_spec.rb +21 -0
  125. data/spec/lib/appsignal/event_formatter/active_record/sql_formatter_spec.rb +21 -0
  126. data/spec/lib/appsignal/event_formatter/elastic_search/search_formatter_spec.rb +52 -0
  127. data/spec/lib/appsignal/event_formatter/faraday/request_formatter_spec.rb +21 -0
  128. data/spec/lib/appsignal/event_formatter/mongo_ruby_driver/query_formatter_spec.rb +113 -0
  129. data/spec/lib/appsignal/event_formatter/moped/query_formatter_spec.rb +112 -0
  130. data/spec/lib/appsignal/event_formatter_spec.rb +100 -0
  131. data/spec/lib/appsignal/extension/jruby_spec.rb +43 -0
  132. data/spec/lib/appsignal/extension_spec.rb +137 -0
  133. data/spec/lib/appsignal/garbage_collection_profiler_spec.rb +66 -0
  134. data/spec/lib/appsignal/hooks/action_cable_spec.rb +370 -0
  135. data/spec/lib/appsignal/hooks/active_support_notifications_spec.rb +92 -0
  136. data/spec/lib/appsignal/hooks/celluloid_spec.rb +35 -0
  137. data/spec/lib/appsignal/hooks/data_mapper_spec.rb +39 -0
  138. data/spec/lib/appsignal/hooks/delayed_job_spec.rb +358 -0
  139. data/spec/lib/appsignal/hooks/mongo_ruby_driver_spec.rb +44 -0
  140. data/spec/lib/appsignal/hooks/net_http_spec.rb +53 -0
  141. data/spec/lib/appsignal/hooks/passenger_spec.rb +30 -0
  142. data/spec/lib/appsignal/hooks/puma_spec.rb +80 -0
  143. data/spec/lib/appsignal/hooks/que_spec.rb +19 -0
  144. data/spec/lib/appsignal/hooks/rake_spec.rb +73 -0
  145. data/spec/lib/appsignal/hooks/redis_spec.rb +55 -0
  146. data/spec/lib/appsignal/hooks/sequel_spec.rb +46 -0
  147. data/spec/lib/appsignal/hooks/shoryuken_spec.rb +192 -0
  148. data/spec/lib/appsignal/hooks/sidekiq_spec.rb +419 -0
  149. data/spec/lib/appsignal/hooks/unicorn_spec.rb +52 -0
  150. data/spec/lib/appsignal/hooks/webmachine_spec.rb +35 -0
  151. data/spec/lib/appsignal/hooks_spec.rb +195 -0
  152. data/spec/lib/appsignal/integrations/data_mapper_spec.rb +65 -0
  153. data/spec/lib/appsignal/integrations/grape_spec.rb +225 -0
  154. data/spec/lib/appsignal/integrations/mongo_ruby_driver_spec.rb +127 -0
  155. data/spec/lib/appsignal/integrations/object_spec.rb +249 -0
  156. data/spec/lib/appsignal/integrations/padrino_spec.rb +323 -0
  157. data/spec/lib/appsignal/integrations/que_spec.rb +174 -0
  158. data/spec/lib/appsignal/integrations/railtie_spec.rb +129 -0
  159. data/spec/lib/appsignal/integrations/resque_active_job_spec.rb +83 -0
  160. data/spec/lib/appsignal/integrations/resque_spec.rb +92 -0
  161. data/spec/lib/appsignal/integrations/sinatra_spec.rb +73 -0
  162. data/spec/lib/appsignal/integrations/webmachine_spec.rb +69 -0
  163. data/spec/lib/appsignal/js_exception_transaction_spec.rb +128 -0
  164. data/spec/lib/appsignal/marker_spec.rb +51 -0
  165. data/spec/lib/appsignal/minutely_spec.rb +50 -0
  166. data/spec/lib/appsignal/rack/generic_instrumentation_spec.rb +90 -0
  167. data/spec/lib/appsignal/rack/js_exception_catcher_spec.rb +147 -0
  168. data/spec/lib/appsignal/rack/rails_instrumentation_spec.rb +117 -0
  169. data/spec/lib/appsignal/rack/sinatra_instrumentation_spec.rb +213 -0
  170. data/spec/lib/appsignal/rack/streaming_listener_spec.rb +161 -0
  171. data/spec/lib/appsignal/system_spec.rb +131 -0
  172. data/spec/lib/appsignal/transaction_spec.rb +1146 -0
  173. data/spec/lib/appsignal/transmitter_spec.rb +152 -0
  174. data/spec/lib/appsignal/utils/params_sanitizer_spec.rb +136 -0
  175. data/spec/lib/appsignal/utils/query_params_sanitizer_spec.rb +192 -0
  176. data/spec/lib/appsignal/utils_spec.rb +150 -0
  177. data/spec/lib/appsignal_spec.rb +1049 -0
  178. data/spec/spec_helper.rb +116 -0
  179. data/spec/support/fixtures/containers/cgroups/docker +14 -0
  180. data/spec/support/fixtures/containers/cgroups/docker_systemd +8 -0
  181. data/spec/support/fixtures/containers/cgroups/lxc +10 -0
  182. data/spec/support/fixtures/containers/cgroups/no_permission +0 -0
  183. data/spec/support/fixtures/containers/cgroups/none +1 -0
  184. data/spec/support/fixtures/generated_config.yml +24 -0
  185. data/spec/support/fixtures/uploaded_file.txt +0 -0
  186. data/spec/support/helpers/api_request_helper.rb +19 -0
  187. data/spec/support/helpers/cli_helpers.rb +26 -0
  188. data/spec/support/helpers/config_helpers.rb +21 -0
  189. data/spec/support/helpers/dependency_helper.rb +73 -0
  190. data/spec/support/helpers/directory_helper.rb +27 -0
  191. data/spec/support/helpers/env_helpers.rb +33 -0
  192. data/spec/support/helpers/example_exception.rb +13 -0
  193. data/spec/support/helpers/example_standard_error.rb +13 -0
  194. data/spec/support/helpers/log_helpers.rb +22 -0
  195. data/spec/support/helpers/std_streams_helper.rb +66 -0
  196. data/spec/support/helpers/system_helpers.rb +8 -0
  197. data/spec/support/helpers/time_helpers.rb +11 -0
  198. data/spec/support/helpers/transaction_helpers.rb +37 -0
  199. data/spec/support/matchers/contains_log.rb +7 -0
  200. data/spec/support/mocks/fake_gc_profiler.rb +19 -0
  201. data/spec/support/mocks/mock_extension.rb +6 -0
  202. data/spec/support/project_fixture/config/application.rb +0 -0
  203. data/spec/support/project_fixture/config/appsignal.yml +32 -0
  204. data/spec/support/project_fixture/config/environments/development.rb +0 -0
  205. data/spec/support/project_fixture/config/environments/production.rb +0 -0
  206. data/spec/support/project_fixture/config/environments/test.rb +0 -0
  207. data/spec/support/project_fixture/log/.gitkeep +0 -0
  208. data/spec/support/rails/my_app.rb +6 -0
  209. data/spec/support/shared_examples/instrument.rb +43 -0
  210. data/spec/support/stubs/delayed_job.rb +0 -0
  211. metadata +483 -0
@@ -0,0 +1,87 @@
1
+ require "appsignal/demo"
2
+
3
+ describe Appsignal::Demo do
4
+ before do
5
+ # Ignore sleeps to speed up the test
6
+ allow(described_class).to receive(:sleep)
7
+ end
8
+
9
+ describe ".transmit" do
10
+ subject { described_class.transmit }
11
+
12
+ context "without config" do
13
+ it "returns false" do
14
+ expect(silence { subject }).to eq(false)
15
+ end
16
+ end
17
+
18
+ context "with config" do
19
+ let(:config) { project_fixture_config("production") }
20
+ before { Appsignal.config = config }
21
+
22
+ it "returns true" do
23
+ expect(subject).to eq(true)
24
+ end
25
+
26
+ it "creates demonstration samples" do
27
+ expect(described_class).to receive(:create_example_error_request)
28
+ expect(described_class).to receive(:create_example_performance_request)
29
+ subject
30
+ end
31
+ end
32
+ end
33
+
34
+ describe ".create_example_error_request" do
35
+ let!(:error_transaction) { http_request_transaction }
36
+ let(:config) { project_fixture_config("production") }
37
+ before do
38
+ Appsignal.config = config
39
+ expect(Appsignal::Transaction).to receive(:new).with(
40
+ kind_of(String),
41
+ Appsignal::Transaction::HTTP_REQUEST,
42
+ kind_of(::Rack::Request),
43
+ kind_of(Hash)
44
+ ).and_return(error_transaction)
45
+ end
46
+ subject { described_class.send(:create_example_error_request) }
47
+
48
+ it "sets an error" do
49
+ expect(error_transaction).to receive(:set_error).with(kind_of(described_class::TestError))
50
+ expect(error_transaction).to receive(:set_metadata).with("path", "/hello")
51
+ expect(error_transaction).to receive(:set_metadata).with("method", "GET")
52
+ expect(error_transaction).to receive(:set_metadata).with("demo_sample", "true")
53
+ expect(error_transaction).to receive(:complete)
54
+ subject
55
+ end
56
+ end
57
+
58
+ describe ".create_example_performance_request" do
59
+ let!(:performance_transaction) { http_request_transaction }
60
+ let(:config) { project_fixture_config("production") }
61
+ before do
62
+ Appsignal.config = config
63
+ expect(Appsignal::Transaction).to receive(:new).with(
64
+ kind_of(String),
65
+ Appsignal::Transaction::HTTP_REQUEST,
66
+ kind_of(::Rack::Request),
67
+ kind_of(Hash)
68
+ ).and_return(performance_transaction)
69
+ end
70
+ subject { described_class.send(:create_example_performance_request) }
71
+
72
+ it "sends a performance sample" do
73
+ expect(performance_transaction).to receive(:start_event)
74
+ expect(performance_transaction).to receive(:finish_event).with(
75
+ "action_view.render",
76
+ "Render hello.html.erb",
77
+ "<h1>Hello world!</h1>",
78
+ Appsignal::EventFormatter::DEFAULT
79
+ )
80
+ expect(performance_transaction).to receive(:set_metadata).with("path", "/hello")
81
+ expect(performance_transaction).to receive(:set_metadata).with("method", "GET")
82
+ expect(performance_transaction).to receive(:set_metadata).with("demo_sample", "true")
83
+ expect(performance_transaction).to receive(:complete)
84
+ subject
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,44 @@
1
+ if DependencyHelper.rails_present?
2
+ require "action_view"
3
+
4
+ describe Appsignal::EventFormatter::ActionView::RenderFormatter do
5
+ before { allow(Rails.root).to receive(:to_s).and_return("/var/www/app/20130101") }
6
+ let(:klass) { Appsignal::EventFormatter::ActionView::RenderFormatter }
7
+ let(:formatter) { klass.new }
8
+
9
+ it "should register render_partial.action_view and render_template.action_view" do
10
+ expect(Appsignal::EventFormatter.registered?("render_partial.action_view", klass)).to be_truthy
11
+ expect(Appsignal::EventFormatter.registered?("render_template.action_view", klass)).to be_truthy
12
+ end
13
+
14
+ describe "#root_path" do
15
+ subject { formatter.root_path }
16
+
17
+ it "returns Rails root path" do
18
+ is_expected.to eq "/var/www/app/20130101/"
19
+ end
20
+ end
21
+
22
+ describe "#format" do
23
+ subject { formatter.format(payload) }
24
+
25
+ context "with an identifier" do
26
+ let(:payload) { { :identifier => "/var/www/app/20130101/app/views/home/index/html.erb" } }
27
+
28
+ it { is_expected.to eq ["app/views/home/index/html.erb", nil] }
29
+ end
30
+
31
+ context "with a frozen identifier" do
32
+ let(:payload) { { :identifier => "/var/www/app/20130101/app/views/home/index/html.erb".freeze } }
33
+
34
+ it { is_expected.to eq ["app/views/home/index/html.erb", nil] }
35
+ end
36
+
37
+ context "without an identifier" do
38
+ let(:payload) { {} }
39
+
40
+ it { is_expected.to be_nil }
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,21 @@
1
+ describe Appsignal::EventFormatter::ActiveRecord::InstantiationFormatter do
2
+ let(:klass) { Appsignal::EventFormatter::ActiveRecord::InstantiationFormatter }
3
+ let(:formatter) { klass.new }
4
+
5
+ it "should register instantiation.active_record" do
6
+ expect(Appsignal::EventFormatter.registered?("instantiation.active_record", klass)).to be_truthy
7
+ end
8
+
9
+ describe "#format" do
10
+ let(:payload) do
11
+ {
12
+ :record_count => 1,
13
+ :class_name => "User"
14
+ }
15
+ end
16
+
17
+ subject { formatter.format(payload) }
18
+
19
+ it { is_expected.to eq ["User", nil] }
20
+ end
21
+ end
@@ -0,0 +1,21 @@
1
+ describe Appsignal::EventFormatter::ActiveRecord::InstantiationFormatter do
2
+ let(:klass) { Appsignal::EventFormatter::ActiveRecord::SqlFormatter }
3
+ let(:formatter) { klass.new }
4
+
5
+ it "should register sql.active_record" do
6
+ expect(Appsignal::EventFormatter.registered?("sql.active_record", klass)).to be_truthy
7
+ end
8
+
9
+ describe "#format" do
10
+ let(:payload) do
11
+ {
12
+ :name => "User load",
13
+ :sql => "SELECT * FROM users"
14
+ }
15
+ end
16
+
17
+ subject { formatter.format(payload) }
18
+
19
+ it { is_expected.to eq ["User load", "SELECT * FROM users", 1] }
20
+ end
21
+ end
@@ -0,0 +1,52 @@
1
+ describe Appsignal::EventFormatter::ElasticSearch::SearchFormatter do
2
+ let(:klass) { Appsignal::EventFormatter::ElasticSearch::SearchFormatter }
3
+ let(:formatter) { klass.new }
4
+
5
+ it "should register query.moped" do
6
+ expect(
7
+ Appsignal::EventFormatter.registered?("search.elasticsearch", klass)
8
+ ).to be_truthy
9
+ end
10
+
11
+ describe "#format" do
12
+ let(:payload) do
13
+ {
14
+ :name => "Search",
15
+ :klass => "User",
16
+ :search => { :index => "users", :type => "user", :q => "John Doe" }
17
+ }
18
+ end
19
+
20
+ it "should return a payload with name and sanitized body" do
21
+ expect(formatter.format(payload)).to eql([
22
+ "Search: User",
23
+ "{:index=>\"users\", :type=>\"user\", :q=>\"?\"}"
24
+ ])
25
+ end
26
+ end
27
+
28
+ describe "#sanitized_search" do
29
+ let(:search) do
30
+ {
31
+ :index => "users",
32
+ :type => "user",
33
+ :q => "John Doe",
34
+ :other => "Other"
35
+ }
36
+ end
37
+
38
+ it "should sanitize non-whitelisted params" do
39
+ expect(
40
+ formatter.sanitized_search(search)
41
+ ).to eql(:index => "users", :type => "user", :q => "?", :other => "?")
42
+ end
43
+
44
+ it "should return nil string when search is nil" do
45
+ expect(formatter.sanitized_search(nil)).to be_nil
46
+ end
47
+
48
+ it "should return nil string when search is not a hash" do
49
+ expect(formatter.sanitized_search([])).to be_nil
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,21 @@
1
+ describe Appsignal::EventFormatter::Faraday::RequestFormatter do
2
+ let(:klass) { Appsignal::EventFormatter::Faraday::RequestFormatter }
3
+ let(:formatter) { klass.new }
4
+
5
+ it "should register request.faraday" do
6
+ expect(Appsignal::EventFormatter.registered?("request.faraday", klass)).to be_truthy
7
+ end
8
+
9
+ describe "#format" do
10
+ let(:payload) do
11
+ {
12
+ :method => :get,
13
+ :url => URI.parse("http://example.org/hello/world?some=param")
14
+ }
15
+ end
16
+
17
+ subject { formatter.format(payload) }
18
+
19
+ it { is_expected.to eq ["GET http://example.org", "GET http://example.org/hello/world"] }
20
+ end
21
+ end
@@ -0,0 +1,113 @@
1
+ describe Appsignal::EventFormatter::MongoRubyDriver::QueryFormatter do
2
+ let(:formatter) { Appsignal::EventFormatter::MongoRubyDriver::QueryFormatter }
3
+
4
+ describe ".format" do
5
+ let(:strategy) { :find }
6
+ let(:command) do
7
+ {
8
+ "find" => "users",
9
+ "filter" => { "_id" => 1 }
10
+ }
11
+ end
12
+
13
+ it "should apply a strategy for each key" do
14
+ expect(formatter).to receive(:apply_strategy)
15
+ .with(:sanitize_document, "_id" => 1)
16
+
17
+ expect(formatter).to receive(:apply_strategy)
18
+ .with(:allow, "users")
19
+
20
+ formatter.format(strategy, command)
21
+ end
22
+
23
+ context "when strategy is unkown" do
24
+ let(:strategy) { :bananas }
25
+
26
+ it "should return an empty hash" do
27
+ expect(formatter.format(strategy, command)).to eql({})
28
+ end
29
+ end
30
+
31
+ context "when command is not a hash " do
32
+ let(:command) { :bananas }
33
+
34
+ it "should return an empty hash" do
35
+ expect(formatter.format(strategy, command)).to eql({})
36
+ end
37
+ end
38
+ end
39
+
40
+ describe ".apply_strategy" do
41
+ context "when strategy is allow" do
42
+ let(:strategy) { :allow }
43
+ let(:value) { { "_id" => 1 } }
44
+
45
+ it "should return the given value" do
46
+ expect(formatter.apply_strategy(strategy, value)).to eql(value)
47
+ end
48
+ end
49
+
50
+ context "when strategy is deny" do
51
+ let(:strategy) { :deny }
52
+ let(:value) { { "_id" => 1 } }
53
+
54
+ it "should return a '?'" do
55
+ expect(formatter.apply_strategy(strategy, value)).to eql("?")
56
+ end
57
+ end
58
+
59
+ context "when strategy is deny_array" do
60
+ let(:strategy) { :deny_array }
61
+ let(:value) { { "_id" => 1 } }
62
+
63
+ it "should return a sanitized array string" do
64
+ expect(formatter.apply_strategy(strategy, value)).to eql("[?]")
65
+ end
66
+ end
67
+
68
+ context "when strategy is sanitize_document" do
69
+ let(:strategy) { :sanitize_document }
70
+ let(:value) { { "_id" => 1 } }
71
+
72
+ it "should return a sanitized document" do
73
+ expect(formatter.apply_strategy(strategy, value)).to eql("_id" => "?")
74
+ end
75
+ end
76
+
77
+ context "when strategy is sanitize_bulk" do
78
+ let(:strategy) { :sanitize_bulk }
79
+ let(:value) { [{ "q" => { "_id" => 1 }, "u" => [{ "foo" => "bar" }] }] }
80
+
81
+ it "should return an array of sanitized bulk documents" do
82
+ expect(formatter.apply_strategy(strategy, value)).to eql([
83
+ { "q" => { "_id" => "?" }, "u" => "[?]" }
84
+ ])
85
+ end
86
+
87
+ context "when bulk has more than one update" do
88
+ let(:value) do
89
+ [
90
+ { "q" => { "_id" => 1 }, "u" => [{ "foo" => "bar" }] },
91
+ { "q" => { "_id" => 2 }, "u" => [{ "foo" => "baz" }] }
92
+ ]
93
+ end
94
+
95
+ it "should return only the first value of sanitized bulk documents" do
96
+ expect(formatter.apply_strategy(strategy, value)).to eql([
97
+ { "q" => { "_id" => "?" }, "u" => "[?]" },
98
+ "[...]"
99
+ ])
100
+ end
101
+ end
102
+ end
103
+
104
+ context "when strategy is missing" do
105
+ let(:strategy) { nil }
106
+ let(:value) { { "_id" => 1 } }
107
+
108
+ it "should return a '?'" do
109
+ expect(formatter.apply_strategy(strategy, value)).to eql("?")
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,112 @@
1
+ describe Appsignal::EventFormatter::Moped::QueryFormatter do
2
+ let(:klass) { Appsignal::EventFormatter::Moped::QueryFormatter }
3
+ let(:formatter) { klass.new }
4
+
5
+ it "should register query.moped" do
6
+ expect(Appsignal::EventFormatter.registered?("query.moped", klass)).to be_truthy
7
+ end
8
+
9
+ describe "#format" do
10
+ let(:payload) { { :ops => [op] } }
11
+ subject { formatter.format(payload) }
12
+
13
+ context "without ops in the payload" do
14
+ let(:payload) { {} }
15
+
16
+ it { is_expected.to be_nil }
17
+ end
18
+
19
+ context "Moped::Protocol::Command" do
20
+ let(:op) do
21
+ double(
22
+ :full_collection_name => "database.collection",
23
+ :selector => { "query" => { "_id" => "abc" } },
24
+ :class => double(:to_s => "Moped::Protocol::Command")
25
+ )
26
+ end
27
+
28
+ it { is_expected.to eq ["Command", '{:database=>"database.collection", :selector=>{"query"=>"?"}}'] }
29
+ end
30
+
31
+ context "Moped::Protocol::Query" do
32
+ let(:op) do
33
+ double(
34
+ :full_collection_name => "database.collection",
35
+ :selector => { "_id" => "abc" },
36
+ :flags => [],
37
+ :limit => 0,
38
+ :skip => 0,
39
+ :fields => nil,
40
+ :class => double(:to_s => "Moped::Protocol::Query")
41
+ )
42
+ end
43
+
44
+ it { is_expected.to eq ["Query", '{:database=>"database.collection", :selector=>{"_id"=>"?"}, :flags=>[], :limit=>0, :skip=>0, :fields=>nil}'] }
45
+ end
46
+
47
+ context "Moped::Protocol::Delete" do
48
+ let(:op) do
49
+ double(
50
+ :full_collection_name => "database.collection",
51
+ :selector => { "_id" => "abc" },
52
+ :flags => [],
53
+ :class => double(:to_s => "Moped::Protocol::Delete")
54
+ )
55
+ end
56
+
57
+ it { is_expected.to eq ["Delete", '{:database=>"database.collection", :selector=>{"_id"=>"?"}, :flags=>[]}'] }
58
+ end
59
+
60
+ context "Moped::Protocol::Insert" do
61
+ let(:op) do
62
+ double(
63
+ :full_collection_name => "database.collection",
64
+ :flags => [],
65
+ :documents => [
66
+ { "_id" => "abc", "events" => { "foo" => [{ "bar" => "baz" }] } },
67
+ { "_id" => "def", "events" => { "foo" => [{ "baz" => "bar" }] } }
68
+ ],
69
+ :class => double(:to_s => "Moped::Protocol::Insert")
70
+ )
71
+ end
72
+
73
+ it { is_expected.to eq ["Insert", '{:database=>"database.collection", :documents=>{"_id"=>"?", "events"=>"?"}, :count=>2, :flags=>[]}'] }
74
+ end
75
+
76
+ context "Moped::Protocol::Update" do
77
+ let(:op) do
78
+ double(
79
+ :full_collection_name => "database.collection",
80
+ :selector => { "_id" => "abc" },
81
+ :update => { "user.name" => "James Bond" },
82
+ :flags => [],
83
+ :class => double(:to_s => "Moped::Protocol::Update")
84
+ )
85
+ end
86
+
87
+ it { is_expected.to eq ["Update", '{:database=>"database.collection", :selector=>{"_id"=>"?"}, :update=>{"user.?"=>"?"}, :flags=>[]}'] }
88
+ end
89
+
90
+ context "Moped::Protocol::KillCursors" do
91
+ let(:op) do
92
+ double(
93
+ :number_of_cursor_ids => 2,
94
+ :class => double(:to_s => "Moped::Protocol::KillCursors")
95
+ )
96
+ end
97
+
98
+ it { is_expected.to eq ["KillCursors", "{:number_of_cursor_ids=>2}"] }
99
+ end
100
+
101
+ context "Moped::Protocol::Other" do
102
+ let(:op) do
103
+ double(
104
+ :full_collection_name => "database.collection",
105
+ :class => double(:to_s => "Moped::Protocol::Other")
106
+ )
107
+ end
108
+
109
+ it { is_expected.to eq ["Other", '{:database=>"database.collection"}'] }
110
+ end
111
+ end
112
+ end