tingyun_rpm 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +7 -0
  2. data/.DS_Store +0 -0
  3. data/.gitignore +14 -0
  4. data/.travis.yml +4 -0
  5. data/CODE_OF_CONDUCT.md +13 -0
  6. data/Gemfile +3 -0
  7. data/Guardfile +25 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +41 -0
  10. data/cert/cacert.pem +0 -0
  11. data/lib/ting_yun/agent/agent.rb +128 -0
  12. data/lib/ting_yun/agent/class_methods.rb +21 -0
  13. data/lib/ting_yun/agent/collector/base_sampler.rb +2 -0
  14. data/lib/ting_yun/agent/collector/error_collector/error_trace_array.rb +88 -0
  15. data/lib/ting_yun/agent/collector/error_collector/noticed_error.rb +129 -0
  16. data/lib/ting_yun/agent/collector/error_collector.rb +165 -0
  17. data/lib/ting_yun/agent/collector/middle_ware_collector/cpu_sampler.rb +68 -0
  18. data/lib/ting_yun/agent/collector/middle_ware_collector/memory_sampler.rb +139 -0
  19. data/lib/ting_yun/agent/collector/middle_ware_collector/middle_ware.rb +13 -0
  20. data/lib/ting_yun/agent/collector/middle_ware_collector/sampler.rb +59 -0
  21. data/lib/ting_yun/agent/collector/middle_ware_collector.rb +80 -0
  22. data/lib/ting_yun/agent/collector/sql_sampler.rb +299 -0
  23. data/lib/ting_yun/agent/collector/stats_engine/metric_stats.rb +170 -0
  24. data/lib/ting_yun/agent/collector/stats_engine/stats_hash.rb +172 -0
  25. data/lib/ting_yun/agent/collector/stats_engine.rb +28 -0
  26. data/lib/ting_yun/agent/collector/transaction_sampler/slowest_sample_buffer.rb +25 -0
  27. data/lib/ting_yun/agent/collector/transaction_sampler/transaction_sample_buffer_base.rb +96 -0
  28. data/lib/ting_yun/agent/collector/transaction_sampler.rb +226 -0
  29. data/lib/ting_yun/agent/container_data_manager.rb +94 -0
  30. data/lib/ting_yun/agent/cross_app/cross_app_monitor.rb +131 -0
  31. data/lib/ting_yun/agent/cross_app/cross_app_tracing.rb +202 -0
  32. data/lib/ting_yun/agent/cross_app/inbound_request_monitor.rb +22 -0
  33. data/lib/ting_yun/agent/database.rb +410 -0
  34. data/lib/ting_yun/agent/datastore/metric_helper.rb +82 -0
  35. data/lib/ting_yun/agent/datastore/mongo.rb +44 -0
  36. data/lib/ting_yun/agent/datastore.rb +33 -0
  37. data/lib/ting_yun/agent/dispatcher.rb +39 -0
  38. data/lib/ting_yun/agent/event/event_listener.rb +47 -0
  39. data/lib/ting_yun/agent/event/event_loop.rb +194 -0
  40. data/lib/ting_yun/agent/instance_methods/connect.rb +164 -0
  41. data/lib/ting_yun/agent/instance_methods/container_data_manager.rb +137 -0
  42. data/lib/ting_yun/agent/instance_methods/handle_errors.rb +71 -0
  43. data/lib/ting_yun/agent/instance_methods/start.rb +219 -0
  44. data/lib/ting_yun/agent/instance_methods/start_worker_thread.rb +51 -0
  45. data/lib/ting_yun/agent/instance_methods.rb +39 -0
  46. data/lib/ting_yun/agent/method_tracer.rb +256 -0
  47. data/lib/ting_yun/agent/method_tracer_helpers.rb +85 -0
  48. data/lib/ting_yun/agent/threading/agent_thread.rb +49 -0
  49. data/lib/ting_yun/agent/transaction/attributes.rb +22 -0
  50. data/lib/ting_yun/agent/transaction/request_attributes.rb +126 -0
  51. data/lib/ting_yun/agent/transaction/trace.rb +125 -0
  52. data/lib/ting_yun/agent/transaction/trace_node.rb +110 -0
  53. data/lib/ting_yun/agent/transaction/traced_method_stack.rb +80 -0
  54. data/lib/ting_yun/agent/transaction/transaction_metrics.rb +51 -0
  55. data/lib/ting_yun/agent/transaction/transaction_sample_builder.rb +63 -0
  56. data/lib/ting_yun/agent/transaction/transaction_state.rb +112 -0
  57. data/lib/ting_yun/agent/transaction.rb +522 -0
  58. data/lib/ting_yun/agent.rb +207 -0
  59. data/lib/ting_yun/configuration/default_source.rb +638 -0
  60. data/lib/ting_yun/configuration/dotted_hash.rb +46 -0
  61. data/lib/ting_yun/configuration/environment_source.rb +116 -0
  62. data/lib/ting_yun/configuration/manager.rb +232 -0
  63. data/lib/ting_yun/configuration/manual_source.rb +14 -0
  64. data/lib/ting_yun/configuration/server_source.rb +88 -0
  65. data/lib/ting_yun/configuration/yaml_source.rb +136 -0
  66. data/lib/ting_yun/configuration.rb +9 -0
  67. data/lib/ting_yun/environment_report.rb +123 -0
  68. data/lib/ting_yun/frameworks/class_methods.rb +47 -0
  69. data/lib/ting_yun/frameworks/external.rb +15 -0
  70. data/lib/ting_yun/frameworks/instance_methods.rb +120 -0
  71. data/lib/ting_yun/frameworks/instrumentation.rb +67 -0
  72. data/lib/ting_yun/frameworks/rails.rb +63 -0
  73. data/lib/ting_yun/frameworks/rails3.rb +26 -0
  74. data/lib/ting_yun/frameworks/rails4.rb +14 -0
  75. data/lib/ting_yun/frameworks/ruby.rb +17 -0
  76. data/lib/ting_yun/frameworks/sinatra.rb +10 -0
  77. data/lib/ting_yun/frameworks.rb +34 -0
  78. data/lib/ting_yun/http/generic_request.rb +8 -0
  79. data/lib/ting_yun/http/net_http_request.rb +46 -0
  80. data/lib/ting_yun/instrumentation/active_record.rb +103 -0
  81. data/lib/ting_yun/instrumentation/middleware_proxy.rb +77 -0
  82. data/lib/ting_yun/instrumentation/middleware_tracing.rb +84 -0
  83. data/lib/ting_yun/instrumentation/mongo.rb +103 -0
  84. data/lib/ting_yun/instrumentation/mongo2.rb +37 -0
  85. data/lib/ting_yun/instrumentation/mongo_command_log_subscriber.rb +97 -0
  86. data/lib/ting_yun/instrumentation/moped.rb +95 -0
  87. data/lib/ting_yun/instrumentation/net.rb +59 -0
  88. data/lib/ting_yun/instrumentation/rack.rb +109 -0
  89. data/lib/ting_yun/instrumentation/rails3/action_controller.rb +63 -0
  90. data/lib/ting_yun/instrumentation/rails3/action_view.rb +115 -0
  91. data/lib/ting_yun/instrumentation/rails4/action_controller_subscriber.rb +124 -0
  92. data/lib/ting_yun/instrumentation/rails4/action_view_subscriber.rb +118 -0
  93. data/lib/ting_yun/instrumentation/rails4/active_record_subscriber.rb +124 -0
  94. data/lib/ting_yun/instrumentation/rails_middleware.rb +38 -0
  95. data/lib/ting_yun/instrumentation/redis.rb +70 -0
  96. data/lib/ting_yun/instrumentation/support/active_record_helper.rb +178 -0
  97. data/lib/ting_yun/instrumentation/support/controller_instrumentation.rb +54 -0
  98. data/lib/ting_yun/instrumentation/support/database.rb +38 -0
  99. data/lib/ting_yun/instrumentation/support/event_formatter.rb +19 -0
  100. data/lib/ting_yun/instrumentation/support/evented_subscriber.rb +97 -0
  101. data/lib/ting_yun/instrumentation/support/external_error.rb +52 -0
  102. data/lib/ting_yun/instrumentation/support/metric_translator.rb +84 -0
  103. data/lib/ting_yun/instrumentation/support/mongo_formatter.rb +49 -0
  104. data/lib/ting_yun/instrumentation/support/parameter_filtering.rb +21 -0
  105. data/lib/ting_yun/instrumentation/support/queue_time.rb +76 -0
  106. data/lib/ting_yun/instrumentation/support/transaction_namer.rb +68 -0
  107. data/lib/ting_yun/instrumentation/thrift.rb +329 -0
  108. data/lib/ting_yun/logger/agent_logger.rb +196 -0
  109. data/lib/ting_yun/logger/log_once.rb +38 -0
  110. data/lib/ting_yun/logger/memory_logger.rb +56 -0
  111. data/lib/ting_yun/logger/null_logger.rb +31 -0
  112. data/lib/ting_yun/logger/startup_logger.rb +13 -0
  113. data/lib/ting_yun/logger.rb +8 -0
  114. data/lib/ting_yun/metrics/metric_data.rb +86 -0
  115. data/lib/ting_yun/metrics/metric_spec.rb +89 -0
  116. data/lib/ting_yun/metrics/stats.rb +158 -0
  117. data/lib/ting_yun/metrics.rb +12 -0
  118. data/lib/ting_yun/support/coerce.rb +86 -0
  119. data/lib/ting_yun/support/collector.rb +29 -0
  120. data/lib/ting_yun/support/exception.rb +79 -0
  121. data/lib/ting_yun/support/hash_extensions.rb +25 -0
  122. data/lib/ting_yun/support/helper.rb +54 -0
  123. data/lib/ting_yun/support/hostname.rb +13 -0
  124. data/lib/ting_yun/support/http_clients/uri_util.rb +49 -0
  125. data/lib/ting_yun/support/language_support.rb +155 -0
  126. data/lib/ting_yun/support/library_detection.rb +129 -0
  127. data/lib/ting_yun/support/local_environment.rb +185 -0
  128. data/lib/ting_yun/support/path.rb +13 -0
  129. data/lib/ting_yun/support/serialize/encodes.rb +61 -0
  130. data/lib/ting_yun/support/serialize/encoding_normalizer.rb +84 -0
  131. data/lib/ting_yun/support/serialize/json_marshaller.rb +73 -0
  132. data/lib/ting_yun/support/serialize/json_wrapper.rb +78 -0
  133. data/lib/ting_yun/support/serialize/marshaller.rb +69 -0
  134. data/lib/ting_yun/support/serialize/ok_json.rb +651 -0
  135. data/lib/ting_yun/support/system_info.rb +206 -0
  136. data/lib/ting_yun/support/timer_lib.rb +29 -0
  137. data/lib/ting_yun/support/version_number.rb +70 -0
  138. data/lib/ting_yun/ting_yun_service/connection.rb +118 -0
  139. data/lib/ting_yun/ting_yun_service/http.rb +41 -0
  140. data/lib/ting_yun/ting_yun_service/request.rb +90 -0
  141. data/lib/ting_yun/ting_yun_service/ssl.rb +45 -0
  142. data/lib/ting_yun/ting_yun_service/upload_service.rb +149 -0
  143. data/lib/ting_yun/ting_yun_service.rb +124 -0
  144. data/lib/ting_yun/version.rb +17 -0
  145. data/lib/tingyun_rpm.rb +47 -0
  146. data/tingyun_rpm.gemspec +60 -0
  147. metadata +415 -0
@@ -0,0 +1,207 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under Ting Yun's license terms.
3
+ require 'ting_yun/configuration/manager'
4
+ require 'ting_yun/logger/startup_logger'
5
+ require 'ting_yun/frameworks'
6
+ require 'ting_yun/agent/transaction/transaction_state'
7
+ require 'ting_yun/agent/transaction'
8
+ require 'ting_yun/agent/collector/middle_ware_collector/middle_ware'
9
+
10
+
11
+ module TingYun
12
+ module Agent
13
+ extend self
14
+
15
+ @agent = nil
16
+ @logger = nil
17
+ @config = ::TingYun::Configuration::Manager.new
18
+
19
+ attr_reader :config
20
+
21
+ UNKNOWN_METRIC = '(unknown)'.freeze
22
+
23
+ def agent
24
+ return @agent if @agent
25
+ TingYun::Agent.logger.warn("Agent unavailable as it hasn't been started.")
26
+ TingYun::Agent.logger.warn(caller.join("\n"))
27
+ nil
28
+ end
29
+
30
+ alias instance agent
31
+
32
+ def agent=(new_instance)
33
+ @agent = new_instance
34
+ end
35
+
36
+
37
+ def logger
38
+ @logger || ::TingYun::Logger::StartupLogger.instance
39
+ end
40
+
41
+ def logger=(log)
42
+ @logger = log
43
+ end
44
+
45
+ def reset_config
46
+ @config.reset_to_defaults
47
+ end
48
+
49
+
50
+ # Record a value for the given metric name.
51
+ #
52
+ # This method should be used to record event-based metrics such as method
53
+ # calls that are associated with a specific duration or magnitude.
54
+ #
55
+ # +metric_name+ should follow a slash separated path convention. Application
56
+ # specific metrics should begin with "Custom/".
57
+ #
58
+ # +value+ should be either a single Numeric value representing the duration/
59
+ # magnitude of the event being recorded, or a Hash containing :count,
60
+ # :total, :min, :max, and :sum_of_squares keys. The latter form is useful
61
+ # for recording pre-aggregated metrics collected externally.
62
+ #
63
+ # This method is safe to use from any thread.
64
+ #
65
+ # @api public
66
+ def record_metric(metric_name, value) #THREAD_LOCAL_ACCESS
67
+ return unless agent
68
+ stats = TingYun::Metrics::Stats.create_from_hash(value) if value.is_a?(Hash)
69
+ agent.stats_engine.tl_record_unscoped_metrics(metric_name, stats || value)
70
+ end
71
+
72
+ # Manual agent configuration and startup/shutdown
73
+
74
+ # Call this to manually start the Agent in situations where the Agent does
75
+ # not auto-start.
76
+ #
77
+ # When the app environment loads, so does the Agent. However, the
78
+ # Agent will only connect to the service if a web front-end is found. If
79
+ # you want to selectively monitor ruby processes that don't use
80
+ # web plugins, then call this method in your code and the Agent
81
+ # will fire up and start reporting to the service.
82
+ #
83
+ # Options are passed in as overrides for values in the
84
+ # tingyun.yml, such as app_name. In addition, the option +log+
85
+ # will take a logger that will be used instead of the standard
86
+ # file logger. The setting for the tingyun.yml section to use
87
+ # (ie, RAILS_ENV) can be overridden with an :env argument.
88
+ #
89
+ # @api public
90
+ #
91
+ def manual_start(options={})
92
+ raise "Options must be a hash" unless Hash === options
93
+ TingYun::Frameworks.init_start({ :'nbs.agent_enabled' => true, :sync_startup => true }.merge(options))
94
+ end
95
+
96
+ # Yield to a block that is run with a database metric name context. This means
97
+ # the Database instrumentation will use this for the metric name if it does not
98
+ # otherwise know about a model. This is re-entrant.
99
+ #
100
+ # @param [String,Class,#to_s] model the DB model class
101
+ #
102
+ # @param [String] method the name of the finder method or other method to
103
+ # identify the operation with.
104
+ #
105
+ def with_database_metric_name(model, method = nil, product = nil, &block) #THREAD_LOCAL_ACCESS
106
+ if txn = TingYun::Agent::TransactionState.tl_get.current_transaction
107
+ txn.with_database_metric_name(model, method, product, &block)
108
+ else
109
+ yield
110
+ end
111
+ end
112
+
113
+
114
+ # Notice the error with the given available options:
115
+ #
116
+ # * <tt>:uri</tt> => Request path, minus request params or query string
117
+ # * <tt>:metric</tt> => The metric name associated with the transaction
118
+ # * <tt>:custom_params</tt> => Custom parameters
119
+ #
120
+ # @api public
121
+ #
122
+ def notice_error(exception, options={})
123
+ TingYun::Agent::Transaction.notice_error(exception, options)
124
+ nil # don't return a noticed error datastructure. it can only hurt.
125
+ end
126
+
127
+ # Register this method as a callback for processes that fork
128
+ # jobs.
129
+ #
130
+ # If the master/parent connects to the agent prior to forking the
131
+ # agent in the forked process will use that agent_run. Otherwise
132
+ # the forked process will establish a new connection with the
133
+ # server.
134
+ #
135
+ # Use this especially when you fork the process to run background
136
+ # jobs or other work. If you are doing this with a web dispatcher
137
+ # that forks worker processes then you will need to force the
138
+ # agent to reconnect, which it won't do by default. Passenger and
139
+ # Rainbows and Unicorn are already handled, nothing special needed for them.
140
+ #
141
+ # Options:
142
+ # * <tt>:force_reconnect => true</tt> to force the spawned process to
143
+ # establish a new connection, such as when forking a long running process.
144
+ # The default is false--it will only connect to the server if the parent
145
+ # had not connected.
146
+ # * <tt>:keep_retrying => false</tt> if we try to initiate a new
147
+ # connection, this tells me to only try it once so this method returns
148
+ # quickly if there is some kind of latency with the server.
149
+ #
150
+ # @api public
151
+ #
152
+ def after_fork(options={})
153
+ agent.after_fork(options) if agent
154
+ end
155
+
156
+
157
+ # Yield to the block without collecting any metrics or traces in
158
+ # any of the subsequent calls. If executed recursively, will keep
159
+ # track of the first entry point and turn on tracing again after
160
+ # leaving that block. This uses the thread local TransactionState.
161
+ #
162
+ # @api public
163
+ #
164
+ def disable_all_tracing
165
+ return yield unless agent
166
+ begin
167
+ agent.push_trace_execution_flag(false)
168
+ yield
169
+ ensure
170
+ agent.pop_trace_execution_flag
171
+ end
172
+ end
173
+
174
+
175
+
176
+ # Register this method as a callback for processes that fork
177
+ # jobs.
178
+ #
179
+ # If the master/parent connects to the agent prior to forking the
180
+ # agent in the forked process will use that agent_run. Otherwise
181
+ # the forked process will establish a new connection with the
182
+ # server.
183
+ #
184
+ # Use this especially when you fork the process to run background
185
+ # jobs or other work. If you are doing this with a web dispatcher
186
+ # that forks worker processes then you will need to force the
187
+ # agent to reconnect, which it won't do by default. Passenger and
188
+ # Rainbows and Unicorn are already handled, nothing special needed for them.
189
+ #
190
+ # Options:
191
+ # * <tt>:force_reconnect => true</tt> to force the spawned process to
192
+ # establish a new connection, such as when forking a long running process.
193
+ # The default is false--it will only connect to the server if the parent
194
+ # had not connected.
195
+ # * <tt>:keep_retrying => false</tt> if we try to initiate a new
196
+ # connection, this tells me to only try it once so this method returns
197
+ # quickly if there is some kind of latency with the server.
198
+ #
199
+ # @api public
200
+ #
201
+ def after_fork(options={})
202
+ agent.after_fork(options) if agent
203
+ end
204
+
205
+
206
+ end
207
+ end