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,123 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under Ting Yun's license terms.
3
+ require 'ting_yun/support/system_info'
4
+
5
+ module TingYun
6
+ # The EnvironmentReport is responsible for analyzing the application's
7
+ # environment and generating the data for the Environment Report in TINGYUN
8
+ #
9
+ # It contains useful system information like Ruby version, OS, loaded gems,
10
+ # etc.
11
+ #
12
+ # Additional logic can be registered by using the EnvironmentReport.report_on
13
+ # hook.
14
+ class EnvironmentReport
15
+
16
+ # This is the main interface for registering logic that should be included
17
+ # in the Environment Report. For example:
18
+ #
19
+ # EnvironmentReport.report_on "Day of week" do
20
+ # Time.now.strftime("%A")
21
+ # end
22
+ #
23
+ # The passed blocks will be run in EnvironmentReport instances on #initialize.
24
+ #
25
+ # Errors raised in passed blocks will be handled and logged at debug, so it
26
+ # is safe to report on things that may not work in certain environments.
27
+ #
28
+ # The blocks should only return strings or arrays full of strings. Falsey
29
+ # values will be ignored.
30
+ def self.report_on(key, &block)
31
+ registered_reporters[key] = block
32
+ end
33
+
34
+ def self.registered_reporters
35
+ @registered_reporters ||= Hash.new
36
+ end
37
+
38
+ # allow the logic to be swapped out in tests
39
+ def self.registered_reporters=(logic)
40
+ @registered_reporters = logic
41
+ end
42
+
43
+ # register reporting logic
44
+ ####################################
45
+ report_on('Gems') do
46
+ begin
47
+ (Bundler.rubygems.all_specs.map { |gem| "#{gem.name}(#{gem.version})" }).join(',')
48
+ rescue
49
+ # There are certain rubygem, bundler, rails combinations (e.g. gem
50
+ # 1.6.2, rails 2.3, bundler 1.2.3) where the code above throws an error
51
+ # in bundler because of rails monkey patching gem. The below does work
52
+ # though so try it if the above fails.
53
+ Bundler.load.specs.map do |spec|
54
+ version = (spec.respond_to?(:version) && spec.version)
55
+ spec.name + (version ? "(#{version})" : "")
56
+ end.join(',')
57
+ end
58
+ end
59
+ report_on('Plugin List') { ::Rails.configuration.plugins.to_a.join(',')}
60
+ report_on('Ruby version') { RUBY_VERSION }
61
+ report_on('Ruby description') { RUBY_DESCRIPTION }
62
+ report_on('Ruby platform') { RUBY_PLATFORM }
63
+ report_on('Ruby patchlevel') { RUBY_PATCHLEVEL.to_s }
64
+ report_on('JRuby version') { JRUBY_VERSION }
65
+ report_on('Java VM version') { ENV_JAVA['java.vm.version'] }
66
+ report_on('Logical Processors') { ::TingYun::Support::SystemInfo.num_logical_processors }
67
+ report_on('Physical Cores') { ::TingYun::Support::SystemInfo.num_physical_cores }
68
+ report_on('os_arch') { ::TingYun::Support::SystemInfo.processor_arch }
69
+ report_on('os_version') { ::TingYun::Support::SystemInfo.os_version }
70
+ report_on('kernel') { ::TingYun::Support::SystemInfo.ruby_os_identifier }
71
+ report_on('Database adapter') do
72
+ ActiveRecord::Base.configurations[::TingYun::Frameworks.framework.env]['adapter']
73
+ end
74
+ report_on('Framework') { TingYun::Agent.config[:framework].to_s }
75
+ report_on('Dispatcher') { TingYun::Agent.config[:dispatcher].to_s }
76
+ report_on('Environment') { ::TingYun::Frameworks.framework.env }
77
+ report_on('Rails version') { ::Rails::VERSION::STRING }
78
+ report_on('Rails threadsafe') do
79
+ ::Rails.configuration.action_controller.allow_concurrency
80
+ end
81
+ report_on('Rails Env') do
82
+ if defined? ::Rails and ::Rails.respond_to?(:env)
83
+ ::Rails.env.to_s
84
+ else
85
+ ENV['RAILS_ENV']
86
+ end
87
+ end
88
+ report_on('OpenSSL version') { ::OpenSSL::OPENSSL_VERSION }
89
+ # end reporting logic
90
+ ####################################
91
+
92
+
93
+ attr_reader :data
94
+ # Generate the report based on the class level logic.
95
+ def initialize
96
+ @data = self.class.registered_reporters.inject(Hash.new) do |data, (key, logic)|
97
+ begin
98
+ value = logic.call
99
+ if value
100
+ data[key] = value
101
+ else
102
+ ::TingYun::Agent.logger.debug("EnvironmentReport ignoring value for #{key.inspect} which came back falsey: #{value.inspect}")
103
+ end
104
+ rescue => e
105
+ ::TingYun::Agent.logger.debug("EnvironmentReport failed to retrieve value for #{key.inspect}: #{e}")
106
+ end
107
+ data
108
+ end
109
+ end
110
+
111
+ def [](key)
112
+ @data[key]
113
+ end
114
+
115
+ def []=(key, value)
116
+ @data[key] = value
117
+ end
118
+
119
+ def to_a
120
+ @data.to_a
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+ require 'ting_yun/agent'
3
+ require 'ting_yun/support/local_environment'
4
+ require 'ting_yun/frameworks'
5
+
6
+
7
+ module TingYun
8
+ module Frameworks
9
+ module ClassMethods
10
+ def instance
11
+ framework = TingYun::Agent.config[:framework]
12
+ if framework == :test
13
+ @instance ||= load_test_framework
14
+ else
15
+ @instance ||= load_framework_class(framework).new(local_env)
16
+ end
17
+
18
+ end
19
+
20
+ def load_test_framework
21
+ config = File.expand_path(File.join('..','..', 'test','config','tingyun.yml'), __FILE__)
22
+ require 'config/test'
23
+ TingYun::Frameworks::Test.new(local_env, config)
24
+ end
25
+
26
+
27
+ def load_framework_class(framework)
28
+ begin
29
+ require 'ting_yun/frameworks/' + framework.to_s
30
+ rescue LoadError
31
+ #to avoid error
32
+ end
33
+ TingYun::Frameworks.const_get(framework.to_s.capitalize)
34
+ end
35
+
36
+ def local_env
37
+ @local_env ||= TingYun::Support::LocalEnvironment.new
38
+ end
39
+
40
+ # clear out memoized Framework and LocalEnv instances
41
+ def reset
42
+ @instance = nil
43
+ @local_env = nil
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,15 @@
1
+ # encoding: utf-8
2
+
3
+ require 'ting_yun/frameworks/ruby'
4
+
5
+ module TingYun
6
+ module Frameworks
7
+ class External < TingYun::Frameworks::Ruby
8
+
9
+ def init_config(options={})
10
+ super
11
+ end
12
+
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,120 @@
1
+ # encoding: utf-8
2
+ require 'ting_yun/configuration/manual_source'
3
+ require 'ting_yun/configuration/yaml_source'
4
+ require 'ting_yun/agent'
5
+ require 'ting_yun/logger/agent_logger'
6
+ require 'ting_yun/agent/agent'
7
+ require 'ting_yun/frameworks/instrumentation'
8
+
9
+ module TingYun
10
+ module Frameworks
11
+ module InstanceMethods
12
+
13
+ include ::TingYun::Frameworks::Instrumentation
14
+
15
+ # The env is the setting used to identify which section of the tingyun.yml
16
+ # to load. This defaults to a framework specific value, such as ENV['RAILS_ENV']
17
+ # but can be overridden as long as you set it before calling #init_plugin
18
+ attr_writer :env
19
+
20
+ # The local environment contains all the information we report
21
+ # to the server about what kind of application this is, what
22
+ # gems and plugins it uses, and many other kinds of
23
+ # machine-dependent information useful in debugging
24
+ attr_reader :local_env
25
+
26
+ # Initialize the plugin/gem and start the agent. This does the
27
+ # necessary configuration based on the framework environment and
28
+ # determines whether or not to start the agent. If the agent is
29
+ # not going to be started then it loads the agent shim which has
30
+ # stubs for all the external api.
31
+ #
32
+ # This may be invoked multiple times, as long as you don't attempt
33
+ # to uninstall the agent after it has been started.
34
+ #
35
+ # If the plugin is initialized and it determines that the agent is
36
+ # not enabled, it will skip starting it and install the shim. But
37
+ # if you later call this with <tt>agent_enabled => true</tt>,
38
+ # then it will install the real agent and start it.
39
+ #
40
+ # What determines whether the agent is launched is the result of
41
+ # calling nbs.agent_enabled? This will indicate whether the
42
+ # instrumentation should/will be installed. If we're in a mode
43
+ # where tracers are not installed then we should not start the
44
+ # agent.
45
+ #
46
+ # Subclasses are not allowed to override, but must implement
47
+ # init_config({}) which is called one or more times.
48
+ def init_plugin(options={})
49
+ env = determine_env(options)
50
+
51
+ configure_agent(env, options)
52
+
53
+ if ::TingYun::Agent.logger.is_startup_logger?
54
+ ::TingYun::Agent.logger = TingYun::Logger::AgentLogger.new(root, options.delete(:log))
55
+ end
56
+
57
+ environment_name = options.delete(:env) and self.env = environment_name
58
+
59
+ init_config(options)
60
+
61
+ TingYun::Agent.agent = TingYun::Agent::Agent.instance
62
+
63
+ if TingYun::Agent.config[:'nbs.agent_enabled'] && !TingYun::Agent.agent.started?
64
+ start_agent
65
+ install_instrumentation
66
+ else
67
+ LibraryDetection.detect!
68
+ end
69
+
70
+ end
71
+
72
+ def determine_env(options)
73
+ env = options[:env] || self.env
74
+ env = env.to_s
75
+
76
+ if @started_in_env && @started_in_env != env
77
+ TingYun::Agent.logger.error("Attempted to start agent in #{env.inspect} environment, but agent was already running in #{@started_in_env.inspect}",
78
+ "The agent will continue running in #{@started_in_env.inspect}. To alter this, ensure the desired environment is set before the agent starts.")
79
+ else
80
+ TingYun::Agent.logger.info("Starting the Ting Yun agent in #{env.inspect} environment.",
81
+ "To prevent agent startup add a TINGYUN_AGENT_ENABLED=false environment variable or modify the #{env.inspect} section of your tingyun.yml.")
82
+ end
83
+
84
+ env
85
+ end
86
+
87
+ def configure_agent(env, options)
88
+ # manual_source
89
+ TingYun::Agent.config.replace_or_add_config(TingYun::Configuration::ManualSource.new(options)) unless options.empty?
90
+
91
+ # yaml_source
92
+ config_file_path = @config_file_override || TingYun::Agent.config[:config_path]
93
+ TingYun::Agent.config.replace_or_add_config(TingYun::Configuration::YamlSource.new(config_file_path,env))
94
+ end
95
+
96
+ def start_agent
97
+ @started_in_env = self.env
98
+ TingYun::Agent.agent.start
99
+ end
100
+
101
+ def framework
102
+ Agent.config[:framework]
103
+ end
104
+
105
+ def [](key)
106
+ TingYun::Agent.config[key.to_sym]
107
+ end
108
+
109
+ def dispatcher
110
+ TingYun::Agent.config[:dispatcher]
111
+ end
112
+
113
+ def root
114
+ '.'
115
+ end
116
+
117
+
118
+ end
119
+ end
120
+ end
@@ -0,0 +1,67 @@
1
+ # encoding: utf-8
2
+
3
+ require 'ting_yun/support/library_detection'
4
+
5
+ module TingYun
6
+ module Frameworks
7
+ # Contains methods that relate to adding and executing files that
8
+ # contain instrumentation for the Ruby Agent
9
+
10
+ module Instrumentation
11
+
12
+ # Signals the agent that it's time to actually load the
13
+ # instrumentation files. May be overridden by subclasses
14
+ def install_instrumentation
15
+ _install_instrumentation
16
+ end
17
+
18
+
19
+ def add_instrumentation(pattern)
20
+ if @instrumented
21
+ load_instrumentation_files pattern
22
+ else
23
+ @instrumentation_files << pattern
24
+ end
25
+ end
26
+
27
+ # Adds a list of files in Dir.glob format
28
+ # (e.g. '/app/foo/**/*_instrumentation.rb')
29
+ # This requires the files within a rescue block, so that any
30
+ # errors within instrumentation files do not affect the overall
31
+ # agent or application in which it runs.
32
+ def load_instrumentation_files(pattern)
33
+ Dir.glob(pattern) do |file|
34
+ begin
35
+ require file.to_s
36
+ rescue LoadError => e
37
+ TingYun::Agent.logger.warn "Error loading instrumentation file '#{file}':", e
38
+ end
39
+ end
40
+ end
41
+
42
+ def detect_dependencies
43
+ LibraryDetection.detect!
44
+ end
45
+
46
+ private
47
+
48
+ def _install_instrumentation
49
+ return if @instrumented
50
+
51
+ # instrumentation for the key code points inside rails for monitoring by TingYun.
52
+ # note this file is loaded only if the tingyun agent is enabled (through config/tingyun.yml)
53
+ instrumentation_path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'instrumentation'))
54
+ @instrumentation_files << File.join(instrumentation_path, '*.rb')
55
+ @instrumentation_files << File.join(instrumentation_path, Agent.config[:framework].to_s, '*.rb')
56
+ @instrumentation_files.each { |pattern| load_instrumentation_files pattern }
57
+
58
+ TingYun::Support::LibraryDetection.detect!
59
+
60
+ ::TingYun::Agent.logger.info('Finished instrumentation')
61
+
62
+ @instrumented = true
63
+ end
64
+
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,63 @@
1
+ # encoding: utf-8
2
+
3
+ require 'ting_yun/frameworks/ruby'
4
+
5
+ module TingYun
6
+ module Frameworks
7
+ # noinspection ALL
8
+ class Rails < TingYun::Frameworks::Ruby
9
+
10
+ def env
11
+ @evn ||= RAILS_ENV.dup
12
+ end
13
+
14
+ def root
15
+ root = rails_root.to_s.empty? ? super : rails_root.to_s
16
+ end
17
+
18
+ # noinspection Rails3Deprecated,Rails3Deprecated
19
+ def rails_root
20
+ RAILS_ROOT if defined?(RAILS_ROOT)
21
+ end
22
+
23
+ def rails_config
24
+ if defined?(::Rails) && ::Rails.respond_to?(:configuration)
25
+ ::Rails.configuration
26
+ else
27
+ @config
28
+ end
29
+ end
30
+
31
+ # In versions of Rails prior to 2.0, the rails config was only available to
32
+ # the init.rb, so it had to be passed on from there. This is a best effort to
33
+ # find a config and use that.
34
+ def init_config(options = {})
35
+ @config = options[:config]
36
+ # Install the dependency detection,
37
+ if rails_config && ::Rails.configuration.respond_to?(:after_initialize)
38
+ rails_config.after_initialize do
39
+ # This will insure we load all the instrumentation as late as possible. If the agent
40
+ # is not enabled, it will load a limited amount of instrumentation.
41
+ TingYun::Support::LibraryDetection.detect!
42
+ end
43
+ end
44
+
45
+ if !Agent.config[:'nbs.agent_enabled']
46
+ # Might not be running if it does not think mongrel, thin, passenger, etc
47
+ # is running, if it thinks it's a rake task, or if the nbs.agent_enabled is false.
48
+ ::TingYun::Agent.logger.info("TingYun Agent is unable to run.")
49
+ end
50
+ rescue => e
51
+ ::TingYun::Agent.logger.error("Failure during init_config for Rails. Is Rails required in a non-Rails app? Set TING_YUN_FRAMEWORK=ruby to avoid this message.",
52
+ "The Ruby agent will continue running, but Rails-specific features may be missing.",
53
+ e)
54
+ end
55
+
56
+ protected
57
+
58
+ def rails_vendor_root
59
+ File.join(root,'vendor','rails')
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+
3
+ require 'ting_yun/frameworks/rails'
4
+
5
+ module TingYun
6
+ module Frameworks
7
+ class Rails3 < TingYun::Frameworks::Rails
8
+
9
+ def env
10
+ @env ||= ::Rails.env.to_s
11
+ end
12
+
13
+ def rails_root
14
+ ::Rails.root.to_s
15
+ end
16
+
17
+ def vendor_root
18
+ @vendor_root ||= File.join(root, 'vendor', 'rails')
19
+ end
20
+
21
+ def version
22
+ @rails_version ||= TingYun::Support::VersionNumber.new(::Rails::VERSION::STRING)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ require 'ting_yun/frameworks/rails3'
4
+
5
+ module TingYun
6
+ module Frameworks
7
+ class Rails4 < TingYun::Frameworks::Rails3
8
+
9
+ def rails_gem_list
10
+ Bundler.rubygems.all_specs.map { |gem| "#{gem.name} (#{gem.version})" }
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+ module TingYun
3
+ module Frameworks
4
+ class Ruby < TingYun::Frameworks::Framework
5
+ def env
6
+ @env ||= ENV['TINGYUN_ENV'] || ENV['RUBY_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
7
+ end
8
+
9
+ def root
10
+ @root ||= ENV['APP_ROOT'] || '.'
11
+ end
12
+
13
+ def init_config(options={})
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ require 'ting_yun/frameworks/ruby'
4
+
5
+ module TingYun
6
+ module Frameworks
7
+ class Sinatra < TingYun::Frameworks::Ruby;
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,34 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under Ting Yun's license terms.
3
+ require 'ting_yun/frameworks/instance_methods'
4
+ require 'ting_yun/frameworks/class_methods'
5
+
6
+
7
+ module TingYun
8
+ module Frameworks
9
+
10
+ def self.framework
11
+ Framework.instance
12
+ end
13
+
14
+ def self.init_start(options={})
15
+ framework.init_plugin(options)
16
+ end
17
+
18
+ class Framework
19
+ include InstanceMethods
20
+ extend ClassMethods
21
+
22
+ protected
23
+
24
+ def initialize(local_env,config_file_override=nil)
25
+ @local_env = local_env
26
+ @started_in_env = nil
27
+ @config_file_override = config_file_override
28
+
29
+ @instrumentation_files = []
30
+ end
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,8 @@
1
+ # encoding: utf-8
2
+ module TingYun
3
+ module Http
4
+ class GenericRequest
5
+
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,46 @@
1
+ require 'ting_yun/http/generic_request'
2
+ module TingYun
3
+ module Http
4
+ class NetHttpRequest < GenericRequest
5
+ def initialize(connection, request)
6
+ @connection = connection
7
+ @request = request
8
+ end
9
+
10
+ def type
11
+ 'Net::HTTP'
12
+ end
13
+
14
+ def host
15
+ if hostname = self['host']
16
+ hostname.split(':').first
17
+ else
18
+ @connection.address
19
+ end
20
+ end
21
+
22
+ def method
23
+ @request.method
24
+ end
25
+
26
+ def [](key)
27
+ @request[key]
28
+ end
29
+
30
+ def []=(key, value)
31
+ @request[key] = value
32
+ end
33
+
34
+ def uri
35
+ case @request.path
36
+ when /^https?:\/\//
37
+ URI(@request.path)
38
+ else
39
+ scheme = @connection.use_ssl? ? 'https' : 'http'
40
+ URI("#{scheme}://#{@connection.address}:#{@connection.port}#{@request.path}")
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+