alacrity-rails 0.9.1 → 0.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d34463ea5561472561eeff41e257a21fbac9d6f5
4
- data.tar.gz: 83290c1e8b6258f4d70bcb35ad91531ac73ca7f5
3
+ metadata.gz: f51d25d010ac8d45e6a5d3c10486287807cce367
4
+ data.tar.gz: 72b159b9b69289d1e61cf5965f7813400de69362
5
5
  SHA512:
6
- metadata.gz: d0e8039ba1aee3b5a52075c8c92170bbc48b6b607b59879c7f848e73e6a1ac7296c020760054367ed7a5c8e94a8b15efdd8a17fa237e4cc6b43aeeed525115ba
7
- data.tar.gz: a775fe03c032002f80288a4f28076ced5a9d3f46b2e4848b3f2b7de0f1f496251d470a7d1f4613b19e1ff9f068ac0bdfaf034510f683ca7a5da27e5f0ca59419
6
+ metadata.gz: 0bb2e12e4df0574273ba587a605691b14feb9a0ee9bb75e73b83e768f9431fdf6ea4a05c044af308ffd58cebc3ab2214dcd3fc95974f0476d0a5203a23671fce
7
+ data.tar.gz: 6c416373659de9361c69724c4d6490830bda2ce839009299ef4de54b0929143d6888396e6d60a3d8e55fc32de7569ecf77f534e585fd421bf4afcf9e63c2891c
@@ -7,6 +7,7 @@ module AlacrityRails
7
7
  autoload :Config, 'alacrity-rails/config'
8
8
  autoload :Client, 'alacrity-rails/client'
9
9
  autoload :Diagnostic, 'alacrity-rails/diagnostic'
10
+ autoload :Instrumentor, 'alacrity-rails/instrumentor'
10
11
  autoload :Middleware, 'alacrity-rails/middleware'
11
12
  autoload :ServerConfig, 'alacrity-rails/server_config'
12
13
  autoload :VERSION, 'alacrity-rails/version'
@@ -26,6 +27,7 @@ module AlacrityRails
26
27
  autoload :Base, 'alacrity-rails/transaction/base'
27
28
  autoload :BulkWebTransaction, 'alacrity-rails/transaction/bulk_web_transaction'
28
29
  autoload :ConnectionTest, 'alacrity-rails/transaction/connection_test'
30
+ autoload :Custom, 'alacrity-rails/transaction/custom'
29
31
  autoload :ServerStartup, 'alacrity-rails/transaction/server_startup'
30
32
  autoload :WebTransaction, 'alacrity-rails/transaction/web_transaction'
31
33
  end
@@ -2,39 +2,41 @@ require 'net/http'
2
2
 
3
3
  module AlacrityRails
4
4
  class Client
5
+ Thread.current[:alacrity_transactions] = []
5
6
 
6
- def self.open_transaction(env)
7
- new_transaction!(env)
7
+ def self.open_transaction(transaction)
8
+ Thread.current[:alacrity_transactions].push(transaction) if config_enabled?
8
9
  end
9
10
 
10
- def self.store_request(data)
11
- transaction&.store_request(data)
11
+ def self.store_metadata(data)
12
+ transactions.each do |transaction|
13
+ transaction.store_metadata(data)
14
+ end
12
15
  end
13
16
 
14
17
  def self.store_timeline_event(data)
15
- transaction&.store_timeline_event(data)
18
+ transactions.each do |transaction|
19
+ transaction.store_timeline_event(data)
20
+ end
16
21
  end
17
22
 
18
23
  def self.close_transaction!(response)
19
- if transaction
20
- transaction.status = response.first
21
- transaction.finalize!
24
+ if transaction = transactions.pop
25
+ transaction.finalize(response)
22
26
  transmit(transaction)
23
27
  end
24
- ensure
25
- cleanup
26
28
  end
27
29
 
28
30
  def self.log_server_startup
29
31
  transmit(AlacrityRails::Transaction::ServerStartup.new)
30
32
  end
31
33
 
32
- def self.transaction
33
- Thread.current[:alacrity_transaction]
34
+ def self.transactions
35
+ Thread.current[:alacrity_transactions] ||= []
34
36
  end
35
37
 
36
- def self.cleanup
37
- Thread.current[:alacrity_transaction] = nil
38
+ def self.reset
39
+ Thread.current[:alacrity_transactions] = []
38
40
  end
39
41
 
40
42
  def self.transmit(transactable)
@@ -45,10 +47,9 @@ module AlacrityRails
45
47
  end
46
48
  end
47
49
 
48
- private
49
-
50
- def self.new_transaction!(env)
51
- Thread.current[:alacrity_transaction] = Transaction::WebTransaction.new(env)
50
+ def self.config_enabled?
51
+ @config_enabled = Config.enabled? if @config_enabled == nil
52
+ @config_enabled
52
53
  end
53
54
 
54
55
  end
@@ -0,0 +1,21 @@
1
+ module AlacrityRails
2
+ class Instrumentor
3
+ def self.instrument(name_or_options)
4
+ options = name_or_options.is_a?(String) ?
5
+ { type: AlacrityRails::Transaction::Custom, data: { name: name_or_options } } :
6
+ name_or_options
7
+
8
+ Client.reset if options[:reset]
9
+
10
+ transaction = options[:type].new(options[:data])
11
+ Client.open_transaction(transaction)
12
+
13
+ yield.tap do |response|
14
+ Client.close_transaction!(response)
15
+ end
16
+ rescue StandardError => e
17
+ Client.transactions.delete(transaction)
18
+ raise e
19
+ end
20
+ end
21
+ end
@@ -1,10 +1,13 @@
1
1
  module AlacrityRails
2
2
  class Middleware < Struct.new(:app)
3
3
  def call(env)
4
- Client.open_transaction(env)
5
- response = app.call(env)
6
- Client.close_transaction!(response)
7
- response
4
+ AlacrityRails::Instrumentor.instrument(
5
+ type: AlacrityRails::Transaction::WebTransaction,
6
+ data: { url: env['REQUEST_URI'], method: env['REQUEST_METHOD'] },
7
+ reset: true
8
+ ) do
9
+ app.call(env)
10
+ end
8
11
  end
9
12
  end
10
13
  end
@@ -3,7 +3,7 @@ module AlacrityRails
3
3
  class ActionController
4
4
  def self.activate
5
5
  ActiveSupport::Notifications.subscribe 'process_action.action_controller' do |name, started, finished, unique_id, data|
6
- AlacrityRails::Client.store_request(
6
+ AlacrityRails::Client.store_metadata(
7
7
  controller: data[:controller],
8
8
  action: data[:action],
9
9
  format: data[:format]
@@ -1,5 +1,13 @@
1
1
  module AlacrityRails::Transaction
2
2
  class Base
3
+ def initialize(data={})
4
+ # May be overridden
5
+ end
6
+
7
+ def finalize(response)
8
+ # May be overridden
9
+ end
10
+
3
11
  def post_request
4
12
  Net::HTTP::Post.new(endpoint, {
5
13
  "Authorization" => authorization_header_value,
@@ -22,6 +30,39 @@ module AlacrityRails::Transaction
22
30
  end
23
31
  def authorization_header_value; self.class.authorization_header_value end
24
32
 
33
+ def store_metadata(data)
34
+ # May be overridden
35
+ end
36
+
37
+ def store_timeline_event(data)
38
+ timeline_events << {
39
+ name: data[:name],
40
+ event_type: data[:event_type],
41
+ engine: data[:engine],
42
+ started_at: data[:started_at],
43
+ finished_at: data[:finished_at],
44
+ detail: data[:detail]
45
+ }
46
+ end
47
+
48
+ def timeline_events
49
+ @timeline_events ||= []
50
+ end
51
+
52
+ def prepared_timeline_events
53
+ timeline_events.uniq do |timeline_event|
54
+ timeline_event.values_at(:engine, :started_at, :finished_at, :detail).join('~')
55
+ end.map do |timeline_event|
56
+ timeline_event[:started_at] = absolute_time(timeline_event[:started_at].to_datetime)
57
+ timeline_event[:finished_at] = absolute_time(timeline_event[:finished_at].to_datetime)
58
+ timeline_event
59
+ end
60
+ end
61
+
62
+ def absolute_time(datetime)
63
+ datetime.strftime('%Q').to_i
64
+ end
65
+
25
66
  def to_json
26
67
  @to_json ||= super.to_json
27
68
  end
@@ -0,0 +1,29 @@
1
+ module AlacrityRails::Transaction
2
+ class Custom < Base
3
+ attr_accessor :data, :start_time, :end_time
4
+
5
+ def initialize(data={})
6
+ self.data = data
7
+ self.start_time = DateTime.now
8
+ end
9
+
10
+ def finalize(response)
11
+ self.end_time = DateTime.now
12
+ end
13
+
14
+ def as_json(*args)
15
+ {
16
+ environment: AlacrityRails::ServerConfig.environment,
17
+ event_type: 'custom',
18
+ started_at: absolute_time(start_time),
19
+ finished_at: absolute_time(end_time),
20
+ data: data,
21
+ timeline_events: prepared_timeline_events
22
+ }
23
+ end
24
+
25
+ def self.endpoint
26
+ @endpoint ||= URI("#{AlacrityRails::Config.collector_host}/v2/events")
27
+ end
28
+ end
29
+ end
@@ -2,30 +2,20 @@ module AlacrityRails::Transaction
2
2
  class WebTransaction < Base
3
3
  attr_accessor :url, :controller, :action, :format, :method, :status
4
4
 
5
- def initialize(env={})
5
+ def initialize(data={})
6
6
  @middleware_started_at = DateTime.now
7
- self.url = env['REQUEST_URI']
8
- self.method = env['REQUEST_METHOD']
7
+ self.url = data[:url]
8
+ self.method = data[:method]
9
9
  end
10
10
 
11
- def store_request(data)
11
+ def store_metadata(data)
12
12
  self.controller = data[:controller]
13
13
  self.action = data[:action]
14
14
  self.format = data[:format]
15
15
  end
16
16
 
17
- def store_timeline_event(data)
18
- timeline_events << {
19
- name: data[:name],
20
- event_type: data[:event_type],
21
- engine: data[:engine],
22
- started_at: data[:started_at],
23
- finished_at: data[:finished_at],
24
- detail: data[:detail]
25
- }
26
- end
27
-
28
- def finalize!
17
+ def finalize(response)
18
+ self.status = response&.first
29
19
  @middleware_finished_at = DateTime.now
30
20
  end
31
21
 
@@ -51,23 +41,5 @@ module AlacrityRails::Transaction
51
41
  def self.endpoint
52
42
  @endpoint ||= URI("#{AlacrityRails::Config.collector_host}/v1/web-transactions")
53
43
  end
54
-
55
- def timeline_events
56
- @timeline_events ||= []
57
- end
58
-
59
- def prepared_timeline_events
60
- timeline_events.uniq do |timeline_event|
61
- timeline_event.values_at(:engine, :started_at, :finished_at, :detail).join('~')
62
- end.map do |timeline_event|
63
- timeline_event[:started_at] = absolute_time(timeline_event[:started_at].to_datetime)
64
- timeline_event[:finished_at] = absolute_time(timeline_event[:finished_at].to_datetime)
65
- timeline_event
66
- end
67
- end
68
-
69
- def absolute_time(datetime)
70
- datetime.strftime('%Q').to_i
71
- end
72
44
  end
73
45
  end
@@ -1,3 +1,3 @@
1
1
  module AlacrityRails
2
- VERSION = '0.9.1'
2
+ VERSION = '0.10.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: alacrity-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alacrity, LLC
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-27 00:00:00.000000000 Z
11
+ date: 2018-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,6 +38,7 @@ files:
38
38
  - lib/alacrity-rails/client.rb
39
39
  - lib/alacrity-rails/config.rb
40
40
  - lib/alacrity-rails/diagnostic.rb
41
+ - lib/alacrity-rails/instrumentor.rb
41
42
  - lib/alacrity-rails/middleware.rb
42
43
  - lib/alacrity-rails/probe/action_controller.rb
43
44
  - lib/alacrity-rails/probe/action_mailer.rb
@@ -51,6 +52,7 @@ files:
51
52
  - lib/alacrity-rails/server_config.rb
52
53
  - lib/alacrity-rails/transaction/base.rb
53
54
  - lib/alacrity-rails/transaction/connection_test.rb
55
+ - lib/alacrity-rails/transaction/custom.rb
54
56
  - lib/alacrity-rails/transaction/server_startup.rb
55
57
  - lib/alacrity-rails/transaction/web_transaction.rb
56
58
  - lib/alacrity-rails/version.rb
@@ -75,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
77
  version: '0'
76
78
  requirements: []
77
79
  rubyforge_project:
78
- rubygems_version: 2.6.8
80
+ rubygems_version: 2.6.14
79
81
  signing_key:
80
82
  specification_version: 4
81
83
  summary: Rails Agent for the Alacrity Application Health Platform