applicaster-logger 0.4.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,2 +1,3 @@
1
1
  require 'applicaster/logger'
2
2
  require "applicaster/rack/request_uuid"
3
+ require "applicaster/sidekiq/middleware"
@@ -33,11 +33,21 @@ module Applicaster
33
33
  Delayed::Worker.logger.formatter =
34
34
  Applicaster::Logger::Formatter.new(facility: "delayed_job")
35
35
  end
36
- if defined?(Sidekiq)
37
- Sidekiq::Logging.logger = LogStashLogger.new(logstash_config)
38
- Sidekiq::Logging.logger.level = app.config.applicaster_logger.level
39
- Sidekiq::Logging.logger.formatter =
40
- Applicaster::Logger::Formatter.new(facility: "sidekiq")
36
+
37
+ if defined?(Sidekiq) && Sidekiq.respond_to?(:configure_server)
38
+ Sidekiq.configure_server do |config|
39
+ config.server_middleware do |chain|
40
+ chain.remove Sidekiq::Middleware::Server::Logging
41
+ chain.add Sidekiq::Middleware::Server::LogstashLogging
42
+ end
43
+ end
44
+ end
45
+
46
+ if defined?(Sidetiq)
47
+ Sidetiq.logger = LogStashLogger.new(logstash_config)
48
+ Sidetiq.logger.level = app.config.applicaster_logger.level
49
+ Sidetiq.logger.formatter =
50
+ Applicaster::Logger::Formatter.new(facility: "sidetiq")
41
51
  end
42
52
  end
43
53
 
@@ -1,5 +1,5 @@
1
1
  module Applicaster
2
2
  module Logger
3
- VERSION = "0.4.0"
3
+ VERSION = "0.5.1"
4
4
  end
5
5
  end
@@ -0,0 +1,106 @@
1
+ module Sidekiq
2
+ module Middleware
3
+ module Server
4
+ class LogstashLogging
5
+
6
+ def call(worker, item, queue)
7
+ Sidekiq::Logging.with_context("#{worker.class.to_s} JID-#{item['jid']}") do
8
+ begin
9
+ logger.info(filter_fields({
10
+ message: "Start: #{worker.class.to_s} JID-#{item['jid']}",
11
+ jid: item['jid'],
12
+ pid: pid,
13
+ tid: tid,
14
+ context: context,
15
+ worker: worker.class.to_s,
16
+ queue: queue,
17
+ params: item['args'],
18
+ latency: Sidekiq::Job.new(Sidekiq.dump_json(item)).latency,
19
+ memory: memory
20
+ }))
21
+
22
+ start = Time.now
23
+
24
+ yield # Pass the torch
25
+
26
+ logger.info(filter_fields({
27
+ message: "Done: #{worker.class.to_s} JID-#{item['jid']}",
28
+ jid: item['jid'],
29
+ pid: pid,
30
+ tid: tid,
31
+ context: context,
32
+ worker: worker.class.to_s,
33
+ queue: queue,
34
+ params: item['args'],
35
+ runtime: elapsed(start),
36
+ memory: memory
37
+ }))
38
+ rescue Exception => e
39
+ logger.error(filter_fields({
40
+ message: "Fail: #{worker.class.to_s} JID-#{item['jid']}",
41
+ jid: item['jid'],
42
+ pid: pid,
43
+ tid: tid,
44
+ context: context,
45
+ worker: worker.class.to_s,
46
+ queue: queue,
47
+ params: item['args'],
48
+ runtime: elapsed(start),
49
+ exception_class: e.class.to_s,
50
+ exception_message: e.message,
51
+ backtrace: e.backtrace.join("\n"),
52
+ memory: memory
53
+ }))
54
+
55
+ raise e
56
+ end
57
+ end
58
+ end
59
+
60
+ def logger
61
+ @logger ||=
62
+ begin
63
+ applicaster_logger = ::Rails.application.config.applicaster_logger
64
+ logger = LogStashLogger.new(applicaster_logger.logstash_config)
65
+ logger.level = applicaster_logger.level
66
+ logger.formatter = Applicaster::Logger::Formatter.new(facility: "sidekiq")
67
+ logger
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ def pid
74
+ ::Process.pid
75
+ end
76
+
77
+ def tid
78
+ ::Thread.current.object_id.to_s(36)
79
+ end
80
+
81
+ def context
82
+ ::Thread.current[:sidekiq_context]
83
+ end
84
+
85
+ def memory
86
+ `ps -o rss= -p #{::Process.pid}`.chomp.to_i
87
+ end
88
+
89
+ def elapsed(start)
90
+ return nil if start.nil?
91
+ (Time.now - start).to_f.round(3)
92
+ end
93
+
94
+ def filter_fields(data)
95
+ data.each do |key, val|
96
+ if val.is_a?(String) && val.length > 32766 # max message length
97
+ data[key] = "[omitted; length = #{val.length}, max = 32766]"
98
+ end
99
+ end
100
+
101
+ data
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: applicaster-logger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-02-02 00:00:00.000000000 Z
12
+ date: 2015-05-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: logstash-logger
@@ -94,6 +94,7 @@ files:
94
94
  - lib/applicaster/logger/railtie.rb
95
95
  - lib/applicaster/logger/version.rb
96
96
  - lib/applicaster/rack/request_uuid.rb
97
+ - lib/applicaster/sidekiq/middleware.rb
97
98
  homepage: ''
98
99
  licenses:
99
100
  - MIT
@@ -107,12 +108,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
107
108
  - - ! '>='
108
109
  - !ruby/object:Gem::Version
109
110
  version: '0'
111
+ segments:
112
+ - 0
113
+ hash: -4213316175037752544
110
114
  required_rubygems_version: !ruby/object:Gem::Requirement
111
115
  none: false
112
116
  requirements:
113
117
  - - ! '>='
114
118
  - !ruby/object:Gem::Version
115
119
  version: '0'
120
+ segments:
121
+ - 0
122
+ hash: -4213316175037752544
116
123
  requirements: []
117
124
  rubyforge_project:
118
125
  rubygems_version: 1.8.23