posthog-ruby 3.14.3 → 3.15.0
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 +4 -4
- data/lib/posthog/client.rb +3 -1
- data/lib/posthog/send_worker.rb +6 -1
- data/lib/posthog/transport.rb +25 -3
- data/lib/posthog/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ab01f90a9fbe05af027e92831b58e906eb38b30914230fd83a35cab28da40fcd
|
|
4
|
+
data.tar.gz: cf06c8facfe324712e096cedfeeef90c755705bcce79384cda2d7b5f4d675cf0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6825662770b6a96fce094e2c46619fe142144ef8098db49c6a67b4f1faf54d731b65c8767f985413c91bf097767db84648fe2b2ba938ca4f788a0201ffa6002e
|
|
7
|
+
data.tar.gz: 8501399ef589da90e3e7c65629df24d99012240c718874a506fede7833225316a1c97881a8eca1df06a831eace6e9090e3b052686c7656c8cd9a3a1815e3a6b7
|
data/lib/posthog/client.rb
CHANGED
|
@@ -76,6 +76,7 @@ module PostHog
|
|
|
76
76
|
# the same API key. Use only when you intentionally need multiple clients. Defaults to +false+.
|
|
77
77
|
# @option opts [Boolean] :skip_ssl_verification +true+ to disable SSL certificate verification for requests.
|
|
78
78
|
# Intended only for local development or custom deployments.
|
|
79
|
+
# @option opts [Boolean] :compress_request Set to +false+ to disable gzip compression for batch uploads.
|
|
79
80
|
# @option opts [Object] :flag_definition_cache_provider An object implementing the {FlagDefinitionCacheProvider}
|
|
80
81
|
# interface for distributed flag definition caching.
|
|
81
82
|
# @option opts [Boolean] :is_server +true+ to stamp captured events with `$is_server => true` so PostHog
|
|
@@ -109,7 +110,8 @@ module PostHog
|
|
|
109
110
|
@transport = Transport.new(
|
|
110
111
|
api_host: opts[:host],
|
|
111
112
|
skip_ssl_verification: opts[:skip_ssl_verification],
|
|
112
|
-
retries: 3
|
|
113
|
+
retries: 3,
|
|
114
|
+
compress_request: opts[:compress_request]
|
|
113
115
|
)
|
|
114
116
|
@sync_lock = Mutex.new
|
|
115
117
|
end
|
data/lib/posthog/send_worker.rb
CHANGED
|
@@ -27,6 +27,7 @@ module PostHog
|
|
|
27
27
|
# @option options [Proc] :on_error Callback invoked as `on_error.call(status, error)`.
|
|
28
28
|
# @option options [String] :host PostHog API host URL.
|
|
29
29
|
# @option options [Boolean] :skip_ssl_verification Disable SSL certificate verification.
|
|
30
|
+
# @option options [Boolean] :compress_request Set to +false+ to disable gzip batch request bodies.
|
|
30
31
|
def initialize(queue, api_key, options = {})
|
|
31
32
|
symbolize_keys! options
|
|
32
33
|
@queue = queue
|
|
@@ -42,7 +43,11 @@ module PostHog
|
|
|
42
43
|
@flush_requested = false
|
|
43
44
|
@shutdown = false
|
|
44
45
|
@pid = Process.pid
|
|
45
|
-
@transport_options = {
|
|
46
|
+
@transport_options = {
|
|
47
|
+
api_host: options[:host],
|
|
48
|
+
skip_ssl_verification: options[:skip_ssl_verification],
|
|
49
|
+
compress_request: options[:compress_request]
|
|
50
|
+
}
|
|
46
51
|
@transport = Transport.new(@transport_options)
|
|
47
52
|
end
|
|
48
53
|
|
data/lib/posthog/transport.rb
CHANGED
|
@@ -8,6 +8,7 @@ require 'posthog/backoff_policy'
|
|
|
8
8
|
require 'net/http'
|
|
9
9
|
require 'net/https'
|
|
10
10
|
require 'json'
|
|
11
|
+
require 'zlib'
|
|
11
12
|
|
|
12
13
|
module PostHog
|
|
13
14
|
# HTTP transport used by the SDK workers.
|
|
@@ -28,6 +29,7 @@ module PostHog
|
|
|
28
29
|
# @option options [Integer] :retries Number of retry attempts for retryable failures.
|
|
29
30
|
# @option options [PostHog::BackoffPolicy] :backoff_policy Backoff policy used between retries.
|
|
30
31
|
# @option options [Boolean] :skip_ssl_verification Disable SSL certificate verification.
|
|
32
|
+
# @option options [Boolean] :compress_request Whether to gzip batch request bodies. Defaults to +true+.
|
|
31
33
|
def initialize(options = {})
|
|
32
34
|
if options[:api_host]
|
|
33
35
|
uri = URI.parse(options[:api_host])
|
|
@@ -44,6 +46,7 @@ module PostHog
|
|
|
44
46
|
@path = options[:path] || PATH
|
|
45
47
|
@retries = options[:retries] || RETRIES
|
|
46
48
|
@backoff_policy = options[:backoff_policy] || PostHog::BackoffPolicy.new
|
|
49
|
+
@compress_request = options[:compress_request] != false
|
|
47
50
|
|
|
48
51
|
http = Net::HTTP.new(options[:host], options[:port])
|
|
49
52
|
http.use_ssl = options[:ssl]
|
|
@@ -144,22 +147,41 @@ module PostHog
|
|
|
144
147
|
def send_request(api_key, batch)
|
|
145
148
|
payload = JSON.generate(api_key: api_key, batch: batch)
|
|
146
149
|
|
|
147
|
-
|
|
150
|
+
request_path, request_headers, request_payload = build_request(@path, @headers, payload)
|
|
151
|
+
request = Net::HTTP::Post.new(request_path, request_headers)
|
|
148
152
|
|
|
149
153
|
if self.class.stub
|
|
150
|
-
logger.debug "stubbed request to #{
|
|
154
|
+
logger.debug "stubbed request to #{request_path}: " \
|
|
151
155
|
"api key = #{api_key}, batch = #{JSON.generate(batch)}"
|
|
152
156
|
|
|
153
157
|
[200, '{}']
|
|
154
158
|
else
|
|
155
159
|
@http_mutex.synchronize do
|
|
156
160
|
@http.start unless @http.started? # Maintain a persistent connection
|
|
157
|
-
response = @http.request(request,
|
|
161
|
+
response = @http.request(request, request_payload)
|
|
158
162
|
[response.code.to_i, response.body]
|
|
159
163
|
end
|
|
160
164
|
end
|
|
161
165
|
end
|
|
162
166
|
|
|
167
|
+
def build_request(path, headers, payload)
|
|
168
|
+
return [path, headers, payload] unless @compress_request
|
|
169
|
+
|
|
170
|
+
compressed_payload = gzip_payload(payload)
|
|
171
|
+
return [path, headers, payload] unless compressed_payload
|
|
172
|
+
|
|
173
|
+
compressed_headers = headers.merge('Content-Encoding' => 'gzip')
|
|
174
|
+
|
|
175
|
+
[path, compressed_headers, compressed_payload]
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def gzip_payload(payload)
|
|
179
|
+
Zlib.gzip(payload)
|
|
180
|
+
rescue Zlib::Error => e
|
|
181
|
+
logger.warn("gzip compression failed; sending uncompressed - #{e.message}")
|
|
182
|
+
nil
|
|
183
|
+
end
|
|
184
|
+
|
|
163
185
|
class << self
|
|
164
186
|
attr_writer :stub
|
|
165
187
|
|
data/lib/posthog/version.rb
CHANGED