tpt-rails 1.6.0 → 1.6.4
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f941f7fce901677f655f42b56d87869e4dde19545e0547ba27d54ff1875604d
|
4
|
+
data.tar.gz: 79f31a854f4b9baea66dbf5d45e85332426e2061350e66cd3ad770195743e9d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d7132817e8d53a875af5d13f74b1b8c8dba725aaac5b8bad18615e15342c4ca5615002226c5d8cb250bc3dd8423da5b019c13013a9f82ca9b3b1afcbf48c6e15
|
7
|
+
data.tar.gz: 4aaf53023b9a035e0a13f65b380c2f9046718667d73f7663c935674c21165e77c2402f2f79c6bb15f7a1c9d5139988b25a84fb8b8bd7396998eada8090bc022c
|
data/README.md
CHANGED
@@ -68,25 +68,53 @@ See the documentation in [lib/tpt/rails.rb](lib/tpt/rails.rb).
|
|
68
68
|
|
69
69
|
`Tpt::Rails::EventBridgePublisher` is the class used to emit events to EventBridge (in batches). To use:
|
70
70
|
|
71
|
-
1.
|
72
|
-
(Failure to do so will result in an error.) When testing, you can pass in a local Statsd, eg:
|
71
|
+
1. Add EventBridge to your gemfile:
|
73
72
|
|
74
73
|
```ruby
|
75
|
-
|
76
|
-
Datadog::Statsd.new('localhost', 8125)
|
77
|
-
)
|
74
|
+
gem "aws-sdk-eventbridge", "~> 1.3.0"
|
78
75
|
```
|
79
76
|
|
80
|
-
2.
|
77
|
+
2. Instantiate with
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
Tpt::Rails::EventBridgePublisher.new
|
81
|
+
```
|
82
|
+
|
83
|
+
3. By default (#new with no arguments) EventBridgePublisher will try to use the following environment
|
81
84
|
variables: `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `TPT_EVENT_SOURCE`, `TPT_SHARED_EVENT_BUS_NAME`.
|
82
85
|
|
83
86
|
Additional configuration can be set as part of the constructor;
|
84
|
-
[see event_bridge_publisher.rb implementation](https://github.com/TeachersPayTeachers/tpt-rails/blob/
|
87
|
+
[see event_bridge_publisher.rb implementation](https://github.com/TeachersPayTeachers/tpt-rails/blob/master/app/models/tpt/rails/event_bridge_publisher.rb) for more details.
|
85
88
|
|
86
|
-
|
89
|
+
4. Create events by instantiating or inheriting from `Tpt::Rails::ApplicationEvent`. Event types are derived from the text before
|
87
90
|
"Event", so ResourceEvent has the event_type "Resource". Pass in an event or an array of events to EventBridgePublisher#publish
|
88
91
|
and they will be published immediately. Large arrays will be batched (default 10 per batch).
|
89
92
|
|
93
|
+
5. EventBridgePublisher uses Tpt::Rails.statsd as its datadog instance by default. If you want to change that behavior, call `Tpt::Rails::EventBridgePublisher.set_metrics`
|
94
|
+
with the statsd you want to use inside an initializer, as part of a `reloader.to_prepare` call:
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
Rails.application.reloader.to_prepare do
|
98
|
+
Tpt::Rails::EventBridgePublisher.set_metrics(
|
99
|
+
# your statsd here
|
100
|
+
)
|
101
|
+
end
|
102
|
+
```
|
103
|
+
|
104
|
+
You can also do this when testing by using the default test publisher (see below).
|
105
|
+
|
106
|
+
### EventBridge Testing
|
107
|
+
|
108
|
+
1. To create a ready-made test publisher, use
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
Tpt::Rails::EventBridgeTestHelper.publisher
|
112
|
+
```
|
113
|
+
|
114
|
+
This method accepts two parameters, batch_size and stub_responses. [See implementation docs](https://github.com/TeachersPayTeachers/tpt-rails/blob/master/app/helpers/tpt/rails/event_bridge_test_helper.rb) for more details.
|
115
|
+
|
116
|
+
2. The test publisher overwrites statsd that EventBridgePublisher uses to be a dummy statsd when called.
|
117
|
+
|
90
118
|
### Error reporting (e.g. Bugsnag/Rollbar)
|
91
119
|
|
92
120
|
If you configure `rollbar_access_token` & `rollbar_enabled`, or `bugsnag_api_key` then your project
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Tpt::Rails
|
2
|
+
module EventBridgeTestHelper
|
3
|
+
###
|
4
|
+
# @description: Instantiates an EventBridgePublisher ready for testing. AWS and DataDog info is faked. Note that this sets the
|
5
|
+
# metrics object for the class to be a stub. The AWS EventBridge gem will never actually be called as the gem's
|
6
|
+
# #put_events method is replaced with a stub.
|
7
|
+
# @param {batch_size}: The number of events to send per batch.
|
8
|
+
# @param {stub_responses}: Responses to stub, eg. { put_events: { failed_entry_count: 0, entries: [{ event_id: "1" }, { event_id: "2" }] } }
|
9
|
+
# @return {EventBridgePublisher}: The EventBridgePublisher object.
|
10
|
+
###
|
11
|
+
def self.publisher(batch_size: 10, stub_responses: {})
|
12
|
+
Tpt::Rails::EventBridgePublisher.set_metrics(
|
13
|
+
Datadog::Statsd.new('localhost', 8125)
|
14
|
+
)
|
15
|
+
|
16
|
+
Tpt::Rails::EventBridgePublisher.new(
|
17
|
+
event_source: ENV.fetch('TPT_EVENT_SOURCE', 'test-source'),
|
18
|
+
batch_size: batch_size,
|
19
|
+
client_options: {
|
20
|
+
access_key_id: '1234',
|
21
|
+
secret_access_key: 'abcd',
|
22
|
+
stub_responses: stub_responses
|
23
|
+
}
|
24
|
+
)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -1,5 +1,10 @@
|
|
1
1
|
module Tpt::Rails
|
2
2
|
class EventBridgePublisher
|
3
|
+
class NoEntriesInResponseData < StandardError; end
|
4
|
+
|
5
|
+
attr_accessor :batch_size
|
6
|
+
attr_reader :client
|
7
|
+
|
3
8
|
def initialize(
|
4
9
|
event_bus_name: default_event_bus_name,
|
5
10
|
event_source: default_event_source,
|
@@ -26,7 +31,10 @@ module Tpt::Rails
|
|
26
31
|
events
|
27
32
|
end
|
28
33
|
|
29
|
-
|
34
|
+
private
|
35
|
+
|
36
|
+
attr_reader :event_bus_name, :event_source
|
37
|
+
|
30
38
|
def publish_batch(events)
|
31
39
|
metrics.time('tpt.event_bridge.publish_batch') do
|
32
40
|
wrapped_events = wrap_events(events)
|
@@ -43,7 +51,7 @@ module Tpt::Rails
|
|
43
51
|
metrics.increment('tpt.event_bridge.publish_failure', by: events.length, tags: ["error_code:publish_batch_failure"])
|
44
52
|
|
45
53
|
# re-reraise the error for users to handle
|
46
|
-
raise
|
54
|
+
raise
|
47
55
|
end
|
48
56
|
|
49
57
|
def wrap_events(events)
|
@@ -59,6 +67,15 @@ module Tpt::Rails
|
|
59
67
|
end
|
60
68
|
|
61
69
|
def handle_response(res, events)
|
70
|
+
unless res&.data&.entries
|
71
|
+
logger.error({
|
72
|
+
msg: "No entries in the response data",
|
73
|
+
response: res,
|
74
|
+
events: events,
|
75
|
+
})
|
76
|
+
raise NoEntriesInResponseData, "No entries in the response data"
|
77
|
+
end
|
78
|
+
|
62
79
|
res.data.entries.each_with_index do |entry, index|
|
63
80
|
if entry.error_code.present?
|
64
81
|
metrics.increment('tpt.event_bridge.publish_failure', tags: ["error_code:#{entry.error_code}"])
|
@@ -77,17 +94,13 @@ module Tpt::Rails
|
|
77
94
|
res.data.entries
|
78
95
|
end
|
79
96
|
|
80
|
-
private
|
81
|
-
|
82
|
-
attr_reader :client, :event_bus_name, :event_source
|
83
|
-
|
84
97
|
def make_client(options)
|
85
98
|
all_options = {
|
86
|
-
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'),
|
87
|
-
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'),
|
99
|
+
access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID', nil),
|
100
|
+
secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY', nil),
|
88
101
|
region: ENV.fetch('AWS_REGION', 'us-east-1'),
|
89
|
-
http_open_timeout:
|
90
|
-
http_read_timeout:
|
102
|
+
http_open_timeout: 3, # default is 15
|
103
|
+
http_read_timeout: 15, # default is 60
|
91
104
|
}.merge(options)
|
92
105
|
|
93
106
|
Aws::EventBridge::Client.new(all_options)
|
@@ -125,8 +138,9 @@ module Tpt::Rails
|
|
125
138
|
end
|
126
139
|
|
127
140
|
def metrics
|
128
|
-
|
141
|
+
@@statsd ||= Tpt::Rails.statsd
|
129
142
|
|
143
|
+
raise "metrics object not set and no default found; use EventBridgePublisher#set_metrics" unless @@statsd
|
130
144
|
@@statsd
|
131
145
|
end
|
132
146
|
end
|
data/lib/tpt/rails/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tpt-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.6.
|
4
|
+
version: 1.6.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- TpT
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -114,6 +114,7 @@ files:
|
|
114
114
|
- app/controllers/tpt/rails/error_tests_controller.rb
|
115
115
|
- app/controllers/tpt/rails/health_checks_controller.rb
|
116
116
|
- app/helpers/tpt/rails/application_helper.rb
|
117
|
+
- app/helpers/tpt/rails/event_bridge_test_helper.rb
|
117
118
|
- app/jobs/tpt/rails/application_job.rb
|
118
119
|
- app/mailers/tpt/rails/application_mailer.rb
|
119
120
|
- app/models/tpt/rails/application_event.rb
|