network_resiliency 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/Gemfile.lock +1 -1
- data/lib/network_resiliency/adapter/faraday.rb +3 -1
- data/lib/network_resiliency/adapter/http.rb +8 -8
- data/lib/network_resiliency/adapter/redis.rb +51 -39
- data/lib/network_resiliency/version.rb +1 -1
- data/lib/network_resiliency.rb +39 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b26da5cafa1321e4b284d8b3ade1184d4423973f388b1e4288b3be3d79153c3b
|
4
|
+
data.tar.gz: 6a4588995b8fdab21e424235bb31d2d83bef8d6b43d2b4ab1f9f3e9df08e19cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6b110ad5631918d1077f4ba77a63da113fa9b19f156f439f6b46a2f8817d770740e157d07d7752b443568b3d1f86f416ff707d3ff0598a71b0355dabf1aa0c8
|
7
|
+
data.tar.gz: ab91f3ac1febb0504b8b0a64f0f12014636fd86bfc1ea3f1c1904a38660b8be791cc07835fed7154775d150bbc131243f4e892e255ecd7a96f68dbd7d1cd637a
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,15 @@
|
|
1
|
+
### v0.2.0 (2023-10-01)
|
2
|
+
- high level patch
|
3
|
+
- redis adapter
|
4
|
+
- mock redis
|
5
|
+
- freeze time in specs
|
6
|
+
|
7
|
+
### v0.1.1 (2023-09-27)
|
8
|
+
- prevent double patching
|
9
|
+
- filter ip addresses
|
10
|
+
- simplify
|
11
|
+
- configure
|
12
|
+
|
1
13
|
### v0.1.0 (2023-09-26)
|
2
14
|
- rework faraday adapter
|
3
15
|
- faraday
|
data/Gemfile.lock
CHANGED
@@ -5,7 +5,9 @@ module NetworkResiliency
|
|
5
5
|
class Faraday < ::Faraday::Adapter::NetHttp
|
6
6
|
def build_connection(env)
|
7
7
|
super.tap do |conn|
|
8
|
-
NetworkResiliency::Adapter::HTTP.
|
8
|
+
unless NetworkResiliency::Adapter::HTTP.patched?(conn)
|
9
|
+
NetworkResiliency::Adapter::HTTP.patch(conn)
|
10
|
+
end
|
9
11
|
end
|
10
12
|
end
|
11
13
|
end
|
@@ -6,6 +6,8 @@ module NetworkResiliency
|
|
6
6
|
extend self
|
7
7
|
|
8
8
|
def patch(instance = nil)
|
9
|
+
return if patched?(instance)
|
10
|
+
|
9
11
|
(instance&.singleton_class || Net::HTTP).prepend(Instrumentation)
|
10
12
|
end
|
11
13
|
|
@@ -27,14 +29,12 @@ module NetworkResiliency
|
|
27
29
|
ensure
|
28
30
|
ts += NetworkResiliency.timestamp
|
29
31
|
|
30
|
-
NetworkResiliency.
|
31
|
-
"
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
error: e&.class,
|
37
|
-
}.compact,
|
32
|
+
NetworkResiliency.record(
|
33
|
+
adapter: "http",
|
34
|
+
action: "connect",
|
35
|
+
destination: address,
|
36
|
+
error: e&.class,
|
37
|
+
duration: ts,
|
38
38
|
)
|
39
39
|
end
|
40
40
|
end
|
@@ -3,61 +3,73 @@ require "redis"
|
|
3
3
|
module NetworkResiliency
|
4
4
|
module Adapter
|
5
5
|
module Redis
|
6
|
-
|
7
|
-
puts "RedisClient.connect(#{client.connect_timeout})"
|
6
|
+
extend self
|
8
7
|
|
9
|
-
|
8
|
+
def patch(instance = nil)
|
9
|
+
return if patched?(instance)
|
10
10
|
|
11
|
-
|
11
|
+
if instance
|
12
|
+
unless instance.is_a?(::Redis)
|
13
|
+
raise ArgumentError, "expected Redis instance, found: #{instance}"
|
14
|
+
end
|
12
15
|
|
13
|
-
|
14
|
-
ts = -NetworkResiliency.timestamp
|
15
|
-
super
|
16
|
-
ensure
|
17
|
-
client.connect_timeout = old_timeout
|
16
|
+
client = instance.instance_variable_get(:@client)
|
18
17
|
|
19
|
-
|
18
|
+
unless client.is_a?(::Redis::Client)
|
19
|
+
raise ArgumentError, "unsupported Redis client: #{client}"
|
20
|
+
end
|
20
21
|
|
21
|
-
|
22
|
+
client.singleton_class.prepend(Instrumentation)
|
23
|
+
else
|
24
|
+
::Redis::Client.prepend(Instrumentation)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def patched?(instance = nil)
|
29
|
+
if instance
|
30
|
+
client = instance.instance_variable_get(:@client)
|
22
31
|
|
23
|
-
|
32
|
+
client && client.singleton_class.ancestors.include?(Instrumentation)
|
33
|
+
else
|
34
|
+
::Redis::Client.ancestors.include?(Instrumentation)
|
35
|
+
end
|
24
36
|
end
|
25
37
|
|
26
|
-
|
27
|
-
|
38
|
+
module Instrumentation
|
39
|
+
# def initialize(...)
|
40
|
+
# super
|
28
41
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
ensure
|
33
|
-
ts += NetworkResiliency.timestamp
|
34
|
-
puts "call time: #{ts}"
|
42
|
+
# @network_resiliency_attempts = options[:reconnect_attempts]
|
43
|
+
# options[:reconnect_attempts] = 0
|
44
|
+
# end
|
35
45
|
|
36
|
-
|
37
|
-
|
46
|
+
def establish_connection
|
47
|
+
return super unless NetworkResiliency.enabled?(:redis)
|
38
48
|
|
39
|
-
|
40
|
-
|
41
|
-
end
|
49
|
+
begin
|
50
|
+
ts = -NetworkResiliency.timestamp
|
42
51
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
52
|
+
super
|
53
|
+
rescue ::Redis::CannotConnectError => e
|
54
|
+
# capture error
|
55
|
+
raise
|
56
|
+
ensure
|
57
|
+
ts += NetworkResiliency.timestamp
|
49
58
|
|
50
|
-
|
51
|
-
|
52
|
-
client.write_timeout = timeout
|
59
|
+
# grab underlying exception within Redis wrapper
|
60
|
+
error = e ? e.cause.class : nil
|
53
61
|
|
54
|
-
|
55
|
-
|
56
|
-
|
62
|
+
NetworkResiliency.record(
|
63
|
+
adapter: "redis",
|
64
|
+
action: "connect",
|
65
|
+
destination: host,
|
66
|
+
error: error,
|
67
|
+
duration: ts,
|
68
|
+
)
|
69
|
+
end
|
70
|
+
end
|
57
71
|
end
|
58
72
|
end
|
59
73
|
end
|
60
74
|
end
|
61
75
|
|
62
|
-
# RedisClient.register(NetworkResiliency::Adapter::Redis)
|
63
|
-
# Redis.new(middlewares: [NetworkResiliency::Adapter::Redis])
|
data/lib/network_resiliency.rb
CHANGED
@@ -11,10 +11,29 @@ module NetworkResiliency
|
|
11
11
|
|
12
12
|
attr_accessor :statsd
|
13
13
|
|
14
|
+
def configure
|
15
|
+
yield self
|
16
|
+
end
|
17
|
+
|
18
|
+
def patch(*adapters)
|
19
|
+
adapters.each do |adapter|
|
20
|
+
case adapter
|
21
|
+
when :http
|
22
|
+
Adapter::HTTP.patch
|
23
|
+
when :redis
|
24
|
+
Adapter::Redis.patch
|
25
|
+
else
|
26
|
+
raise NotImplementedError
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
14
31
|
def enabled?(adapter)
|
15
32
|
return true if @enabled.nil?
|
16
33
|
|
17
|
-
@enabled.is_a?(Proc) ?
|
34
|
+
@enabled.is_a?(Proc) ? !!@enabled.call(adapter) : @enabled
|
35
|
+
rescue
|
36
|
+
false
|
18
37
|
end
|
19
38
|
|
20
39
|
def enabled=(enabled)
|
@@ -48,6 +67,25 @@ module NetworkResiliency
|
|
48
67
|
Process.clock_gettime(Process::CLOCK_MONOTONIC) * 1_000
|
49
68
|
end
|
50
69
|
|
70
|
+
# private
|
71
|
+
|
72
|
+
IP_ADDRESS_REGEX = Regexp.new(/\d{1,3}(\.\d{1,3}){3}/)
|
73
|
+
|
74
|
+
def record(adapter:, action:, destination:, duration:, error: nil)
|
75
|
+
# filter raw IP addresses
|
76
|
+
return if IP_ADDRESS_REGEX.match?(destination)
|
77
|
+
|
78
|
+
NetworkResiliency.statsd&.distribution(
|
79
|
+
"network_resiliency.#{action}",
|
80
|
+
duration,
|
81
|
+
tags: {
|
82
|
+
adapter: adapter,
|
83
|
+
destination: destination,
|
84
|
+
error: error,
|
85
|
+
}.compact,
|
86
|
+
)
|
87
|
+
end
|
88
|
+
|
51
89
|
def reset
|
52
90
|
@enabled = nil
|
53
91
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: network_resiliency
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Pepper
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-10-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: byebug
|