logtail-rack 0.1.2 → 0.1.6

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: 1586d9af9309d5e2195cb64715fec586f80990faad899b745bf4451ae033d206
4
- data.tar.gz: 6971194227e47830e74da08039a6878b8b92b3fbcc879c085b41a6caa053dad7
3
+ metadata.gz: 76c8e9ab5c1364f1a345a7a20e6c24ef277ff8be9b8f3c9a173bbc9263f6d695
4
+ data.tar.gz: ad6237cc17307742c7263ec59afe93e77476d7410b8e94fe4a0f32129f22565b
5
5
  SHA512:
6
- metadata.gz: 921675d8114b08db6429503b412e914d21e0f50358d921628e296ccc4fb01b43d4b2d256aa6194a545ff9940922ad42e64470435b66235cdc8fd1030fa6718f8
7
- data.tar.gz: e1a28533af67cc52e3b2803abeb3c8c2ed3db970f8e02354adc1678cf6ce1d879d24c90b4c85a338f685a548aae64c457bdc3cfebf12145db8924a8e656d9eb5
6
+ metadata.gz: 2d520a022a670a9a8116a0b460aa3a765d3ddcdce4598c30ace1bea5d7cc4e8494ebea7fcdaa01210eac676d7220eba6010d9cabf3f182e4edf36db0a224f00f
7
+ data.tar.gz: 6fdb85a21302164570c07359a948c8a2a036ef582f0fa7a0a4078c0d7ad86d3d19d30a455709253e88c600a45e09c722e77bfdb89775247bb7308f6218bfbf50
@@ -136,14 +136,14 @@ module Logtail
136
136
  end
137
137
 
138
138
  elsif collapse_into_single_event?
139
- start = Time.now
140
-
139
+ request_start = Time.now
141
140
  status, headers, body = @app.call(env)
141
+ request_end = Time.now
142
142
 
143
143
  Config.instance.logger.info do
144
144
  http_context = CurrentContext.fetch(:http)
145
- content_length = headers[CONTENT_LENGTH_KEY]
146
- duration_ms = (Time.now - start) * 1000.0
145
+ content_length = safe_to_i(headers[CONTENT_LENGTH_KEY])
146
+ duration_ms = (request_end - request_start) * 1000.0
147
147
 
148
148
  http_response = HTTPResponse.new(
149
149
  content_length: content_length,
@@ -174,21 +174,19 @@ module Logtail
174
174
 
175
175
  [status, headers, body]
176
176
  else
177
- start = Time.now
178
-
179
177
  Config.instance.logger.info do
180
178
  event_body = capture_request_body? ? request.body_content : nil
181
179
  http_request = HTTPRequest.new(
182
180
  body: event_body,
183
- content_length: request.content_length,
181
+ content_length: safe_to_i(request.content_length),
184
182
  headers: request.headers,
185
- host: request.host,
183
+ host: force_encoding(request.host),
186
184
  method: request.request_method,
187
185
  path: request.path,
188
186
  port: request.port,
189
- query_string: request.query_string,
187
+ query_string: force_encoding(request.query_string),
190
188
  request_id: request.request_id,
191
- scheme: request.scheme,
189
+ scheme: force_encoding(request.scheme),
192
190
  body_limit: self.class.http_body_limit,
193
191
  headers_to_sanitize: self.class.http_header_filters,
194
192
  )
@@ -213,12 +211,14 @@ module Logtail
213
211
  }
214
212
  end
215
213
 
214
+ request_start = Time.now
216
215
  status, headers, body = @app.call(env)
216
+ request_end = Time.now
217
217
 
218
218
  Config.instance.logger.info do
219
219
  event_body = capture_response_body? ? body : nil
220
- content_length = headers[CONTENT_LENGTH_KEY]
221
- duration_ms = (Time.now - start) * 1000.0
220
+ content_length = safe_to_i(headers[CONTENT_LENGTH_KEY])
221
+ duration_ms = (request_end - request_start) * 1000.0
222
222
 
223
223
  http_response = HTTPResponse.new(
224
224
  body: event_body,
@@ -271,6 +271,18 @@ module Logtail
271
271
  false
272
272
  end
273
273
  end
274
+
275
+ def safe_to_i(val)
276
+ val.nil? ? nil : val.to_i
277
+ end
278
+
279
+ def force_encoding(value)
280
+ if value.respond_to?(:force_encoding)
281
+ value.dup.force_encoding('UTF-8')
282
+ else
283
+ value
284
+ end
285
+ end
274
286
  end
275
287
  end
276
288
  end
@@ -1,3 +1,5 @@
1
+ require "logtail-rack/util/encoding"
2
+
1
3
  module Logtail
2
4
  module Integrations
3
5
  module Rack
@@ -24,7 +26,7 @@ module Logtail
24
26
  @service_name = attributes[:service_name]
25
27
 
26
28
  if @headers
27
- @headers_json = @headers.to_json
29
+ @headers_json = Util::Encoding.force_utf8_encoding(@headers).to_json
28
30
  end
29
31
  end
30
32
 
@@ -1,3 +1,5 @@
1
+ require "logtail-rack/util/encoding"
2
+
1
3
  module Logtail
2
4
  module Integrations
3
5
  module Rack
@@ -19,7 +21,7 @@ module Logtail
19
21
  @duration_ms = attributes[:duration_ms]
20
22
 
21
23
  if @headers
22
- @headers_json = @headers.to_json
24
+ @headers_json = Util::Encoding.force_utf8_encoding(@headers).to_json
23
25
  end
24
26
  end
25
27
 
@@ -0,0 +1,15 @@
1
+ module Logtail
2
+ module Util
3
+ class Encoding
4
+ def self.force_utf8_encoding(data)
5
+ if data.respond_to?(:force_encoding)
6
+ data.dup.force_encoding('UTF-8')
7
+ elsif data.respond_to?(:transform_values)
8
+ data.transform_values { |val| Logtail::Util::Encoding.force_utf8_encoding(val) }
9
+ else
10
+ data
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,7 +1,7 @@
1
1
  module Logtail
2
2
  module Integrations
3
3
  module Rack
4
- VERSION = "0.1.2"
4
+ VERSION = "0.1.6"
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logtail-rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Logtail
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-04-20 00:00:00.000000000 Z
11
+ date: 2021-10-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: logtail
@@ -110,6 +110,7 @@ files:
110
110
  - lib/logtail-rack/middleware.rb
111
111
  - lib/logtail-rack/session_context.rb
112
112
  - lib/logtail-rack/user_context.rb
113
+ - lib/logtail-rack/util/encoding.rb
113
114
  - lib/logtail-rack/util/request.rb
114
115
  - lib/logtail-rack/version.rb
115
116
  - logtail-ruby-rack.gemspec