newrelic_rpm 3.11.2.286 → 3.12.0.288

Sign up to get free protection for your applications and to get access to all the features.
Files changed (269) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +3 -2
  3. data/.yardopts +2 -0
  4. data/CHANGELOG +39 -4
  5. data/README.md +4 -2
  6. data/lib/new_relic/agent.rb +229 -214
  7. data/lib/new_relic/agent/agent.rb +23 -12
  8. data/lib/new_relic/agent/attribute_filter.rb +242 -0
  9. data/lib/new_relic/agent/attribute_processing.rb +62 -0
  10. data/lib/new_relic/agent/commands/xray_session_collection.rb +4 -1
  11. data/lib/new_relic/agent/configuration/default_source.rb +284 -30
  12. data/lib/new_relic/agent/configuration/high_security_source.rb +0 -8
  13. data/lib/new_relic/agent/configuration/manager.rb +1 -1
  14. data/lib/new_relic/agent/configuration/server_source.rb +86 -31
  15. data/lib/new_relic/agent/configuration/yaml_source.rb +1 -1
  16. data/lib/new_relic/agent/cross_app_monitor.rb +8 -13
  17. data/lib/new_relic/agent/cross_app_tracing.rb +15 -15
  18. data/lib/new_relic/agent/custom_event_aggregator.rb +6 -2
  19. data/lib/new_relic/agent/database.rb +15 -2
  20. data/lib/new_relic/agent/datastores.rb +52 -38
  21. data/lib/new_relic/agent/datastores/metric_helper.rb +2 -1
  22. data/lib/new_relic/agent/encoding_normalizer.rb +82 -0
  23. data/lib/new_relic/agent/error_collector.rb +125 -169
  24. data/lib/new_relic/agent/hash_extensions.rb +26 -0
  25. data/lib/new_relic/agent/http_clients/excon_wrappers.rb +13 -11
  26. data/lib/new_relic/agent/http_clients/uri_util.rb +9 -0
  27. data/lib/new_relic/agent/instrumentation/action_controller_subscriber.rb +1 -3
  28. data/lib/new_relic/agent/instrumentation/action_view_subscriber.rb +1 -1
  29. data/lib/new_relic/agent/instrumentation/active_record_helper.rb +1 -1
  30. data/lib/new_relic/agent/instrumentation/active_record_subscriber.rb +12 -5
  31. data/lib/new_relic/agent/instrumentation/controller_instrumentation.rb +6 -7
  32. data/lib/new_relic/agent/instrumentation/curb.rb +6 -6
  33. data/lib/new_relic/agent/instrumentation/excon/middleware.rb +4 -4
  34. data/lib/new_relic/agent/instrumentation/grape.rb +4 -3
  35. data/lib/new_relic/agent/instrumentation/rails/action_controller.rb +6 -3
  36. data/lib/new_relic/agent/instrumentation/rails/errors.rb +9 -3
  37. data/lib/new_relic/agent/instrumentation/rails3/action_controller.rb +6 -1
  38. data/lib/new_relic/agent/instrumentation/rails3/errors.rb +9 -7
  39. data/lib/new_relic/agent/instrumentation/rails4/errors.rb +8 -6
  40. data/lib/new_relic/agent/instrumentation/resque.rb +2 -3
  41. data/lib/new_relic/agent/instrumentation/sidekiq.rb +3 -3
  42. data/lib/new_relic/agent/instrumentation/typhoeus.rb +2 -2
  43. data/lib/new_relic/agent/javascript_instrumentor.rb +24 -16
  44. data/lib/new_relic/agent/parameter_filtering.rb +8 -1
  45. data/lib/new_relic/agent/samplers/delayed_job_sampler.rb +2 -2
  46. data/lib/new_relic/agent/sql_sampler.rb +1 -0
  47. data/lib/new_relic/agent/stats.rb +0 -4
  48. data/lib/new_relic/agent/stats_engine/metric_stats.rb +5 -3
  49. data/lib/new_relic/agent/stats_engine/stats_hash.rb +4 -0
  50. data/lib/new_relic/agent/supported_versions.rb +2 -2
  51. data/lib/new_relic/agent/traced_method_stack.rb +3 -3
  52. data/lib/new_relic/agent/transaction.rb +141 -118
  53. data/lib/new_relic/agent/transaction/attributes.rb +161 -0
  54. data/lib/new_relic/agent/transaction/developer_mode_sample_buffer.rb +4 -4
  55. data/lib/new_relic/agent/transaction/trace.rb +150 -0
  56. data/lib/new_relic/agent/transaction/trace_node.rb +190 -0
  57. data/lib/new_relic/agent/transaction/transaction_sample_buffer.rb +2 -2
  58. data/lib/new_relic/agent/transaction_event_aggregator.rb +23 -7
  59. data/lib/new_relic/agent/transaction_sample_builder.rb +37 -62
  60. data/lib/new_relic/agent/transaction_sampler.rb +29 -66
  61. data/lib/new_relic/cli/commands/install.rb +2 -2
  62. data/lib/new_relic/coerce.rb +15 -28
  63. data/lib/new_relic/json_wrapper.rb +14 -73
  64. data/lib/new_relic/noticed_error.rb +81 -5
  65. data/lib/new_relic/rack/browser_monitoring.rb +14 -19
  66. data/lib/new_relic/rack/developer_mode.rb +68 -14
  67. data/lib/new_relic/rack/developer_mode/segment_summary.rb +56 -0
  68. data/lib/new_relic/version.rb +2 -2
  69. data/newrelic.yml +19 -196
  70. data/test/agent_helper.rb +42 -36
  71. data/test/config/newrelic.yml +0 -1
  72. data/test/environments/rails40/Gemfile +1 -1
  73. data/test/environments/rails41/Gemfile +1 -1
  74. data/test/environments/rails42/Gemfile +1 -1
  75. data/test/fixtures/cross_agent_tests/attribute_configuration.json +35 -0
  76. data/test/fixtures/cross_agent_tests/sql_obfuscation/README.md +19 -12
  77. data/test/fixtures/cross_agent_tests/sql_obfuscation/sql_obfuscation.json +365 -0
  78. data/test/multiverse/lib/multiverse/suite.rb +5 -1
  79. data/test/multiverse/suites/active_record/active_record_test.rb +8 -8
  80. data/test/multiverse/suites/agent_only/agent_attributes_test.rb +145 -0
  81. data/test/multiverse/suites/agent_only/cross_application_tracing_test.rb +8 -0
  82. data/test/multiverse/suites/agent_only/custom_queue_time_test.rb +5 -1
  83. data/test/multiverse/suites/agent_only/encoding_handling_test.rb +6 -6
  84. data/test/multiverse/suites/agent_only/marshaling_test.rb +1 -1
  85. data/test/multiverse/suites/agent_only/set_transaction_name_test.rb +2 -3
  86. data/test/multiverse/suites/agent_only/synthetics_test.rb +3 -3
  87. data/test/multiverse/suites/agent_only/testing_app.rb +4 -0
  88. data/test/multiverse/suites/agent_only/thread_profiling_test.rb +1 -1
  89. data/test/multiverse/suites/agent_only/utilization_data_collection_test.rb +9 -7
  90. data/test/multiverse/suites/curb/Envfile +5 -6
  91. data/test/multiverse/suites/curb/curb_test.rb +4 -5
  92. data/test/multiverse/suites/datamapper/datamapper_test.rb +2 -2
  93. data/test/multiverse/suites/excon/Envfile +11 -4
  94. data/test/multiverse/suites/excon/excon_test.rb +5 -5
  95. data/test/multiverse/suites/grape/config/newrelic.yml +1 -0
  96. data/test/multiverse/suites/grape/grape_test.rb +76 -12
  97. data/test/multiverse/suites/grape/grape_test_api.rb +16 -0
  98. data/test/multiverse/suites/high_security/config/newrelic.yml +43 -3
  99. data/test/multiverse/suites/high_security/high_security_test.rb +165 -9
  100. data/test/multiverse/suites/httpclient/Envfile +5 -1
  101. data/test/multiverse/suites/httpclient/httpclient_test.rb +2 -2
  102. data/test/multiverse/suites/memcached/Envfile +1 -1
  103. data/test/multiverse/suites/mongo/Envfile +8 -1
  104. data/test/multiverse/suites/mongo/helpers/mongo_operation_tests.rb +29 -29
  105. data/test/multiverse/suites/mongo/mongo_unsupported_version_test.rb +43 -8
  106. data/test/multiverse/suites/rack/rack_parameter_filtering_test.rb +13 -3
  107. data/test/multiverse/suites/rails/Envfile +3 -3
  108. data/test/multiverse/suites/rails/error_tracing_test.rb +52 -31
  109. data/test/multiverse/suites/rails/gc_instrumentation_test.rb +1 -1
  110. data/test/multiverse/suites/rails/ignore_test.rb +1 -1
  111. data/test/multiverse/suites/rails/parameter_capture_test.rb +108 -40
  112. data/test/multiverse/suites/rails/request_statistics_test.rb +10 -4
  113. data/test/multiverse/suites/rails/view_instrumentation_test.rb +24 -24
  114. data/test/multiverse/suites/resque/instrumentation_test.rb +46 -12
  115. data/test/multiverse/suites/sequel/sequel_extension_test.rb +8 -8
  116. data/test/multiverse/suites/sequel/sequel_helpers.rb +11 -11
  117. data/test/multiverse/suites/sequel/sequel_plugin_test.rb +11 -11
  118. data/test/multiverse/suites/sidekiq/Envfile +1 -4
  119. data/test/multiverse/suites/sidekiq/after_suite.rb +9 -0
  120. data/test/multiverse/suites/sidekiq/sidekiq_instrumentation_test.rb +49 -16
  121. data/test/multiverse/suites/sidekiq/test_worker.rb +1 -2
  122. data/test/multiverse/suites/sinatra/Envfile +1 -1
  123. data/test/multiverse/suites/sinatra/config/newrelic.yml +1 -0
  124. data/test/multiverse/suites/sinatra/sinatra_classic_test.rb +0 -4
  125. data/test/multiverse/suites/sinatra/sinatra_modular_test.rb +0 -4
  126. data/test/multiverse/suites/sinatra/sinatra_parameter_capture_test.rb +65 -0
  127. data/test/multiverse/suites/sinatra/sinatra_test_cases.rb +0 -11
  128. data/test/multiverse/suites/typhoeus/Envfile +8 -2
  129. data/test/multiverse/suites/typhoeus/typhoeus_test.rb +4 -4
  130. data/test/new_relic/agent/agent/connect_test.rb +13 -9
  131. data/test/new_relic/agent/agent_test.rb +34 -24
  132. data/test/new_relic/agent/attribute_filter_test.rb +218 -0
  133. data/test/new_relic/agent/attribute_processing_test.rb +160 -0
  134. data/test/new_relic/agent/configuration/default_source_test.rb +88 -0
  135. data/test/new_relic/agent/configuration/manager_test.rb +3 -4
  136. data/test/new_relic/agent/configuration/orphan_configuration_test.rb +3 -1
  137. data/test/new_relic/agent/configuration/server_source_test.rb +39 -0
  138. data/test/new_relic/agent/cross_app_monitor_test.rb +6 -30
  139. data/test/new_relic/agent/cross_app_tracing_test.rb +12 -12
  140. data/test/new_relic/agent/database/sql_obfuscation_test.rb +39 -65
  141. data/test/new_relic/agent/datastores/metric_helper_test.rb +36 -0
  142. data/test/new_relic/agent/encoding_normalizer_test.rb +66 -0
  143. data/test/new_relic/agent/error_collector_test.rb +181 -34
  144. data/test/new_relic/agent/hash_extensions_test.rb +34 -0
  145. data/test/new_relic/agent/instrumentation/action_controller_subscriber_test.rb +20 -23
  146. data/test/new_relic/agent/instrumentation/action_view_subscriber_test.rb +12 -12
  147. data/test/new_relic/agent/instrumentation/active_record_subscriber_test.rb +5 -5
  148. data/test/new_relic/agent/instrumentation/controller_instrumentation_test.rb +4 -4
  149. data/test/new_relic/agent/instrumentation/task_instrumentation_test.rb +21 -11
  150. data/test/new_relic/agent/javascript_instrumentor_test.rb +69 -45
  151. data/test/new_relic/agent/pipe_service_test.rb +2 -2
  152. data/test/new_relic/agent/stats_engine/gc_profiler_test.rb +1 -1
  153. data/test/new_relic/agent/stats_engine/metric_stats_test.rb +8 -7
  154. data/test/new_relic/agent/stats_engine/stats_hash_test.rb +2 -2
  155. data/test/new_relic/agent/threading/backtrace_node_test.rb +2 -2
  156. data/test/new_relic/agent/transaction/attributes_test.rb +276 -0
  157. data/test/new_relic/agent/transaction/developer_mode_sample_buffer_test.rb +10 -10
  158. data/test/new_relic/agent/transaction/trace_node_test.rb +361 -0
  159. data/test/new_relic/agent/transaction/trace_test.rb +394 -0
  160. data/test/new_relic/agent/transaction/xray_sample_buffer_test.rb +1 -1
  161. data/test/new_relic/agent/transaction_event_aggregator_test.rb +127 -57
  162. data/test/new_relic/agent/transaction_sample_builder_test.rb +70 -78
  163. data/test/new_relic/agent/transaction_sampler_test.rb +76 -185
  164. data/test/new_relic/agent/transaction_test.rb +283 -135
  165. data/test/new_relic/agent_test.rb +27 -12
  166. data/test/new_relic/cli/commands/install_test.rb +27 -0
  167. data/test/new_relic/coerce_test.rb +0 -59
  168. data/test/new_relic/data_container_tests.rb +5 -5
  169. data/test/new_relic/fake_collector.rb +27 -9
  170. data/test/new_relic/filtering_test_app.rb +2 -1
  171. data/test/new_relic/http_client_test_cases.rb +16 -16
  172. data/test/new_relic/json_wrapper_test.rb +0 -54
  173. data/test/new_relic/marshalling_test_cases.rb +1 -0
  174. data/test/new_relic/multiverse_helpers.rb +144 -0
  175. data/test/new_relic/noticed_error_test.rb +112 -9
  176. data/test/new_relic/rack/browser_monitoring_test.rb +12 -7
  177. data/test/new_relic/{transaction_analysis → rack/developer_mode}/segment_summary_test.rb +5 -4
  178. data/test/new_relic/rack/developer_mode_test.rb +17 -3
  179. data/test/new_relic/rack/error_collector_test.rb +1 -1
  180. data/test/performance/lib/performance/instrumentation/stackprof.rb +1 -1
  181. data/test/performance/script/runner +2 -2
  182. data/test/performance/suites/active_record.rb +3 -3
  183. data/test/performance/suites/agent_attributes.rb +62 -0
  184. data/test/performance/suites/rack_middleware.rb +78 -28
  185. data/test/performance/suites/transaction_tracing.rb +35 -0
  186. data/test/test_helper.rb +9 -1
  187. data/ui/helpers/developer_mode_helper.rb +16 -23
  188. data/ui/views/newrelic/_sample.rhtml +3 -3
  189. data/ui/views/newrelic/_segment.rhtml +1 -1
  190. data/ui/views/newrelic/_show_sample_summary.rhtml +1 -1
  191. data/ui/views/newrelic/show_sample.rhtml +5 -4
  192. metadata +23 -80
  193. data/lib/new_relic/agent/transaction/force_persist_sample_buffer.rb +0 -25
  194. data/lib/new_relic/transaction_analysis.rb +0 -80
  195. data/lib/new_relic/transaction_analysis/segment_summary.rb +0 -53
  196. data/lib/new_relic/transaction_sample.rb +0 -207
  197. data/lib/new_relic/transaction_sample/composite_segment.rb +0 -31
  198. data/lib/new_relic/transaction_sample/fake_segment.rb +0 -13
  199. data/lib/new_relic/transaction_sample/segment.rb +0 -197
  200. data/lib/new_relic/transaction_sample/summary_segment.rb +0 -25
  201. data/lib/new_relic/url_rule.rb +0 -18
  202. data/test/fixtures/cross_agent_tests/sql_obfuscation/back_quoted_identifiers.mysql.obfuscated +0 -1
  203. data/test/fixtures/cross_agent_tests/sql_obfuscation/back_quoted_identifiers.mysql.sql +0 -1
  204. data/test/fixtures/cross_agent_tests/sql_obfuscation/comment_delimiters_in_strings.obfuscated +0 -1
  205. data/test/fixtures/cross_agent_tests/sql_obfuscation/comment_delimiters_in_strings.sql +0 -1
  206. data/test/fixtures/cross_agent_tests/sql_obfuscation/double_quoted_identifiers.postgres.obfuscated +0 -1
  207. data/test/fixtures/cross_agent_tests/sql_obfuscation/double_quoted_identifiers.postgres.sql +0 -1
  208. data/test/fixtures/cross_agent_tests/sql_obfuscation/end_of_line_comment_in_string.obfuscated +0 -2
  209. data/test/fixtures/cross_agent_tests/sql_obfuscation/end_of_line_comment_in_string.sql +0 -2
  210. data/test/fixtures/cross_agent_tests/sql_obfuscation/end_of_query_comment_cstyle.obfuscated +0 -1
  211. data/test/fixtures/cross_agent_tests/sql_obfuscation/end_of_query_comment_cstyle.sql +0 -1
  212. data/test/fixtures/cross_agent_tests/sql_obfuscation/end_of_query_comment_doubledash.obfuscated +0 -2
  213. data/test/fixtures/cross_agent_tests/sql_obfuscation/end_of_query_comment_doubledash.sql +0 -2
  214. data/test/fixtures/cross_agent_tests/sql_obfuscation/end_of_query_comment_hash.obfuscated +0 -1
  215. data/test/fixtures/cross_agent_tests/sql_obfuscation/end_of_query_comment_hash.sql +0 -1
  216. data/test/fixtures/cross_agent_tests/sql_obfuscation/escape_string_constants.postgres.obfuscated +0 -1
  217. data/test/fixtures/cross_agent_tests/sql_obfuscation/escape_string_constants.postgres.sql +0 -4
  218. data/test/fixtures/cross_agent_tests/sql_obfuscation/malformed/unterminated_double_quoted_string.mysql.sql +0 -1
  219. data/test/fixtures/cross_agent_tests/sql_obfuscation/malformed/unterminated_single_quoted_string.sql +0 -1
  220. data/test/fixtures/cross_agent_tests/sql_obfuscation/multiple_literal_types.mysql.obfuscated +0 -1
  221. data/test/fixtures/cross_agent_tests/sql_obfuscation/multiple_literal_types.mysql.sql +0 -1
  222. data/test/fixtures/cross_agent_tests/sql_obfuscation/numbers_in_identifiers.obfuscated +0 -1
  223. data/test/fixtures/cross_agent_tests/sql_obfuscation/numbers_in_identifiers.sql +0 -1
  224. data/test/fixtures/cross_agent_tests/sql_obfuscation/numeric_literals.obfuscated +0 -1
  225. data/test/fixtures/cross_agent_tests/sql_obfuscation/numeric_literals.sql +0 -1
  226. data/test/fixtures/cross_agent_tests/sql_obfuscation/pathological/README.md +0 -4
  227. data/test/fixtures/cross_agent_tests/sql_obfuscation/pathological/end_of_line_comments_with_quotes.obfuscated +0 -1
  228. data/test/fixtures/cross_agent_tests/sql_obfuscation/pathological/end_of_line_comments_with_quotes.sql +0 -2
  229. data/test/fixtures/cross_agent_tests/sql_obfuscation/pathological/mixed_comments_and_quotes.obfuscated +0 -1
  230. data/test/fixtures/cross_agent_tests/sql_obfuscation/pathological/mixed_comments_and_quotes.sql +0 -2
  231. data/test/fixtures/cross_agent_tests/sql_obfuscation/pathological/mixed_quotes_comments_and_newlines.obfuscated +0 -1
  232. data/test/fixtures/cross_agent_tests/sql_obfuscation/pathological/mixed_quotes_comments_and_newlines.sql +0 -4
  233. data/test/fixtures/cross_agent_tests/sql_obfuscation/pathological/mixed_quotes_end_of_line_comments.obfuscated +0 -1
  234. data/test/fixtures/cross_agent_tests/sql_obfuscation/pathological/mixed_quotes_end_of_line_comments.sql +0 -3
  235. data/test/fixtures/cross_agent_tests/sql_obfuscation/pathological/quote_delimiters_in_comments.obfuscated +0 -1
  236. data/test/fixtures/cross_agent_tests/sql_obfuscation/pathological/quote_delimiters_in_comments.sql +0 -1
  237. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_double_quoted.mysql.obfuscated +0 -1
  238. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_double_quoted.mysql.sql +0 -1
  239. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_single_quoted.obfuscated +0 -1
  240. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_single_quoted.sql +0 -1
  241. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_backslash_and_twin_single_quotes.obfuscated +0 -1
  242. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_backslash_and_twin_single_quotes.sql +0 -4
  243. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_embedded_double_quote.obfuscated +0 -1
  244. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_embedded_double_quote.sql +0 -1
  245. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_embedded_newline.obfuscated +0 -1
  246. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_embedded_newline.sql +0 -2
  247. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_embedded_single_quote.mysql.obfuscated +0 -1
  248. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_embedded_single_quote.mysql.sql +0 -1
  249. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_escaped_quotes.mysql.obfuscated +0 -1
  250. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_escaped_quotes.mysql.sql +0 -1
  251. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_trailing_backslash.obfuscated +0 -1
  252. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_trailing_backslash.sql +0 -4
  253. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_trailing_escaped_backslash.mysql.obfuscated +0 -1
  254. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_trailing_escaped_backslash.mysql.sql +0 -1
  255. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_trailing_escaped_backslash_single_quoted.obfuscated +0 -1
  256. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_trailing_escaped_backslash_single_quoted.sql +0 -1
  257. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_trailing_escaped_quote.obfuscated +0 -1
  258. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_trailing_escaped_quote.sql +0 -1
  259. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_twin_single_quotes.obfuscated +0 -1
  260. data/test/fixtures/cross_agent_tests/sql_obfuscation/string_with_twin_single_quotes.sql +0 -1
  261. data/test/new_relic/agent/error_collector/notice_error_test.rb +0 -261
  262. data/test/new_relic/agent/transaction/force_persist_sample_buffer_test.rb +0 -52
  263. data/test/new_relic/transaction_analysis_test.rb +0 -125
  264. data/test/new_relic/transaction_sample/composite_segment_test.rb +0 -38
  265. data/test/new_relic/transaction_sample/fake_segment_test.rb +0 -18
  266. data/test/new_relic/transaction_sample/segment_test.rb +0 -361
  267. data/test/new_relic/transaction_sample/summary_segment_test.rb +0 -34
  268. data/test/new_relic/transaction_sample_subtest_test.rb +0 -41
  269. data/test/new_relic/transaction_sample_test.rb +0 -361
@@ -0,0 +1,161 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require 'new_relic/agent/attribute_processing'
6
+
7
+ module NewRelic
8
+ module Agent
9
+ class Transaction
10
+ class Attributes
11
+ KEY_LIMIT = 255
12
+ VALUE_LIMIT = 255
13
+ COUNT_LIMIT = 64
14
+
15
+ EMPTY_HASH = {}.freeze
16
+
17
+ CAN_BYTESLICE = String.instance_methods.include?(:byteslice)
18
+
19
+ def initialize(filter)
20
+ @filter = filter
21
+
22
+ @custom_attributes = {}
23
+ @agent_attributes = {}
24
+ @intrinsic_attributes = {}
25
+
26
+ @custom_destinations = {}
27
+ @agent_destinations = {}
28
+ end
29
+
30
+ def add_agent_attribute(key, value, default_destinations)
31
+ destinations = @filter.apply(key, default_destinations)
32
+ return if destinations == AttributeFilter::DST_NONE
33
+
34
+ @agent_destinations[key] = destinations
35
+ add(@agent_attributes, key, value)
36
+ end
37
+
38
+ def add_agent_attribute_with_key_check(key, value, default_destinations)
39
+ if exceeds_bytesize_limit? key, KEY_LIMIT
40
+ NewRelic::Agent.logger.debug("Agent attribute #{key} was dropped for exceeding key length limit #{KEY_LIMIT}")
41
+ return
42
+ end
43
+
44
+ add_agent_attribute(key, value, default_destinations)
45
+ end
46
+
47
+ def add_intrinsic_attribute(key, value)
48
+ add(@intrinsic_attributes, key, value)
49
+ end
50
+
51
+ def merge_untrusted_agent_attributes(attributes, prefix, default_destinations)
52
+ return if @filter.high_security?
53
+ return if !@filter.might_allow_prefix?(prefix)
54
+
55
+ AttributeProcessing.flatten_and_coerce(attributes, prefix) do |k, v|
56
+ add_agent_attribute_with_key_check(k, v, AttributeFilter::DST_NONE)
57
+ end
58
+ end
59
+
60
+ def merge_custom_attributes(other)
61
+ return if other.empty?
62
+ AttributeProcessing.flatten_and_coerce(other) do |k, v|
63
+ add_custom_attribute(k, v)
64
+ end
65
+ end
66
+
67
+ def custom_attributes_for(destination)
68
+ for_destination(@custom_attributes, @custom_destinations, destination)
69
+ end
70
+
71
+ def agent_attributes_for(destination)
72
+ for_destination(@agent_attributes, @agent_destinations, destination)
73
+ end
74
+
75
+ def intrinsic_attributes_for(destination)
76
+ if destination == NewRelic::Agent::AttributeFilter::DST_TRANSACTION_TRACER ||
77
+ destination == NewRelic::Agent::AttributeFilter::DST_ERROR_COLLECTOR
78
+ @intrinsic_attributes
79
+ else
80
+ EMPTY_HASH
81
+ end
82
+ end
83
+
84
+ private
85
+
86
+ def add_custom_attribute(key, value)
87
+ if @custom_attributes.size >= COUNT_LIMIT
88
+ unless @already_warned_count_limit
89
+ NewRelic::Agent.logger.warn("Custom attributes count exceeded limit of #{COUNT_LIMIT}. Any additional custom attributes during this transaction will be dropped.")
90
+ @already_warned_count_limit = true
91
+ end
92
+ return
93
+ end
94
+
95
+ if @filter.high_security?
96
+ NewRelic::Agent.logger.debug("Unable to add custom attribute #{key} while in high security mode.")
97
+ return
98
+ end
99
+
100
+ if exceeds_bytesize_limit?(key, KEY_LIMIT)
101
+ NewRelic::Agent.logger.warn("Custom attribute key '#{key}' was longer than limit of #{KEY_LIMIT} bytes. This attribute will be dropped.")
102
+ return
103
+ end
104
+
105
+ destinations = @filter.apply(key, AttributeFilter::DST_ALL)
106
+ return if destinations == AttributeFilter::DST_NONE
107
+
108
+ @custom_destinations[key] = destinations
109
+ add(@custom_attributes, key, value)
110
+ end
111
+
112
+ def add(attributes, key, value)
113
+ if exceeds_bytesize_limit?(value, VALUE_LIMIT)
114
+ value = slice(value)
115
+ end
116
+
117
+ attributes[key] = value
118
+ end
119
+
120
+ def for_destination(attributes, calculated_destinations, destination)
121
+ # Avoid allocating anything if there are no attrs at all
122
+ return EMPTY_HASH if attributes.empty?
123
+
124
+ return attributes.dup if destination == NewRelic::Agent::AttributeFilter::DST_DEVELOPER_MODE
125
+
126
+ attributes.inject({}) do |memo, (key, value)|
127
+ if @filter.allows?(calculated_destinations[key], destination)
128
+ memo[key] = value
129
+ end
130
+ memo
131
+ end
132
+ end
133
+
134
+ def exceeds_bytesize_limit?(value, limit)
135
+ if value.respond_to?(:bytesize)
136
+ value.bytesize > limit
137
+ elsif value.is_a?(Symbol)
138
+ value.to_s.bytesize > limit
139
+ else
140
+ false
141
+ end
142
+ end
143
+
144
+ # Take one byte past our limit. Why? This lets us unconditionally chop!
145
+ # the end. It'll either remove the one-character-too-many we have, or
146
+ # peel off the partial, mangled character left by the byteslice.
147
+ def slice(incoming)
148
+ if CAN_BYTESLICE
149
+ result = incoming.to_s.byteslice(0, VALUE_LIMIT + 1)
150
+ else
151
+ # < 1.9.3 doesn't have byteslice, so we take off bytes instead.
152
+ result = incoming.to_s.bytes.take(VALUE_LIMIT + 1).pack("C*")
153
+ end
154
+
155
+ result.chop!
156
+ result
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
@@ -39,14 +39,14 @@ module NewRelic
39
39
  def store_previous(*)
40
40
  end
41
41
 
42
- # Captures the stack trace for a segment
42
+ # Captures the stack trace for a node
43
43
  # This is expensive and not for production mode
44
- def visit_segment(segment)
45
- return unless enabled? && segment
44
+ def visit_node(node)
45
+ return unless enabled? && node
46
46
 
47
47
  trace = strip_newrelic_frames(caller)
48
48
  trace = trace.first(40) if trace.length > 40
49
- segment[:backtrace] = trace
49
+ node[:backtrace] = trace
50
50
  end
51
51
 
52
52
  def strip_newrelic_frames(trace)
@@ -0,0 +1,150 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ require 'new_relic/agent/transaction/trace_node'
6
+
7
+ module NewRelic
8
+ module Agent
9
+ class Transaction
10
+ class Trace
11
+ class FinishedTraceError < StandardError; end
12
+
13
+ attr_reader :start_time, :root_node
14
+ attr_accessor :transaction_name, :uri, :guid, :xray_session_id,
15
+ :attributes, :node_count, :finished, :threshold,
16
+ :profile
17
+
18
+ def initialize(start_time)
19
+ @start_time = start_time
20
+ @node_count = 0
21
+ @root_node = NewRelic::Agent::Transaction::TraceNode.new(0.0, "ROOT")
22
+ end
23
+
24
+ def sample_id
25
+ self.object_id
26
+ end
27
+
28
+ def count_nodes
29
+ self.node_count
30
+ end
31
+
32
+ def duration
33
+ self.root_node.duration
34
+ end
35
+
36
+ def forced?
37
+ return true if NewRelic::Coerce.int_or_nil(xray_session_id)
38
+ false
39
+ end
40
+
41
+ def synthetics_resource_id
42
+ intrinsic_attributes = attributes.intrinsic_attributes_for(NewRelic::Agent::AttributeFilter::DST_TRANSACTION_TRACER)
43
+ intrinsic_attributes[:synthetics_resource_id]
44
+ end
45
+
46
+ def to_s_compact
47
+ @root_node.to_s_compact
48
+ end
49
+
50
+ def create_node(time_since_start, metric_name = nil)
51
+ raise FinishedTraceError.new "Can't create additional node for finished trace." if self.finished
52
+ self.node_count += 1
53
+ NewRelic::Agent::Transaction::TraceNode.new(time_since_start, metric_name)
54
+ end
55
+
56
+ def each_node(&block)
57
+ self.root_node.each_node(&block)
58
+ end
59
+
60
+ def prepare_to_send!
61
+ return self if @prepared
62
+
63
+ if NewRelic::Agent::Database.should_record_sql?
64
+ collect_explain_plans!
65
+ prepare_sql_for_transmission!
66
+ else
67
+ strip_sql!
68
+ end
69
+
70
+ @prepared = true
71
+ self
72
+ end
73
+
74
+ def collect_explain_plans!
75
+ return unless NewRelic::Agent::Database.should_collect_explain_plans?
76
+ threshold = NewRelic::Agent.config[:'transaction_tracer.explain_threshold']
77
+
78
+ each_node do |node|
79
+ if node[:sql] && node.duration > threshold
80
+ node[:explain_plan] = node.explain_sql
81
+ end
82
+ end
83
+ end
84
+
85
+ def prepare_sql_for_transmission!
86
+ strategy = NewRelic::Agent::Database.record_sql_method
87
+ each_node do |node|
88
+ next unless node[:sql]
89
+
90
+ case strategy
91
+ when :obfuscated
92
+ node[:sql] = NewRelic::Agent::Database.obfuscate_sql(node[:sql])
93
+ when :raw
94
+ node[:sql] = node[:sql].to_s
95
+ else
96
+ node[:sql] = nil
97
+ end
98
+ end
99
+ end
100
+
101
+ def strip_sql!
102
+ each_node do |node|
103
+ node.params.delete(:sql)
104
+ end
105
+ end
106
+
107
+ # Iterates recursively over each node in the entire transaction
108
+ # sample tree while keeping track of nested nodes
109
+ def each_node_with_nest_tracking(&block)
110
+ @root_node.each_node_with_nest_tracking(&block)
111
+ end
112
+
113
+ def trace_tree
114
+ destination = NewRelic::Agent::AttributeFilter::DST_TRANSACTION_TRACER
115
+
116
+ agent_attributes = self.attributes.agent_attributes_for(destination)
117
+ custom_attributes = self.attributes.custom_attributes_for(destination)
118
+ intrinsic_attributes = self.attributes.intrinsic_attributes_for(destination)
119
+
120
+ [
121
+ NewRelic::Coerce.float(self.start_time),
122
+ {},
123
+ {},
124
+ self.root_node.to_array,
125
+ {
126
+ 'agentAttributes' => agent_attributes,
127
+ 'userAttributes' => custom_attributes,
128
+ 'intrinsics' => intrinsic_attributes
129
+ }
130
+ ]
131
+ end
132
+
133
+ def to_collector_array(encoder)
134
+ [
135
+ NewRelic::Helper.time_to_millis(self.start_time),
136
+ NewRelic::Helper.time_to_millis(self.root_node.duration),
137
+ NewRelic::Coerce.string(self.transaction_name),
138
+ NewRelic::Coerce.string(self.uri),
139
+ encoder.encode(trace_tree),
140
+ NewRelic::Coerce.string(self.guid),
141
+ nil,
142
+ forced?,
143
+ NewRelic::Coerce.int_or_nil(xray_session_id),
144
+ NewRelic::Coerce.string(self.synthetics_resource_id)
145
+ ]
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,190 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under New Relic's license terms.
3
+ # See https://github.com/newrelic/rpm/blob/master/LICENSE for complete details.
4
+
5
+ module NewRelic
6
+ module Agent
7
+ class Transaction
8
+ class TraceNode
9
+ attr_reader :entry_timestamp
10
+ # The exit timestamp will be relative except for the outermost sample which will
11
+ # have a timestamp.
12
+ attr_reader :exit_timestamp
13
+ attr_reader :parent_node
14
+
15
+ attr_accessor :metric_name
16
+
17
+ UNKNOWN_NODE_NAME = '<unknown>'.freeze
18
+
19
+ def initialize(timestamp, metric_name)
20
+ @entry_timestamp = timestamp
21
+ @metric_name = metric_name || UNKNOWN_NODE_NAME
22
+ end
23
+
24
+ # sets the final timestamp on a node to indicate the exit
25
+ # point of the node
26
+ def end_trace(timestamp)
27
+ @exit_timestamp = timestamp
28
+ end
29
+
30
+ def add_called_node(s)
31
+ @called_nodes ||= []
32
+ @called_nodes << s
33
+ s.parent_node = self
34
+ end
35
+
36
+ def to_s
37
+ to_debug_str(0)
38
+ end
39
+
40
+ def to_array
41
+ [ NewRelic::Helper.time_to_millis(@entry_timestamp),
42
+ NewRelic::Helper.time_to_millis(@exit_timestamp),
43
+ NewRelic::Coerce.string(@metric_name),
44
+ (@params || {}) ] +
45
+ [ (@called_nodes ? @called_nodes.map{|s| s.to_array} : []) ]
46
+ end
47
+
48
+ def path_string
49
+ "#{metric_name}[#{called_nodes.collect {|node| node.path_string }.join('')}]"
50
+ end
51
+
52
+ def to_s_compact
53
+ str = ""
54
+ str << metric_name
55
+ if called_nodes.any?
56
+ str << "{#{called_nodes.map { | cs | cs.to_s_compact }.join(",")}}"
57
+ end
58
+ str
59
+ end
60
+
61
+ def to_debug_str(depth)
62
+ tab = " " * depth
63
+ s = tab.clone
64
+ s << ">> #{'%3i ms' % (@entry_timestamp*1000)} [#{self.class.name.split("::").last}] #{metric_name} \n"
65
+ unless params.empty?
66
+ params.each do |k,v|
67
+ s << "#{tab} -#{'%-16s' % k}: #{v.to_s[0..80]}\n"
68
+ end
69
+ end
70
+ called_nodes.each do |cs|
71
+ s << cs.to_debug_str(depth + 1)
72
+ end
73
+ s << tab + "<< "
74
+ s << case @exit_timestamp
75
+ when nil then ' n/a'
76
+ when Numeric then '%3i ms' % (@exit_timestamp*1000)
77
+ else @exit_timestamp.to_s
78
+ end
79
+ s << " #{metric_name}\n"
80
+ end
81
+
82
+ def called_nodes
83
+ @called_nodes || []
84
+ end
85
+
86
+ # return the total duration of this node
87
+ def duration
88
+ (@exit_timestamp - @entry_timestamp).to_f
89
+ end
90
+
91
+ # return the duration of this node without
92
+ # including the time in the called nodes
93
+ def exclusive_duration
94
+ d = duration
95
+
96
+ called_nodes.each do |node|
97
+ d -= node.duration
98
+ end
99
+ d
100
+ end
101
+
102
+ def count_nodes
103
+ count = 1
104
+ called_nodes.each { | node | count += node.count_nodes }
105
+ count
106
+ end
107
+
108
+ def []=(key, value)
109
+ # only create a parameters field if a parameter is set; this will save
110
+ # bandwidth etc as most nodes have no parameters
111
+ params[key] = value
112
+ end
113
+
114
+ def [](key)
115
+ params[key]
116
+ end
117
+
118
+ def params
119
+ @params ||= {}
120
+ end
121
+
122
+ def params=(p)
123
+ @params = p
124
+ end
125
+
126
+ # call the provided block for this node and each
127
+ # of the called nodes
128
+ def each_node(&block)
129
+ block.call self
130
+
131
+ if @called_nodes
132
+ @called_nodes.each do |node|
133
+ node.each_node(&block)
134
+ end
135
+ end
136
+ end
137
+
138
+ # call the provided block for this node and each
139
+ # of the called nodes while keeping track of nested nodes
140
+ def each_node_with_nest_tracking(&block)
141
+ summary = block.call self
142
+ summary.current_nest_count += 1 if summary
143
+
144
+ if @called_nodes
145
+ @called_nodes.each do |node|
146
+ node.each_node_with_nest_tracking(&block)
147
+ end
148
+ end
149
+
150
+ summary.current_nest_count -= 1 if summary
151
+ end
152
+
153
+ # This is only for use by developer mode
154
+ def find_node(id)
155
+ return self if object_id == id
156
+ called_nodes.each do |node|
157
+ found = node.find_node(id)
158
+ return found if found
159
+ end
160
+ nil
161
+ end
162
+
163
+ def explain_sql
164
+ return params[:explain_plan] if params.key?(:explain_plan)
165
+
166
+ statement = params[:sql]
167
+ return nil unless statement.respond_to?(:config) &&
168
+ statement.respond_to?(:explainer)
169
+
170
+ NewRelic::Agent::Database.explain_sql(statement,
171
+ statement.config,
172
+ &statement.explainer)
173
+ end
174
+
175
+ def obfuscated_sql
176
+ NewRelic::Agent::Database.obfuscate_sql(params[:sql])
177
+ end
178
+
179
+ def called_nodes=(nodes)
180
+ @called_nodes = nodes
181
+ end
182
+
183
+ protected
184
+ def parent_node=(s)
185
+ @parent_node = s
186
+ end
187
+ end
188
+ end
189
+ end
190
+ end