apptrail-application-events-sdk 0.0.4 → 0.0.5

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: cd6577d90aff851fc6436c50a0b30264b3c453c4290f2f0a336a8e0bf3fa7dc8
4
- data.tar.gz: 6dc2dc70b4f05cf188a755925f1e50e08e277eeacc49efeb7d82aefb8cc4359d
3
+ metadata.gz: 033ced91d5d347d03c478bfd99656e901920d6eadb8823b19fd0b5751ac3b736
4
+ data.tar.gz: 38f63305471ef6aa3ea25392df9c5b72676b3410ab4d6ec5055c8d1597f655e2
5
5
  SHA512:
6
- metadata.gz: bc49132efe676aa804cdc41f4d4da3a26bd565d107d69342f341ead0bb9f049a9a5b20cdcc6797892294f40fcc1db24383890a1c5d73ef901f891d2e23d6d142
7
- data.tar.gz: f8abe7ba65ff9d5a3e080a5a7cc230c6a19247340a77fcfd8d51e7034ba239b343308f61c5360b2fdbd1504e8858e70241fdc5dfdf8d449d5b3d60009750e060
6
+ metadata.gz: efe5076ba194024da52d8b9305fcaf7319717243eab7f2bba2320cfdb31eb353b194d4ee7fd350295e43520dfaedfa8cf5b4c881a3c2aecccc602527e82e31e9
7
+ data.tar.gz: 288c4f04e2508aaa7636d685dc14c873382f597ab96a7a83db99f0e2097580671cff16ff6c7bf7ade18e457757d0145c7593a60d10982a9176ce457091accf1c
@@ -37,6 +37,15 @@ module Apptrail
37
37
  max_elapsed_time: 4,
38
38
  rand_factor: 0.2
39
39
  }
40
+ c.contexts[:session] = {
41
+ tries: 3,
42
+ base_interval: 0.04,
43
+ on: Apptrail::ApptrailRetryableError,
44
+ on_retry: proc { Apptrail.logger.debug('Retrying refresh session.') },
45
+ max_interval: 0.12,
46
+ max_elapsed_time: 4,
47
+ rand_factor: 0.2
48
+ }
40
49
  end
41
50
 
42
51
  # You can use the Apptrail Application Events Client to send
@@ -44,9 +53,6 @@ module Apptrail
44
53
  #
45
54
  # See [Sending events](https://apptrail.com/docs/applications/guide/working-with-events/overview).
46
55
  class ApptrailEventsClient
47
- @_form_data = nil
48
- @_upload_url = nil
49
-
50
56
  SCHEMA = JSON.parse(File.read(
51
57
  File.expand_path('raw-event-schema.json', __dir__)
52
58
  )).freeze
@@ -72,6 +78,14 @@ module Apptrail
72
78
 
73
79
  @_base_api_url = "https://events.#{region}.apptrail.com/applications/session"
74
80
  @_api_key = api_key
81
+ @_session_client = HTTP.persistent @_base_api_url
82
+
83
+ @_form_data = nil
84
+ @_upload_url = nil
85
+ @_upload_client = nil
86
+
87
+ @_finalize_id = SecureRandom.uuid
88
+ ObjectSpace.define_finalizer(@_finalize_id, proc { close })
75
89
  end
76
90
 
77
91
  # Send a single audit event to log to Apptrail.
@@ -94,18 +108,18 @@ module Apptrail
94
108
  raise Apptrail::ApptrailError, e.message
95
109
  end
96
110
 
97
- _refresh_post_policy() if @_upload_url.nil? || @_form_data.nil?
111
+ _refresh_post_policy() if @_upload_url.nil? || @_form_data.nil? || @_upload_client.nil?
98
112
 
99
- content = ''
113
+ content = StringIO.new
100
114
  events.each do |evt|
101
- content << JSON.generate(evt)
115
+ content << JSON.fast_generate(evt)
102
116
  content << "\n"
103
117
  end
104
118
 
105
119
  filename = "#{SecureRandom.uuid}.jsonl"
106
120
  s3_key = File.join(@_application_id, filename)
107
121
 
108
- form_file = HTTP::FormData::File.new(StringIO.new(content), filename: filename)
122
+ form_file = HTTP::FormData::File.new(StringIO.new(content.string), filename: filename)
109
123
 
110
124
  new_form_opts = {
111
125
  **@_form_data,
@@ -113,9 +127,10 @@ module Apptrail
113
127
  file: form_file
114
128
  }
115
129
 
130
+ resp = nil
116
131
  begin
117
132
  Retriable.with_context(:s3) do
118
- resp = HTTP.post(@_upload_url, form: new_form_opts)
133
+ resp = @_upload_client.post(@_upload_url, form: new_form_opts)
119
134
  unless resp.status.success?
120
135
  if resp.status.server_error?
121
136
  raise ApptrailRetryableError, 'Server Error while putting events.'
@@ -135,20 +150,54 @@ module Apptrail
135
150
  raise ApptrailError, "Failed to put #{events.length} events. Encountered error."
136
151
  else
137
152
  Apptrail.logger.info("Successfully wrote #{events.length} events.")
153
+ ensure
154
+ resp.flush if resp
138
155
  end
139
156
  end
140
157
 
158
+ def close
159
+ # https://github.com/appsignal/rdkafka-ruby/pull/160/files
160
+ ObjectSpace.undefine_finalizer(@_finalize_id)
161
+ @_session_client.close if @_session_client
162
+ @_upload_client.close if @_upload_client
163
+ end
164
+
141
165
  private
142
166
 
143
167
  def _refresh_post_policy
144
- resp = HTTP
168
+ fail_default = -> { raise ApptrailError, "Error refreshing credentials." }
169
+ resp = nil
170
+
171
+ begin
172
+ Retriable.with_context(:session) do
173
+ resp = @_session_client
145
174
  .auth("Bearer #{@_api_key}")
146
175
  .get(@_base_api_url)
147
- raise ApptrailError, "Error refreshing credentials, status code: #{resp.code}" unless resp.status.success?
148
176
 
149
- result = JSON.parse(resp.body.to_s)
150
- @_upload_url = result['uploadUrl']
151
- @_form_data = result['form']
177
+ unless resp.status.success?
178
+ if resp.status.server_error?
179
+ raise ApptrailRetryableError, 'Server Error refreshing session.'
180
+ elsif resp.status.client_error?
181
+ if resp.status.code == 429
182
+ raise ApptrailRetryableError, 'Throttling'
183
+ else
184
+ fail_default.call
185
+ end
186
+ else
187
+ fail_default.call
188
+ end
189
+ end
190
+
191
+ result = JSON.parse(resp.body.to_s)
192
+ @_upload_url = result['uploadUrl']
193
+ @_form_data = result['form']
194
+ @_upload_client = HTTP.persistent @_upload_url
195
+ end
196
+ rescue StandardError => e
197
+ fail_default.call
198
+ ensure
199
+ resp.flush if resp
200
+ end
152
201
  end
153
202
  end
154
203
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apptrail-application-events-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Apptrail Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-28 00:00:00.000000000 Z
11
+ date: 2022-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: http