omniai 3.7.0 → 3.7.1

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: 7464f0580c8f8392155996aa7b4675491bec1fe62b48ff9b12e53264952a50e5
4
- data.tar.gz: 3ef5570bd7eac6ed72a9826c90313565dc0fd00b6ec52745fe02a04ce2855264
3
+ metadata.gz: e22534811cc346b74927ec47382420e86faee449ff730cbb0665d30a714cd872
4
+ data.tar.gz: 1c3e58ad36506a2564563c46a700e8be04731b7266d8008bbd63bcc22d9cfa64
5
5
  SHA512:
6
- metadata.gz: e5ab80ba78ab316ca74e46dffb0bdea26ed13b83f4ec4ff7c5e985580c5460d926366d71bf547ac905273968db44cf1949eb57c56b0560077f1b805f52a4c93d
7
- data.tar.gz: bd453f4fd6355066833db225b771b1078fd8d916138213a1f0d4d18f37316cae7b60f47a215c46d7a76fb096eeece2e6d887a8dc9a4819c8c54dd65f3ece731f
6
+ metadata.gz: ea3efdf811897d5fff8f55b0e4051b71f151d077230518b139fde0ef38656c6b784f9aafcfccb43c692fe85a102b47d0ca41c8696af767294b1a2270dfeee5fa
7
+ data.tar.gz: f3e87e1dd51ca72ec1d8706d3833a06c10b65947fdaf1e9a375518ca2b3d78f03a43af1c2869de1e84fb1c5cd77226ecbd5aa45fa7092f84691f0ea5f5afd183
@@ -8,27 +8,78 @@ module OmniAI
8
8
  @logger = logger
9
9
  end
10
10
 
11
+ # ActiveSupport::Notifications-compatible instrument.
12
+ #
13
+ # On http 6 the instrumentation feature drives every request through
14
+ # `around_request`, which (per http's own feature docs) "emits two events on
15
+ # every request: `start_request.http` before the request is made [and]
16
+ # `request.http` after the response is received". Both are delivered here as
17
+ # `instrument(name) { ... }` calls: the start event carries an empty block,
18
+ # and the request event's block wraps the exchange and returns the response.
19
+ # The block of the request event MUST be yielded and its value returned,
20
+ # otherwise the response is lost as `nil` (http uses the return value as the
21
+ # response). We log the request on the start event and the response on the
22
+ # request event. The event namespace is caller-configurable (the names are
23
+ # `start_request.#{namespace}` / `request.#{namespace}`), so #start_event?
24
+ # prefix-matches rather than comparing the full name.
25
+ #
26
+ # On http 5 this is only ever called without a block (for the start and
27
+ # error events); request/response logging there happens via #start / #finish.
28
+ #
11
29
  # @param name [String]
12
30
  # @param payload [Hash]
31
+ # @option payload [HTTP::Request] :request
32
+ # @option payload [HTTP::Response] :response
13
33
  # @option payload [Exception] :error
14
34
  def instrument(name, payload = {})
15
35
  error = payload[:error]
16
- return unless error
36
+ @logger.error("#{name}: #{error.message}") if error
17
37
 
18
- @logger.error("#{name}: #{error.message}")
38
+ return unless block_given?
39
+
40
+ if start_event?(name)
41
+ log_request(payload[:request])
42
+ yield payload
43
+ else
44
+ response = yield payload
45
+ log_response(payload[:response] || response)
46
+ response
47
+ end
19
48
  end
20
49
 
21
50
  # @param payload [Hash]
22
51
  # @option payload [HTTP::Request] :request
23
52
  def start(_, payload)
24
- request = payload[:request]
25
- @logger.info("#{request.verb.upcase} #{request.uri}")
53
+ log_request(payload[:request])
26
54
  end
27
55
 
28
56
  # @param payload [Hash]
29
57
  # @option payload [HTTP::Response] :response
30
58
  def finish(_, payload)
31
- response = payload[:response]
59
+ log_response(payload[:response])
60
+ end
61
+
62
+ private
63
+
64
+ # @param name [String] the http instrumentation event name, e.g.
65
+ # "start_request.http" (pre-flight) or "request.http" (the exchange).
66
+ #
67
+ # @return [Boolean] true for the pre-flight "start_..." event
68
+ def start_event?(name)
69
+ name.to_s.start_with?("start_")
70
+ end
71
+
72
+ # @param request [HTTP::Request, nil]
73
+ def log_request(request)
74
+ return unless request
75
+
76
+ @logger.info("#{request.verb.upcase} #{request.uri}")
77
+ end
78
+
79
+ # @param response [HTTP::Response, nil]
80
+ def log_response(response)
81
+ return unless response
82
+
32
83
  @logger.info("#{response.status.code} #{response.status.reason}")
33
84
  end
34
85
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OmniAI
4
- VERSION = "3.7.0"
4
+ VERSION = "3.7.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: omniai
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.7.0
4
+ version: 3.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Sylvestre