apptrail-application-events-sdk 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/apptrail-application-events-sdk.rb +46 -62
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69363a69d481f39af9b1a6e6be6559e29e5e3468cb866c452bc05d8b3665e783
|
4
|
+
data.tar.gz: 7bb490fac18cc57737981319c378bfcbf1520ed3f0867a37991d66fdaf74fbed
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 149fbe6a85a1070cd6e546953241b4871eb8bc76ab41e82c734ad031db291ac273140ec0a2a1a9caed6e4864df787530dbaddbdc74669640f73c1eb6ea8f0b10
|
7
|
+
data.tar.gz: 3245bf906b316403f4bc635059c1a4950cf6448e25be554226ea5e8bcb0efc17e34eac2cda2a0d623e61ab379377930b7ff271a15e484b2aaf4473b628eb0069
|
@@ -1,13 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'logger'
|
2
4
|
require 'stringio'
|
3
|
-
require
|
4
|
-
require
|
5
|
+
require 'base64'
|
6
|
+
require 'json'
|
5
7
|
require 'securerandom'
|
6
|
-
require
|
7
|
-
require
|
8
|
-
require
|
9
|
-
|
10
|
-
#HTTP.default_options = HTTP::Options.new(features: {logging: { logger: Logger.new(STDOUT)}})
|
8
|
+
require 'http'
|
9
|
+
require 'json-schema'
|
10
|
+
require 'retriable'
|
11
11
|
|
12
12
|
module Apptrail
|
13
13
|
class << self
|
@@ -15,15 +15,13 @@ module Apptrail
|
|
15
15
|
# For example, log to STDOUT by setting this to Logger.new(STDOUT).
|
16
16
|
#
|
17
17
|
# @return [Logger]
|
18
|
-
|
18
|
+
attr_writer :logger
|
19
19
|
|
20
20
|
def logger
|
21
21
|
@logger ||= Logger.new($stdout).tap do |log|
|
22
|
-
log.progname =
|
22
|
+
log.progname = name
|
23
23
|
end
|
24
24
|
end
|
25
|
-
|
26
|
-
|
27
25
|
end
|
28
26
|
|
29
27
|
class ApptrailError < StandardError; end
|
@@ -34,10 +32,10 @@ module Apptrail
|
|
34
32
|
tries: 3,
|
35
33
|
base_interval: 0.04,
|
36
34
|
on: Apptrail::ApptrailRetryableError,
|
37
|
-
on_retry:
|
35
|
+
on_retry: proc { Apptrail.logger.debug('Retrying upload.') },
|
38
36
|
max_interval: 0.12,
|
39
37
|
max_elapsed_time: 4,
|
40
|
-
rand_factor: 0.2
|
38
|
+
rand_factor: 0.2
|
41
39
|
}
|
42
40
|
end
|
43
41
|
|
@@ -50,8 +48,8 @@ module Apptrail
|
|
50
48
|
@_upload_url = nil
|
51
49
|
|
52
50
|
SCHEMA = JSON.parse(File.read(
|
53
|
-
|
54
|
-
|
51
|
+
File.expand_path('raw-event-schema.json', __dir__)
|
52
|
+
)).freeze
|
55
53
|
private_constant :SCHEMA
|
56
54
|
|
57
55
|
def inspect
|
@@ -59,17 +57,17 @@ module Apptrail
|
|
59
57
|
end
|
60
58
|
|
61
59
|
def to_s
|
62
|
-
|
60
|
+
'ApptrailEventsClient{}'
|
63
61
|
end
|
64
62
|
|
65
63
|
def initialize(region:, api_key:)
|
66
64
|
@_region = region
|
67
65
|
begin
|
68
66
|
parsed_key = Base64.urlsafe_decode64(api_key)
|
69
|
-
application_id,
|
67
|
+
application_id, = parsed_key.split(',', 3)
|
70
68
|
@_application_id = application_id
|
71
|
-
rescue
|
72
|
-
raise ArgumentError,
|
69
|
+
rescue StandardError
|
70
|
+
raise ArgumentError, 'Invalid API Key.'
|
73
71
|
end
|
74
72
|
|
75
73
|
@_base_api_url = "https://events.#{region}.apptrail.com/applications/session"
|
@@ -83,88 +81,74 @@ module Apptrail
|
|
83
81
|
put_events([event])
|
84
82
|
end
|
85
83
|
|
86
|
-
|
87
84
|
# Send a list of up to 1000 audit events to log to Apptrail.
|
88
85
|
#
|
89
86
|
# @param events [Array<ApptrailEvent>]] An array of events to log. See [Apptrail Event Format](https://apptrail.com/docs/applications/guide/event-format) for a full description of the event format.
|
90
87
|
def put_events(events)
|
91
|
-
unless events.is_a?(Array)
|
92
|
-
|
93
|
-
end
|
94
|
-
unless events.length <= 1000
|
95
|
-
raise ArgumentError, "Can not send more than 1000 events in a single PutEvents call."
|
96
|
-
end
|
88
|
+
raise TypeError, 'Must pass in array of events.' unless events.is_a?(Array)
|
89
|
+
raise ArgumentError, 'Can not send more than 1000 events in a single PutEvents call.' unless events.length <= 1000
|
97
90
|
|
98
91
|
begin
|
99
|
-
JSON::Validator.validate!(SCHEMA, events, :
|
92
|
+
JSON::Validator.validate!(SCHEMA, events, list: true, parse_data: false)
|
100
93
|
rescue JSON::Schema::ValidationError => e
|
101
|
-
raise Apptrail::ApptrailError
|
94
|
+
raise Apptrail::ApptrailError, e.message
|
102
95
|
end
|
103
96
|
|
104
|
-
if @_upload_url.nil? || @_form_data.nil?
|
105
|
-
_refresh_post_policy()
|
106
|
-
end
|
97
|
+
_refresh_post_policy if @_upload_url.nil? || @_form_data.nil?
|
107
98
|
|
108
|
-
content =
|
99
|
+
content = ''
|
109
100
|
events.each do |evt|
|
110
101
|
content << JSON.generate(evt)
|
111
102
|
content << "\n"
|
112
103
|
end
|
113
104
|
|
114
|
-
filename = SecureRandom.uuid
|
105
|
+
filename = "#{SecureRandom.uuid}.jsonl"
|
115
106
|
s3_key = File.join(@_application_id, filename)
|
116
107
|
|
117
|
-
form_file = HTTP::FormData::File.new(StringIO.new(content), :
|
108
|
+
form_file = HTTP::FormData::File.new(StringIO.new(content), filename: filename)
|
118
109
|
|
119
110
|
new_form_opts = {
|
120
111
|
**@_form_data,
|
121
|
-
:
|
122
|
-
:
|
112
|
+
key: s3_key,
|
113
|
+
file: form_file
|
123
114
|
}
|
124
115
|
|
125
116
|
begin
|
126
117
|
Retriable.with_context(:s3) do
|
127
|
-
resp = HTTP.post(@_upload_url, :
|
128
|
-
|
129
|
-
if
|
130
|
-
raise ApptrailRetryableError
|
131
|
-
elsif
|
132
|
-
if
|
133
|
-
_refresh_post_policy
|
134
|
-
raise ApptrailRetryableError
|
118
|
+
resp = HTTP.post(@_upload_url, form: new_form_opts)
|
119
|
+
unless resp.status.success?
|
120
|
+
if resp.status.server_error?
|
121
|
+
raise ApptrailRetryableError, 'Server Error while putting events.'
|
122
|
+
elsif resp.status.client_error?
|
123
|
+
if resp.status.code == 403 && resp.body.to_s.include?('Policy expired')
|
124
|
+
_refresh_post_policy
|
125
|
+
raise ApptrailRetryableError, 'Session expired.'
|
135
126
|
else
|
136
|
-
|
137
|
-
raise ApptrailError.new("Error while putting events.")
|
127
|
+
raise ApptrailError, 'Error while putting events.'
|
138
128
|
end
|
139
129
|
else
|
140
|
-
raise ApptrailError
|
130
|
+
raise ApptrailError, 'Error while putting events.'
|
141
131
|
end
|
142
132
|
end
|
143
133
|
end
|
144
|
-
rescue
|
145
|
-
raise ApptrailError
|
134
|
+
rescue StandardError
|
135
|
+
raise ApptrailError, "Failed to put #{events.length} events. Encountered error."
|
146
136
|
else
|
147
|
-
Apptrail
|
137
|
+
Apptrail.logger.info("Successfully wrote #{events.length} events.")
|
148
138
|
end
|
149
|
-
|
150
139
|
end
|
151
140
|
|
152
141
|
private
|
153
142
|
|
154
143
|
def _refresh_post_policy
|
155
|
-
puts @_base_api_url
|
156
144
|
resp = HTTP
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
end
|
145
|
+
.auth("Bearer #{@_api_key}")
|
146
|
+
.get(@_base_api_url)
|
147
|
+
raise ApptrailError, "Error refreshing credentials, status code: #{resp.code}" unless resp.status.success?
|
148
|
+
|
162
149
|
result = JSON.parse(resp.body.to_s)
|
163
|
-
@_upload_url = result[
|
164
|
-
@_form_data = result[
|
150
|
+
@_upload_url = result['uploadUrl']
|
151
|
+
@_form_data = result['form']
|
165
152
|
end
|
166
|
-
|
167
|
-
|
168
153
|
end
|
169
|
-
|
170
154
|
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
|
+
version: 0.0.2
|
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-
|
11
|
+
date: 2022-01-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: http
|