barkibu-kb 1.2.0 → 1.3.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/CHANGELOG.md +6 -1
- data/Gemfile.lock +4 -3
- data/README.md +42 -5
- data/barkibu-kb.gemspec +1 -0
- data/lib/barkibu-kb.rb +4 -0
- data/lib/kb/client.rb +6 -7
- data/lib/kb/instrumentation/datadog.rb +12 -0
- data/lib/kb/models/concerns/listable.rb +0 -2
- data/lib/kb/retry_policy.rb +88 -0
- data/lib/kb/version.rb +1 -1
- metadata +16 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 72ba3947460c029c6dee0dc965b1066b03b645d3bb6a891b291c2383b8d581e3
|
|
4
|
+
data.tar.gz: d0b5ca779b79f1e7017dbafca93376215faac264ec55210ef1af8f981f352ec5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0d596248f6f49451a5a4bcb9ea1648b3175b3d8b1d7cbf0665082332841d63c32867e7f408d221f2a47ef7387e1b70836c74af5bfd8e5e37ae9536ea9b19ec42
|
|
7
|
+
data.tar.gz: e832f256a27bfbe905d6e122b81245370c932dad1801d5772608778851131c9da1e761817e164df8a91e5e770af79804cd2c0cfdc90310148048bd92ef761ea7
|
data/CHANGELOG.md
CHANGED
|
@@ -6,7 +6,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
8
|
## [unreleased]
|
|
9
|
-
- See diff: https://github.com/barkibu/kb-ruby/compare/v1.
|
|
9
|
+
- See diff: https://github.com/barkibu/kb-ruby/compare/v1.3.0...HEAD
|
|
10
|
+
|
|
11
|
+
## [1.3.0]
|
|
12
|
+
- Retry transport failures once (`faraday-retry`, already in the Faraday 1.10 bundle, now an explicit dependency). `KB::RetryPolicy` decides by the underlying error, not the Faraday class: failures where the request never left (`Net::OpenTimeout`, `ECONNREFUSED`, `EHOSTUNREACH`, `ENETUNREACH`, `EADDRNOTAVAIL`, `SocketError`) retry for every verb; any other transport failure (read/write timeout, reset, EOF, TLS) retries for GET/HEAD only (and not when the call raised its own `read_timeout:`, so a 30s read isn't doubled); HTTP error responses never retry. New settings `KB.config.request.retries` (default 1, 0 disables) and `retry_interval` (default 0.1s, randomized up to 2x). Worst-case latency is now two attempts' worth of phase budgets.
|
|
13
|
+
- `request.kb_client` payload gains `retries` and `retry_errors` on retried calls; the Datadog subscriber tags them as `kb.retries` / `kb.retry_errors`. The event and span cover all attempts, so a call that succeeded on retry is not an error.
|
|
14
|
+
- `KB::Listable.all` (and so `PetParent.all`, `Pet.all`, `Breed.all`, `Product.all`, `Plan.all`, `Assessment.all`) now wraps `Faraday::ConnectionFailed` in `KB::Error` like every other model call, instead of re-raising it raw. Code rescuing `Faraday::ConnectionFailed` around `.all` must rescue `KB::Error` instead; none of Funnel, Global Admin or connected_health does.
|
|
10
15
|
|
|
11
16
|
## [1.2.0]
|
|
12
17
|
- `KB::Client` emits one `request.kb_client` `ActiveSupport::Notifications` event per KB call (`KB::Client::REQUEST_EVENT`), wrapping cache lookup, connect, TLS, write, read and parsing. Payload: `verb`, `path`, `base_url`, `cache_hit` (GET only), `status`, plus ActiveSupport's `exception`/`exception_object` when the call raised. Every public method now goes through one private `perform` seam; no behaviour change (same cache keys, params and error classes).
|
data/Gemfile.lock
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
barkibu-kb (1.
|
|
4
|
+
barkibu-kb (1.3.0)
|
|
5
5
|
activemodel (>= 4.0.2)
|
|
6
6
|
activerecord
|
|
7
7
|
activesupport (>= 3.0.0)
|
|
8
8
|
dry-configurable (~> 0.9)
|
|
9
9
|
faraday
|
|
10
10
|
faraday-net_http (~> 1.0)
|
|
11
|
+
faraday-retry (~> 1.0)
|
|
11
12
|
faraday_middleware
|
|
12
13
|
i18n
|
|
13
|
-
barkibu-kb-fake (1.
|
|
14
|
-
barkibu-kb (= 1.
|
|
14
|
+
barkibu-kb-fake (1.3.0)
|
|
15
|
+
barkibu-kb (= 1.3.0)
|
|
15
16
|
countries
|
|
16
17
|
sinatra
|
|
17
18
|
webmock
|
data/README.md
CHANGED
|
@@ -74,14 +74,49 @@ and write budgets stay global:
|
|
|
74
74
|
KB::Pet.kb_client.request('birthdays', filters: { month: 9, day: 22, size: 1000 }, read_timeout: 30)
|
|
75
75
|
```
|
|
76
76
|
|
|
77
|
+
#### Retries
|
|
78
|
+
|
|
79
|
+
The client retries a failed call once when the failure is in the transport, never
|
|
80
|
+
when KB answered with an HTTP error. Which calls retry depends on whether the
|
|
81
|
+
request can have reached KB (`KB::RetryPolicy`):
|
|
82
|
+
|
|
83
|
+
| Failure | Retried for |
|
|
84
|
+
|---|---|
|
|
85
|
+
| Never sent: connect/TLS timeout (`Net::OpenTimeout`), connection refused, host/network unreachable, DNS failure | every verb, POST included |
|
|
86
|
+
| Maybe sent: read/write timeout, connection reset, EOF, TLS error mid-stream | GET and HEAD only |
|
|
87
|
+
| HTTP 4xx/5xx | never |
|
|
88
|
+
|
|
89
|
+
PUT and DELETE are not retried on "maybe sent" failures: `upsert` and
|
|
90
|
+
`PetParent#merge!` are PUTs whose second run is not a no-op on KB's side, and a
|
|
91
|
+
repeated DELETE would turn a success into a 404. A call that raised its own read
|
|
92
|
+
budget (`read_timeout:` on `KB::Client#request`) is not retried on "maybe sent"
|
|
93
|
+
failures either, so a 30s birthdays read can't become 60s; failures that never
|
|
94
|
+
reached KB are still retried.
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
# config/initializers/kb_ruby.rb
|
|
98
|
+
KB.config.request.retries = 1 # default; 0 disables retries
|
|
99
|
+
KB.config.request.retry_interval = 0.1 # default, seconds; each wait is 1x-2x this
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Like the timeouts, these are read when a client builds its connection, i.e. on
|
|
103
|
+
its first call, so set them in an initializer.
|
|
104
|
+
|
|
105
|
+
Worst case, a call now takes two attempts' worth of phase budgets plus the
|
|
106
|
+
interval, e.g. a GET that read-times-out twice takes about 2 x (1 + 3 + 5)s with
|
|
107
|
+
the default timeouts.
|
|
108
|
+
|
|
77
109
|
#### Instrumentation
|
|
78
110
|
|
|
79
111
|
Every KB call emits one `request.kb_client` event through
|
|
80
112
|
`ActiveSupport::Notifications`, wrapping the whole call: cache lookup, TCP
|
|
81
113
|
connect, TLS, write, read and JSON parsing. The payload carries `verb`, `path`,
|
|
82
|
-
`base_url`, `cache_hit` (GET calls only), `status` (when a response arrived)
|
|
83
|
-
|
|
84
|
-
|
|
114
|
+
`base_url`, `cache_hit` (GET calls only), `status` (when a response arrived),
|
|
115
|
+
`retries` and `retry_errors` (only when the call was retried: the count, and the
|
|
116
|
+
underlying error class of each failed attempt, e.g. `["Net::OpenTimeout"]`) and
|
|
117
|
+
ActiveSupport's `exception` / `exception_object` when the call raised. The event
|
|
118
|
+
covers the whole call including retries, so `exception` is set only when every
|
|
119
|
+
attempt failed. Subscribe to it for logging, metrics or anything else:
|
|
85
120
|
|
|
86
121
|
```ruby
|
|
87
122
|
ActiveSupport::Notifications.subscribe(KB::Client::REQUEST_EVENT) do |event|
|
|
@@ -110,8 +145,10 @@ tracer's own Net::HTTP spans nest under it. It inherits the app's service
|
|
|
110
145
|
knowledge-base service either: it measures the client's whole call, not a KB
|
|
111
146
|
operation. Resources are low-cardinality (`GET /v1/pets/birthdays`,
|
|
112
147
|
`GET /v1/pets/?/contracts`). Tags: `peer.hostname` (the KB host used),
|
|
113
|
-
`kb.method`, `kb.cache_hit` (GET calls only), `http.status_code`,
|
|
114
|
-
|
|
148
|
+
`kb.method`, `kb.cache_hit` (GET calls only), `http.status_code`, `kb.retries`
|
|
149
|
+
and `kb.retry_errors` (retried calls only), plus the standard
|
|
150
|
+
`error.type`/`error.message` when the call raises. A span with `kb.retries` and
|
|
151
|
+
no error is a failure the retry absorbed.
|
|
115
152
|
|
|
116
153
|
Why not rely on the Net::HTTP tracer alone: faraday-net_http opens the socket
|
|
117
154
|
before `Net::HTTP#request`, the method that tracer patches, so a connect timeout
|
data/barkibu-kb.gemspec
CHANGED
|
@@ -54,5 +54,6 @@ Gem::Specification.new do |spec|
|
|
|
54
54
|
spec.add_runtime_dependency 'faraday'
|
|
55
55
|
spec.add_runtime_dependency 'faraday-net_http', '~> 1.0'
|
|
56
56
|
spec.add_runtime_dependency 'faraday_middleware'
|
|
57
|
+
spec.add_runtime_dependency 'faraday-retry', '~> 1.0'
|
|
57
58
|
spec.add_runtime_dependency 'i18n'
|
|
58
59
|
end
|
data/lib/barkibu-kb.rb
CHANGED
|
@@ -21,6 +21,9 @@ module KB
|
|
|
21
21
|
setting :connect_timeout, default: 1
|
|
22
22
|
setting :write_timeout, default: 3
|
|
23
23
|
setting :read_timeout, default: 5
|
|
24
|
+
# Retries after a transport failure, per KB::RetryPolicy. 0 disables retries.
|
|
25
|
+
setting :retries, default: 1
|
|
26
|
+
setting :retry_interval, default: 0.1
|
|
24
27
|
end
|
|
25
28
|
end
|
|
26
29
|
|
|
@@ -29,6 +32,7 @@ require 'kb/inflections'
|
|
|
29
32
|
require 'kb/cache'
|
|
30
33
|
require 'kb/client_resolver'
|
|
31
34
|
require 'kb/errors'
|
|
35
|
+
require 'kb/retry_policy'
|
|
32
36
|
require 'kb/client'
|
|
33
37
|
|
|
34
38
|
require 'kb/concerns'
|
data/lib/kb/client.rb
CHANGED
|
@@ -2,6 +2,7 @@ module KB
|
|
|
2
2
|
class Client
|
|
3
3
|
# Emitted once per KB call, wrapping cache lookup and the HTTP request.
|
|
4
4
|
# Payload: verb, path, base_url, cache_hit (GET only), status (when a response arrived),
|
|
5
|
+
# retries / retry_errors (only when the call was retried, see KB::RetryPolicy),
|
|
5
6
|
# plus ActiveSupport's exception/exception_object when the call raised.
|
|
6
7
|
REQUEST_EVENT = 'request.kb_client'.freeze
|
|
7
8
|
|
|
@@ -74,7 +75,10 @@ module KB
|
|
|
74
75
|
end
|
|
75
76
|
|
|
76
77
|
def http(event, payload, read_timeout)
|
|
77
|
-
response = connection.public_send(event[:verb], event[:path], payload
|
|
78
|
+
response = connection.public_send(event[:verb], event[:path], payload) do |req|
|
|
79
|
+
req.options.read_timeout = read_timeout if read_timeout
|
|
80
|
+
RetryPolicy.track(req, event, read_timeout)
|
|
81
|
+
end
|
|
78
82
|
event[:status] = response.status
|
|
79
83
|
response.body
|
|
80
84
|
rescue Faraday::ClientError, Faraday::ServerError => e
|
|
@@ -101,6 +105,7 @@ module KB
|
|
|
101
105
|
|
|
102
106
|
def connection
|
|
103
107
|
@connection ||= Faraday.new(url: base_url, headers: headers, request: request_timeouts) do |conn|
|
|
108
|
+
conn.request :retry, RetryPolicy.middleware_options
|
|
104
109
|
conn.response :json
|
|
105
110
|
conn.response :raise_error
|
|
106
111
|
if KB.config.log_level == :debugger
|
|
@@ -112,12 +117,6 @@ module KB
|
|
|
112
117
|
end
|
|
113
118
|
end
|
|
114
119
|
|
|
115
|
-
def request_options(read_timeout)
|
|
116
|
-
return nil if read_timeout.nil?
|
|
117
|
-
|
|
118
|
-
->(req) { req.options.read_timeout = read_timeout }
|
|
119
|
-
end
|
|
120
|
-
|
|
121
120
|
def request_timeouts
|
|
122
121
|
{
|
|
123
122
|
open_timeout: KB.config.request.connect_timeout,
|
|
@@ -72,9 +72,21 @@ module KB
|
|
|
72
72
|
|
|
73
73
|
span.set_tag('kb.cache_hit', payload[:cache_hit].to_s) if payload.key?(:cache_hit)
|
|
74
74
|
span.set_tag('http.status_code', payload[:status].to_s) if payload[:status]
|
|
75
|
+
tag_retries(span, payload)
|
|
75
76
|
span.set_error(payload[:exception_object]) if payload[:exception_object]
|
|
76
77
|
span.finish
|
|
77
78
|
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
# Only on retried calls: `kb.retries` (numeric) and the distinct underlying
|
|
83
|
+
# errors that triggered them, e.g. `Net::OpenTimeout`.
|
|
84
|
+
def tag_retries(span, payload)
|
|
85
|
+
return unless payload[:retries]
|
|
86
|
+
|
|
87
|
+
span.set_tag('kb.retries', payload[:retries])
|
|
88
|
+
span.set_tag('kb.retry_errors', payload[:retry_errors].uniq.join(','))
|
|
89
|
+
end
|
|
78
90
|
end
|
|
79
91
|
end
|
|
80
92
|
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
require 'socket'
|
|
2
|
+
require 'net/http'
|
|
3
|
+
require 'faraday/retry'
|
|
4
|
+
|
|
5
|
+
module KB
|
|
6
|
+
# Decides which failed KB calls the client retries.
|
|
7
|
+
#
|
|
8
|
+
# Two classes of transport failure, told apart by the underlying Ruby error
|
|
9
|
+
# rather than the Faraday class (adapters disagree on the Faraday class: the
|
|
10
|
+
# net_http adapter wraps Net::OpenTimeout as ConnectionFailed, the persistent
|
|
11
|
+
# one as TimeoutError):
|
|
12
|
+
#
|
|
13
|
+
# - never sent: TCP connect / TLS handshake did not complete, so KB cannot have
|
|
14
|
+
# seen the request. Safe to retry for every verb, POST included.
|
|
15
|
+
# - maybe sent: anything else the transport raises (read/write timeout, reset,
|
|
16
|
+
# EOF, TLS error mid-stream). KB may have processed it, so only GET/HEAD are
|
|
17
|
+
# retried. PUT/DELETE are left out on purpose: `upsert` and `merge!` are PUTs
|
|
18
|
+
# whose second run is not a no-op on KB's side, and a repeated DELETE would
|
|
19
|
+
# turn a success into a 404.
|
|
20
|
+
# A call that raised its own read budget (`read_timeout:`, e.g. 30s for
|
|
21
|
+
# birthdays) is not retried on these either, so its worst case isn't doubled.
|
|
22
|
+
#
|
|
23
|
+
# HTTP responses (4xx/5xx) are never retried: KB answered.
|
|
24
|
+
module RetryPolicy
|
|
25
|
+
NOT_SENT_ERRORS = [
|
|
26
|
+
Net::OpenTimeout,
|
|
27
|
+
Errno::ECONNREFUSED,
|
|
28
|
+
Errno::EHOSTUNREACH,
|
|
29
|
+
Errno::ENETUNREACH,
|
|
30
|
+
Errno::EADDRNOTAVAIL,
|
|
31
|
+
SocketError # DNS resolution
|
|
32
|
+
].freeze
|
|
33
|
+
TRANSPORT_ERRORS = [Faraday::ConnectionFailed, Faraday::TimeoutError, Faraday::SSLError].freeze
|
|
34
|
+
MAYBE_SENT_VERBS = %i[get head].freeze
|
|
35
|
+
|
|
36
|
+
module_function
|
|
37
|
+
|
|
38
|
+
def retry?(verb, error, own_read_budget: false)
|
|
39
|
+
return false unless TRANSPORT_ERRORS.any? { |klass| error.is_a?(klass) }
|
|
40
|
+
|
|
41
|
+
not_sent?(error) || (MAYBE_SENT_VERBS.include?(verb) && !own_read_budget)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def not_sent?(error)
|
|
45
|
+
cause = root_cause(error)
|
|
46
|
+
NOT_SENT_ERRORS.any? { |klass| cause.is_a?(klass) }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# The Ruby error behind a Faraday error. `wrapped_exception` is Faraday's own
|
|
50
|
+
# explicit link to it (Ruby's `cause` is only whatever was being rescued at
|
|
51
|
+
# the raise, usually the same object). Faraday's adapters wrap the Ruby error
|
|
52
|
+
# one level deep, so one level is enough.
|
|
53
|
+
def root_cause(error)
|
|
54
|
+
(error.respond_to?(:wrapped_exception) && error.wrapped_exception) || error.cause || error
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Options for faraday-retry's middleware, read from KB.config.request.
|
|
58
|
+
def middleware_options
|
|
59
|
+
{
|
|
60
|
+
max: KB.config.request.retries,
|
|
61
|
+
interval: KB.config.request.retry_interval,
|
|
62
|
+
interval_randomness: 1, # 1x-2x the interval, so a burst of callers doesn't retry in lockstep
|
|
63
|
+
exceptions: TRANSPORT_ERRORS,
|
|
64
|
+
methods: [], # always ask retry_if
|
|
65
|
+
retry_if: lambda do |env, error|
|
|
66
|
+
retry?(env[:method], error, own_read_budget: env[:request].context&.dig(:kb_own_read_budget))
|
|
67
|
+
end,
|
|
68
|
+
retry_block: ->(env, _options, _retries_left, error) { record(env, error) }
|
|
69
|
+
}
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Hands the request.kb_client event payload (and whether the call set its own
|
|
73
|
+
# read budget) to the middleware through the request context, so each retry
|
|
74
|
+
# is reported on the call's own event.
|
|
75
|
+
def track(request, event, read_timeout = nil)
|
|
76
|
+
tracking = { kb_event: event, kb_own_read_budget: !read_timeout.nil? }
|
|
77
|
+
request.options.context = (request.options.context || {}).merge(tracking)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def record(env, error)
|
|
81
|
+
event = env[:request].context&.dig(:kb_event)
|
|
82
|
+
return unless event
|
|
83
|
+
|
|
84
|
+
event[:retries] = event.fetch(:retries, 0) + 1
|
|
85
|
+
(event[:retry_errors] ||= []) << root_cause(error).class.name
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
data/lib/kb/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: barkibu-kb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Léo Figea
|
|
@@ -261,6 +261,20 @@ dependencies:
|
|
|
261
261
|
- - ">="
|
|
262
262
|
- !ruby/object:Gem::Version
|
|
263
263
|
version: '0'
|
|
264
|
+
- !ruby/object:Gem::Dependency
|
|
265
|
+
name: faraday-retry
|
|
266
|
+
requirement: !ruby/object:Gem::Requirement
|
|
267
|
+
requirements:
|
|
268
|
+
- - "~>"
|
|
269
|
+
- !ruby/object:Gem::Version
|
|
270
|
+
version: '1.0'
|
|
271
|
+
type: :runtime
|
|
272
|
+
prerelease: false
|
|
273
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
274
|
+
requirements:
|
|
275
|
+
- - "~>"
|
|
276
|
+
- !ruby/object:Gem::Version
|
|
277
|
+
version: '1.0'
|
|
264
278
|
- !ruby/object:Gem::Dependency
|
|
265
279
|
name: i18n
|
|
266
280
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -352,6 +366,7 @@ files:
|
|
|
352
366
|
- lib/kb/models/referral.rb
|
|
353
367
|
- lib/kb/models/search_result.rb
|
|
354
368
|
- lib/kb/models/symptom.rb
|
|
369
|
+
- lib/kb/retry_policy.rb
|
|
355
370
|
- lib/kb/type/array_of_conditions_type.rb
|
|
356
371
|
- lib/kb/type/array_of_strings_type.rb
|
|
357
372
|
- lib/kb/type/array_of_symptoms_type.rb
|