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 +4 -4
- data/lib/omniai/instrumentation.rb +56 -5
- data/lib/omniai/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: e22534811cc346b74927ec47382420e86faee449ff730cbb0665d30a714cd872
|
|
4
|
+
data.tar.gz: 1c3e58ad36506a2564563c46a700e8be04731b7266d8008bbd63bcc22d9cfa64
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
-
|
|
36
|
+
@logger.error("#{name}: #{error.message}") if error
|
|
17
37
|
|
|
18
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
data/lib/omniai/version.rb
CHANGED