appoptics_apm 4.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (226) hide show
  1. checksums.yaml +7 -0
  2. data/.codeclimate.yml +43 -0
  3. data/.dockerignore +5 -0
  4. data/.gitignore +23 -0
  5. data/.rubocop.yml +5 -0
  6. data/.travis.yml +82 -0
  7. data/CHANGELOG.md +769 -0
  8. data/CONFIG.md +33 -0
  9. data/Dockerfile +41 -0
  10. data/Dockerfile_test +66 -0
  11. data/Gemfile +41 -0
  12. data/LICENSE +193 -0
  13. data/README.md +351 -0
  14. data/Rakefile +202 -0
  15. data/Vagrantfile +67 -0
  16. data/appoptics_apm.gemspec +55 -0
  17. data/build_gems.sh +15 -0
  18. data/docker-compose.yml +73 -0
  19. data/examples/DNT.md +35 -0
  20. data/examples/carrying_context.rb +220 -0
  21. data/examples/instrumenting_metal_controller.rb +8 -0
  22. data/examples/puma_on_heroku_config.rb +17 -0
  23. data/examples/tracing_async_threads.rb +124 -0
  24. data/examples/tracing_background_jobs.rb +53 -0
  25. data/examples/tracing_forked_processes.rb +99 -0
  26. data/examples/unicorn_on_heroku_config.rb +28 -0
  27. data/ext/oboe_metal/extconf.rb +54 -0
  28. data/ext/oboe_metal/lib/.keep +0 -0
  29. data/ext/oboe_metal/lib/liboboe-1.0.so.0.0.0 +0 -0
  30. data/ext/oboe_metal/noop/noop.c +7 -0
  31. data/ext/oboe_metal/src/VERSION +1 -0
  32. data/ext/oboe_metal/src/bson/bson.h +221 -0
  33. data/ext/oboe_metal/src/bson/platform_hacks.h +91 -0
  34. data/ext/oboe_metal/src/oboe.h +883 -0
  35. data/ext/oboe_metal/src/oboe.hpp +793 -0
  36. data/ext/oboe_metal/src/oboe_debug.h +50 -0
  37. data/ext/oboe_metal/src/oboe_wrap.cxx +6088 -0
  38. data/ext/oboe_metal/tests/test.rb +11 -0
  39. data/gemfiles/delayed_job.gemfile +36 -0
  40. data/gemfiles/frameworks.gemfile +44 -0
  41. data/gemfiles/instrumentation_mocked.gemfile +29 -0
  42. data/gemfiles/libraries.gemfile +85 -0
  43. data/gemfiles/rails23.gemfile +39 -0
  44. data/gemfiles/rails30.gemfile +42 -0
  45. data/gemfiles/rails31.gemfile +44 -0
  46. data/gemfiles/rails32.gemfile +54 -0
  47. data/gemfiles/rails40.gemfile +27 -0
  48. data/gemfiles/rails41.gemfile +27 -0
  49. data/gemfiles/rails42.gemfile +35 -0
  50. data/gemfiles/rails50.gemfile +44 -0
  51. data/gemfiles/rails51.gemfile +44 -0
  52. data/get_version.rb +5 -0
  53. data/init.rb +4 -0
  54. data/lib/appoptics_apm/api/layerinit.rb +39 -0
  55. data/lib/appoptics_apm/api/logging.rb +359 -0
  56. data/lib/appoptics_apm/api/memcache.rb +34 -0
  57. data/lib/appoptics_apm/api/profiling.rb +201 -0
  58. data/lib/appoptics_apm/api/tracing.rb +152 -0
  59. data/lib/appoptics_apm/api/util.rb +128 -0
  60. data/lib/appoptics_apm/api.rb +18 -0
  61. data/lib/appoptics_apm/base.rb +252 -0
  62. data/lib/appoptics_apm/config.rb +281 -0
  63. data/lib/appoptics_apm/frameworks/grape.rb +93 -0
  64. data/lib/appoptics_apm/frameworks/padrino/templates.rb +58 -0
  65. data/lib/appoptics_apm/frameworks/padrino.rb +52 -0
  66. data/lib/appoptics_apm/frameworks/rails/inst/action_controller.rb +106 -0
  67. data/lib/appoptics_apm/frameworks/rails/inst/action_controller2.rb +61 -0
  68. data/lib/appoptics_apm/frameworks/rails/inst/action_controller3.rb +58 -0
  69. data/lib/appoptics_apm/frameworks/rails/inst/action_controller4.rb +48 -0
  70. data/lib/appoptics_apm/frameworks/rails/inst/action_controller5.rb +50 -0
  71. data/lib/appoptics_apm/frameworks/rails/inst/action_controller_api.rb +50 -0
  72. data/lib/appoptics_apm/frameworks/rails/inst/action_view.rb +58 -0
  73. data/lib/appoptics_apm/frameworks/rails/inst/action_view_2x.rb +56 -0
  74. data/lib/appoptics_apm/frameworks/rails/inst/action_view_30.rb +50 -0
  75. data/lib/appoptics_apm/frameworks/rails/inst/active_record.rb +27 -0
  76. data/lib/appoptics_apm/frameworks/rails/inst/connection_adapters/mysql.rb +43 -0
  77. data/lib/appoptics_apm/frameworks/rails/inst/connection_adapters/mysql2.rb +28 -0
  78. data/lib/appoptics_apm/frameworks/rails/inst/connection_adapters/postgresql.rb +30 -0
  79. data/lib/appoptics_apm/frameworks/rails/inst/connection_adapters/utils.rb +120 -0
  80. data/lib/appoptics_apm/frameworks/rails/inst/connection_adapters/utils5x.rb +101 -0
  81. data/lib/appoptics_apm/frameworks/rails.rb +116 -0
  82. data/lib/appoptics_apm/frameworks/sinatra/templates.rb +56 -0
  83. data/lib/appoptics_apm/frameworks/sinatra.rb +71 -0
  84. data/lib/appoptics_apm/inst/bunny-client.rb +148 -0
  85. data/lib/appoptics_apm/inst/bunny-consumer.rb +92 -0
  86. data/lib/appoptics_apm/inst/curb.rb +329 -0
  87. data/lib/appoptics_apm/inst/dalli.rb +85 -0
  88. data/lib/appoptics_apm/inst/delayed_job.rb +92 -0
  89. data/lib/appoptics_apm/inst/em-http-request.rb +105 -0
  90. data/lib/appoptics_apm/inst/excon.rb +130 -0
  91. data/lib/appoptics_apm/inst/faraday.rb +77 -0
  92. data/lib/appoptics_apm/inst/http.rb +83 -0
  93. data/lib/appoptics_apm/inst/httpclient.rb +176 -0
  94. data/lib/appoptics_apm/inst/memcache.rb +102 -0
  95. data/lib/appoptics_apm/inst/memcached.rb +94 -0
  96. data/lib/appoptics_apm/inst/mongo.rb +242 -0
  97. data/lib/appoptics_apm/inst/mongo2.rb +225 -0
  98. data/lib/appoptics_apm/inst/moped.rb +466 -0
  99. data/lib/appoptics_apm/inst/rack.rb +146 -0
  100. data/lib/appoptics_apm/inst/redis.rb +275 -0
  101. data/lib/appoptics_apm/inst/resque.rb +151 -0
  102. data/lib/appoptics_apm/inst/rest-client.rb +50 -0
  103. data/lib/appoptics_apm/inst/sequel.rb +178 -0
  104. data/lib/appoptics_apm/inst/sidekiq-client.rb +53 -0
  105. data/lib/appoptics_apm/inst/sidekiq-worker.rb +67 -0
  106. data/lib/appoptics_apm/inst/twitter-cassandra.rb +294 -0
  107. data/lib/appoptics_apm/inst/typhoeus.rb +113 -0
  108. data/lib/appoptics_apm/instrumentation.rb +22 -0
  109. data/lib/appoptics_apm/legacy_method_profiling.rb +97 -0
  110. data/lib/appoptics_apm/loading.rb +66 -0
  111. data/lib/appoptics_apm/logger.rb +41 -0
  112. data/lib/appoptics_apm/method_profiling.rb +33 -0
  113. data/lib/appoptics_apm/ruby.rb +35 -0
  114. data/lib/appoptics_apm/support.rb +135 -0
  115. data/lib/appoptics_apm/test.rb +94 -0
  116. data/lib/appoptics_apm/thread_local.rb +26 -0
  117. data/lib/appoptics_apm/util.rb +312 -0
  118. data/lib/appoptics_apm/version.rb +15 -0
  119. data/lib/appoptics_apm/xtrace.rb +103 -0
  120. data/lib/appoptics_apm.rb +72 -0
  121. data/lib/joboe_metal.rb +214 -0
  122. data/lib/oboe/README +2 -0
  123. data/lib/oboe/backward_compatibility.rb +80 -0
  124. data/lib/oboe/inst/rack.rb +11 -0
  125. data/lib/oboe.rb +7 -0
  126. data/lib/oboe_metal.rb +187 -0
  127. data/lib/rails/generators/appoptics_apm/install_generator.rb +45 -0
  128. data/lib/rails/generators/appoptics_apm/templates/appoptics_apm_initializer.rb +222 -0
  129. data/ruby_setup.sh +47 -0
  130. data/run_docker_build_gem_upload_to_packagecloud.sh +20 -0
  131. data/run_tests_docker.rb +32 -0
  132. data/test/benchmark/README.md +65 -0
  133. data/test/benchmark/logging_bench.rb +54 -0
  134. data/test/benchmark/with_libraries_gemfile/bunny_bench.rb +69 -0
  135. data/test/benchmark/with_rails5x_gemfile/action_controller5x_bench.rb +43 -0
  136. data/test/frameworks/apps/grape_nested.rb +33 -0
  137. data/test/frameworks/apps/grape_simple.rb +80 -0
  138. data/test/frameworks/apps/padrino_simple.rb +80 -0
  139. data/test/frameworks/apps/sinatra_simple.rb +55 -0
  140. data/test/frameworks/grape_test.rb +286 -0
  141. data/test/frameworks/padrino_test.rb +222 -0
  142. data/test/frameworks/rails3x_test.rb +554 -0
  143. data/test/frameworks/rails4x_test.rb +570 -0
  144. data/test/frameworks/rails5x_api_test.rb +210 -0
  145. data/test/frameworks/rails5x_test.rb +376 -0
  146. data/test/frameworks/rails_shared_tests.rb +172 -0
  147. data/test/frameworks/sinatra_test.rb +140 -0
  148. data/test/instrumentation/bunny_client_test.rb +276 -0
  149. data/test/instrumentation/bunny_consumer_test.rb +204 -0
  150. data/test/instrumentation/curb_test.rb +398 -0
  151. data/test/instrumentation/dalli_test.rb +177 -0
  152. data/test/instrumentation/em_http_request_test.rb +89 -0
  153. data/test/instrumentation/excon_test.rb +231 -0
  154. data/test/instrumentation/faraday_test.rb +228 -0
  155. data/test/instrumentation/http_test.rb +143 -0
  156. data/test/instrumentation/httpclient_test.rb +320 -0
  157. data/test/instrumentation/memcache_test.rb +260 -0
  158. data/test/instrumentation/memcached_test.rb +229 -0
  159. data/test/instrumentation/mongo_v1_test.rb +479 -0
  160. data/test/instrumentation/mongo_v2_index_test.rb +124 -0
  161. data/test/instrumentation/mongo_v2_test.rb +584 -0
  162. data/test/instrumentation/mongo_v2_view_test.rb +435 -0
  163. data/test/instrumentation/moped_test.rb +517 -0
  164. data/test/instrumentation/rack_test.rb +165 -0
  165. data/test/instrumentation/redis_hashes_test.rb +268 -0
  166. data/test/instrumentation/redis_keys_test.rb +321 -0
  167. data/test/instrumentation/redis_lists_test.rb +310 -0
  168. data/test/instrumentation/redis_misc_test.rb +163 -0
  169. data/test/instrumentation/redis_sets_test.rb +296 -0
  170. data/test/instrumentation/redis_sortedsets_test.rb +328 -0
  171. data/test/instrumentation/redis_strings_test.rb +349 -0
  172. data/test/instrumentation/resque_test.rb +185 -0
  173. data/test/instrumentation/rest-client_test.rb +288 -0
  174. data/test/instrumentation/sequel_mysql2_test.rb +353 -0
  175. data/test/instrumentation/sequel_mysql_test.rb +334 -0
  176. data/test/instrumentation/sequel_pg_test.rb +336 -0
  177. data/test/instrumentation/sidekiq-client_test.rb +159 -0
  178. data/test/instrumentation/sidekiq-worker_test.rb +180 -0
  179. data/test/instrumentation/twitter-cassandra_test.rb +424 -0
  180. data/test/instrumentation/typhoeus_test.rb +284 -0
  181. data/test/jobs/delayed_job/db_worker_job.rb +29 -0
  182. data/test/jobs/delayed_job/error_worker_job.rb +10 -0
  183. data/test/jobs/delayed_job/remote_call_worker_job.rb +20 -0
  184. data/test/jobs/resque/db_worker_job.rb +29 -0
  185. data/test/jobs/resque/error_worker_job.rb +10 -0
  186. data/test/jobs/resque/remote_call_worker_job.rb +20 -0
  187. data/test/jobs/sidekiq/db_worker_job.rb +29 -0
  188. data/test/jobs/sidekiq/error_worker_job.rb +10 -0
  189. data/test/jobs/sidekiq/remote_call_worker_job.rb +20 -0
  190. data/test/minitest_helper.rb +276 -0
  191. data/test/mocked/curb_mocked_test.rb +311 -0
  192. data/test/mocked/excon_mocked_test.rb +166 -0
  193. data/test/mocked/faraday_mocked_test.rb +93 -0
  194. data/test/mocked/http_mocked_test.rb +129 -0
  195. data/test/mocked/httpclient_mocked_test.rb +245 -0
  196. data/test/mocked/rest_client_mocked_test.rb +103 -0
  197. data/test/mocked/typhoeus_mocked_test.rb +192 -0
  198. data/test/models/widget.rb +36 -0
  199. data/test/profiling/legacy_method_profiling_test.rb +201 -0
  200. data/test/profiling/method_profiling_test.rb +631 -0
  201. data/test/queues/delayed_job-client_test.rb +95 -0
  202. data/test/queues/delayed_job-worker_test.rb +91 -0
  203. data/test/reporter/reporter_test.rb +14 -0
  204. data/test/servers/delayed_job.rb +107 -0
  205. data/test/servers/rackapp_8101.rb +29 -0
  206. data/test/servers/rails3x_8140.rb +96 -0
  207. data/test/servers/rails4x_8140.rb +96 -0
  208. data/test/servers/rails5x_8140.rb +95 -0
  209. data/test/servers/rails5x_api_8150.rb +78 -0
  210. data/test/servers/sidekiq.rb +29 -0
  211. data/test/servers/sidekiq.yml +7 -0
  212. data/test/servers/sidekiq_initializer.rb +25 -0
  213. data/test/settings +0 -0
  214. data/test/support/auto_tracing_test.rb +50 -0
  215. data/test/support/backcompat_test.rb +276 -0
  216. data/test/support/config_test.rb +149 -0
  217. data/test/support/dnt_test.rb +98 -0
  218. data/test/support/init_report_test.rb +25 -0
  219. data/test/support/liboboe_settings_test.rb +110 -0
  220. data/test/support/logging_test.rb +130 -0
  221. data/test/support/noop_test.rb +88 -0
  222. data/test/support/sql_sanitize_test.rb +55 -0
  223. data/test/support/tracing_mode_test.rb +33 -0
  224. data/test/support/tvalias_test.rb +15 -0
  225. data/test/support/xtrace_test.rb +41 -0
  226. metadata +475 -0
data/README.md ADDED
@@ -0,0 +1,351 @@
1
+ # Welcome to the AppOpticsAPM Ruby Gem
2
+
3
+ The appoptics_apm gem provides [AppOptics APM](https://www.appoptics.com/) performance instrumentation for Ruby.
4
+
5
+ ![Ruby AppOpticsAPM](http://www.appoptics.com/images/ruby_readme/oboe-ruby-header.png)
6
+
7
+ It has the ability to report performance metrics on an array of libraries, databases and frameworks such as Rails, Mongo, Memcache, ActiveRecord, Cassandra, Rack, Resque [and more](http://docs.appoptics.solarwinds.com/Instrumentation/ruby.html#ruby-support-matrix).
8
+
9
+ It requires an [AppOptics](https://www.appoptics.com/) account to view metrics. Get yours, [it's free](https://https://my.appoptics.com/sign_up).
10
+
11
+ [![Gem Version](https://badge.fury.io/rb/appoptics.png)](https://badge.fury.io/rb/appoptics)
12
+ [![Build Status](https://travis-ci.org/tracelytics/ruby-appoptics.png?branch=master)](https://travis-ci.org/tracelytics/ruby-appoptics)
13
+ [![Code Climate](https://codeclimate.com/github/tracelytics/ruby-appoptics.png)](https://codeclimate.com/github/tracelytics/ruby-appoptics)
14
+
15
+ _Note: The repository name has been changed to ruby-appoptics. Please update your github remotes with `git remote set-url origin git@github.com:tracelytics/ruby-appoptics.git`._
16
+
17
+ # Installation
18
+
19
+ _Before installing the gem below, make sure that you have the [dependencies](http://docs.appoptics.com/kb/apm_tracing/ruby/install#dependencies) installed on your host first._
20
+
21
+ The appoptics_apm gem is [available on Rubygems](https://rubygems.org/gems/appoptics_apm) and can be installed with:
22
+
23
+ ```bash
24
+ gem install appoptics_apm
25
+ ```
26
+
27
+ or added to _the end_ of your bundle Gemfile and running `bundle install`:
28
+
29
+ ```ruby
30
+ gem 'appoptics_apm'
31
+ ```
32
+
33
+ ## if you're dogfooding:
34
+ Get the gem
35
+ ```
36
+ source 'https://73bf9c35xxxxxxxxxxb4d26c3:@packagecloud.io/AppOptics/apm-instrumentation/' do
37
+ gem 'appoptics_apm', '>= 4.0.0'
38
+ end
39
+ ```
40
+ Set the env to report to staging (your_app_name can be any string, it is only used as name in the services list):
41
+ ```
42
+ unset APPOPTICS_TRUSTEDPATH
43
+ export APPOPTICS_COLLECTOR=collector-stg.appoptics.com
44
+ export APPOPTICS_SERVICE_KEY=795fb4947d15275d208c49cfd2412d4a5bf38742045b47236c94c4fe5f5b17c7:<your_app_name>
45
+ ```
46
+ If you're reporting to production, only set `APPOPTICS_SERVICE_KEY`
47
+
48
+ # Running
49
+
50
+ ## Rails
51
+
52
+ ![Ruby on Rails](http://docs.appoptics.com/kb/apm_tracing/ruby)
53
+
54
+ No special steps are needed to instrument Ruby on Rails. Once part of the bundle, the appoptics gem will automatically detect Rails and instrument on stack initialization.
55
+
56
+ *Note: You will still need to decide on your `tracing_mode` depending on whether you are running with an instrumented Apache or nginx in front of your Rails stack. See below for more details.*
57
+
58
+ ### The Install Generator
59
+
60
+ The appoptics_apm gem provides a Rails generator used to seed an initializer where you can configure and control `tracing_mode` and [other options](http://docs.appoptics.com/kb/apm_tracing/ruby/configure).
61
+
62
+ To run the install generator run:
63
+
64
+ ```bash
65
+ bundle exec rails generate appoptics:install
66
+ ```
67
+
68
+ After the prompts, this will create an initializer: `config/initializers/appoptics.rb`.
69
+
70
+ ## Sinatra
71
+
72
+ ![Sinatra](http://docs.appoptics.solarwinds.com/images/ruby_readme/sinatra.png)
73
+
74
+ You can instrument your Sinatra application by adding the following code to your `config.ru` Rackup file:
75
+
76
+ ```ruby
77
+ # If you're not using Bundler.require. Make sure this is done
78
+ # after the Sinatra require directive.
79
+ require 'appoptics_apm'
80
+
81
+ AppOpticsAPM
82
+ AppOpticsAPM
83
+ ```
84
+
85
+ Make sure that the appoptics_apm gem is loaded _after_ Sinatra either by listing `gem 'appoptics_apm'` after Sinatra in your Gemfile or calling the `require 'appoptics_gem'` directive after Sinatra is loaded.
86
+
87
+ With this, the appoptics_apm gem will automatically detect Sinatra on boot and instrument key components.
88
+
89
+ ## Padrino
90
+
91
+ ![Padrino](http://docs.appoptics.solarwinds.com/images/ruby_readme/padrino.png)
92
+
93
+ As long as the appoptics_apm gem is in your `Gemfile` (inserted after the `gem 'padrino'` directive) and you are calling `Bundler.require`, the appoptics_apm gem will automatically instrument Padrino applications.
94
+
95
+ If you need to set `AppOpticsAPM::Config` values on stack boot, you can do so by adding the following
96
+ to your `config/boot.rb` file:
97
+
98
+ ```ruby
99
+ Padrino.before_load do
100
+ # Verbose output of instrumentation initialization
101
+ AppOpticsAPM
102
+ end
103
+ ```
104
+
105
+ ## Grape
106
+
107
+ ![Grape](http://docs.appoptics.solarwinds.com/images/ruby_readme/grape.png)
108
+
109
+ You can instrument your Grape application by adding the following code to your `config.ru` Rackup file:
110
+
111
+ ```ruby
112
+ # If you're not using Bundler.require. Make sure this is done
113
+ # after the Grape require directive.
114
+ require 'appoptics_apm'
115
+
116
+ ...
117
+
118
+ class App < Grape::API
119
+ use AppOpticsAPM::Rack
120
+ end
121
+ ```
122
+
123
+ Make sure that the appoptics gem is loaded _after_ Grape either by listing `gem 'appoptics_apm'` after Grape in your Gemfile or calling the `require 'appoptics_apm'` directive after Grape is loaded.
124
+
125
+ You must explicitly tell your Grape application to use AppOpticsAPM::Rack for tracing to occur.
126
+
127
+ ## Custom Ruby Scripts & Applications
128
+
129
+ The appoptics_apm gem has the ability to instrument any arbitrary Ruby application or script.
130
+
131
+ ```ruby
132
+ require 'rubygems'
133
+ require 'bundler'
134
+
135
+ Bundler.require
136
+
137
+ require 'appoptics_apm'
138
+ ```
139
+
140
+ From here, you can use the Tracing API to instrument areas of code using `AppOpticsAPM::API.start_trace` (see below). If you prefer to instead dive directly into code, take a look at [this example](https://gist.github.com/pglombardo/8550713) of an instrumented Ruby script.
141
+
142
+ Once inside of the `AppOpticsAPM::API.start_trace` block, performance metrics will be automatically collected for all supported libraries and gems (Redis, Mongo, ActiveRecord etc..).
143
+
144
+ # Custom Tracing
145
+
146
+ You can add even more visibility into any part of your application or scripts by adding custom instrumentation. If you want to see the performance of an existing method see Method Profiling. To trace blocks of code see the Tracing API.
147
+
148
+ ## The Tracing API
149
+
150
+ You can instrument any arbitrary block of code using `AppOpticsAPM::API.trace`. The code and any supported calls for libraries that we support, will automatically get traced and reported to your dashboard.
151
+
152
+ ```ruby
153
+ # layer_name will show up in the AppOptics app dashboard
154
+ layer_name = 'subsystemX'
155
+
156
+ # report_kvs are a set of information Key/Value pairs that are sent to
157
+ # AppOptics dashboard along with the performance metrics. These KV
158
+ # pairs are used to report request, environment and/or client specific
159
+ # information.
160
+
161
+ report_kvs = {}
162
+ report_kvs[:mykey] = @client.id
163
+
164
+ AppOpticsAPM::API.trace(layer_name, report_kvs) do
165
+ # the block of code to be traced
166
+ end
167
+ ```
168
+
169
+ `AppOpticsAPM::API.trace` is used within the context of a request. It will follow the upstream state of the request being traced. i.e. the block of code will only be traced when the parent request is being traced.
170
+
171
+ This tracing state of a request can also be queried by using `AppOpticsAPM.tracing?`.
172
+
173
+ If you need to instrument code outside the context of a request (such as a cron job, background job or an arbitrary ruby script), use `AppOpticsAPM::API.start_trace` instead which will initiate new traces based on configuration and probability (based on the sample rate).
174
+
175
+ Find more details in the [RubyDoc page](http://rdoc.info/gems/appoptics/AppOpticsAPM/API/Tracing) or in [this example](https://gist.github.com/pglombardo/8550713) on how to use the Tracing API in an independent Ruby script.
176
+
177
+ ## Tracing Methods
178
+
179
+ With AppOptics, you can profile any method in your application or even in the Ruby language using `AppOpticsAPM::API.profile_method`.
180
+
181
+ If, for example, you wanted to see the performance for the `Array::sort`, you could simply call the following in your startup code:
182
+
183
+ ```
184
+ AppOpticsAPM::API.profile_method(Array, :sort)
185
+ ```
186
+
187
+ For full documentation, options and reporting custom KVs, see our documentation on [method profiling](http://docs.appoptics.solarwinds.com/Instrumentation/ruby.html#ruby-function-profiling).
188
+
189
+ # Support
190
+
191
+ If you find a bug or would like to request an enhancement, feel free to file an issue. For all other support requests, see our [support portal](https://tracelytics.freshdesk.com).
192
+
193
+ # Contributing
194
+
195
+ You are obviously a person of great sense and intelligence. We happily appreciate all contributions to the appoptics gem whether it is documentation, a bug fix, new instrumentation for a library or framework or anything else we haven't thought of.
196
+
197
+ We welcome you to send us PRs. We also humbly request that any new instrumentation submissions have corresponding tests that accompany them. This way we don't break any of your additions when we (and others) make changes after the fact.
198
+
199
+ ## Developer Resources
200
+
201
+ We have made a large effort to expose as much technical information as possible to assist developers wishing to contribute to the appoptics gem. Below is a good source for information and help for developers:
202
+
203
+ * The [AppOptics Knowledge Base](https://docs.appoptics.com/kb/apm_tracing) has a large collection of technical articles or, if needed, you can submit a support request directly to the team.
204
+
205
+ If you have any questions or ideas, don't hesitate to contact us anytime.
206
+
207
+ ## Layout of the Gem
208
+
209
+ The appoptics gem uses a standard gem layout. Here are the notable directories.
210
+
211
+ lib/appoptics/inst # Auto load directory for various instrumented libraries
212
+ lib/appoptics/frameworks # Framework instrumentation directory
213
+ lib/appoptics/frameworks/rails # Files specific to Rails instrumentation
214
+ lib/rails # A Rails required directory for the Rails install generator
215
+ lib/api # The AppOpticsAPM Tracing API: layers, logging, profiling and tracing
216
+ ext/oboe_metal # The Ruby c extension that links against the system liboboe library
217
+
218
+ ## Building the Gem
219
+
220
+ The appoptics gem is built with the standard `gem build` command passing in the gemspec:
221
+
222
+ ```bash
223
+ gem build appoptics_apm.gemspec
224
+ ```
225
+
226
+ ## Writing Custom Instrumentation
227
+
228
+ Custom instrumentation for a library, database or other service can be authored fairly easily. Generally, instrumentation of a library is done by wrapping select operations of that library and timing their execution using the AppOpticsAPM Tracing API which then reports the metrics to the users' AppOptics dashboard.
229
+
230
+ Here, I'll use a stripped down version of the Dalli instrumentation (`lib/appoptics/inst/dalli.rb`) as a quick example of how to instrument a client library (the dalli gem).
231
+
232
+ The Dalli gem nicely routes all memcache operations through a single `perform` operation. Wrapping this method allows us to capture all Dalli operations called by an application.
233
+
234
+ First, we define a module (AppOpticsAPM::Inst::Dalli) and our own custom `perform_with_appoptics` method that we will use as a wrapper around Dalli's `perform` method. We also declare an `included` method which automatically gets called when this module is included by another. See ['included' Ruby reference documentation](https://www.omniref.com/ruby/2.2.1/symbols/Module/included).
235
+
236
+ ```ruby
237
+ module AppOpticsAPM
238
+ module Inst
239
+ module Dalli
240
+ include AppOpticsAPM::API::Memcache
241
+
242
+ def self.included(cls)
243
+ cls.class_eval do
244
+ if ::Dalli::Client.private_method_defined? :perform
245
+ alias perform_without_appoptics perform
246
+ alias perform perform_with_appoptics
247
+ end
248
+ end
249
+ end
250
+
251
+ def perform_with_appoptics(*all_args, &blk)
252
+ op, key, *args = *all_args
253
+
254
+ if AppOpticsAPM.tracing?
255
+ opts = {}
256
+ opts[:KVOp] = op
257
+ opts[:KVKey] = key
258
+
259
+ AppOpticsAPM::API.trace('memcache', opts || {}) do
260
+ result = perform_without_appoptics(*all_args, &blk)
261
+ if op == :get and key.class == String
262
+ AppOpticsAPM::API.log('memcache', 'info', { :KVHit => memcache_hit?(result) })
263
+ end
264
+ result
265
+ end
266
+ else
267
+ perform_without_appoptics(*all_args, &blk)
268
+ end
269
+ end
270
+ end
271
+ end
272
+ end
273
+ ```
274
+
275
+ Second, we tail onto the end of the instrumentation file a simple `::Dalli::Client.module_eval` call to tell the Dalli module to include our newly defined instrumentation module. Doing this will invoke our previously defined `included` method.
276
+
277
+ ```ruby
278
+ if defined?(Dalli) and AppOpticsAPM::Config[:dalli][:enabled]
279
+ ::Dalli::Client.module_eval do
280
+ include AppOpticsAPM::Inst::Dalli
281
+ end
282
+ end
283
+ ```
284
+
285
+ Third, in our wrapper method, we capture the arguments passed in, collect the operation and key information into a local hash and then invoke the `AppOpticsAPM::API.trace` method to time the execution of the original operation.
286
+
287
+ The `AppOpticsAPM::API.trace` method calls Dalli's native operation and reports the timing metrics and your custom `report_kvs` up to AppOptics servers to be shown on the user's dashboard.
288
+
289
+ Some other tips and guidelines:
290
+
291
+ * You can point your Gemfile directly at your cloned appoptics gem source by using `gem 'appoptics', :path => '/path/to/ruby-appoptics'`
292
+
293
+ * If instrumenting a library, database or service, place your new instrumentation file into the `lib/appoptics/inst/` directory. From there, the appoptics gem will detect it and automatically load the instrumentation file.
294
+
295
+ * If instrumenting a new framework, place your instrumentation file in `lib/appoptics/frameworks`. Refer to the Rails instrumentation for on ideas on how to load the appoptics gem correctly in your framework.
296
+
297
+ * Review other existing instrumentation similar to the one you wish to author. `lib/appoptics/inst/` is a great place to start.
298
+
299
+ * Depending on the configured `:sample_rate`, not all requests will be traced. Use `AppOpticsAPM.tracing?` to determine of this is a request that is being traced.
300
+
301
+ * Performance is paramount. Make sure that your wrapped methods don't slow down users applications.
302
+
303
+ * Include tests with your instrumentation. See `test/instrumentation/` for some examples of existing instrumentation tests.
304
+
305
+ ## Compiling the C extension
306
+
307
+ The appoptics gem utilizes a C extension to interface with a core library bundled in with the gem which handles reporting the trace and performance data back to AppOptics servers.
308
+
309
+ C extensions are usually built on `gem install` but when working out of a local git repository, it's required that you manually build this C extension for the gem to function.
310
+
311
+ To make this simpler, we've included a few rake tasks to automate this process:
312
+
313
+ ```bash
314
+ rake compile # Build the gem's c extension
315
+ rake distclean # Remove all built files and extensions
316
+ rake recompile # Rebuild the gem's c extension
317
+ ```
318
+
319
+ To see the code related to the C extension, take a look at `ext/oboe_metal/extconf.rb` for details.
320
+
321
+ You can read more about Ruby gems with C extensions in the [Rubygems Guides](http://guides.rubygems.org/gems-with-extensions/).
322
+
323
+ ## Running the Tests
324
+
325
+ ![AppOpticsAPM Ruby Tests](http://docs.appoptics.solarwinds.com/images/ruby_readme/tv_ruby_tests.png)
326
+
327
+ The tests bundled with the gem are implemented using [Minitest](https://github.com/seattlerb/minitest). The tests are currently used to validate the sanity of the traces generated and basic gem functionality.
328
+
329
+ After a bundle install, the tests can be run as:
330
+
331
+ ```bash
332
+ bundle exec rake test
333
+ ```
334
+
335
+ This will run a full end-to-end test suite that covers all supported libraries and databases. Note that this requires all of the supported software (Cassandra, Memcache, Mongo etc.) to be installed, configured and available.
336
+
337
+ Since this is overly burdensome for casual users, you can run just the tests that you're interested in.
338
+
339
+ To run just the tests for the dalli gem trace validation:
340
+
341
+ ```bash
342
+ bundle exec rake test TEST=test/instrumentation/dalli_test.rb
343
+ ```
344
+
345
+ We humbly request that any submitted instrumentation is delivered with corresponding test coverage.
346
+
347
+ # License
348
+
349
+ Copyright (c) 2018 SolarWinds, LLC
350
+
351
+ Released under the [Librato Open License](http://docs.appoptics.solarwinds.com/Instrumentation/librato-open-license.html)
data/Rakefile ADDED
@@ -0,0 +1,202 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require 'rubygems'
4
+ require 'fileutils'
5
+ require 'open-uri'
6
+ require 'bundler/setup'
7
+ require 'rake/testtask'
8
+ require 'appoptics_apm/test'
9
+
10
+ Rake::TestTask.new do |t|
11
+ t.verbose = false
12
+ t.warning = false
13
+ t.ruby_opts = []
14
+ t.libs << 'test'
15
+
16
+ # Since we support so many libraries and frameworks, tests
17
+ # runs are segmented into gemfiles that have different
18
+ # sets and versions of gems (libraries and frameworks).
19
+ #
20
+ # Here we detect the Gemfile the tests are being run against
21
+ # and load the appropriate tests.
22
+ #
23
+ case AppOpticsAPM::Test.gemfile
24
+ when /delayed_job/
25
+ require 'delayed/tasks'
26
+ t.test_files = FileList['test/queues/delayed_job*_test.rb']
27
+ when /rails/
28
+ # Pre-load rails to get the major version number
29
+ require 'rails'
30
+
31
+ if Rails::VERSION::MAJOR == 5
32
+ t.test_files = FileList["test/frameworks/rails#{Rails::VERSION::MAJOR}x_test.rb"] +
33
+ FileList["test/frameworks/rails#{Rails::VERSION::MAJOR}x_api_test.rb"]
34
+ else
35
+ t.test_files = FileList["test/frameworks/rails#{Rails::VERSION::MAJOR}x_test.rb"]
36
+ end
37
+
38
+ when /frameworks/
39
+ t.test_files = FileList['test/frameworks/sinatra*_test.rb'] +
40
+ FileList['test/frameworks/padrino*_test.rb'] +
41
+ FileList['test/frameworks/grape*_test.rb']
42
+ when /libraries/
43
+ t.test_files = FileList['test/support/*_test.rb'] +
44
+ FileList['test/reporter/*_test.rb'] +
45
+ FileList['test/instrumentation/*_test.rb'] +
46
+ FileList['test/profiling/*_test.rb'] -
47
+ ['test/instrumentation/twitter-cassandra_test.rb']
48
+ # FIXME: exclude cassandra tests for now
49
+ # TODO: they need refactoring to use the 'cassandra-driver' gem
50
+ # ____ instead of the 'cassandra' gem, which hasn't had a commit since 09/2014
51
+ when /instrumentation_mocked/
52
+ # WebMock is interfering with other tests, so these have to run seperately
53
+ t.test_files = FileList['test/mocked/*_test.rb']
54
+ end
55
+
56
+ if defined?(JRUBY_VERSION)
57
+ t.ruby_opts << ['-J-javaagent:/usr/local/tracelytics/tracelyticsagent.jar']
58
+ end
59
+ end
60
+
61
+ desc "Fetch extension dependency files"
62
+ task :fetch_ext_deps do
63
+ swig_version = %x{swig -version} rescue ''
64
+ if swig_version.scan(/swig version 3.0.8/i).empty?
65
+ raise "!! Did not find required swig version 3.0.8, found #{swig_version.inspect}"
66
+ end
67
+ oboe_version = ENV['OBOE_VERSION'] || 'latest'
68
+ oboe_arch = ENV['OBOE_ARCH'] || 'x86_64'
69
+ oboe_src_dir = "https://s3-us-west-2.amazonaws.com/rc-files-t2/c-lib/#{oboe_version}"
70
+ ext_lib_dir = File.expand_path('ext/oboe_metal/lib')
71
+ ext_src_dir = File.expand_path('ext/oboe_metal/src')
72
+
73
+ %w(oboe.i oboe.h oboe.hpp oboe_debug.h liboboe-1.0.so.0.0.0 VERSION).each do |filename|
74
+ if filename =~ /^liboboe-.+so.+/
75
+ remote_file = File.join(oboe_src_dir,
76
+ filename.sub('.so',"-#{oboe_arch}.so"))
77
+ local_file = File.join(ext_lib_dir, filename)
78
+ else
79
+ remote_file = File.join(oboe_src_dir, filename)
80
+ local_file = File.join(ext_src_dir, filename)
81
+ end
82
+ puts "fetching #{remote_file} to #{local_file}"
83
+ open(remote_file, 'rb') do |rf|
84
+ content = rf.read
85
+ File.open(local_file, 'wb') {|f| f.puts content}
86
+ end
87
+ end
88
+ FileUtils.cd(ext_src_dir) do
89
+ system('swig -c++ -ruby -module oboe_metal oboe.i')
90
+ FileUtils.rm('oboe.i')
91
+ end
92
+ end
93
+
94
+ desc "Build the gem's c extension"
95
+ task :compile do
96
+ if !defined?(JRUBY_VERSION)
97
+ puts "== Building the c extension against Ruby #{RUBY_VERSION}"
98
+
99
+ pwd = Dir.pwd
100
+ ext_dir = File.expand_path('ext/oboe_metal')
101
+ symlink = File.expand_path('lib/oboe_metal.so')
102
+ so_file = File.expand_path('ext/oboe_metal/oboe_metal.so')
103
+
104
+ Dir.chdir ext_dir
105
+ cmd = [Gem.ruby, 'extconf.rb']
106
+ sh cmd.join(' ')
107
+ sh '/usr/bin/env make'
108
+
109
+ File.delete symlink if File.exist? symlink
110
+
111
+ if File.exist? so_file
112
+ File.symlink so_file, symlink
113
+ Dir.chdir pwd
114
+ puts "== Extension built and symlink'd to #{symlink}"
115
+ else
116
+ Dir.chdir pwd
117
+ puts '!! Extension failed to build (see above). Have the required binary and header files been fetched?'
118
+ puts '!! Try the tasks in this order: clean > fetchsource > compile.'
119
+ end
120
+ else
121
+ puts '== Nothing to do under JRuby.'
122
+ end
123
+ end
124
+
125
+ desc 'Clean up extension build files'
126
+ task :clean do
127
+ if !defined?(JRUBY_VERSION)
128
+ pwd = Dir.pwd
129
+ ext_dir = File.expand_path('ext/oboe_metal')
130
+ symlinks = [
131
+ File.expand_path('lib/oboe_metal.so'),
132
+ File.expand_path('ext/oboe_metal/lib/liboboe.so'),
133
+ File.expand_path('ext/oboe_metal/lib/liboboe-1.0.so.0')
134
+ ]
135
+
136
+ symlinks.each do |symlink|
137
+ FileUtils.rm_f symlink
138
+ end
139
+ Dir.chdir ext_dir
140
+ sh '/usr/bin/env make clean' if File.exist? 'Makefile'
141
+
142
+ Dir.chdir pwd
143
+ else
144
+ puts '== Nothing to do under JRuby.'
145
+ end
146
+ end
147
+
148
+ desc 'Remove all built files and extensions'
149
+ task :distclean do
150
+ if !defined?(JRUBY_VERSION)
151
+ pwd = Dir.pwd
152
+ ext_dir = File.expand_path('ext/oboe_metal')
153
+ mkmf_log = File.expand_path('ext/oboe_metal/mkmf.log')
154
+ symlinks = [
155
+ File.expand_path('lib/oboe_metal.so'),
156
+ File.expand_path('ext/oboe_metal/lib/liboboe.so'),
157
+ File.expand_path('ext/oboe_metal/lib/liboboe-1.0.so.0')
158
+ ]
159
+
160
+ if File.exist? mkmf_log
161
+ symlinks.each do |symlink|
162
+ FileUtils.rm_f symlink
163
+ end
164
+ Dir.chdir ext_dir
165
+ sh '/usr/bin/env make distclean' if File.exist? 'Makefile'
166
+
167
+ Dir.chdir pwd
168
+ else
169
+ puts 'Nothing to distclean. (nothing built yet?)'
170
+ end
171
+ else
172
+ puts '== Nothing to do under JRuby.'
173
+ end
174
+ end
175
+
176
+ desc "Rebuild the gem's c extension"
177
+ task :recompile => [:distclean, :compile]
178
+
179
+ task :environment do
180
+ ENV['APPOPTICS_GEM_VERBOSE'] = 'true'
181
+
182
+ Bundler.require(:default, :development)
183
+ AppOpticsAPM::Config[:tracing_mode] = :always
184
+ AppOpticsAPM::Test.load_extras
185
+
186
+ if AppOpticsAPM::Test.gemfile?(:delayed_job)
187
+ require 'delayed/tasks'
188
+ end
189
+ end
190
+
191
+ task :console => :environment do
192
+ ARGV.clear
193
+ if AppOpticsAPM::Test.gemfile?(:delayed_job)
194
+ require './test/servers/delayed_job'
195
+ end
196
+ Pry.start
197
+ end
198
+
199
+ # Used when testing Resque locally
200
+ task 'resque:setup' => :environment do
201
+ require 'resque/tasks'
202
+ end
data/Vagrantfile ADDED
@@ -0,0 +1,67 @@
1
+ # -*- mode: ruby -*-
2
+ # vi: set ft=ruby :
3
+
4
+ # This Vagrantfile creates a build machine for our appoptics_apm gem.
5
+ # Usage:
6
+ # $ vagrant up
7
+ # $ vagrant ssh
8
+ # $ ./build_gems.bash
9
+ #
10
+ # The project directory on your host will be shared to /vagrant in the VM
11
+
12
+ # Your rubygems push credentials, if saved.
13
+ dot_gem_credentials = File.expand_path(ENV['vg_dot_gem_credentials'] || '~/.gem/credentials')
14
+
15
+ Vagrant.configure("2") do |config|
16
+
17
+ config.vm.box = "ubuntu/trusty64"
18
+
19
+ config.vm.provision "shell", privileged: false, inline: <<-SHELL
20
+ # install OS packages
21
+ sudo apt-get update
22
+ sudo apt-get install -y build-essential curl git-core libssl-dev libreadline-dev openjdk-7-jdk zlib1g-dev
23
+
24
+ # rbenv setup
25
+ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
26
+ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
27
+ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.profile
28
+ echo 'eval "$(rbenv init -)"' >> ~/.profile
29
+ . ~/.profile
30
+
31
+ # install rubies to build our gem against
32
+ rbenv install 2.3.1
33
+ rbenv install jruby-9.0.5.0
34
+
35
+ # install swig 3.0.8
36
+ sudo apt-get install -y libpcre3-dev
37
+ curl -LO http://kent.dl.sourceforge.net/project/swig/swig/swig-3.0.8/swig-3.0.8.tar.gz
38
+ tar xzf swig-3.0.8.tar.gz
39
+ pushd swig-3.0.8
40
+ ./configure && make && sudo make install
41
+ popd
42
+ SHELL
43
+
44
+ if File.exist? dot_gem_credentials
45
+ config.vm.provision "file",
46
+ source: dot_gem_credentials,
47
+ destination: "~/.gem/credentials"
48
+ end
49
+
50
+ config.vm.provision "shell", privileged: false, inline: <<-SHELL
51
+ cat <<'BUILDSCRIPT'> ~/build_gems.bash
52
+ #!/usr/bin/env bash
53
+ cd /vagrant
54
+ export RBENV_VERSION=jruby-9.0.5.0
55
+ jruby -S gem build appoptics_apm.gemspec
56
+
57
+ export RBENV_VERSION=2.3.1
58
+ gem build appoptics_apm.gemspec
59
+
60
+ ls -la appoptics_apm*.gem
61
+
62
+ echo "publish to rubygems via: gem push <gem>"
63
+ BUILDSCRIPT
64
+ chmod 755 ~/build_gems.bash
65
+ SHELL
66
+
67
+ end
@@ -0,0 +1,55 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "appoptics_apm/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = %q{appoptics_apm}
6
+ s.version = AppOpticsAPM::Version::STRING
7
+ s.date = Time.now.strftime('%Y-%m-%d')
8
+
9
+ s.license = "Librato Open License, Version 1.0"
10
+
11
+ s.authors = ["Maia Engeli", "Peter Giacomo Lombardo", "Spiros Eliopoulos"]
12
+ s.email = %q{support@appoptics.com}
13
+ s.homepage = %q{https://www.appoptics.com/}
14
+ s.summary = %q{AppOptics APM performance instrumentation gem for Ruby}
15
+ s.description = %q{The AppOpticsAPM gem provides performance instrumentation for MRI Ruby and related frameworks.}
16
+
17
+ s.extra_rdoc_files = ["LICENSE"]
18
+ s.files = `git ls-files`.split("\n")
19
+ s.files += ['ext/oboe_metal/lib/liboboe-1.0.so.0.0.0',
20
+ 'ext/oboe_metal/src/oboe.h',
21
+ 'ext/oboe_metal/src/oboe.hpp',
22
+ 'ext/oboe_metal/src/oboe_debug.h',
23
+ 'ext/oboe_metal/src/oboe_wrap.cxx',
24
+ 'ext/oboe_metal/src/VERSION']
25
+ s.test_files = Dir.glob("{test}/**/*.rb")
26
+
27
+ # TODO this is commented out util we can actually provide gems for different platforms
28
+ # s.platform = defined?(JRUBY_VERSION) ? 'java' : Gem::Platform::CURRENT
29
+ s.extensions = ['ext/oboe_metal/extconf.rb'] unless defined?(JRUBY_VERSION)
30
+
31
+ s.add_runtime_dependency('json', '>= 0')
32
+
33
+ # Development dependencies used in gem development & testing
34
+ s.add_development_dependency('rake', '>= 0.9.0')
35
+
36
+ unless defined?(JRUBY_VERSION)
37
+ case RUBY_VERSION
38
+ when /^1\.9/
39
+ s.add_development_dependency('debugger', '>= 1.6.7')
40
+ s.add_development_dependency('pry', '>= 0.10.0')
41
+ when /^2\./
42
+ s.add_development_dependency('byebug', '>= 8.0.0')
43
+ s.add_development_dependency('pry', '>= 0.10.0')
44
+ s.add_development_dependency('pry-byebug', '>= 3.0.0')
45
+ end
46
+ else
47
+ s.add_development_dependency('pry', '>= 0.10.0')
48
+ end
49
+
50
+ s.add_development_dependency('benchmark-ips')
51
+ s.add_development_dependency('benchmark-memory')
52
+ s.add_development_dependency('ruby-prof')
53
+
54
+ s.required_ruby_version = '>= 1.9.3'
55
+ end
data/build_gems.sh ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env bash
2
+ # builds the appoptics_apm gem for JRuby and MRI.
3
+
4
+ echo -e "\n=== building for MRI ===\n"
5
+ export RBENV_VERSION=2.3.1
6
+ rm -f Gemfile.lock
7
+ bundle install
8
+ bundle exec rake distclean
9
+ bundle exec rake fetch_ext_deps
10
+ gem build appoptics_apm.gemspec
11
+
12
+ echo -e "\n=== built gems ===\n"
13
+ ls -la appoptics_apm*.gem
14
+
15
+ echo -e "\n=== publish to rubygems via: gem push <gem> ===\n"