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,54 @@
1
+ # encoding: utf-8
2
+
3
+
4
+ require 'ting_yun/support/language_support'
5
+
6
+ module TingYun
7
+ # A singleton for shared generic helper methods
8
+ module Helper
9
+ extend self
10
+
11
+ # confirm a string is correctly encoded (in >= 1.9)
12
+ # If not force the encoding to ASCII-8BIT (binary)
13
+ if TingYun::Support::LanguageSupport.supports_string_encodings?
14
+ def correctly_encoded(string)
15
+ return string unless string.is_a? String
16
+ # The .dup here is intentional, since force_encoding mutates the target,
17
+ # and we don't know who is going to use this string downstream of us.
18
+ string.valid_encoding? ? string : string.dup.force_encoding("ASCII-8BIT")
19
+ end
20
+ else
21
+ #noop
22
+ def correctly_encoded(string)
23
+ string
24
+ end
25
+ end
26
+
27
+ def instance_method_visibility(klass, method_name)
28
+ if klass.private_instance_methods.map{|s|s.to_sym}.include? method_name.to_sym
29
+ :private
30
+ elsif klass.protected_instance_methods.map{|s|s.to_sym}.include? method_name.to_sym
31
+ :protected
32
+ else
33
+ :public
34
+ end
35
+ end
36
+
37
+ def instance_methods_include?(klass, method_name)
38
+ method_name_sym = method_name.to_sym
39
+ (
40
+ klass.instance_methods.map{ |s| s.to_sym }.include?(method_name_sym) ||
41
+ klass.protected_instance_methods.map{ |s|s.to_sym }.include?(method_name_sym) ||
42
+ klass.private_instance_methods.map{ |s|s.to_sym }.include?(method_name_sym)
43
+ )
44
+ end
45
+
46
+ def time_to_millis(time)
47
+ (time.to_f * 1000).round
48
+ end
49
+
50
+ def milliseconds_to_seconds(milliseconds)
51
+ milliseconds / 1000.0
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+
3
+ module TingYun
4
+ module Support
5
+ module Hostname
6
+
7
+ def self.get
8
+ Socket.gethostname
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,49 @@
1
+ # encoding: utf-8
2
+
3
+
4
+ # This module includes utilities for manipulating URIs, particularly from the
5
+ # context of Net::HTTP requests. We don't always have direct access to the full
6
+ # URI from our instrumentation points in Net::HTTP, and we want to filter out
7
+ # some URI parts before saving URIs from instrumented calls - logic for that
8
+ # lives here.
9
+
10
+ module TingYun
11
+ module Agent
12
+ module HTTPClients
13
+ module URIUtil
14
+
15
+ def self.filter_uri(original)
16
+ filtered = original.dup
17
+ filtered.user = nil
18
+ filtered.password = nil
19
+ filtered.query = nil
20
+ filtered.fragment = nil
21
+ filtered.to_s
22
+ end
23
+
24
+ # There are valid URI strings that some HTTP client libraries will
25
+ # accept that the stdlib URI module doesn't handle. If we find that
26
+ # Addressable is around, use that to normalize out our URL's.
27
+ def self.parse_url(url)
28
+ if defined?(::Addressable::URI)
29
+ address = ::Addressable::URI.parse(url)
30
+ address.normalize!
31
+ URI.parse(address.to_s)
32
+ else
33
+ URI.parse(url)
34
+ end
35
+ end
36
+
37
+ QUESTION_MARK = "?".freeze
38
+
39
+ def self.strip_query_string(fragment)
40
+ if(fragment.include?(QUESTION_MARK))
41
+ fragment.split(QUESTION_MARK).first
42
+ else
43
+ fragment
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,155 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under Ting Yun's license terms.
3
+
4
+ module TingYun
5
+ module Support
6
+ module LanguageSupport
7
+ extend self
8
+
9
+ # need to use syck rather than psych when possible
10
+ def needs_syck?
11
+ !LanguageSupport.using_engine?('jruby') &&
12
+ LanguageSupport.using_version?('1.9.2')
13
+ end
14
+
15
+ @@forkable = nil
16
+
17
+ def can_fork?
18
+ # this is expensive to check, so we should only check once
19
+ return @@forkable if @@forkable != nil
20
+
21
+ if Process.respond_to?(:fork)
22
+ # if this is not 1.9.2 or higher, we have to make sure
23
+ @@forkable = ::RUBY_VERSION < '1.9.2' ? test_forkability : true
24
+ else
25
+ @@forkable = false
26
+ end
27
+
28
+ @@forkable
29
+ end
30
+
31
+ def using_engine?(engine)
32
+ if defined?(::RUBY_ENGINE)
33
+ ::RUBY_ENGINE == engine
34
+ else
35
+ engine == 'ruby'
36
+ end
37
+ end
38
+
39
+ def broken_gc?
40
+ LanguageSupport.using_version?('1.8.7') &&
41
+ RUBY_PATCHLEVEL < 348 &&
42
+ !LanguageSupport.using_engine?('jruby') &&
43
+ !LanguageSupport.using_engine?('rbx')
44
+ end
45
+
46
+ def with_disabled_gc
47
+ if defined?(::GC) && ::GC.respond_to?(:disable)
48
+ val = nil
49
+ begin
50
+ ::GC.disable
51
+ val = yield
52
+ ensure
53
+ ::GC.enable
54
+ end
55
+ val
56
+ else
57
+ yield
58
+ end
59
+ end
60
+
61
+ def with_cautious_gc
62
+ if broken_gc?
63
+ with_disabled_gc { yield }
64
+ else
65
+ yield
66
+ end
67
+ end
68
+
69
+ def gc_profiler_usable?
70
+ if defined?(::GC::Profiler) && !jruby?
71
+ true
72
+ else
73
+ false
74
+ end
75
+ end
76
+
77
+ def gc_profiler_enabled?
78
+ if gc_profiler_usable? && ::GC::Profiler.enabled? && !::TingYun::Agent.config[:disable_gc_profiler]
79
+ true
80
+ else
81
+ false
82
+ end
83
+ end
84
+
85
+ def object_space_usable?
86
+ if defined?(::JRuby) && JRuby.respond_to?(:runtime)
87
+ JRuby.runtime.is_object_space_enabled
88
+ elsif defined?(::ObjectSpace) && !rubinius?
89
+ true
90
+ else
91
+ false
92
+ end
93
+ end
94
+
95
+ def jruby?
96
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'
97
+ end
98
+
99
+ def rubinius?
100
+ defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
101
+ end
102
+
103
+ def ree?
104
+ defined?(RUBY_DESCRIPTION) && RUBY_DESCRIPTION =~ /Ruby Enterprise Edition/
105
+ end
106
+
107
+ def using_version?(version)
108
+ numbers = version.split('.')
109
+ numbers == ::RUBY_VERSION.split('.')[0, numbers.size]
110
+ end
111
+
112
+ def supports_string_encodings?
113
+ RUBY_VERSION >= '1.9.0'
114
+ end
115
+
116
+ def constantize(const_name)
117
+ const_name.to_s.sub(/\A::/, '').split('::').inject(Object) do |namespace, name|
118
+ begin
119
+ result = namespace.const_get(name)
120
+
121
+ # const_get looks up the inheritence chain, so if it's a class
122
+ # in the constant make sure we found the one in our namespace.
123
+ #
124
+ # Can't help if the constant isn't a class...
125
+ if result.is_a?(Module)
126
+ expected_name = "#{namespace}::#{name}".gsub(/^Object::/, "")
127
+ return unless expected_name == result.to_s
128
+ end
129
+
130
+ result
131
+ rescue NameError
132
+ nil
133
+ end
134
+ end
135
+ end
136
+
137
+ def test_forkability
138
+ child = Process.fork { exit! }
139
+ # calling wait here doesn't seem like it should necessary, but it seems to
140
+ # resolve some weird edge cases with resque forking.
141
+ Process.wait child
142
+ true
143
+ rescue NotImplementedError
144
+ false
145
+ end
146
+
147
+ def bundled_gem?(gem_name)
148
+ defined?(Bundler) && Bundler.rubygems.all_specs.map(&:name).include?(gem_name)
149
+ rescue => e
150
+ ::TingYun::Agent.logger.info("Could not determine if third party #{gem_name} gem is installed", e)
151
+ false
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,129 @@
1
+ # encoding: utf-8
2
+
3
+
4
+ #
5
+ #
6
+ #@items=[{dependent => dependencies}...]
7
+ #every dependent has some dependencies
8
+ #
9
+ module TingYun
10
+ module Support
11
+ module LibraryDetection
12
+
13
+ module_function
14
+
15
+ @items = []
16
+
17
+ def defer(&block)
18
+ item = Dependent.new
19
+ item.instance_eval(&block)
20
+
21
+ if item.name
22
+ seen_names = @items.map { |i| i.name }.compact
23
+ if seen_names.include?(item.name)
24
+ TingYun::Agent.logger.warn("Refusing to re-register LibraryDetection block with name '#{item.name}'")
25
+ return @items
26
+ end
27
+ end
28
+
29
+ @items << item
30
+ end
31
+
32
+ def detect!
33
+ @items.each do |item|
34
+ if item.dependencies_satisfied?
35
+ item.execute
36
+ end
37
+ end
38
+ end
39
+
40
+ def dependency_by_name(name)
41
+ @items.find {|i| i.name == name }
42
+ end
43
+
44
+ def installed?(name)
45
+ item = dependency_by_name(name)
46
+ item && item.executed
47
+ end
48
+
49
+ def items
50
+ @items
51
+ end
52
+
53
+ def items=(new_items)
54
+ @items = new_items
55
+ end
56
+
57
+
58
+ class Dependent
59
+ attr_reader :dependencies
60
+ attr_reader :executed
61
+ attr_accessor :name
62
+
63
+ def executed!
64
+ @executed = true
65
+ end
66
+
67
+ def dependencies_satisfied?
68
+ !executed and check_dependencies
69
+ end
70
+
71
+ def initialize
72
+ @dependencies = []
73
+ @executes = []
74
+ @name = nil
75
+ end
76
+
77
+ def execute
78
+ @executes.each do |e|
79
+ begin
80
+ e.call
81
+ rescue => err
82
+ TingYun::Agent.logger.error( "Error while installing #{self.name} instrumentation:", err )
83
+ break
84
+ end
85
+ end
86
+ ensure
87
+ executed!
88
+ end
89
+
90
+ def check_dependencies
91
+ return false unless allowed_by_config? && dependencies
92
+
93
+ dependencies.all? do |depend|
94
+ begin
95
+ depend.call
96
+ rescue => err
97
+ TingYun::Agent.logger.error( "Error while detecting #{self.name}:", err )
98
+ false
99
+ end
100
+ end
101
+ end
102
+
103
+ def depends_on
104
+ @dependencies << Proc.new
105
+ end
106
+
107
+ def allowed_by_config?
108
+ # If we don't have a name, can't check config so allow it
109
+ return true if self.name.nil?
110
+
111
+ key = "disable_#{self.name}".to_sym
112
+ if TingYun::Agent.config[key]
113
+ TingYun::Agent.logger.debug("Not installing #{self.name} instrumentation because of configuration #{key}")
114
+ else
115
+ true
116
+ end
117
+ end
118
+
119
+ def named(new_name)
120
+ self.name = new_name
121
+ end
122
+
123
+ def executes
124
+ @executes << Proc.new
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,185 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under Ting Yun's license terms.
3
+
4
+ module TingYun
5
+ module Support
6
+ class LocalEnvironment
7
+
8
+ def discovered_dispatcher
9
+ discover_dispatcher unless @discovered_dispatcher
10
+ @discovered_dispatcher
11
+ end
12
+
13
+ def initialize
14
+ # Extend self with any any submodules of LocalEnvironment. These can override
15
+ # the discover methods to discover new framworks and dispatchers.
16
+ TingYun::Support::LocalEnvironment.constants.each do | const |
17
+ mod = TingYun::Support::LocalEnvironment.const_get const
18
+ self.extend mod if mod.instance_of? Module
19
+ end
20
+ @discovered_dispatcher = nil
21
+ discover_dispatcher
22
+ end
23
+
24
+ # Runs through all the objects in ObjectSpace to find the first one that
25
+ # match the provided class
26
+ def find_class_in_object_space(klass)
27
+ if TingYun::Support::LanguageSupport.object_space_usable?
28
+ ObjectSpace.each_object(klass) do |x|
29
+ return x
30
+ end
31
+ end
32
+ return nil
33
+ end
34
+
35
+
36
+ private
37
+
38
+ def discover_dispatcher
39
+ dispatchers = %w[
40
+ passenger
41
+ torquebox
42
+ trinidad
43
+ glassfish
44
+ resque
45
+ sidekiq
46
+ delayed_job
47
+ puma
48
+ thin
49
+ mongrel
50
+ litespeed
51
+ webrick
52
+ fastcgi
53
+ rainbows
54
+ unicorn
55
+ ]
56
+ while dispatchers.any? && @discovered_dispatcher.nil?
57
+ send 'check_for_'+(dispatchers.shift)
58
+ end
59
+ end
60
+
61
+
62
+ def check_for_torquebox
63
+ return unless defined?(::JRuby) &&
64
+ ( org.torquebox::TorqueBox rescue nil)
65
+ @discovered_dispatcher = :torquebox
66
+ end
67
+
68
+ def check_for_glassfish
69
+ return unless defined?(::JRuby) &&
70
+ (((com.sun.grizzly.jruby.rack.DefaultRackApplicationFactory rescue nil) &&
71
+ defined?(com::sun::grizzly::jruby::rack::DefaultRackApplicationFactory)) ||
72
+ (jruby_rack? && defined?(::GlassFish::Server)))
73
+ @discovered_dispatcher = :glassfish
74
+ end
75
+
76
+ def check_for_trinidad
77
+ return unless defined?(::JRuby) && jruby_rack? && defined?(::Trinidad::Server)
78
+ @discovered_dispatcher = :trinidad
79
+ end
80
+
81
+ def jruby_rack?
82
+ defined?(JRuby::Rack::VERSION)
83
+ end
84
+
85
+ def check_for_webrick
86
+ return unless defined?(::WEBrick) && defined?(::WEBrick::VERSION)
87
+ @discovered_dispatcher = :webrick
88
+ end
89
+
90
+ def check_for_fastcgi
91
+ return unless defined?(::FCGI)
92
+ @discovered_dispatcher = :fastcgi
93
+ end
94
+
95
+ # this case covers starting by mongrel_rails
96
+ def check_for_mongrel
97
+ return unless defined?(::Mongrel) && defined?(::Mongrel::HttpServer)
98
+ @discovered_dispatcher = :mongrel
99
+ end
100
+
101
+ def check_for_unicorn
102
+ if (defined?(::Unicorn) && defined?(::Unicorn::HttpServer)) && TingYun::Support::LanguageSupport.object_space_usable?
103
+ v = find_class_in_object_space(::Unicorn::HttpServer)
104
+ @discovered_dispatcher = :unicorn if v
105
+ end
106
+ end
107
+
108
+ def check_for_rainbows
109
+ if (defined?(::Rainbows) && defined?(::Rainbows::HttpServer)) && TingYun::Support::LanguageSupport.object_space_usable?
110
+ v = find_class_in_object_space(::Rainbows::HttpServer)
111
+ @discovered_dispatcher = :rainbows if v
112
+ end
113
+ end
114
+
115
+ def check_for_puma
116
+ if defined?(::Puma) && File.basename($0) == 'puma'
117
+ @discovered_dispatcher = :puma
118
+ end
119
+ end
120
+
121
+ def check_for_delayed_job
122
+ if $0 =~ /delayed_job$/ || (File.basename($0) == 'rake' && ARGV.include?('jobs:work'))
123
+ @discovered_dispatcher = :delayed_job
124
+ end
125
+ end
126
+
127
+ def check_for_resque
128
+ has_queue = ENV['QUEUE'] || ENV['QUEUES']
129
+ resque_rake = executable == 'rake' && ARGV.include?('resque:work')
130
+ resque_pool_rake = executable == 'rake' && ARGV.include?('resque:pool')
131
+ resque_pool_executable = executable == 'resque-pool' && defined?(::Resque::Pool)
132
+
133
+ using_resque = defined?(::Resque) &&
134
+ (has_queue && resque_rake) ||
135
+ (resque_pool_executable || resque_pool_rake)
136
+
137
+ @discovered_dispatcher = :resque if using_resque
138
+ end
139
+
140
+ def check_for_sidekiq
141
+ if defined?(::Sidekiq) && File.basename($0) == 'sidekiq'
142
+ @discovered_dispatcher = :sidekiq
143
+ end
144
+ end
145
+
146
+ def check_for_thin
147
+ if defined?(::Thin) && defined?(::Thin::Server)
148
+ # If ObjectSpace is available, use it to search for a Thin::Server
149
+ # instance. Otherwise, just the presence of the constant is sufficient.
150
+ if TingYun::Support::LanguageSupport.object_space_usable?
151
+ ObjectSpace.each_object(Thin::Server) do |thin_dispatcher|
152
+ @discovered_dispatcher = :thin
153
+ end
154
+ else
155
+ @discovered_dispatcher = :thin
156
+ end
157
+ end
158
+ end
159
+
160
+ def check_for_litespeed
161
+ if caller.pop =~ /fcgi-bin\/RailsRunner\.rb/
162
+ @discovered_dispatcher = :litespeed
163
+ end
164
+ end
165
+
166
+ def check_for_passenger
167
+ if defined?(::PhusionPassenger)
168
+ @discovered_dispatcher = :passenger
169
+ end
170
+ end
171
+
172
+ public
173
+ # outputs a human-readable description
174
+ def to_s
175
+ s = "LocalEnvironment["
176
+ s << ";dispatcher=#{@discovered_dispatcher}" if @discovered_dispatcher
177
+ s << "]"
178
+ end
179
+
180
+ def executable
181
+ File.basename($0)
182
+ end
183
+ end
184
+ end
185
+ end
@@ -0,0 +1,13 @@
1
+ # encoding: utf-8
2
+
3
+ module TingYun
4
+ module Support
5
+ module Path
6
+ # The root directory for the plugin or gem
7
+ def self.ting_yun_root
8
+ File.expand_path(File.join("..", "..", "..",".."), __FILE__)
9
+ end
10
+
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,61 @@
1
+ # encoding: utf-8
2
+ # This file is distributed under Ting Yun's license terms.
3
+
4
+ require 'base64'
5
+ require 'zlib'
6
+ require 'ting_yun/support/serialize/json_wrapper'
7
+
8
+ module TingYun
9
+ module Support
10
+ module Serialize
11
+ module Encoders
12
+ module Identity
13
+ def self.encode(data, opts=nil)
14
+ data
15
+ end
16
+ end
17
+
18
+ module Compressed
19
+ def self.encode(data, opts=nil)
20
+ Zlib::Deflate.deflate(data, Zlib::DEFAULT_COMPRESSION)
21
+ end
22
+ end
23
+
24
+ module Base64CompressedJSON
25
+ def self.encode(data, opts={})
26
+ normalize_encodings = if opts[:skip_normalization]
27
+ false
28
+ else
29
+ TingYun::Agent.config[:normalize_json_string_encodings]
30
+ end
31
+ json = JSONWrapper.dump(data, :normalize => normalize_encodings)
32
+ Base64.encode64(Compressed.encode(json))
33
+ end
34
+ end
35
+ module CompressedJSON
36
+ def self.encode(data, opts={})
37
+ normalize_encodings = if opts[:skip_normalization]
38
+ false
39
+ else
40
+ TingYun::Agent.config[:normalize_json_string_encodings]
41
+ end
42
+ json = JSONWrapper.dump(data, :normalize => normalize_encodings)
43
+ Compressed.encode(json)
44
+ end
45
+ end
46
+
47
+ module Json
48
+ def self.encode(data, opts={})
49
+ normalize_encodings = if opts[:skip_normalization]
50
+ false
51
+ else
52
+ TingYun::Agent.config[:normalize_json_string_encodings]
53
+ end
54
+ json = JSONWrapper.dump(data, :normalize => normalize_encodings)
55
+ return json
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end