sqreen 1.18.6-java → 1.20.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (190) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +27 -0
  3. data/lib/sqreen/actions.rb +2 -0
  4. data/lib/sqreen/actions/actions_index.rb +16 -0
  5. data/lib/sqreen/actions/base.rb +4 -10
  6. data/lib/sqreen/actions/block_ip.rb +2 -0
  7. data/lib/sqreen/actions/block_user.rb +2 -0
  8. data/lib/sqreen/actions/ip_range_indexed_action_class.rb +4 -24
  9. data/lib/sqreen/actions/ip_ranges_index.rb +32 -11
  10. data/lib/sqreen/actions/redirect_ip.rb +2 -0
  11. data/lib/sqreen/actions/redirect_user.rb +2 -0
  12. data/lib/sqreen/actions/repository.rb +27 -8
  13. data/lib/sqreen/actions/unknown_action_type.rb +4 -0
  14. data/lib/sqreen/actions/user_action_class.rb +5 -30
  15. data/lib/sqreen/actions/users_index.rb +35 -0
  16. data/lib/sqreen/agent.rb +2 -1
  17. data/lib/sqreen/aggregated_metric.rb +25 -0
  18. data/lib/sqreen/attack_blocked.rb +2 -0
  19. data/lib/sqreen/binding_accessor.rb +2 -0
  20. data/lib/sqreen/binding_accessor/path_elem.rb +2 -0
  21. data/lib/sqreen/binding_accessor/transforms.rb +8 -1
  22. data/lib/sqreen/call_countable.rb +2 -0
  23. data/lib/sqreen/capped_queue.rb +2 -0
  24. data/lib/sqreen/cb.rb +2 -0
  25. data/lib/sqreen/cb_tree.rb +2 -0
  26. data/lib/sqreen/condition_evaluator.rb +2 -0
  27. data/lib/sqreen/conditionable.rb +2 -0
  28. data/lib/sqreen/configuration.rb +19 -1
  29. data/lib/sqreen/context.rb +2 -0
  30. data/lib/sqreen/default_cb.rb +2 -0
  31. data/lib/sqreen/deferred_logger.rb +2 -0
  32. data/lib/sqreen/deliveries.rb +2 -0
  33. data/lib/sqreen/deliveries/batch.rb +6 -1
  34. data/lib/sqreen/deliveries/simple.rb +6 -0
  35. data/lib/sqreen/dependency.rb +3 -1
  36. data/lib/sqreen/dependency/detector.rb +22 -14
  37. data/lib/sqreen/dependency/libsqreen.rb +4 -0
  38. data/lib/sqreen/dependency/new_relic.rb +2 -0
  39. data/lib/sqreen/dependency/rack.rb +10 -5
  40. data/lib/sqreen/dependency/rails.rb +4 -0
  41. data/lib/sqreen/dependency/sentry.rb +2 -0
  42. data/lib/sqreen/dependency/sinatra.rb +12 -1
  43. data/lib/sqreen/encoding_sanitizer.rb +2 -0
  44. data/lib/sqreen/error_handling_middleware.rb +2 -0
  45. data/lib/sqreen/event.rb +9 -5
  46. data/lib/sqreen/events/attack.rb +25 -18
  47. data/lib/sqreen/events/remote_exception.rb +2 -22
  48. data/lib/sqreen/events/request_record.rb +17 -70
  49. data/lib/sqreen/exception.rb +2 -0
  50. data/lib/sqreen/formatter_with_tid.rb +2 -0
  51. data/lib/sqreen/framework_cb.rb +2 -0
  52. data/lib/sqreen/frameworks.rb +2 -0
  53. data/lib/sqreen/frameworks/generic.rb +2 -0
  54. data/lib/sqreen/frameworks/rails.rb +1 -0
  55. data/lib/sqreen/frameworks/rails3.rb +2 -0
  56. data/lib/sqreen/frameworks/request_recorder.rb +15 -2
  57. data/lib/sqreen/frameworks/sinatra.rb +2 -0
  58. data/lib/sqreen/frameworks/sqreen_test.rb +2 -0
  59. data/lib/sqreen/graft.rb +12 -0
  60. data/lib/sqreen/graft/call.rb +150 -0
  61. data/lib/sqreen/{dependency → graft}/callback.rb +12 -4
  62. data/lib/sqreen/graft/hook.rb +316 -0
  63. data/lib/sqreen/{dependency → graft}/hook_point.rb +152 -33
  64. data/lib/sqreen/graft/hook_point_error.rb +10 -0
  65. data/lib/sqreen/invalid_signature_exception.rb +2 -0
  66. data/lib/sqreen/js.rb +2 -0
  67. data/lib/sqreen/js/call_context.rb +2 -0
  68. data/lib/sqreen/js/context_pool.rb +2 -0
  69. data/lib/sqreen/js/exec_js_runnable.rb +2 -0
  70. data/lib/sqreen/js/execjs_adapter.rb +2 -0
  71. data/lib/sqreen/js/executable_js.rb +2 -0
  72. data/lib/sqreen/js/js_service.rb +2 -0
  73. data/lib/sqreen/js/js_service_adapter.rb +2 -0
  74. data/lib/sqreen/js/mini_racer_adapter.rb +2 -0
  75. data/lib/sqreen/js/mini_racer_executable_js.rb +2 -0
  76. data/lib/sqreen/js/thread_local_exec_js_runnable.rb +2 -0
  77. data/lib/sqreen/kit/signals/specialized/aggregated_metric.rb +72 -0
  78. data/lib/sqreen/kit/signals/specialized/attack.rb +57 -0
  79. data/lib/sqreen/kit/signals/specialized/binning_metric.rb +76 -0
  80. data/lib/sqreen/kit/signals/specialized/http_trace.rb +26 -0
  81. data/lib/sqreen/kit/signals/specialized/sdk_track_call.rb +50 -0
  82. data/lib/sqreen/kit/signals/specialized/sqreen_exception.rb +57 -0
  83. data/lib/sqreen/{backport.rb → legacy.rb} +3 -2
  84. data/lib/sqreen/{instrumentation.rb → legacy/instrumentation.rb} +31 -2
  85. data/lib/sqreen/legacy/old_event_submission_strategy.rb +221 -0
  86. data/lib/sqreen/legacy/waf_redactions.rb +49 -0
  87. data/lib/sqreen/log.rb +2 -0
  88. data/lib/sqreen/log/loggable.rb +28 -0
  89. data/lib/sqreen/logger.rb +2 -0
  90. data/lib/sqreen/metrics.rb +2 -0
  91. data/lib/sqreen/metrics/average.rb +2 -0
  92. data/lib/sqreen/metrics/base.rb +5 -0
  93. data/lib/sqreen/metrics/binning.rb +2 -0
  94. data/lib/sqreen/metrics/collect.rb +2 -0
  95. data/lib/sqreen/metrics/sum.rb +2 -0
  96. data/lib/sqreen/metrics_store.rb +24 -12
  97. data/lib/sqreen/metrics_store/already_registered_metric.rb +2 -0
  98. data/lib/sqreen/metrics_store/unknown_metric.rb +2 -0
  99. data/lib/sqreen/metrics_store/unregistered_metric.rb +2 -0
  100. data/lib/sqreen/middleware.rb +2 -0
  101. data/lib/sqreen/mono_time.rb +2 -0
  102. data/lib/sqreen/node.rb +2 -0
  103. data/lib/sqreen/not_implemented_yet.rb +2 -0
  104. data/lib/sqreen/null_logger.rb +2 -0
  105. data/lib/sqreen/payload_creator.rb +2 -0
  106. data/lib/sqreen/payload_creator/header_section.rb +2 -0
  107. data/lib/sqreen/performance_notifications.rb +2 -0
  108. data/lib/sqreen/performance_notifications/binned_metrics.rb +10 -2
  109. data/lib/sqreen/performance_notifications/log.rb +2 -0
  110. data/lib/sqreen/performance_notifications/log_performance.rb +2 -0
  111. data/lib/sqreen/performance_notifications/metrics.rb +2 -0
  112. data/lib/sqreen/performance_notifications/newrelic.rb +2 -0
  113. data/lib/sqreen/prefix.rb +2 -0
  114. data/lib/sqreen/rails_middleware.rb +2 -0
  115. data/lib/sqreen/remote_command.rb +2 -0
  116. data/lib/sqreen/remote_command/failure_output.rb +5 -0
  117. data/lib/sqreen/rules.rb +6 -2
  118. data/lib/sqreen/rules/attrs.rb +2 -0
  119. data/lib/sqreen/rules/auth_track_cb.rb +2 -0
  120. data/lib/sqreen/rules/binding_accessor_matcher_cb.rb +2 -0
  121. data/lib/sqreen/rules/binding_accessor_metrics.rb +2 -0
  122. data/lib/sqreen/rules/blacklist_ips_cb.rb +2 -0
  123. data/lib/sqreen/rules/count_http_codes.rb +2 -0
  124. data/lib/sqreen/rules/crawler_user_agent_matches_cb.rb +2 -0
  125. data/lib/sqreen/rules/crawler_user_agent_matches_metrics_cb.rb +2 -0
  126. data/lib/sqreen/rules/custom_error_cb.rb +2 -0
  127. data/lib/sqreen/rules/devise_auth_track_cb.rb +2 -0
  128. data/lib/sqreen/rules/devise_signup_track_cb.rb +2 -0
  129. data/lib/sqreen/rules/execjs_cb.rb +2 -0
  130. data/lib/sqreen/rules/headers_insert_cb.rb +7 -0
  131. data/lib/sqreen/rules/matcher_rule.rb +2 -0
  132. data/lib/sqreen/rules/not_found_cb.rb +7 -0
  133. data/lib/sqreen/rules/rails_parameters_cb.rb +2 -0
  134. data/lib/sqreen/rules/record_request_context.rb +2 -0
  135. data/lib/sqreen/rules/regexp_rule_cb.rb +2 -0
  136. data/lib/sqreen/rules/rule_cb.rb +4 -0
  137. data/lib/sqreen/rules/run_req_start_actions.rb +3 -1
  138. data/lib/sqreen/rules/run_user_actions.rb +3 -1
  139. data/lib/sqreen/rules/shell_env_cb.rb +2 -0
  140. data/lib/sqreen/rules/signup_track_cb.rb +2 -0
  141. data/lib/sqreen/rules/update_request_context.rb +2 -0
  142. data/lib/sqreen/rules/url_matches_cb.rb +2 -0
  143. data/lib/sqreen/rules/user_agent_matches_cb.rb +2 -0
  144. data/lib/sqreen/rules/waf_cb.rb +41 -16
  145. data/lib/sqreen/rules/xss_cb.rb +2 -0
  146. data/lib/sqreen/run_when_called_cb.rb +2 -0
  147. data/lib/sqreen/runner.rb +68 -12
  148. data/lib/sqreen/runtime_infos.rb +2 -0
  149. data/lib/sqreen/safe_json.rb +2 -0
  150. data/lib/sqreen/sdk.rb +4 -0
  151. data/lib/sqreen/sensitive_data_redactor.rb +21 -31
  152. data/lib/sqreen/serializer.rb +2 -0
  153. data/lib/sqreen/session.rb +41 -37
  154. data/lib/sqreen/shared_storage.rb +2 -0
  155. data/lib/sqreen/shared_storage23.rb +2 -0
  156. data/lib/sqreen/shrink_wrap.rb +16 -0
  157. data/lib/sqreen/signals/conversions.rb +283 -0
  158. data/lib/sqreen/signals/http_trace_redaction.rb +111 -0
  159. data/lib/sqreen/signals/signals_submission_strategy.rb +78 -0
  160. data/lib/sqreen/signature_verifier.rb +2 -0
  161. data/lib/sqreen/sinatra_middleware.rb +2 -0
  162. data/lib/sqreen/sqreen_signed_verifier.rb +2 -0
  163. data/lib/sqreen/token_invalid_exception.rb +2 -0
  164. data/lib/sqreen/token_not_found_exception.rb +2 -0
  165. data/lib/sqreen/trie.rb +2 -0
  166. data/lib/sqreen/unauthorized.rb +2 -0
  167. data/lib/sqreen/util.rb +5 -0
  168. data/lib/sqreen/util/capped_array.rb +2 -0
  169. data/lib/sqreen/util/capped_hash.rb +2 -0
  170. data/lib/sqreen/util/capped_string.rb +2 -0
  171. data/lib/sqreen/util/capper.rb +2 -0
  172. data/lib/sqreen/version.rb +3 -1
  173. data/lib/sqreen/waf_error.rb +2 -0
  174. data/lib/sqreen/weave.rb +12 -0
  175. data/lib/sqreen/weave/hardcoded.rb +19 -0
  176. data/lib/sqreen/weave/instrumentor.rb +48 -0
  177. data/lib/sqreen/weave/legacy.rb +12 -0
  178. data/lib/sqreen/weave/legacy/instrumentation.rb +406 -0
  179. data/lib/sqreen/web_server.rb +2 -0
  180. data/lib/sqreen/web_server/generic.rb +2 -0
  181. data/lib/sqreen/web_server/passenger.rb +2 -0
  182. data/lib/sqreen/web_server/puma.rb +2 -0
  183. data/lib/sqreen/web_server/rainbows.rb +2 -0
  184. data/lib/sqreen/web_server/thin.rb +2 -0
  185. data/lib/sqreen/web_server/unicorn.rb +2 -0
  186. data/lib/sqreen/web_server/webrick.rb +2 -0
  187. data/lib/sqreen/worker.rb +2 -0
  188. metadata +65 -9
  189. data/lib/sqreen/backport/original_name.rb +0 -86
  190. data/lib/sqreen/dependency/hook.rb +0 -102
@@ -1,3 +1,5 @@
1
+ # typed: true
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -28,27 +30,5 @@ module Sqreen
28
30
  def klass
29
31
  payload['exception'].class.name
30
32
  end
31
-
32
- def to_hash
33
- exception = payload['exception']
34
- ev = {
35
- :klass => exception.class.name,
36
- :message => exception.message,
37
- :params => payload['request_params'],
38
- :time => payload['time'],
39
- :infos => {
40
- :client_ip => payload['client_ip'],
41
- },
42
- :request => payload['request_infos'],
43
- :headers => payload['headers'],
44
- :rule_name => payload['rule_name'],
45
- :rulespack_id => payload['rulespack_id'],
46
- }
47
-
48
- ev[:infos].merge!(payload['infos']) if payload['infos']
49
- return ev unless exception.backtrace
50
- ev[:context] = { :backtrace => exception.backtrace.map(&:to_s) }
51
- ev
52
- end
53
33
  end
54
34
  end
@@ -1,3 +1,5 @@
1
+ # typed: false
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -12,6 +14,10 @@ require 'sqreen/sensitive_data_redactor'
12
14
  module Sqreen
13
15
  # When a request is deeemed worthy of being sent to the backend
14
16
  class RequestRecord < Sqreen::Event
17
+ attr_reader :redactor
18
+
19
+ # @param [Hash] payload
20
+ # @param [Sqreen::SensitiveDataRedactor] redactor
15
21
  def initialize(payload, redactor = nil)
16
22
  @redactor = redactor
17
23
  super(payload)
@@ -21,74 +27,18 @@ module Sqreen
21
27
  (payload && payload[:observed]) || {}
22
28
  end
23
29
 
24
- def to_hash
25
- res = { :version => '20171208' }
26
- if payload[:observed]
27
- res[:observed] = payload[:observed].dup
28
- rulespack = nil
29
- if observed[:attacks]
30
- res[:observed][:attacks] = observed[:attacks].map do |att|
31
- natt = att.dup
32
- rulespack = natt.delete(:rulespack_id) || rulespack
33
- natt
34
- end
35
- end
36
- if observed[:sqreen_exceptions]
37
- res[:observed][:sqreen_exceptions] = observed[:sqreen_exceptions].map do |exc|
38
- nex = exc.dup
39
- excp = nex.delete(:exception)
40
- if excp
41
- nex[:message] = excp.message
42
- nex[:klass] = excp.class.name
43
- end
44
- rulespack = nex.delete(:rulespack_id) || rulespack
45
- nex
46
- end
47
- end
48
- res[:rulespack_id] = rulespack unless rulespack.nil?
49
- if observed[:observations]
50
- res[:observed][:observations] = observed[:observations].map do |cat, key, value, time|
51
- { :category => cat, :key => key, :value => value, :time => time }
52
- end
53
- end
54
- if observed[:sdk]
55
- res[:observed][:sdk] = processed_sdk_calls
56
- end
57
- end
58
- res[:local] = payload['local'] if payload['local']
59
- if payload['request']
60
- res[:request] = payload['request'].dup
61
- res[:client_ip] = res[:request].delete(:client_ip) if res[:request][:client_ip]
62
- else
63
- res[:request] = {}
64
- end
65
- if payload['response']
66
- res[:response] = payload['response'].dup
67
- else
68
- res[:response] = {}
69
- end
70
-
71
- res[:request][:parameters] = payload['params'] if payload['params']
72
- res[:request][:headers] = payload['headers'] if payload['headers']
73
-
74
- res = Sqreen::EncodingSanitizer.sanitize(res)
30
+ def last_identify_args
31
+ return nil unless observed[:sdk]
75
32
 
76
- if @redactor
77
- res[:request], redacted = @redactor.redact(res[:request])
78
- if redacted.any? && res[:observed] && res[:observed][:attacks]
79
- res[:observed][:attacks] = @redactor.redact_attacks!(res[:observed][:attacks], redacted)
80
- end
81
- if redacted.any? && res[:observed] && res[:observed][:sqreen_exceptions]
82
- res[:observed][:sqreen_exceptions] = @redactor.redact_exceptions!(res[:observed][:sqreen_exceptions], redacted)
83
- end
33
+ observed[:sdk].reverse_each do |meth, _time, *args|
34
+ next unless meth == :identify
35
+ return args
84
36
  end
85
-
86
- res
37
+ nil
87
38
  end
88
39
 
89
- private
90
-
91
40
  def processed_sdk_calls
41
+ return [] unless observed[:sdk]
92
42
  auth_keys = last_identify_id
93
43
 
94
44
  observed[:sdk].map do |meth, time, *args|
@@ -100,6 +50,8 @@ module Sqreen
100
50
  end
101
51
  end
102
52
 
53
+ private
54
+
103
55
  def inject_identifiers(args, meth, auth_keys)
104
56
  return args unless meth == :track && auth_keys
105
57
 
@@ -116,13 +68,8 @@ module Sqreen
116
68
  end
117
69
 
118
70
  def last_identify_id
119
- return nil unless observed[:sdk]
120
-
121
- observed[:sdk].reverse_each do |meth, _time, *args|
122
- next unless meth == :identify
123
- return args.first if args.respond_to? :first
124
- end
125
- nil
71
+ args = last_identify_args
72
+ args.first if args.respond_to? :first
126
73
  end
127
74
  end
128
75
  end
@@ -1,3 +1,5 @@
1
+ # typed: false
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -1,3 +1,5 @@
1
+ # typed: true
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -1,3 +1,5 @@
1
+ # typed: true
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -1,3 +1,5 @@
1
+ # typed: ignore
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -1,3 +1,5 @@
1
+ # typed: ignore
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -1,3 +1,4 @@
1
+ # typed: ignore
1
2
  # frozen_string_literal: true
2
3
 
3
4
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
@@ -1,3 +1,5 @@
1
+ # typed: ignore
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -1,3 +1,5 @@
1
+ # typed: ignore
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -56,12 +58,20 @@ module Sqreen
56
58
  Sqreen.log.debug { "close_request_record called. observed_items: #{observed_items}" }
57
59
 
58
60
  clean_request_record if observed_items.nil?
59
- if only_metric_observation
61
+ if Sqreen.features['use_signals'] || only_metric_observation
60
62
  push_metrics(observations_queue, queue)
61
- return clean_request_record
62
63
  end
64
+
65
+ if only_metric_observation
66
+ clean_request_record
67
+ return
68
+ end
69
+
70
+ # signals require request section to be present
71
+ payload_requests << 'request'
63
72
  payload = payload_creator.payload(payload_requests)
64
73
  payload[:observed] = observed_items
74
+
65
75
  queue.push create_request_record(payload)
66
76
  clean_request_record
67
77
  end
@@ -77,10 +87,13 @@ module Sqreen
77
87
  @redactor ||= SensitiveDataRedactor.from_config
78
88
  end
79
89
 
90
+ # pushes metric observations to the observations queue
91
+ # and clears the list for the request record
80
92
  def push_metrics(observations_queue, event_queue)
81
93
  observed_items[:observations].each do |obs|
82
94
  observations_queue.push obs
83
95
  end
96
+ observed_items[:observations] = []
84
97
  return unless observations_queue.size > MAX_OBS_QUEUE_LENGTH / 2
85
98
  event_queue.push Sqreen::METRICS_EVENT
86
99
  end
@@ -1,3 +1,5 @@
1
+ # typed: ignore
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -1,3 +1,5 @@
1
+ # typed: true
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
@@ -0,0 +1,12 @@
1
+ # typed: strong
2
+
3
+ # Copyright (c) 2015 Sqreen. All Rights Reserved.
4
+ # Please refer to our terms for more information: https://www.sqreen.com/terms.html
5
+
6
+ require 'sqreen/log/loggable'
7
+
8
+ module Sqreen
9
+ module Graft
10
+ include Sqreen::Log::Loggable
11
+ end
12
+ end
@@ -0,0 +1,150 @@
1
+ # typed: false
2
+
3
+ # Copyright (c) 2015 Sqreen. All Rights Reserved.
4
+ # Please refer to our terms for more information: https://www.sqreen.com/terms.html
5
+
6
+ require 'sqreen/backport/clock_gettime'
7
+ require 'sqreen/graft'
8
+
9
+ module Sqreen
10
+ module Graft
11
+ CallbackCall = Struct.new(:callback, :remaining, :instance, :args, :raised, :returned)
12
+ HookedCall = Struct.new(:instance, :args_passed, :args_pass, :raised, :returned, :return, :returning, :retrying, :args_passing, :raising, :raise)
13
+
14
+ class Ball
15
+ def return(value)
16
+ Flow.return(value)
17
+ end
18
+
19
+ def args(value)
20
+ Flow.args(value)
21
+ end
22
+
23
+ def retry
24
+ Flow.retry
25
+ end
26
+
27
+ def raise(value)
28
+ Flow.raise(value)
29
+ end
30
+ end
31
+
32
+ class Flow
33
+ class << self
34
+ def return(value)
35
+ new(:return, value)
36
+ end
37
+
38
+ def args(value)
39
+ new(:args, value)
40
+ end
41
+
42
+ def retry
43
+ new(:retry)
44
+ end
45
+
46
+ def raise(value)
47
+ new(:raise, value)
48
+ end
49
+ end
50
+
51
+ def initialize(action, value, brk = false)
52
+ @action = action
53
+ @value = value
54
+ @break = brk
55
+ end
56
+
57
+ def return?
58
+ @action == :return
59
+ end
60
+
61
+ def return
62
+ @value if return?
63
+ end
64
+
65
+ def raise?
66
+ @action == :raise
67
+ end
68
+
69
+ def raise
70
+ @value if raise?
71
+ end
72
+
73
+ def args?
74
+ @action == :args
75
+ end
76
+
77
+ def args
78
+ @value if args?
79
+ end
80
+
81
+ def retry?
82
+ @action == :retry
83
+ end
84
+
85
+ def break!
86
+ @break = true
87
+
88
+ self
89
+ end
90
+
91
+ def break?
92
+ @break ? true : false
93
+ end
94
+ end
95
+
96
+ class Timer
97
+ def self.read
98
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
99
+ end
100
+
101
+ attr_reader :tag
102
+
103
+ def initialize(tag, &block)
104
+ @tag = tag
105
+ @blips = []
106
+ @block = block
107
+ end
108
+
109
+ def duration
110
+ @blips.each_with_index.reduce(0) { |a, (e, i)| i.even? ? a - e : a + e }
111
+ end
112
+
113
+ def elapsed
114
+ @blips.each_with_index.reduce(0) { |a, (e, i)| i.even? ? a - e : a + e } + Timer.read
115
+ end
116
+
117
+ def ignore
118
+ @blips << Timer.read
119
+ yield(self)
120
+ ensure
121
+ @blips << Timer.read
122
+ end
123
+
124
+ def measure
125
+ @blips << Timer.read
126
+ yield(self)
127
+ ensure
128
+ @blips << Timer.read
129
+ @block.call(self) if @block
130
+ Sqreen::Graft.logger.debug { "#{@tag}: time=%.03fus" % (duration * 1_000_000) }
131
+ end
132
+
133
+ def start
134
+ @blips << Timer.read
135
+ end
136
+
137
+ def stop
138
+ @blips << Timer.read
139
+ end
140
+
141
+ def size
142
+ @blips.size
143
+ end
144
+
145
+ def to_s
146
+ "#{@tag}: time=%.03fus" % (duration * 1_000_000)
147
+ end
148
+ end
149
+ end
150
+ end
@@ -1,19 +1,27 @@
1
+ # typed: false
2
+
1
3
  # Copyright (c) 2015 Sqreen. All Rights Reserved.
2
4
  # Please refer to our terms for more information: https://www.sqreen.com/terms.html
3
5
 
6
+ require 'sqreen/graft'
7
+
4
8
  module Sqreen
5
- module Dependency
9
+ module Graft
6
10
  class Callback
7
- attr_reader :name
11
+ attr_reader :name, :rank, :mandatory, :flow, :ignore
8
12
 
9
- def initialize(name = nil, &block)
13
+ def initialize(name = nil, opts = {}, &block)
10
14
  @name = name
15
+ @rank = opts[:rank] || 0
16
+ @mandatory = opts[:mandatory] || false
17
+ @flow = opts[:flow] || false
18
+ @ignore = opts[:ignore] || false
11
19
  @block = block
12
20
  @disabled = false
13
21
  end
14
22
 
15
23
  def call(*args, &block)
16
- Sqreen.log.debug "[#{Process.pid}] Callback #{@name} disabled:#{disabled?}"
24
+ Sqreen::Graft.logger.debug { "[#{Process.pid}] Callback #{@name} disabled:#{disabled?}" }
17
25
  return if @disabled
18
26
  @block.call(*args, &block)
19
27
  end