tpt-rails 1.6.2 → 1.6.4

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
  SHA256:
3
- metadata.gz: 7a7e431aaf9c9d3546eb414ab15564aba43048d9a3b64104671a1dd589bdc61f
4
- data.tar.gz: 4ecdc856396d0d987e8e847694534b620b5140a442714f5f6e7e236f2625f869
3
+ metadata.gz: 0f941f7fce901677f655f42b56d87869e4dde19545e0547ba27d54ff1875604d
4
+ data.tar.gz: 79f31a854f4b9baea66dbf5d45e85332426e2061350e66cd3ad770195743e9d8
5
5
  SHA512:
6
- metadata.gz: 71faac471d35a3e67356427581afdac4f44fcb66c5840b9a8d06571b15e4e47672e36c252352787be31bd5da35e53d69eff3901c79a6c5685abaa49b0c575057
7
- data.tar.gz: 68670905648c6cb7456768e0805e04b4c904cd808ada80db3961a73ad35e961f0c51ed597c06b25d2b307fca91822b33998caead07b9b7bf998837d01a65f933
6
+ metadata.gz: d7132817e8d53a875af5d13f74b1b8c8dba725aaac5b8bad18615e15342c4ca5615002226c5d8cb250bc3dd8423da5b019c13013a9f82ca9b3b1afcbf48c6e15
7
+ data.tar.gz: 4aaf53023b9a035e0a13f65b380c2f9046718667d73f7663c935674c21165e77c2402f2f79c6bb15f7a1c9d5139988b25a84fb8b8bd7396998eada8090bc022c
data/README.md CHANGED
@@ -74,25 +74,47 @@ See the documentation in [lib/tpt/rails.rb](lib/tpt/rails.rb).
74
74
  gem "aws-sdk-eventbridge", "~> 1.3.0"
75
75
  ```
76
76
 
77
- 2. In an initializer, pass in the `Datadog::Statsd` object you're using to the class method ::set_metrics.
78
- (Failure to do so will result in an error.) When testing, you can pass in a local Statsd, eg:
77
+ 2. Instantiate with
79
78
 
80
79
  ```ruby
81
- Tpt::Rails::EventBridgePublisher.set_metrics(
82
- Datadog::Statsd.new('localhost', 8125)
83
- )
80
+ Tpt::Rails::EventBridgePublisher.new
84
81
  ```
85
82
 
86
83
  3. By default (#new with no arguments) EventBridgePublisher will try to use the following environment
87
84
  variables: `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `TPT_EVENT_SOURCE`, `TPT_SHARED_EVENT_BUS_NAME`.
88
85
 
89
86
  Additional configuration can be set as part of the constructor;
90
- [see event_bridge_publisher.rb implementation](https://github.com/TeachersPayTeachers/tpt-rails/blob/ad768d3bac0c8bd28e60a48a89aed36f01c4e17b/app/models/tpt/rails/event_bridge_publisher.rb) for more details.
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.
91
88
 
92
89
  4. Create events by instantiating or inheriting from `Tpt::Rails::ApplicationEvent`. Event types are derived from the text before
93
90
  "Event", so ResourceEvent has the event_type "Resource". Pass in an event or an array of events to EventBridgePublisher#publish
94
91
  and they will be published immediately. Large arrays will be batched (default 10 per batch).
95
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
+
96
118
  ### Error reporting (e.g. Bugsnag/Rollbar)
97
119
 
98
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
@@ -3,6 +3,7 @@ module Tpt::Rails
3
3
  class NoEntriesInResponseData < StandardError; end
4
4
 
5
5
  attr_accessor :batch_size
6
+ attr_reader :client
6
7
 
7
8
  def initialize(
8
9
  event_bus_name: default_event_bus_name,
@@ -32,7 +33,7 @@ module Tpt::Rails
32
33
 
33
34
  private
34
35
 
35
- attr_reader :client, :event_bus_name, :event_source
36
+ attr_reader :event_bus_name, :event_source
36
37
 
37
38
  def publish_batch(events)
38
39
  metrics.time('tpt.event_bridge.publish_batch') do
@@ -137,8 +138,9 @@ module Tpt::Rails
137
138
  end
138
139
 
139
140
  def metrics
140
- raise "metrics object not set; use EventBridgePublisher#set_metrics" unless @@statsd
141
+ @@statsd ||= Tpt::Rails.statsd
141
142
 
143
+ raise "metrics object not set and no default found; use EventBridgePublisher#set_metrics" unless @@statsd
142
144
  @@statsd
143
145
  end
144
146
  end
@@ -1,6 +1,6 @@
1
1
  module Tpt
2
2
  module Rails
3
3
  # Do not change this manually. Our tooling will do it automatically.
4
- VERSION = '1.6.2'
4
+ VERSION = '1.6.4'
5
5
  end
6
6
  end
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.2
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: 2022-01-05 00:00:00.000000000 Z
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