appoptics_apm-zearn 4.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (145) hide show
  1. checksums.yaml +7 -0
  2. data/.dockerignore +5 -0
  3. data/.github/ISSUE_TEMPLATE/bug-or-feature-request.md +16 -0
  4. data/.github/workflows/build_and_release_gem.yml +103 -0
  5. data/.github/workflows/build_for_packagecloud.yml +70 -0
  6. data/.github/workflows/docker-images.yml +47 -0
  7. data/.github/workflows/run_cpluplus_tests.yml +73 -0
  8. data/.github/workflows/run_tests.yml +168 -0
  9. data/.github/workflows/scripts/test_install.rb +23 -0
  10. data/.github/workflows/swig/swig-v4.0.2.tar.gz +0 -0
  11. data/.github/workflows/test_on_4_linux.yml +159 -0
  12. data/.gitignore +36 -0
  13. data/.rubocop.yml +29 -0
  14. data/.travis.yml +130 -0
  15. data/.yardopts +6 -0
  16. data/CHANGELOG.md +769 -0
  17. data/CONFIG.md +33 -0
  18. data/Gemfile +14 -0
  19. data/LICENSE +202 -0
  20. data/README.md +393 -0
  21. data/appoptics_apm.gemspec +70 -0
  22. data/bin/appoptics_apm_config +15 -0
  23. data/examples/prepend.rb +13 -0
  24. data/examples/sdk_examples.rb +158 -0
  25. data/ext/oboe_metal/README.md +69 -0
  26. data/ext/oboe_metal/extconf.rb +151 -0
  27. data/ext/oboe_metal/lib/.keep +0 -0
  28. data/ext/oboe_metal/lib/liboboe-1.0-alpine-x86_64.so.0.0.0.sha256 +1 -0
  29. data/ext/oboe_metal/lib/liboboe-1.0-x86_64.so.0.0.0.sha256 +1 -0
  30. data/ext/oboe_metal/noop/noop.c +8 -0
  31. data/ext/oboe_metal/src/README.md +6 -0
  32. data/ext/oboe_metal/src/VERSION +2 -0
  33. data/ext/oboe_metal/src/bson/bson.h +220 -0
  34. data/ext/oboe_metal/src/bson/platform_hacks.h +91 -0
  35. data/ext/oboe_metal/src/frames.cc +246 -0
  36. data/ext/oboe_metal/src/frames.h +40 -0
  37. data/ext/oboe_metal/src/init_appoptics_apm.cc +21 -0
  38. data/ext/oboe_metal/src/logging.cc +95 -0
  39. data/ext/oboe_metal/src/logging.h +35 -0
  40. data/ext/oboe_metal/src/oboe.h +1156 -0
  41. data/ext/oboe_metal/src/oboe_api.cpp +652 -0
  42. data/ext/oboe_metal/src/oboe_api.hpp +431 -0
  43. data/ext/oboe_metal/src/oboe_debug.h +59 -0
  44. data/ext/oboe_metal/src/oboe_swig_wrap.cc +7329 -0
  45. data/ext/oboe_metal/src/profiling.cc +435 -0
  46. data/ext/oboe_metal/src/profiling.h +78 -0
  47. data/ext/oboe_metal/test/CMakeLists.txt +53 -0
  48. data/ext/oboe_metal/test/FindGMock.cmake +43 -0
  49. data/ext/oboe_metal/test/README.md +56 -0
  50. data/ext/oboe_metal/test/frames_test.cc +164 -0
  51. data/ext/oboe_metal/test/profiling_test.cc +93 -0
  52. data/ext/oboe_metal/test/ruby_inc_dir.rb +8 -0
  53. data/ext/oboe_metal/test/ruby_prefix.rb +8 -0
  54. data/ext/oboe_metal/test/ruby_test_helper.rb +67 -0
  55. data/ext/oboe_metal/test/test.h +11 -0
  56. data/ext/oboe_metal/test/test_main.cc +32 -0
  57. data/init.rb +4 -0
  58. data/lib/appoptics_apm/api/layerinit.rb +41 -0
  59. data/lib/appoptics_apm/api/logging.rb +381 -0
  60. data/lib/appoptics_apm/api/memcache.rb +37 -0
  61. data/lib/appoptics_apm/api/metrics.rb +63 -0
  62. data/lib/appoptics_apm/api/tracing.rb +57 -0
  63. data/lib/appoptics_apm/api/util.rb +120 -0
  64. data/lib/appoptics_apm/api.rb +21 -0
  65. data/lib/appoptics_apm/base.rb +231 -0
  66. data/lib/appoptics_apm/config.rb +299 -0
  67. data/lib/appoptics_apm/frameworks/grape.rb +98 -0
  68. data/lib/appoptics_apm/frameworks/padrino.rb +78 -0
  69. data/lib/appoptics_apm/frameworks/rails/inst/action_controller.rb +104 -0
  70. data/lib/appoptics_apm/frameworks/rails/inst/action_controller4.rb +48 -0
  71. data/lib/appoptics_apm/frameworks/rails/inst/action_controller5.rb +50 -0
  72. data/lib/appoptics_apm/frameworks/rails/inst/action_controller6.rb +50 -0
  73. data/lib/appoptics_apm/frameworks/rails/inst/action_controller_api.rb +50 -0
  74. data/lib/appoptics_apm/frameworks/rails/inst/action_view.rb +88 -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 +29 -0
  78. data/lib/appoptics_apm/frameworks/rails/inst/connection_adapters/postgresql.rb +31 -0
  79. data/lib/appoptics_apm/frameworks/rails/inst/connection_adapters/utils.rb +119 -0
  80. data/lib/appoptics_apm/frameworks/rails/inst/connection_adapters/utils5x.rb +114 -0
  81. data/lib/appoptics_apm/frameworks/rails/inst/logger_formatters.rb +27 -0
  82. data/lib/appoptics_apm/frameworks/rails.rb +100 -0
  83. data/lib/appoptics_apm/frameworks/sinatra.rb +96 -0
  84. data/lib/appoptics_apm/inst/bunny-client.rb +148 -0
  85. data/lib/appoptics_apm/inst/bunny-consumer.rb +89 -0
  86. data/lib/appoptics_apm/inst/curb.rb +332 -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 +101 -0
  90. data/lib/appoptics_apm/inst/excon.rb +125 -0
  91. data/lib/appoptics_apm/inst/faraday.rb +106 -0
  92. data/lib/appoptics_apm/inst/graphql.rb +240 -0
  93. data/lib/appoptics_apm/inst/grpc_client.rb +159 -0
  94. data/lib/appoptics_apm/inst/grpc_server.rb +120 -0
  95. data/lib/appoptics_apm/inst/http.rb +81 -0
  96. data/lib/appoptics_apm/inst/httpclient.rb +174 -0
  97. data/lib/appoptics_apm/inst/logger_formatter.rb +50 -0
  98. data/lib/appoptics_apm/inst/logging_log_event.rb +28 -0
  99. data/lib/appoptics_apm/inst/lumberjack_formatter.rb +13 -0
  100. data/lib/appoptics_apm/inst/memcached.rb +86 -0
  101. data/lib/appoptics_apm/inst/mongo.rb +246 -0
  102. data/lib/appoptics_apm/inst/mongo2.rb +225 -0
  103. data/lib/appoptics_apm/inst/moped.rb +466 -0
  104. data/lib/appoptics_apm/inst/rack.rb +182 -0
  105. data/lib/appoptics_apm/inst/rack_cache.rb +35 -0
  106. data/lib/appoptics_apm/inst/redis.rb +274 -0
  107. data/lib/appoptics_apm/inst/resque.rb +151 -0
  108. data/lib/appoptics_apm/inst/rest-client.rb +48 -0
  109. data/lib/appoptics_apm/inst/sequel.rb +178 -0
  110. data/lib/appoptics_apm/inst/sidekiq-client.rb +55 -0
  111. data/lib/appoptics_apm/inst/sidekiq-worker.rb +66 -0
  112. data/lib/appoptics_apm/inst/twitter-cassandra.rb +294 -0
  113. data/lib/appoptics_apm/inst/typhoeus.rb +108 -0
  114. data/lib/appoptics_apm/instrumentation.rb +22 -0
  115. data/lib/appoptics_apm/loading.rb +65 -0
  116. data/lib/appoptics_apm/logger.rb +14 -0
  117. data/lib/appoptics_apm/noop/README.md +9 -0
  118. data/lib/appoptics_apm/noop/context.rb +27 -0
  119. data/lib/appoptics_apm/noop/metadata.rb +25 -0
  120. data/lib/appoptics_apm/noop/profiling.rb +21 -0
  121. data/lib/appoptics_apm/oboe_init_options.rb +211 -0
  122. data/lib/appoptics_apm/ruby.rb +35 -0
  123. data/lib/appoptics_apm/sdk/current_trace.rb +77 -0
  124. data/lib/appoptics_apm/sdk/custom_metrics.rb +94 -0
  125. data/lib/appoptics_apm/sdk/logging.rb +37 -0
  126. data/lib/appoptics_apm/sdk/tracing.rb +434 -0
  127. data/lib/appoptics_apm/support/profiling.rb +18 -0
  128. data/lib/appoptics_apm/support/transaction_metrics.rb +67 -0
  129. data/lib/appoptics_apm/support/transaction_settings.rb +219 -0
  130. data/lib/appoptics_apm/support/x_trace_options.rb +110 -0
  131. data/lib/appoptics_apm/support_report.rb +119 -0
  132. data/lib/appoptics_apm/test.rb +95 -0
  133. data/lib/appoptics_apm/thread_local.rb +26 -0
  134. data/lib/appoptics_apm/util.rb +326 -0
  135. data/lib/appoptics_apm/version.rb +16 -0
  136. data/lib/appoptics_apm/xtrace.rb +115 -0
  137. data/lib/appoptics_apm.rb +77 -0
  138. data/lib/joboe_metal.rb +212 -0
  139. data/lib/oboe.rb +7 -0
  140. data/lib/oboe_metal.rb +172 -0
  141. data/lib/rails/generators/appoptics_apm/install_generator.rb +47 -0
  142. data/lib/rails/generators/appoptics_apm/templates/appoptics_apm_initializer.rb +425 -0
  143. data/log/.keep +0 -0
  144. data/yardoc_frontpage.md +26 -0
  145. metadata +231 -0
@@ -0,0 +1,434 @@
1
+ #--
2
+ # Copyright (c) 2016 SolarWinds, LLC.
3
+ # All rights reserved.
4
+ #++
5
+
6
+ module AppOpticsAPM
7
+ module SDK
8
+
9
+ ##
10
+ # Traces are best created with an <tt>AppOpticsAPM::SDK.start_trace</tt> block and
11
+ # <tt>AppOpticsAPM::SDK.trace</tt> blocks around calls to be traced.
12
+ # These two methods guarantee proper nesting of traces, handling of the tracing context, as well as avoiding
13
+ # broken traces in case of exceptions.
14
+ #
15
+ # Some optional keys that can be used in the +opts+ hash:
16
+ # * +:Controller+
17
+ # * +:Action+
18
+ # * +:HTTP-Host+
19
+ # * +:URL+
20
+ # * +:Method+
21
+ #
22
+ # as well as custom keys. The information will show up in the raw data view of a span.
23
+ #
24
+ # Invalid keys: +:Label+, +:Layer+, +:Edge+, +:Timestamp+, +:Timestamp_u+, +:TransactionName+ (allowed in start_trace)
25
+ #
26
+ # The methods are exposed as singleton methods for AppOpticsAPM::SDK.
27
+ #
28
+ # === Usage:
29
+ # * +AppOpticsAPM::SDK.appoptics_ready?+
30
+ # * +AppOpticsAPM::SDK.get_transaction_name+
31
+ # * +AppOpticsAPM::SDK.set_transaction_name+
32
+ # * +AppOpticsAPM::SDK.start_trace+
33
+ # * +AppOpticsAPM::SDK.start_trace_with_target+
34
+ # * +AppOpticsAPM::SDK.trace+
35
+ # * +AppOpticsAPM::SDK.trace_method+
36
+ # * +AppOpticsAPM::SDK.tracing?+
37
+ #
38
+ # === Example:
39
+ # class MonthlyCouponEmailJob
40
+ # def perform(*args)
41
+ #
42
+ # # KVs to report to the dashboard
43
+ # report_kvs = {}
44
+ # report_kvs[:Spec] = :job
45
+ # report_kvs[:Controller] = :MonthlyEmailJob
46
+ # report_kvs[:Action] = :CouponEmailer
47
+ #
48
+ # # Start tracing this job with start_trace
49
+ # AppOpticsAPM::SDK.start_trace('starling', nil, report_kvs) do
50
+ # monthly = MonthlyEmail.new(:CouponEmailer)
51
+ #
52
+ # # Trace a sub-component of this trace
53
+ # AppOpticsAPM::SDK.trace(self.class.name) do
54
+ #
55
+ # # The work to be done
56
+ # users = User.all
57
+ # users.each do |u|
58
+ # monthly.send(u.email)
59
+ # end
60
+ #
61
+ # end
62
+ # end
63
+ # end
64
+ # end
65
+ #
66
+ module Tracing
67
+
68
+ # Trace a given block of code.
69
+ #
70
+ # Also detects any exceptions thrown by the block and report errors.
71
+ #
72
+ # === Arguments:
73
+ # * +:span+ - The span the block of code belongs to.
74
+ # * +:opts+ - (optional) A hash containing key/value pairs that will be reported along with the first event of this span.
75
+ # * +:protect_op+ - (optional) The operation being traced. Used to avoid double tracing operations that call each other.
76
+ #
77
+ # === Example:
78
+ #
79
+ # def computation_with_appoptics(n)
80
+ # AppOpticsAPM::SDK.trace('computation', { :number => n }, :computation) do
81
+ # return n if n == 0
82
+ # n + computation_with_appoptics(n-1)
83
+ # end
84
+ # end
85
+ #
86
+ # result = computation_with_appoptics(100)
87
+ #
88
+ # === Returns:
89
+ # * The result of the block.
90
+ #
91
+ def trace(span, opts = {}, protect_op = nil)
92
+ return yield if !AppOpticsAPM.loaded || !AppOpticsAPM.tracing? || AppOpticsAPM.tracing_layer_op?(protect_op)
93
+
94
+ opts.delete(:TransactionName)
95
+ opts.delete('TransactionName')
96
+
97
+ AppOpticsAPM::API.log_entry(span, opts, protect_op)
98
+ opts[:Backtrace] && opts.delete(:Backtrace) # to avoid sending backtrace twice (faster to check presence here)
99
+ begin
100
+ yield
101
+ rescue Exception => e
102
+ AppOpticsAPM::API.log_exception(span, e)
103
+ raise
104
+ ensure
105
+ AppOpticsAPM::API.log_exit(span, opts, protect_op)
106
+ end
107
+ end
108
+
109
+
110
+ # Collect metrics and start tracing a given block of code.
111
+ #
112
+ # This will start a trace depending on configuration and probability, detect any exceptions
113
+ # thrown by the block, and report errors.
114
+ #
115
+ # When start_trace returns control to the calling context, the trace will be
116
+ # completed and the tracing context will be cleared.
117
+ #
118
+ # === Arguments:
119
+ #
120
+ # * +span+ - Name for the span to be used as label in the trace view.
121
+ # * +xtrace+ - (optional) incoming X-Trace identifier to be continued.
122
+ # * +opts+ - (optional) hash containing key/value pairs that will be reported with this span.
123
+ # The value of :TransactionName will set the transaction_name.
124
+ #
125
+ # === Example:
126
+ #
127
+ # def handle_request(request, response)
128
+ # # ... code that processes request and response ...
129
+ # end
130
+ #
131
+ # def handle_request_with_appoptics(request, response)
132
+ # AppOpticsAPM::SDK.start_trace('custom_trace', nil, :TransactionName => 'handle_request') do
133
+ # handle_request(request, response)
134
+ # end
135
+ # end
136
+ #
137
+ # === Returns:
138
+ # * The result of the block.
139
+ #
140
+ def start_trace(span, xtrace = nil, opts = {})
141
+ start_trace_with_target(span, xtrace, {}, opts) { yield }
142
+ end
143
+
144
+ # Collect metrics, trace a given block of code, and assign trace info to target.
145
+ #
146
+ # This will start a trace depending on configuration and probability, detect any exceptions
147
+ # thrown by the block, report errors, and assign an X-Trace to the target.
148
+ #
149
+ # The motivating use case for this is HTTP streaming in rails3. We need
150
+ # access to the exit event's trace id so we can set the header before any
151
+ # work is done, and before any headers are sent back to the client.
152
+ #
153
+ # === Arguments:
154
+ # * +span+ - The span the block of code belongs to.
155
+ # * +xtrace+ - (optional) incoming X-Trace identifier to be continued.
156
+ # * +target+ - (optional) has to respond to #[]=, The target object in which to place the trace information.
157
+ # * +opts+ - (optional) hash containing key/value pairs that will be reported with this span.
158
+ #
159
+ # === Example:
160
+ #
161
+ # def handle_request(request, response)
162
+ # # ... code that processes request and response ...
163
+ # end
164
+ #
165
+ # def handle_request_with_appoptics(request, response)
166
+ # AppOpticsAPM::SDK.start_trace_with_target('rails', request['X-Trace'], response) do
167
+ # handle_request(request, response)
168
+ # end
169
+ # end
170
+ #
171
+ # === Returns:
172
+ # * The result of the block.
173
+ #
174
+ def start_trace_with_target(span, xtrace, target, opts = {})
175
+ return yield unless AppOpticsAPM.loaded
176
+
177
+ if AppOpticsAPM::Context.isValid # not an entry span!
178
+ result = trace(span, opts) { yield }
179
+ target['X-Trace'] = AppOpticsAPM::Context.toString
180
+ return result
181
+ end
182
+
183
+ # :TransactionName and 'TransactionName' need to be removed from opts
184
+ AppOpticsAPM.transaction_name = opts.delete('TransactionName') || opts.delete(:TransactionName)
185
+
186
+ AppOpticsAPM::API.log_start(span, xtrace, opts)
187
+ opts[:Backtrace] && opts.delete(:Backtrace) # to avoid sending backtrace twice (faster to check presence here)
188
+
189
+ # AppOpticsAPM::Event.startTrace creates an Event without an Edge
190
+ exit_evt = AppOpticsAPM::Event.startTrace(AppOpticsAPM::Context.get)
191
+
192
+ result = begin
193
+ AppOpticsAPM::API.send_metrics(span, opts) do
194
+ target['X-Trace'] = AppOpticsAPM::EventUtil.metadataString(exit_evt)
195
+ yield
196
+ end
197
+ rescue Exception => e
198
+ AppOpticsAPM::API.log_exception(span, e)
199
+ exit_evt.addEdge(AppOpticsAPM::Context.get)
200
+ xtrace = AppOpticsAPM::API.log_end(span, opts, exit_evt)
201
+ e.instance_variable_set(:@xtrace, xtrace)
202
+ raise
203
+ end
204
+
205
+ exit_evt.addEdge(AppOpticsAPM::Context.get)
206
+ AppOpticsAPM::API.log_end(span, opts, exit_evt)
207
+
208
+ result
209
+ end
210
+
211
+ ##
212
+ # Add tracing to a given method
213
+ #
214
+ # This instruments the given method so that every time it is called it
215
+ # will create a span depending on the current context.
216
+ #
217
+ # The method can be of any (accessible) type (instance, singleton,
218
+ # private, protected etc.).
219
+ #
220
+ # The motivating use case for this is MetalController methods in Rails,
221
+ # which can't be auto-instrumented.
222
+ #
223
+ # === Arguments:
224
+ # * +klass+ - The module/class the method belongs to.
225
+ # * +method+ - The method name as symbol
226
+ # * +config+ - (optional) possible keys are:
227
+ # :name the name of the span (default: the method name)
228
+ # :backtrace true/false (default: false) if true the backtrace will be added to the space
229
+ # * +opts+ - (optional) hash containing key/value pairs that will be reported with this span.
230
+ #
231
+ # === Example:
232
+ #
233
+ # module ExampleModule
234
+ # def do_sum(a, b)
235
+ # a + b
236
+ # end
237
+ # end
238
+ #
239
+ # AppOpticsAPM::SDK.trace_method(ExampleModule, :do_sum, {name: 'computation', backtrace: true}, { CustomKey: "some_info"})
240
+ #
241
+ def trace_method(klass, method, config = {}, opts = {})
242
+ # If we're on an unsupported platform (ahem Mac), just act
243
+ # like we did something to nicely play the no-op part.
244
+ return true unless AppOpticsAPM.loaded
245
+
246
+ if !klass.is_a?(Module)
247
+ AppOpticsAPM.logger.warn "[appoptics_apm/error] trace_method: Not sure what to do with #{klass}. Send a class or module."
248
+ return false
249
+ end
250
+
251
+ if method.is_a?(String)
252
+ method = method.to_sym
253
+ elsif !method.is_a?(Symbol)
254
+ AppOpticsAPM.logger.warn "[appoptics_apm/error] trace_method: Not sure what to do with #{method}. Send a string or symbol for method."
255
+ return false
256
+ end
257
+
258
+ instance_method = klass.instance_methods.include?(method) || klass.private_instance_methods.include?(method)
259
+ class_method = klass.singleton_methods.include?(method)
260
+
261
+ # Make sure the request klass::method exists
262
+ if !instance_method && !class_method
263
+ AppOpticsAPM.logger.warn "[appoptics_apm/error] trace_method: Can't instrument #{klass}.#{method} as it doesn't seem to exist."
264
+ AppOpticsAPM.logger.warn "[appoptics_apm/error] #{__FILE__}:#{__LINE__}"
265
+ return false
266
+ end
267
+
268
+ # Strip '!' or '?' from method if present
269
+ safe_method_name = method.to_s.chop if method.to_s =~ /\?$|\!$/
270
+ safe_method_name ||= method
271
+
272
+ without_appoptics = "#{safe_method_name}_without_appoptics"
273
+ with_appoptics = "#{safe_method_name}_with_appoptics"
274
+
275
+ # Check if already profiled
276
+ if instance_method && klass.instance_methods.include?(with_appoptics.to_sym) ||
277
+ class_method && klass.singleton_methods.include?(with_appoptics.to_sym)
278
+ AppOpticsAPM.logger.warn "[appoptics_apm/error] trace_method: #{klass}::#{method} already instrumented.\n#{__FILE__}:#{__LINE__}"
279
+ return false
280
+ end
281
+
282
+ report_kvs = opts.dup
283
+ if defined?(::AbstractController::Base) && klass.ancestors.include?(::AbstractController::Base)
284
+ report_kvs[:Controller] = klass.to_s
285
+ report_kvs[:Action] = method.to_s
286
+ else
287
+ klass.is_a?(Class) ? report_kvs[:Class] = klass.to_s : report_kvs[:Module] = klass.to_s
288
+ report_kvs[:MethodName] = safe_method_name
289
+ end
290
+ backtrace = config[:backtrace]
291
+
292
+ span = config[:name] || method
293
+ if instance_method
294
+ klass.class_eval do
295
+ define_method(with_appoptics) do |*args, &block|
296
+ # if this is a rails controller we want to set the transaction for the outbound metrics
297
+ if report_kvs[:Controller] && defined?(request) && defined?(request.env)
298
+ request.env['appoptics_apm.controller'] = report_kvs[:Controller]
299
+ request.env['appoptics_apm.action'] = report_kvs[:Action]
300
+ end
301
+
302
+ AppOpticsAPM::SDK.trace(span, report_kvs) do
303
+ report_kvs[:Backtrace] = AppOpticsAPM::API.backtrace if backtrace
304
+ send(without_appoptics, *args, &block)
305
+ end
306
+ end
307
+
308
+ alias_method without_appoptics, method.to_s
309
+ alias_method method.to_s, with_appoptics
310
+ end
311
+ elsif class_method
312
+ klass.define_singleton_method(with_appoptics) do |*args, &block|
313
+ AppOpticsAPM::SDK.trace(span, report_kvs) do
314
+ report_kvs[:Backtrace] = AppOpticsAPM::API.backtrace if backtrace
315
+ send(without_appoptics, *args, &block)
316
+ end
317
+ end
318
+
319
+ klass.singleton_class.class_eval do
320
+ alias_method without_appoptics, method.to_s
321
+ alias_method method.to_s, with_appoptics
322
+ end
323
+ end
324
+ true
325
+ end
326
+
327
+ ##
328
+ # Provide a custom transaction name
329
+ #
330
+ # The AppOpticsAPM gem tries to create meaningful transaction names from controller+action
331
+ # or something similar depending on the framework used. However, you may want to override the
332
+ # transaction name to better describe your instrumented operation.
333
+ #
334
+ # Take note that on the dashboard the transaction name is converted to lowercase, and might be
335
+ # truncated with invalid characters replaced. Method calls with an empty string or a non-string
336
+ # argument won't change the current transaction name.
337
+ #
338
+ # The configuration +AppOpticsAPM.Config+['transaction_name']+['prepend_domain']+ can be set to
339
+ # true to have the domain name prepended to the transaction name when an event or a metric are
340
+ # logged. This is a global setting.
341
+ #
342
+ # === Argument:
343
+ #
344
+ # * +name+ - A non-empty string with the custom transaction name
345
+ #
346
+ # === Example:
347
+ #
348
+ # class DogfoodsController < ApplicationController
349
+ #
350
+ # def create
351
+ # @dogfood = Dogfood.new(params.permit(:brand, :name))
352
+ # @dogfood.save
353
+ #
354
+ # AppOpticsAPM::SDK.set_transaction_name("dogfoodscontroller.create_for_#{params[:brand]}")
355
+ #
356
+ # redirect_to @dogfood
357
+ # end
358
+ #
359
+ # end
360
+ #
361
+ # === Returns:
362
+ # * (String or nil) the current transaction name
363
+ #
364
+ def set_transaction_name(name)
365
+ if name.is_a?(String) && name.strip != ''
366
+ AppOpticsAPM.transaction_name = name
367
+ else
368
+ AppOpticsAPM.logger.debug "[appoptics_apm/api] Could not set transaction name, provided name is empty or not a String."
369
+ end
370
+ AppOpticsAPM.transaction_name
371
+ end
372
+
373
+
374
+ # Get the currently set custom transaction name.
375
+ #
376
+ # This is provided for testing
377
+ #
378
+ # === Returns:
379
+ # * (String or nil) the current transaction name (without domain prepended)
380
+ #
381
+ def get_transaction_name
382
+ AppOpticsAPM.transaction_name
383
+ end
384
+
385
+ # Determine if this transaction is being traced.
386
+ #
387
+ # Tracing puts some extra load on a system, therefor not all transaction are traced.
388
+ # The +tracing?+ method helps to determine this so that extra work can be avoided when not tracing.
389
+ #
390
+ # === Example:
391
+ #
392
+ # kvs = expensive_info_gathering_method if AppOpticsAPM::SDK.tracing?
393
+ # AppOpticsAPM::SDK.trace('some_span', kvs) do
394
+ # # this may not create a trace every time it runs
395
+ # db_request
396
+ # end
397
+ #
398
+ def tracing?
399
+ AppOpticsAPM.tracing?
400
+ end
401
+
402
+ # Wait for AppOptics to be ready to send traces.
403
+ #
404
+ # This may be useful in short lived background processes when it is important to capture
405
+ # information during the whole time the process is running. Usually AppOptics doesn't block an
406
+ # application while it is starting up.
407
+ #
408
+ # === Argument:
409
+ #
410
+ # * +wait_milliseconds+ (int, default 3000) the maximum time to wait in milliseconds
411
+ #
412
+ # === Example:
413
+ #
414
+ # unless AppOpticsAPM::SDK.appoptics_ready?(10_000)
415
+ # Logger.info "AppOptics not ready after 10 seconds, no metrics will be sent"
416
+ # end
417
+ #
418
+ def appoptics_ready?(wait_milliseconds = 3000)
419
+ return false unless AppOpticsAPM.loaded
420
+ # These codes are returned by isReady:
421
+ # OBOE_SERVER_RESPONSE_UNKNOWN 0
422
+ # OBOE_SERVER_RESPONSE_OK 1
423
+ # OBOE_SERVER_RESPONSE_TRY_LATER 2
424
+ # OBOE_SERVER_RESPONSE_LIMIT_EXCEEDED 3
425
+ # OBOE_SERVER_RESPONSE_INVALID_API_KEY 4
426
+ # OBOE_SERVER_RESPONSE_CONNECT_ERROR 5
427
+ AppOpticsAPM::Context.isReady(wait_milliseconds) == 1
428
+ end
429
+ end
430
+
431
+ extend Tracing
432
+
433
+ end
434
+ end
@@ -0,0 +1,18 @@
1
+ # Copyright (c) 2020 SolarWinds, LLC.
2
+ # All rights reserved.
3
+
4
+ module AppOpticsAPM
5
+ class Profiling
6
+
7
+ def self.run
8
+ # allow enabling and disabling and setting interval interactively
9
+ return yield unless AppOpticsAPM::Config.profiling == :enabled && AppOpticsAPM.tracing?
10
+
11
+ CProfiler.run(Thread.current, AppOpticsAPM::Config.profiling_interval) do
12
+ # for some reason `return` is needed here
13
+ # this is yielded by c-code, but why it needs `return` ... ????
14
+ return yield
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,67 @@
1
+ # Copyright (c) 2018 SolarWinds, LLC.
2
+ # All rights reserved.
3
+
4
+ module AppOpticsAPM
5
+ ##
6
+ # This module sends the duration of the call and
7
+ # sets the transaction_name
8
+ #
9
+ class TransactionMetrics
10
+ class << self
11
+
12
+ ##
13
+ # sends the duration of the call and
14
+ # sets the transaction_name
15
+ def metrics(env, settings)
16
+ if settings.do_metrics
17
+ req = ::Rack::Request.new(env)
18
+ # TODO rails 3x is not supported anymore ...
19
+ url = req.url # saving it here because rails3.2 overrides it when there is a 500 error
20
+ start = Time.now
21
+
22
+ begin
23
+ status, headers, response = yield
24
+
25
+ AppOpticsAPM.transaction_name = send_metrics(env, req, url, start, status)
26
+ rescue
27
+ AppOpticsAPM.transaction_name = send_metrics(env, req, url, start, status || '500')
28
+ raise
29
+ end
30
+ else
31
+ status, headers, response = yield
32
+ AppOpticsAPM.transaction_name = "#{domain(req)}#{transaction_name(env)}" if settings.do_sample
33
+ end
34
+
35
+ [status, headers, response]
36
+ end
37
+
38
+ private
39
+
40
+ def send_metrics(env, req, url, start, status)
41
+ name = transaction_name(env)
42
+
43
+ status = status.to_i
44
+ error = status.between?(500,599) ? 1 : 0
45
+ duration =(1000 * 1000 * (Time.now - start)).round(0)
46
+ method = req.request_method
47
+ # AppOpticsAPM.logger.warn "%%% Sending metrics: #{name}, #{url}, #{status} %%%"
48
+ AppOpticsAPM::Span.createHttpSpan(name, url, domain(req), duration, status, method, error) || ''
49
+ end
50
+
51
+ def domain(req)
52
+ if AppOpticsAPM::Config['transaction_name']['prepend_domain']
53
+ [80, 443].include?(req.port) ? req.host : "#{req.host}:#{req.port}"
54
+ end
55
+ end
56
+
57
+ def transaction_name(env)
58
+ return AppOpticsAPM.transaction_name if AppOpticsAPM.transaction_name
59
+
60
+ if env['appoptics_apm.controller'] && env['appoptics_apm.action']
61
+ [env['appoptics_apm.controller'], env['appoptics_apm.action']].join('.')
62
+ end
63
+ end
64
+
65
+ end
66
+ end
67
+ end