@@ -7,39 +7,61 @@ module Middleware
7
7
# which are intended as ActiveSupport::Notifications.subscribe
8
8
# consumers.
9
9
class Subscriber
10
- attr_reader :configuration
11
- attr_reader :hook_name
10
+ attr_reader :configuration , :hook_name , :start , :finish , :payload
12
11
13
- def initialize ( configuration , hook_name )
12
+ def initialize ( configuration : , hook_name : , start : , finish : , payload : )
14
13
@configuration = configuration
15
14
@hook_name = hook_name
15
+ @start = start
16
+ @finish = finish
17
+ @payload = payload
16
18
end
17
19
18
- def call ( _name , start , finish , _id , payload )
19
- write_metric ( start , finish , payload )
20
+ def self . call ( name , start , finish , _id , payload )
21
+ new (
22
+ configuration : InfluxDB ::Rails . configuration ,
23
+ start : start ,
24
+ finish : finish ,
25
+ payload : payload ,
26
+ hook_name : name
27
+ ) . write
28
+ end
29
+
30
+ def write
31
+ return if disabled?
32
+
33
+ metric . write
20
34
rescue StandardError => e
21
35
::Rails . logger . error ( "[InfluxDB::Rails] Unable to write points: #{ e . message } " )
22
36
end
23
37
24
38
private
25
39
26
- def write_metric ( start , finish , payload )
40
+ def metric
27
41
InfluxDB ::Rails ::Metric . new (
28
- values : values ( start , ( ( finish - start ) * 1000 ) . ceil , payload ) ,
29
- tags : tags ( payload ) ,
42
+ values : values ,
43
+ tags : tags ,
30
44
configuration : configuration ,
31
- timestamp : finish ,
32
- hook_name : hook_name
33
- ) . write
45
+ timestamp : finish
46
+ )
34
47
end
35
48
36
- def tags ( * )
49
+ def tags
37
50
raise NotImplementedError , "must be implemented in subclass"
38
51
end
39
52
40
- def values ( * )
53
+ def values
41
54
raise NotImplementedError , "must be implemented in subclass"
42
55
end
56
+
57
+ def duration
58
+ ( ( finish - start ) * 1000 ) . ceil
59
+ end
60
+
61
+ def disabled?
62
+ configuration . ignore_current_environment? ||
63
+ configuration . ignored_hooks . include? ( hook_name )
64
+ end
43
65
end
44
66
end
45
67
end
0 commit comments