sync_machine 1.1.1 → 1.2.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: 382d073ea7c61af6da6f38ca3a9c014e4913a7b5fd381b6a84e5f0403edacca8
4
- data.tar.gz: 67e6f4008630a67ae804f9530f9905fcdd78137e991369f1f43412386dd2074b
3
+ metadata.gz: 102155af0851cece6b27470411c50f11c945be750d08c6acb98093b011f65e85
4
+ data.tar.gz: 956031cb81b308903a5606d43c877eed613f4c3a3302b655592f1ce891197357
5
5
  SHA512:
6
- metadata.gz: eb69f9a240373ca62e4817c8f5c15c2dfafc14a1e46211dc254608cb031e423104e1ba2ee382e660543cc9414b63cdd49e2293502f5e6bc7434d8a706bbbf2bc
7
- data.tar.gz: bcd1474c68ebb58a04169bdd26ae940268cb289968db9855a58e81b15607fae69f364040503d04cda10e03f4d5de8d23a51f4ca1b8d20450878460224d2c42d3
6
+ metadata.gz: 0e0718d0e199a23ca685eccd530cffa3bdd49969925536d2881fea680126aa96a83a8ebae0ea871e93179670928df40b4564962c02d10641bf2b92d6159f571a
7
+ data.tar.gz: 52ac953901aed56ca103e58e7285fc15bb2acdec89a9ad9f88531b01e91d080aaed78892e9d835b6df6e55b7219c475681fd10708d79b1f94557b4a269ac633a
data/.rubocop.yml CHANGED
@@ -17,6 +17,9 @@ Layout/SpaceInsideHashLiteralBraces:
17
17
  Layout/TrailingBlankLines:
18
18
  Enabled: false
19
19
 
20
+ Lint/RescueException:
21
+ Enabled: false
22
+
20
23
  Metrics/BlockLength:
21
24
  Exclude:
22
25
  - 'spec/**/*'
@@ -10,7 +10,7 @@ module SyncMachine
10
10
 
11
11
  [:build, :check_publishable, :publish, :after_publish].each do |hook|
12
12
  define_singleton_method(hook) do |&block|
13
- hooks[hook] = block
13
+ hooks[hook] = Hook.new(hook.to_s, block)
14
14
  end
15
15
  end
16
16
 
@@ -35,5 +35,19 @@ module SyncMachine
35
35
  subject_class = sync_module.subject_class
36
36
  subject_class.find(subject_id)
37
37
  end
38
+
39
+ # Wrap build, check_publishable, etc blocks.
40
+ class Hook
41
+ def initialize(name, block)
42
+ @name = name
43
+ @block = block
44
+ end
45
+
46
+ def call(*args)
47
+ TracerAdapters.tracer_adapter.start_active_span(@name) do
48
+ @block.call(*args)
49
+ end
50
+ end
51
+ end
38
52
  end
39
53
  end
@@ -11,7 +11,7 @@ module SyncMachine
11
11
 
12
12
  def self.add_hook(meth, block)
13
13
  sync_module = SyncMachine.sync_module(self)
14
- hooks[meth] = Hook.new(sync_module, block)
14
+ hooks[meth] = Hook.new(meth.to_s, sync_module, block)
15
15
  end
16
16
  private_class_method :add_hook
17
17
 
@@ -52,20 +52,23 @@ module SyncMachine
52
52
 
53
53
  # Wrap a "subject_ids_from_*" block.
54
54
  class Hook
55
- def initialize(sync_module, block)
55
+ def initialize(name, sync_module, block)
56
+ @name = name.to_s
56
57
  @sync_module = sync_module
57
58
  @block = block
58
59
  end
59
60
 
60
61
  def call(record, changed_keys)
61
- raw_source_ids = if @block.arity == 2
62
- @block.call(record, changed_keys)
63
- else
64
- @block.call(record)
65
- end
66
- Array.wrap(raw_source_ids).map { |raw_source_id|
67
- @sync_module.orm_adapter.record_id_for_job(raw_source_id)
68
- }
62
+ TracerAdapters.tracer_adapter.start_active_span(@name) do
63
+ raw_source_ids = if @block.arity == 2
64
+ @block.call(record, changed_keys)
65
+ else
66
+ @block.call(record)
67
+ end
68
+ Array.wrap(raw_source_ids).map { |raw_source_id|
69
+ @sync_module.orm_adapter.record_id_for_job(raw_source_id)
70
+ }
71
+ end
69
72
  end
70
73
  end
71
74
  end
@@ -1,8 +1,8 @@
1
1
  begin
2
2
  require 'wisper/activerecord'
3
3
  rescue LoadError
4
- Kernel.abort(
5
- "Please install the wisper-activerecord gem when using SyncMachine with ActiveRecord."
4
+ SyncMachine.abort_with_installation_hint(
5
+ 'wisper-activerecord', 'ActiveRecord'
6
6
  )
7
7
  end
8
8
 
@@ -1,9 +1,7 @@
1
1
  begin
2
2
  require 'wisper/mongoid'
3
3
  rescue LoadError
4
- Kernel.abort(
5
- "Please install the fhwang-wisper-mongoid gem when using SyncMachine with Mongoid."
6
- )
4
+ SyncMachine.abort_with_installation_hint('fhwang-wisper-mongoid', 'Mongoid')
7
5
  end
8
6
 
9
7
  module SyncMachine
@@ -6,6 +6,21 @@ module SyncMachine
6
6
  require("sync_machine/orm_adapters/active_record_adapter")
7
7
  Module.const_defined?(:Mongoid) && \
8
8
  require("sync_machine/orm_adapters/mongoid_adapter")
9
+ if Module.const_defined?(:OpenTracing)
10
+ begin
11
+ require 'sidekiq-opentracing'
12
+ rescue LoadError
13
+ SyncMachine.abort_with_installation_hint(
14
+ 'sfx-sidekiq-opentracing', 'OpenTracing'
15
+ )
16
+ end
17
+ end
18
+ end
19
+
20
+ config.after_initialize do
21
+ if Module.const_defined?(:OpenTracing)
22
+ Sidekiq::Tracer.instrument(tracer: OpenTracing.global_tracer)
23
+ end
9
24
  end
10
25
  end
11
26
  end
@@ -0,0 +1,37 @@
1
+ module SyncMachine
2
+ module TracerAdapters
3
+ # Adapt OpenTracing for use with SyncMachine.
4
+ module OpenTracingAdapter
5
+ def self.start_active_span(name, &block)
6
+ scope = tracer.start_active_span(
7
+ name,
8
+ child_of: parent_span.context,
9
+ tags: tags
10
+ )
11
+ block.call
12
+ rescue Exception => exception
13
+ log_errors(scope.span, exception) if scope
14
+ raise exception
15
+ ensure
16
+ scope.close if scope
17
+ end
18
+
19
+ def self.log_errors(span, exception)
20
+ span.set_tag('error', true)
21
+ span.log_kv(event: 'error', :'error.object' => exception)
22
+ end
23
+
24
+ def self.parent_span
25
+ OpenTracing.active_span
26
+ end
27
+
28
+ def self.tags
29
+ { 'component' => 'SyncMachine', 'span.kind' => 'server' }
30
+ end
31
+
32
+ def self.tracer
33
+ OpenTracing.global_tracer
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,21 @@
1
+ require 'sync_machine/tracer_adapters/open_tracing_adapter'
2
+
3
+ module SyncMachine
4
+ # Adapt SyncMachine functionality to a specific distributed tracer.
5
+ module TracerAdapters
6
+ def self.tracer_adapter
7
+ if const_defined?(:OpenTracing)
8
+ OpenTracingAdapter
9
+ else
10
+ NullAdapter
11
+ end
12
+ end
13
+
14
+ # Do not log spans anywhere.
15
+ module NullAdapter
16
+ def self.start_active_span(_name)
17
+ yield
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,3 +1,3 @@
1
1
  module SyncMachine
2
- VERSION = "1.1.1".freeze
2
+ VERSION = "1.2.0".freeze
3
3
  end
data/lib/sync_machine.rb CHANGED
@@ -5,6 +5,7 @@ require "sync_machine/change_listener"
5
5
  require "sync_machine/ensure_publication"
6
6
  require "sync_machine/ensure_publication_worker"
7
7
  require "sync_machine/find_subjects_worker"
8
+ require "sync_machine/tracer_adapters"
8
9
  require "sync_machine/orm_adapters"
9
10
  require "sync_machine/version"
10
11
  require "sync_machine/railtie" if defined?(Rails::Railtie)
@@ -12,6 +13,12 @@ require "sync_machine/railtie" if defined?(Rails::Railtie)
12
13
  # A mini-framework for intelligently publishing complex model changes to an
13
14
  # external API..
14
15
  module SyncMachine
16
+ def self.abort_with_installation_hint(gem_name, dependency)
17
+ Kernel.abort(
18
+ "Please install the #{gem_name} gem when using SyncMachine with #{dependency}."
19
+ )
20
+ end
21
+
15
22
  # Force loading of all relevant classes. Should only be necessary when
16
23
  # running your application in a way that it defers loading constants, i.e.,
17
24
  # Rails' development or test mode.
data/sync_machine.gemspec CHANGED
@@ -44,6 +44,7 @@ Gem::Specification.new do |spec|
44
44
  spec.add_development_dependency "factory_bot"
45
45
  spec.add_development_dependency "fhwang-wisper-mongoid"
46
46
  spec.add_development_dependency "mongoid"
47
+ spec.add_development_dependency "opentracing"
47
48
  spec.add_development_dependency "rake", "~> 10.0"
48
49
  spec.add_development_dependency "rspec", "~> 3.0"
49
50
  spec.add_development_dependency "rspec_junit_formatter"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sync_machine
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Francis Hwang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-25 00:00:00.000000000 Z
11
+ date: 2019-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -122,6 +122,20 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: opentracing
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: rake
127
141
  requirement: !ruby/object:Gem::Requirement
@@ -236,6 +250,8 @@ files:
236
250
  - lib/sync_machine/orm_adapters/active_record_adapter.rb
237
251
  - lib/sync_machine/orm_adapters/mongoid_adapter.rb
238
252
  - lib/sync_machine/railtie.rb
253
+ - lib/sync_machine/tracer_adapters.rb
254
+ - lib/sync_machine/tracer_adapters/open_tracing_adapter.rb
239
255
  - lib/sync_machine/version.rb
240
256
  - sync_machine.gemspec
241
257
  homepage: https://github.com/fhwang/sync_machine