appsignal 3.7.0 → 3.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,206 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Appsignal
4
- class Minutely
5
- class ProbeCollection
6
- def initialize
7
- @probes = {}
8
- end
9
-
10
- # @return [Integer] Number of probes that are registered.
11
- def count
12
- probes.count
13
- end
14
-
15
- # Clears all probes from the list.
16
- # @return [void]
17
- def clear
18
- probes.clear
19
- end
20
-
21
- # Fetch a probe using its name.
22
- # @param key [Symbol/String] The name of the probe to fetch.
23
- # @return [Object] Returns the registered probe.
24
- def [](key)
25
- probes[key]
26
- end
27
-
28
- # Register a new minutely probe.
29
- #
30
- # Supported probe types are:
31
- #
32
- # - Lambda - A lambda is an object that listens to a `call` method call.
33
- # This `call` method is called every minute.
34
- # - Class - A class object is an object that listens to a `new` and
35
- # `call` method call. The `new` method is called when the Minutely
36
- # probe thread is started to initialize all probes. This allows probes
37
- # to load dependencies once beforehand. Their `call` method is called
38
- # every minute.
39
- # - Class instance - A class instance object is an object that listens to
40
- # a `call` method call. The `call` method is called every minute.
41
- #
42
- # @example Register a new probe
43
- # Appsignal::Minutely.probes.register :my_probe, lambda {}
44
- #
45
- # @example Overwrite an existing registered probe
46
- # Appsignal::Minutely.probes.register :my_probe, lambda {}
47
- # Appsignal::Minutely.probes.register :my_probe, lambda { puts "hello" }
48
- #
49
- # @example Add a lambda as a probe
50
- # Appsignal::Minutely.probes.register :my_probe, lambda { puts "hello" }
51
- # # "hello" # printed every minute
52
- #
53
- # @example Add a probe instance
54
- # class MyProbe
55
- # def initialize
56
- # puts "started"
57
- # end
58
- #
59
- # def call
60
- # puts "called"
61
- # end
62
- # end
63
- #
64
- # Appsignal::Minutely.probes.register :my_probe, MyProbe.new
65
- # # "started" # printed immediately
66
- # # "called" # printed every minute
67
- #
68
- # @example Add a probe class
69
- # class MyProbe
70
- # def initialize
71
- # # Add things that only need to be done on start up for this probe
72
- # require "some/library/dependency"
73
- # @cache = {} # initialize a local cache variable
74
- # puts "started"
75
- # end
76
- #
77
- # def call
78
- # puts "called"
79
- # end
80
- # end
81
- #
82
- # Appsignal::Minutely.probes.register :my_probe, MyProbe
83
- # Appsignal::Minutely.start # This is called for you
84
- # # "started" # Printed on Appsignal::Minutely.start
85
- # # "called" # Repeated every minute
86
- #
87
- # @param name [Symbol/String] Name of the probe. Can be used with {[]}.
88
- # This name will be used in errors in the log and allows overwriting of
89
- # probes by registering new ones with the same name.
90
- # @param probe [Object] Any object that listens to the `call` method will
91
- # be used as a probe.
92
- # @return [void]
93
- def register(name, probe)
94
- if probes.key?(name)
95
- logger.debug "A probe with the name `#{name}` is already " \
96
- "registered. Overwriting the entry with the new probe."
97
- end
98
- probes[name] = probe
99
- end
100
-
101
- # @api private
102
- def each(&block)
103
- probes.each(&block)
104
- end
105
-
106
- private
107
-
108
- attr_reader :probes
109
-
110
- def logger
111
- Appsignal.internal_logger
112
- end
113
- end
114
-
115
- class << self
116
- # @see ProbeCollection
117
- # @return [ProbeCollection] Returns list of probes.
118
- def probes
119
- @probes ||= ProbeCollection.new
120
- end
121
-
122
- # @api private
123
- def start
124
- stop
125
- @thread = Thread.new do
126
- # Advise multi-threaded app servers to ignore this thread
127
- # for the purposes of fork safety warnings
128
- if Thread.current.respond_to?(:thread_variable_set)
129
- Thread.current.thread_variable_set(:fork_safe, true)
130
- end
131
-
132
- sleep initial_wait_time
133
- initialize_probes
134
- loop do
135
- logger = Appsignal.internal_logger
136
- logger.debug("Gathering minutely metrics with #{probe_instances.count} probes")
137
- probe_instances.each do |name, probe|
138
- logger.debug("Gathering minutely metrics with '#{name}' probe")
139
- probe.call
140
- rescue => ex
141
- logger.error "Error in minutely probe '#{name}': #{ex}"
142
- logger.debug ex.backtrace.join("\n")
143
- end
144
- sleep wait_time
145
- end
146
- end
147
- end
148
-
149
- # @api private
150
- def stop
151
- defined?(@thread) && @thread.kill
152
- probe_instances.clear
153
- end
154
-
155
- # @api private
156
- def wait_time
157
- 60 - Time.now.sec
158
- end
159
-
160
- private
161
-
162
- def initial_wait_time
163
- remaining_seconds = 60 - Time.now.sec
164
- return remaining_seconds if remaining_seconds > 30
165
-
166
- remaining_seconds + 60
167
- end
168
-
169
- def initialize_probes
170
- probes.each do |name, probe|
171
- initialize_probe(name, probe)
172
- end
173
- end
174
-
175
- def initialize_probe(name, probe)
176
- if probe.respond_to? :new
177
- instance = probe.new
178
- klass = probe
179
- else
180
- instance = probe
181
- klass = instance.class
182
- end
183
- unless dependencies_present?(klass)
184
- Appsignal.internal_logger.debug "Skipping '#{name}' probe, " \
185
- "#{klass}.dependency_present? returned falsy"
186
- return
187
- end
188
- probe_instances[name] = instance
189
- rescue => error
190
- logger = Appsignal.internal_logger
191
- logger.error "Error while initializing minutely probe '#{name}': #{error}"
192
- logger.debug error.backtrace.join("\n")
193
- end
194
-
195
- def dependencies_present?(probe)
196
- return true unless probe.respond_to? :dependencies_present?
197
-
198
- probe.dependencies_present?
199
- end
200
-
201
- def probe_instances
202
- @probe_instances ||= {}
203
- end
204
- end
205
- end
206
- end
@@ -1,16 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Appsignal
4
- module Utils
5
- module DeprecationMessage
6
- def self.message(message, logger = Appsignal.internal_logger)
7
- Kernel.warn "appsignal WARNING: #{message}"
8
- logger.warn message
9
- end
10
-
11
- def deprecation_message(message, logger = Appsignal.internal_logger)
12
- Appsignal::Utils::DeprecationMessage.message(message, logger)
13
- end
14
- end
15
- end
16
- end