hanami-events-cloud_pubsub 2.6.2 → 2.9.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: a98d3008033cab806ac81b85a905da7b99b4004d05aa2f66ebace18393e06661
4
- data.tar.gz: 0b5f35229ed7284a4cf31fe019b7332e95c33157c1c0c4910340059f8babc152
3
+ metadata.gz: a5ffe95cabc82141bd06ee9514ad0af66aaba9c5ebc719190e7cc9efba855274
4
+ data.tar.gz: 119b219d3294f50b37a13daa36a4a55edf0d85f5ac14e82676fee3cad62f135a
5
5
  SHA512:
6
- metadata.gz: 204b4a858ddc8dada4ab5a7ccf6959dd9a5e25e579cfc96c562a20f7ca8138201f32ef8568859a27e51071ead172272acfa6c0b5090fc373946d25aa353d9c9d
7
- data.tar.gz: 5f0d93acec60125c75b4c0b489cccd9fe282edcfc0918a850d8cbf9b828f700a0ba5061c4b6d68b96a1fa8c5998bec0597043ae11c70c05f755c7cbfd2c44851
6
+ metadata.gz: a3475491db3dab17ab2fd423c056dca407977321223bd457903b3773bdbd7ef9080b2446e70305e8e64f136d6bd434c157b90ae72bd48f91bd388cb060005ee3
7
+ data.tar.gz: 93bd30e26d6a85281f21dfd394f6aa7ea41a340459426f8b280f798e56b1fb3dc857390e6384e8d4605a286cbd313174eab2207e971e0c02374663cd91180744
@@ -1 +1 @@
1
- ruby 2.6.6
1
+ ruby 2.7.1
@@ -9,8 +9,9 @@ env:
9
9
  - CC_TEST_REPORTER_ID=7ab310950456b913cd7e947f6671e3e9c16822f33bd8c572dadcc14593fe4fb5
10
10
  - COVERAGE=true
11
11
  rvm:
12
- - 2.4
12
+ - 2.5
13
13
  - 2.6
14
+ - 2.7
14
15
  before_install:
15
16
  - gem install bundler -v 1.16.1
16
17
  before_script:
data/Gemfile CHANGED
@@ -11,3 +11,5 @@ gem 'hanami-events', github: 'hanami/events'
11
11
  gem 'pry'
12
12
  gem 'rubocop'
13
13
  gem 'simplecov', require: false
14
+
15
+ gem 'request_id', '~> 0.4.3'
@@ -8,9 +8,9 @@ GIT
8
8
  PATH
9
9
  remote: .
10
10
  specs:
11
- hanami-events-cloud_pubsub (2.6.2)
11
+ hanami-events-cloud_pubsub (2.9.0)
12
12
  dry-configurable (>= 0.8)
13
- google-cloud-pubsub (>= 0.38.1, < 1.4)
13
+ google-cloud-pubsub (>= 0.38.1, < 1.7)
14
14
  hanami-cli (~> 0.2)
15
15
  hanami-events (~> 0.2.0)
16
16
  rack
@@ -20,12 +20,12 @@ GEM
20
20
  specs:
21
21
  addressable (2.7.0)
22
22
  public_suffix (>= 2.0.2, < 5.0)
23
- ast (2.4.0)
24
- coderay (1.1.2)
23
+ ast (2.4.1)
24
+ coderay (1.1.3)
25
25
  concurrent-ruby (1.1.6)
26
- diff-lcs (1.3)
26
+ diff-lcs (1.4.4)
27
27
  docile (1.3.2)
28
- dry-configurable (0.11.5)
28
+ dry-configurable (0.11.6)
29
29
  concurrent-ruby (~> 1.0)
30
30
  dry-core (~> 0.4, >= 0.4.7)
31
31
  dry-equalizer (~> 0.2)
@@ -40,10 +40,10 @@ GEM
40
40
  google-cloud-core (1.5.0)
41
41
  google-cloud-env (~> 1.0)
42
42
  google-cloud-errors (~> 1.0)
43
- google-cloud-env (1.3.1)
43
+ google-cloud-env (1.3.3)
44
44
  faraday (>= 0.17.3, < 2.0)
45
- google-cloud-errors (1.0.0)
46
- google-cloud-pubsub (1.3.1)
45
+ google-cloud-errors (1.0.1)
46
+ google-cloud-pubsub (1.6.1)
47
47
  concurrent-ruby (~> 1.1)
48
48
  google-cloud-core (~> 1.2)
49
49
  google-gax (~> 1.8)
@@ -56,22 +56,22 @@ GEM
56
56
  googleauth (~> 0.9)
57
57
  grpc (~> 1.24)
58
58
  rly (~> 0.2.3)
59
- google-protobuf (3.11.4)
59
+ google-protobuf (3.12.4)
60
60
  googleapis-common-protos (1.3.10)
61
61
  google-protobuf (~> 3.11)
62
62
  googleapis-common-protos-types (>= 1.0.5, < 2.0)
63
63
  grpc (~> 1.27)
64
64
  googleapis-common-protos-types (1.0.5)
65
65
  google-protobuf (~> 3.11)
66
- googleauth (0.12.0)
66
+ googleauth (0.13.1)
67
67
  faraday (>= 0.17.3, < 2.0)
68
68
  jwt (>= 1.4, < 3.0)
69
69
  memoist (~> 0.16)
70
70
  multi_json (~> 1.11)
71
71
  os (>= 0.9, < 2.0)
72
72
  signet (~> 0.14)
73
- grpc (1.28.0)
74
- google-protobuf (~> 3.11)
73
+ grpc (1.30.2)
74
+ google-protobuf (~> 3.12)
75
75
  googleapis-common-protos-types (~> 1.0)
76
76
  grpc-google-iam-v1 (0.6.10)
77
77
  google-protobuf (~> 3.11)
@@ -83,47 +83,51 @@ GEM
83
83
  hanami-utils (1.3.6)
84
84
  concurrent-ruby (~> 1.0)
85
85
  transproc (~> 1.0)
86
- jaro_winkler (1.5.4)
87
86
  jwt (2.2.1)
88
87
  memoist (0.16.2)
89
88
  method_source (1.0.0)
90
- multi_json (1.14.1)
89
+ multi_json (1.15.0)
91
90
  multipart-post (2.1.1)
92
- os (1.1.0)
93
- parallel (1.19.1)
94
- parser (2.7.1.1)
95
- ast (~> 2.4.0)
96
- prometheus-client (2.0.0)
91
+ os (1.1.1)
92
+ parallel (1.19.2)
93
+ parser (2.7.1.4)
94
+ ast (~> 2.4.1)
95
+ prometheus-client (2.1.0)
97
96
  pry (0.13.1)
98
97
  coderay (~> 1.1)
99
98
  method_source (~> 1.0)
100
- public_suffix (4.0.4)
101
- rack (2.2.2)
99
+ public_suffix (4.0.5)
100
+ rack (2.2.3)
102
101
  rainbow (3.0.0)
103
102
  rake (13.0.1)
103
+ regexp_parser (1.7.1)
104
+ request_id (0.4.3)
104
105
  rexml (3.2.4)
105
106
  rly (0.2.3)
106
107
  rspec (3.9.0)
107
108
  rspec-core (~> 3.9.0)
108
109
  rspec-expectations (~> 3.9.0)
109
110
  rspec-mocks (~> 3.9.0)
110
- rspec-core (3.9.1)
111
- rspec-support (~> 3.9.1)
112
- rspec-expectations (3.9.1)
111
+ rspec-core (3.9.2)
112
+ rspec-support (~> 3.9.3)
113
+ rspec-expectations (3.9.2)
113
114
  diff-lcs (>= 1.2.0, < 2.0)
114
115
  rspec-support (~> 3.9.0)
115
116
  rspec-mocks (3.9.1)
116
117
  diff-lcs (>= 1.2.0, < 2.0)
117
118
  rspec-support (~> 3.9.0)
118
- rspec-support (3.9.2)
119
- rubocop (0.82.0)
120
- jaro_winkler (~> 1.5.1)
119
+ rspec-support (3.9.3)
120
+ rubocop (0.89.0)
121
121
  parallel (~> 1.10)
122
- parser (>= 2.7.0.1)
122
+ parser (>= 2.7.1.1)
123
123
  rainbow (>= 2.2.2, < 4.0)
124
+ regexp_parser (>= 1.7)
124
125
  rexml
126
+ rubocop-ast (>= 0.1.0, < 1.0)
125
127
  ruby-progressbar (~> 1.7)
126
128
  unicode-display_width (>= 1.4.0, < 2.0)
129
+ rubocop-ast (0.3.0)
130
+ parser (>= 2.7.1.4)
127
131
  ruby-progressbar (1.10.1)
128
132
  signet (0.14.0)
129
133
  addressable (~> 2.3)
@@ -147,6 +151,7 @@ DEPENDENCIES
147
151
  prometheus-client
148
152
  pry
149
153
  rake (~> 13.0)
154
+ request_id (~> 0.4.3)
150
155
  rspec (~> 3.0)
151
156
  rubocop
152
157
  simplecov
data/README.md CHANGED
@@ -106,6 +106,19 @@ Then, run the worker process:
106
106
  bundle exec cloudpubsub run
107
107
  ```
108
108
 
109
+ # `RequestId` Integration
110
+
111
+ To gem has an integration with [request_id](https://github.com/remind101/request_id), so you can trace interactions between various components in your app. To make it work properly, make sure the `request_id` is added to your Gemfile, then require the middleware:
112
+
113
+ ```ruby
114
+ require 'hanami/events/cloud_pubsub/middleware/request_id'
115
+
116
+ Hanami::Events::CloudPubsub.configure do |config|
117
+ # ...
118
+ end
119
+ ```
120
+
121
+
109
122
  # Prometheus Integration
110
123
 
111
124
  If you have the `prometheus-client` gem installed, a `/metrics` endpoint will
@@ -23,7 +23,7 @@ Gem::Specification.new do |spec|
23
23
  spec.require_paths = ['lib']
24
24
 
25
25
  spec.add_dependency 'dry-configurable', '>= 0.8'
26
- spec.add_dependency 'google-cloud-pubsub', '>= 0.38.1', '< 1.4'
26
+ spec.add_dependency 'google-cloud-pubsub', '>= 0.38.1', '< 1.7'
27
27
  spec.add_dependency 'hanami-cli', '~> 0.2'
28
28
  spec.add_dependency 'hanami-events', '~> 0.2.0'
29
29
  spec.add_dependency 'rack'
@@ -11,7 +11,7 @@ module Hanami
11
11
  #
12
12
  # @api private
13
13
  class CloudPubsub
14
- attr_reader :subscribers, :listeners, :topic_registry
14
+ attr_reader :subscribers, :listeners, :topic_registry, :middleware
15
15
 
16
16
  def initialize(params)
17
17
  @pubsub = params[:pubsub]
@@ -22,20 +22,23 @@ module Hanami
22
22
  @serializer_type = params.fetch(:serializer, :json).to_sym
23
23
  @topic_registry = {}
24
24
  @mutex = Mutex.new
25
+ @middleware = ::Hanami::Events::CloudPubsub.config.client_middleware
25
26
  end
26
27
 
27
28
  # Brodcasts event to all subscribes
28
29
  #
29
30
  # @param event [Symbol, String] the event name
30
31
  # @param payload [Hash] the event data
31
- def broadcast(name, payload)
32
+ def broadcast(name, payload, **message_opts)
32
33
  event_name = namespaced(name)
33
34
  topic = topic_for event_name
34
35
  payload = serializer.serialize(payload)
35
36
  attributes = { id: SecureRandom.uuid, event_name: event_name }
36
37
 
37
- topic.publish_async(payload, attributes) do |result|
38
- logger.info "Published event #{result.inspect}"
38
+ middleware.invoke(payload, **attributes, **message_opts) do |*args|
39
+ topic.publish_async(*args) do |result|
40
+ logger.info "Published event #{result.inspect}"
41
+ end
39
42
  end
40
43
  end
41
44
 
@@ -97,11 +100,15 @@ module Hanami
97
100
 
98
101
  # rubocop:disable Metrics/LineLength
99
102
  def topic_for(name)
100
- @topic_registry[name.to_s] ||= begin
101
- @pubsub.find_topic(name) ||
102
- (Hanami::Events::CloudPubsub.auto_create_topics && @pubsub.create_topic(name)) ||
103
- raise(::Hanami::Events::CloudPubsub::Errors::TopicNotFoundError, "no topic named: #{name}")
104
- end
103
+ return @topic_registry[name.to_s] if @topic_registry[name.to_s]
104
+
105
+ topic = @pubsub.find_topic(name) ||
106
+ (Hanami::Events::CloudPubsub.auto_create_topics && @pubsub.create_topic(name)) ||
107
+ raise(::Hanami::Events::CloudPubsub::Errors::TopicNotFoundError, "no topic named: #{name}")
108
+
109
+ topic.enable_message_ordering!
110
+
111
+ @topic_registry[name.to_s] = topic
105
112
  end
106
113
  # rubocop:enable Metrics/LineLength
107
114
 
@@ -72,6 +72,21 @@ module Hanami
72
72
 
73
73
  setting :middleware, middleware_stack
74
74
 
75
+ client_middleware_stack = Middleware::Stack.new
76
+
77
+ begin
78
+ require 'request_id'
79
+ require 'hanami/events/cloud_pubsub/middleware/client/request_id'
80
+ require 'hanami/events/cloud_pubsub/middleware/request_id'
81
+
82
+ client_middleware_stack.prepend(Middleware::Client::RequestId.new)
83
+ middleware_stack.prepend(Middleware::RequestId.new)
84
+ rescue LoadError
85
+ # ok
86
+ end
87
+
88
+ setting :client_middleware, client_middleware_stack
89
+
75
90
  setting :on_shutdown_handlers, [], reader: true
76
91
 
77
92
  def self.finalize_settings!
@@ -36,7 +36,7 @@ module Hanami
36
36
  def register
37
37
  subscription = subscription_for(subscriber_id)
38
38
 
39
- listener = subscription.listen(subscriber_opts) do |message|
39
+ listener = subscription.listen(**subscriber_opts) do |message|
40
40
  handle_message(message)
41
41
  end
42
42
 
@@ -14,10 +14,10 @@ module Hanami
14
14
  @max_attempts = max_attempts
15
15
  end
16
16
 
17
- def call(message, args = {})
17
+ def call(message, **args)
18
18
  succeeded = false
19
19
  failed = false
20
- yield(args)
20
+ yield(**args)
21
21
  succeeded = true
22
22
  rescue StandardError => e
23
23
  failed = true
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hanami
4
+ module Events
5
+ module CloudPubsub
6
+ module Middleware
7
+ module Client
8
+ # Broadcasts events with current request id
9
+ class RequestId
10
+ def call(payload, attributes = {})
11
+ attributes.merge!(
12
+ request_id: ::RequestId.request_id || SecureRandom.uuid
13
+ )
14
+
15
+ yield(payload, **attributes)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -10,9 +10,9 @@ module Hanami
10
10
  @logger = logger
11
11
  end
12
12
 
13
- def call(msg, opts = {})
13
+ def call(msg, **opts)
14
14
  started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
15
- yield(opts)
15
+ yield(**opts)
16
16
  ensure
17
17
  ended_at = Process.clock_gettime(Process::CLOCK_MONOTONIC)
18
18
  seconds = ended_at - started_at
@@ -14,16 +14,26 @@ module Hanami
14
14
  @events_counter = prometheus.counter(
15
15
  :received_pubsub_events,
16
16
  docstring: 'A counter of received pubsub events',
17
- labels: %i[event_name subscription]
17
+ labels: %i[event_name subscription status]
18
18
  )
19
19
  end
20
20
 
21
- def call(msg, opts = {})
22
- yield(opts)
21
+ def call(msg, **opts)
22
+ status = :running
23
+
24
+ begin
25
+ ret = yield(**opts)
26
+ status = :succeeded
27
+ ret
28
+ rescue StandardError
29
+ status = :failed
30
+ raise
31
+ end
23
32
  ensure
24
33
  sub = msg.subscription.subscriber.subscription_name
25
34
  event_name = msg.attributes['event_name']
26
- events_counter.increment(labels: { event_name: event_name, subscription: sub })
35
+ labels = { event_name: event_name, subscription: sub, status: status }
36
+ events_counter.increment(labels: labels)
27
37
  end
28
38
  end
29
39
  end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'request_id'
5
+ rescue LoadError
6
+ abort 'Please add "request_id" to your Gemfile to use this middleware'
7
+ end
8
+
9
+ module Hanami
10
+ module Events
11
+ module CloudPubsub
12
+ module Middleware
13
+ # Middleware used for logging useful information about an event
14
+ class RequestId
15
+ def call(msg, **opts)
16
+ setup_request_id(msg)
17
+ yield(**opts)
18
+ ensure
19
+ clear_request_id(msg)
20
+ end
21
+
22
+ private
23
+
24
+ def setup_request_id(msg)
25
+ id = msg.attributes['request_id'] || msg.attributes[:request_id]
26
+
27
+ ::RequestId.request_id = id || SecureRandom.uuid
28
+ rescue StandardError => e
29
+ warn "Could not set request_id (#{e.message})"
30
+ end
31
+
32
+ def clear_request_id(_msg)
33
+ ::RequestId.request_id = nil
34
+ rescue StandardError => e
35
+ warn "Could not clear request_id (#{e.message})"
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -40,14 +40,14 @@ module Hanami
40
40
  @entries.shift
41
41
  end
42
42
 
43
- def invoke(message)
43
+ def invoke(*args)
44
44
  stack = entries.dup
45
45
 
46
46
  traverse_stack = proc do |**opts|
47
47
  if stack.empty?
48
- yield(message)
48
+ yield(*args)
49
49
  else
50
- stack.shift.call(message, **opts, &traverse_stack)
50
+ stack.shift.call(*args, **opts, &traverse_stack)
51
51
  end
52
52
  end
53
53
 
@@ -3,7 +3,7 @@
3
3
  module Hanami
4
4
  module Events
5
5
  module CloudPubsub
6
- VERSION = '2.6.2'
6
+ VERSION = '2.9.0'
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanami-events-cloud_pubsub
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.2
4
+ version: 2.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ian Ker-Seymer
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-28 00:00:00.000000000 Z
11
+ date: 2020-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-configurable
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: 0.38.1
34
34
  - - "<"
35
35
  - !ruby/object:Gem::Version
36
- version: '1.4'
36
+ version: '1.7'
37
37
  type: :runtime
38
38
  prerelease: false
39
39
  version_requirements: !ruby/object:Gem::Requirement
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 0.38.1
44
44
  - - "<"
45
45
  - !ruby/object:Gem::Version
46
- version: '1.4'
46
+ version: '1.7'
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: hanami-cli
49
49
  requirement: !ruby/object:Gem::Requirement
@@ -179,8 +179,10 @@ files:
179
179
  - lib/hanami/events/cloud_pubsub/integration.rb
180
180
  - lib/hanami/events/cloud_pubsub/listener.rb
181
181
  - lib/hanami/events/cloud_pubsub/middleware/auto_retry.rb
182
+ - lib/hanami/events/cloud_pubsub/middleware/client/request_id.rb
182
183
  - lib/hanami/events/cloud_pubsub/middleware/logging.rb
183
184
  - lib/hanami/events/cloud_pubsub/middleware/prometheus.rb
185
+ - lib/hanami/events/cloud_pubsub/middleware/request_id.rb
184
186
  - lib/hanami/events/cloud_pubsub/middleware/stack.rb
185
187
  - lib/hanami/events/cloud_pubsub/register.rb
186
188
  - lib/hanami/events/cloud_pubsub/runner.rb
@@ -208,7 +210,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
208
210
  - !ruby/object:Gem::Version
209
211
  version: '0'
210
212
  requirements: []
211
- rubygems_version: 3.0.3
213
+ rubygems_version: 3.1.2
212
214
  signing_key:
213
215
  specification_version: 4
214
216
  summary: Google Cloud Pub/Sub adapter for the hanami-events gem