logstasher 2.0.2 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c6c820ae3e9564dbca07d91c80e75ded3a87bdbf11e0df7ff0b5561ce3a21c0
4
- data.tar.gz: a5cc2935b0db9c9daad29921df6f9461379f9dd0cca76abf0a802f110e44be40
3
+ metadata.gz: d7bf19f1cccbb941b087f15328764d07da66e7006a0db9dd64c14f192d0fbb9b
4
+ data.tar.gz: dc5af64c9333ac0c42a67977519049d6f78233a7c50e3ce353120ff382f9c97b
5
5
  SHA512:
6
- metadata.gz: 4e6b0ae61b5f545e29cd3dff4b3d28bdf3f092f8064978c389d444cb6dad35217b860ac8176034d5fef35d479c68efc818643f2779003e95062e8bbce66f194f
7
- data.tar.gz: e942332d068bf347f588daa955b1d749d7f553b296e2d76bdd33e32555ec4b7e4c6d9ab0b8ef3da92fd363b00a94a71704ca2b50076248559da438ebbdb647c1
6
+ metadata.gz: 60064a14f73422c0a1be36a9052d8d73741287d370876592b610ac4f9b27e4fec42ab3173b8cfd9d65e75e8ae331558a1516aca17ede477e39a4b8d575f3ae67
7
+ data.tar.gz: 17323dff796913fd335fc6142f640c10a281e618ce08ce0836a33b26c669d1a669317a394ecc594e4a2e9d3b8c35bcb19b74200636118c6cf42ce2e12d62a9a4
@@ -36,7 +36,9 @@ module LogStasher
36
36
  unsubscribe(:action_mailer, subscriber)
37
37
  when 'ActiveRecord::LogSubscriber'
38
38
  unsubscribe(:active_record, subscriber)
39
- when 'ActiveJob::Logging::LogSubscriber'
39
+ when 'ActiveJob::LogSubscriber' # For Rails 6
40
+ unsubscribe(:active_job, subscriber)
41
+ when 'ActiveJob::Logging::LogSubscriber' # For Rails 5
40
42
  unsubscribe(:active_job, subscriber)
41
43
  end
42
44
  end
@@ -1,92 +1,98 @@
1
- begin
2
- # `rescue nil` didn't work for some Ruby versions
3
- require 'active_job/logging'
4
- rescue LoadError
1
+ if ActiveJob::VERSION::MAJOR >= 6 && ActiveJob::VERSION::MINOR >= 1
2
+ require 'active_job/log_subscriber'
3
+ else
4
+ require 'active_job/logging'
5
5
  end
6
6
 
7
- if defined?(::ActiveJob::Logging::LogSubscriber)
8
- module LogStasher
9
- module ActiveJob
10
- class LogSubscriber < ::ActiveJob::Logging::LogSubscriber
11
- def enqueue(event)
12
- process_event(event, 'enqueue')
13
- end
14
-
15
- def enqueue_at(event)
16
- process_event(event, 'enqueue_at')
17
- end
18
-
19
- def perform(event)
20
- process_event(event, 'perform')
21
-
22
- # Revert the request id back, in the event that the inline adapter is being used or a
23
- # perform_now was used.
24
- LogStasher.request_context[:request_id] = Thread.current[:old_request_id]
25
- Thread.current[:old_request_id] = nil
26
- end
27
-
28
- def perform_start(event)
29
- # Use the job_id as the request id, so that any custom logging done for a job
30
- # shares a request id, and has the job id in each log line.
31
- #
32
- # It's not being set when the job is enqueued, so enqueuing a job will have it's default
33
- # request_id. In a lot of cases, it will be because of a web request.
34
- #
35
- # Hang onto the old request id, so we can revert after the job is done being performed.
36
- Thread.current[:old_request_id] = LogStasher.request_context[:request_id]
37
- LogStasher.request_context[:request_id] = event.payload[:job].job_id
38
-
39
- process_event(event, 'perform_start')
40
- end
41
-
42
- def logger
43
- LogStasher.logger
44
- end
45
-
46
- private
47
-
48
- def process_event(event, type)
49
- data = extract_metadata(event)
50
- data.merge! extract_exception(event)
51
- data.merge! extract_scheduled_at(event) if type == 'enqueue_at'
52
- data.merge! extract_duration(event) if type == 'perform'
53
- data.merge! request_context
54
-
55
- tags = ['job', type]
56
- tags.push('exception') if data[:exception]
57
- logger << LogStasher.build_logstash_event(data, tags).to_json + "\n"
58
- end
59
-
60
- def extract_metadata(event)
61
- {
62
- job_id: event.payload[:job].job_id,
63
- queue_name: queue_name(event),
64
- job_class: event.payload[:job].class.to_s,
65
- job_args: args_info(event.payload[:job])
66
- }
67
- end
68
-
69
- def extract_duration(event)
70
- { duration: event.duration.to_f.round(2) }
71
- end
72
-
73
- def extract_exception(event)
74
- event.payload.slice(:exception)
75
- end
76
-
77
- def extract_scheduled_at(event)
78
- { scheduled_at: scheduled_at(event) }
79
- end
80
-
81
- def request_context
82
- LogStasher.request_context
83
- end
84
-
85
- # The default args_info makes a string. We need objects to turn into JSON.
86
- def args_info(job)
87
- job.arguments.map { |arg| arg.try(:to_global_id).try(:to_s) || arg }
88
- end
7
+ module LogStasher
8
+ module ActiveJob
9
+
10
+ BASE_SUBSCRIBER = if defined?(::ActiveJob::LogSubscriber)
11
+ ::ActiveJob::LogSubscriber
12
+ else
13
+ ::ActiveJob::Logging::LogSubscriber
14
+ end
15
+
16
+ class LogSubscriber < BASE_SUBSCRIBER
17
+ def enqueue(event)
18
+ process_event(event, 'enqueue')
19
+ end
20
+
21
+ def enqueue_at(event)
22
+ process_event(event, 'enqueue_at')
23
+ end
24
+
25
+ def perform(event)
26
+ process_event(event, 'perform')
27
+
28
+ # Revert the request id back, in the event that the inline adapter is being used or a
29
+ # perform_now was used.
30
+ LogStasher.request_context[:request_id] = Thread.current[:old_request_id]
31
+ Thread.current[:old_request_id] = nil
32
+ end
33
+
34
+ def perform_start(event)
35
+ # Use the job_id as the request id, so that any custom logging done for a job
36
+ # shares a request id, and has the job id in each log line.
37
+ #
38
+ # It's not being set when the job is enqueued, so enqueuing a job will have it's default
39
+ # request_id. In a lot of cases, it will be because of a web request.
40
+ #
41
+ # Hang onto the old request id, so we can revert after the job is done being performed.
42
+ Thread.current[:old_request_id] = LogStasher.request_context[:request_id]
43
+ LogStasher.request_context[:request_id] = event.payload[:job].job_id
44
+
45
+ process_event(event, 'perform_start')
46
+ end
47
+
48
+ def logger
49
+ LogStasher.logger
50
+ end
51
+
52
+ private
53
+
54
+ def process_event(event, type)
55
+ data = extract_metadata(event)
56
+ data.merge! extract_exception(event)
57
+ data.merge! extract_scheduled_at(event) if type == 'enqueue_at'
58
+ data.merge! extract_duration(event) if type == 'perform'
59
+ data.merge! request_context
60
+
61
+ tags = ['job', type]
62
+ tags.push('exception') if data[:exception]
63
+ logger << LogStasher.build_logstash_event(data, tags).to_json + "\n"
64
+ end
65
+
66
+ def extract_metadata(event)
67
+ {
68
+ job_id: event.payload[:job].job_id,
69
+ queue_name: queue_name(event),
70
+ job_class: event.payload[:job].class.to_s,
71
+ job_args: args_info(event.payload[:job])
72
+ }
73
+ end
74
+
75
+ def extract_duration(event)
76
+ { duration: event.duration.to_f.round(2) }
77
+ end
78
+
79
+ def extract_exception(event)
80
+ event.payload.slice(:exception)
81
+ end
82
+
83
+ def extract_scheduled_at(event)
84
+ { scheduled_at: scheduled_at(event) }
85
+ end
86
+
87
+ def request_context
88
+ LogStasher.request_context
89
+ end
90
+
91
+ # The default args_info makes a string. We need objects to turn into JSON.
92
+ def args_info(job)
93
+ job.arguments.map { |arg| arg.try(:to_global_id).try(:to_s) || arg }
89
94
  end
90
95
  end
91
96
  end
92
97
  end
98
+
@@ -10,6 +10,11 @@ module LogStasher
10
10
  process_event(event, %w[mailer deliver])
11
11
  end
12
12
 
13
+ # This method will only be invoked on Rails 6.0 and prior.
14
+ # Starting in Rails 6.0 the receive method was deprecated in
15
+ # favor of ActionMailbox. The receive method was removed
16
+ # from ActionMailer in Rails 6.1, and there doesn't appear to
17
+ # be corresponding instrumentation for ActionMailbox.
13
18
  def receive(event)
14
19
  process_event(event, %w[mailer receive])
15
20
  end
@@ -1,3 +1,3 @@
1
1
  module LogStasher
2
- VERSION = '2.0.2'.freeze
2
+ VERSION = '2.1.0'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logstasher
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shadab Ahmed
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '5.0'
19
+ version: '5.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: '5.0'
26
+ version: '5.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: request_store
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: activerecord
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '5.0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '5.0'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: bundler
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -72,14 +58,14 @@ dependencies:
72
58
  requirements:
73
59
  - - ">="
74
60
  - !ruby/object:Gem::Version
75
- version: '5.0'
61
+ version: '5.2'
76
62
  type: :development
77
63
  prerelease: false
78
64
  version_requirements: !ruby/object:Gem::Requirement
79
65
  requirements:
80
66
  - - ">="
81
67
  - !ruby/object:Gem::Version
82
- version: '5.0'
68
+ version: '5.2'
83
69
  - !ruby/object:Gem::Dependency
84
70
  name: rspec
85
71
  requirement: !ruby/object:Gem::Requirement