currentsh 0.2.0 → 0.3.0

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: 7ecb923ee99155d461618165552acea0938a7dc4
4
- data.tar.gz: 4976337e606613ec578fb0a6d7ae05244af3ec87
3
+ metadata.gz: c4f702d9774084c47f2c4cc5c03d8c30adb4a1f3
4
+ data.tar.gz: bd38902531b45780da0fa1b171e752be475418c9
5
5
  SHA512:
6
- metadata.gz: 2d14bf9642aaf7113ec28bf803e27b26adffb46a2c1e20cf41279ef87dd45da717ede08b40a5bc553a0385ba63b3d9431247ddcee01220eabd8b628bf18bf34c
7
- data.tar.gz: 915a81449601f1da462c7ef100a11eaa20f2123dfc47a56a39d130e6f1a31c28e2a4e6f402f194aa9fd13821812b911f36444fac322f783e5e50cfee0651ca5e
6
+ metadata.gz: 5567fbc614ca388e8bb2378044a72a02cdd5c99e0c27c8183133e859378aabadac9266a82cb7fde71a5656ac549a78000e101e99da7cec8cf9d295b7a72a2850
7
+ data.tar.gz: 4520830c7bed4048a089cbb38c913fe10eacb3c7adf931119454a746b53d7b07afc9accea9fc09a7368ed43ce033a158e56ceb6e4121f9b9f33d628d9e440bc0
data/lib/currentsh.rb CHANGED
@@ -11,3 +11,4 @@ module Currentsh
11
11
  end
12
12
 
13
13
  require 'currentsh/railtie' if defined?(Rails)
14
+ require 'currentsh/sidekiq' if defined?(Sidekiq)
@@ -4,8 +4,39 @@ module Currentsh
4
4
  @io = io
5
5
  end
6
6
 
7
+ def format(data)
8
+ "@current: #{data.to_json}\n"
9
+ end
10
+
7
11
  def <<(data)
8
- @io.puts "@current: #{data.to_json}"
12
+ @io << format(data)
13
+ end
14
+
15
+ def self.with_context(msg)
16
+ Thread.current[:currrentsh_sidekiq_context] ||= []
17
+ Thread.current[:currrentsh_sidekiq_context] << msg
18
+ yield
19
+ ensure
20
+ Thread.current[:currrentsh_sidekiq_context].pop
21
+ end
22
+
23
+ def call(severity, time, program_name, message)
24
+ data = {
25
+ time: time,
26
+ process: ::Process.pid,
27
+ thread: Thread.current.object_id.to_s(36),
28
+ message: message
29
+ }
30
+
31
+ if ctx = context
32
+ data.merge! ctx
33
+ end
34
+
35
+ format data
36
+ end
37
+
38
+ def context
39
+ Thread.current[:currrentsh_sidekiq_context]
9
40
  end
10
41
  end
11
42
  end
@@ -5,9 +5,16 @@ module Currentsh
5
5
  def initialize
6
6
  @output = LogOutput.new
7
7
  @trackers = []
8
+ @initializers = []
9
+ end
10
+
11
+ def add_initializer(&b)
12
+ @initializers << b
8
13
  end
9
14
 
10
15
  def track_rails!(app)
16
+ @initializers.each { |i| i.call }
17
+
11
18
  @output << { :type => "boot", :application => app.class.name }
12
19
  rails = RailsTracker.new(@output)
13
20
  rails.register
@@ -2,11 +2,21 @@ require 'rails/railtie'
2
2
 
3
3
  module Currentsh
4
4
  class Railtie < Rails::Railtie
5
+ DYNO_RUN = /^run\./
6
+
5
7
  config.currentsh = ActiveSupport::OrderedOptions.new
6
8
  config.currentsh.enabled = false
9
+ config.currentsh.heroku_detect = true
7
10
 
8
11
  config.after_initialize do |app|
9
- Currentsh::PROCESS.track_rails!(app) if app.config.currentsh.enabled
12
+ next unless app.config.currentsh.enabled
13
+
14
+ if config.currentsh.heroku_detect
15
+ # Disable logging in a heroku run session
16
+ next if dyno = ENV['DYNO'] and DYNO_RUN.match(dyno)
17
+ end
18
+
19
+ Currentsh::PROCESS.track_rails!(app)
10
20
  end
11
21
  end
12
22
  end
@@ -0,0 +1,105 @@
1
+ module Currentsh
2
+ class Sidekiq
3
+ def call(worker, msg, queue)
4
+ klass = msg['wrapped'] || worker.class.to_s
5
+
6
+ jid = msg['jid']
7
+ context = {
8
+ :class => klass,
9
+ :jit => jid
10
+ }
11
+
12
+ if bid = msg['bid']
13
+ context[:bid] = bid
14
+ end
15
+
16
+ LogOutput.with_context(context) do
17
+ begin
18
+ ts = start context
19
+
20
+ yield
21
+ rescue StandardError => ex
22
+ case ex
23
+ when Interrupt, SystemExit, SignalException
24
+ raise ex
25
+ else
26
+ error context, ex, ts
27
+ raise ex
28
+ end
29
+ else
30
+ stop context, ts
31
+ end
32
+ end
33
+ end
34
+
35
+ def start(context)
36
+ time = Time.now
37
+
38
+ data = {
39
+ time: time,
40
+ process: ::Process.pid,
41
+ thread: Thread.current.object_id.to_s(36),
42
+ event: "start",
43
+ }.merge!(context)
44
+
45
+ $stdout.puts "@current: #{JSON.generate(data)}"
46
+
47
+ time
48
+ end
49
+
50
+ def error(context, error, start)
51
+ data = {
52
+ time: Time.now,
53
+ process: ::Process.pid,
54
+ thread: Thread.current.object_id.to_s(36),
55
+ event: "error",
56
+ error: error.to_s,
57
+ elapse: (Time.now - start)
58
+ }.merge!(context)
59
+
60
+ $stdout.puts "@current: #{JSON.generate(data)}"
61
+ end
62
+
63
+ def stop(context, start)
64
+ data = {
65
+ time: Time.now,
66
+ process: ::Process.pid,
67
+ thread: Thread.current.object_id.to_s(36),
68
+ event: "stop",
69
+ elapse: (Time.now - start)
70
+ }.merge!(context)
71
+
72
+ $stdout.puts "@current: #{JSON.generate(data)}"
73
+ end
74
+
75
+ S = ::Sidekiq
76
+
77
+ def self.configure
78
+ S.configure_server do |config|
79
+ config.server_middleware do |chain|
80
+ chain.insert_before S::Middleware::Server::Logging, self
81
+ chain.remove S::Middleware::Server::Logging
82
+ end
83
+ end
84
+
85
+ oldlogger = S::Logging.logger
86
+
87
+ logger = Logger.new($stdout)
88
+ logger.level = Logger::INFO
89
+ logger.formatter = LogOutput.new
90
+
91
+ S::Logging.logger = logger
92
+ end
93
+ end
94
+ end
95
+
96
+ # If using Sidekiq with Rails (likely) then the Railtie will sort this out
97
+
98
+ if defined?(Rails)
99
+ Currentsh::PROCESS.add_initializer do
100
+ Currentsh::Sidekiq.configure
101
+ end
102
+ else
103
+ Currentsh::Sidekiq.configure
104
+ end
105
+
@@ -1,3 +1,3 @@
1
1
  module Currentsh
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: currentsh
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Evan Phoenix
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-17 00:00:00.000000000 Z
11
+ date: 2015-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -57,6 +57,7 @@ files:
57
57
  - lib/currentsh/process.rb
58
58
  - lib/currentsh/rails.rb
59
59
  - lib/currentsh/railtie.rb
60
+ - lib/currentsh/sidekiq.rb
60
61
  - lib/currentsh/version.rb
61
62
  homepage: http://github.com/vektra/currentsh
62
63
  licenses: []