monus 0.1.5 → 0.1.8

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
  SHA1:
3
- metadata.gz: c2e07d32bf02ceecba6f076d9a36f5761d88603a
4
- data.tar.gz: cbd46daca888e4557fcf1bf6c3b710e47c05d7cd
3
+ metadata.gz: d06a333007c12ef83cbf10f6b52d25f8cdd38717
4
+ data.tar.gz: fee56317634433c00b9da1e8c6c61b73e22470c8
5
5
  SHA512:
6
- metadata.gz: 23cf5413788123080a710c8907f33add6216d01f4ae18d23d356edeb248bae219a7344c885d99cfe5e24f9222111426c3c18c24a205c7925340b89cbfcb04e55
7
- data.tar.gz: 32d3ff3c4f2af77ff55e22c0908bb0f36cc2d2bffb0d81126547cd41f6f020e99c9aefd49391af716f6f19b77bb0fe9e2b27e2aed120002a92709de90cc43dcd
6
+ metadata.gz: c2452aaece8e5a2278cec6a48155669fddb160d498289b3fb672a732888d492465f1ef15835cc63bb0a64edf82c8e5a39f327cde8844f84847d6253eb4836ae2
7
+ data.tar.gz: 2283d374c3977ac89f9795e486df93416c1e2326a1d775760d68cdd21ad32ceb10868746f738dad515eeb9f75fd48ad842755ea437081d1d7e26dfa179fb8dd8
@@ -0,0 +1,41 @@
1
+ module Monus::BuiltInMetric::EmCallbacks
2
+ def activate
3
+ @interval = Monus.options.dig(:em_callbacks_metric_options, :interval) || 1
4
+
5
+ begin
6
+ require 'lspace/eventmachine'
7
+ rescue LoadError
8
+ raise LoadError, 'in order to use em_callbacks metric you should add `lspace` gem to your Gemfile'
9
+ end
10
+
11
+ timings = []
12
+
13
+ LSpace.around_filter do |&block|
14
+ time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
15
+ begin
16
+ block.call
17
+ ensure
18
+ delta = Process.clock_gettime(Process::CLOCK_MONOTONIC) - time
19
+ timings.push(delta)
20
+ end
21
+ end
22
+
23
+ Monus.engine.every @interval do
24
+ slice = timings.slice!(0..-1)
25
+
26
+ next if slice.empty?
27
+
28
+ number = slice.size
29
+ maximum = slice.max
30
+ total = slice.sum
31
+
32
+ Monus.write em_callbacks_number: number,
33
+ em_callbacks_maximum: maximum,
34
+ em_callbacks_total: total
35
+ end
36
+ end
37
+
38
+ extend self
39
+
40
+ Monus::BuiltInMetric.register :em_callbacks, self
41
+ end
@@ -0,0 +1,27 @@
1
+ module Monus::BuiltInMetric::EmLatency
2
+ def activate
3
+ unless Monus.engine.kind_of? Monus::Engine::EventMachine
4
+ raise LoadError, 'in order to use em_latency metric you should use :eventmachine engine'
5
+ end
6
+
7
+ @interval = Monus.options.dig(:em_latency_metric_options, :interval) || 1
8
+
9
+ last_time, current_time = nil, nil
10
+
11
+ Monus.engine.every @interval do
12
+ current_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
13
+
14
+ if last_time
15
+ latency = current_time - last_time - @interval
16
+
17
+ Monus.set :em_latency, latency
18
+ end
19
+
20
+ last_time = current_time
21
+ end
22
+ end
23
+
24
+ extend self
25
+
26
+ Monus::BuiltInMetric.register :em_latency, self
27
+ end
@@ -2,24 +2,30 @@ module Monus::BuiltInMetric::EmThreadpool
2
2
  def activate
3
3
  @interval = Monus.options.dig(:em_threadpool_metric_options, :interval) || 1
4
4
 
5
- threadpool = nil
5
+ threadpool, threadqueue = nil, nil
6
6
 
7
7
  Monus.engine.every @interval do
8
8
  threadpool ||= EM.send(:instance_variable_get, :@threadpool)
9
+ threadqueue ||= EM.send(:instance_variable_get, :@threadqueue)
9
10
 
10
- if threadpool
11
- by_status = threadpool.group_by(&:status).transform_values(&:size)
12
- by_status.default = 0
13
-
11
+ if threadpool and threadqueue
14
12
  total = threadpool.size
15
- run = by_status['run']
16
- load_percent = run.to_f / total
17
- broken = total - run - by_status['sleep']
13
+ alive = threadpool.count(&:alive?)
14
+ broken = total - alive
15
+
16
+ free = threadqueue.num_waiting
17
+ busy = alive - free
18
+ load_percent = busy.to_f / alive
19
+
20
+ threadqueue_length = threadqueue.length
18
21
 
19
22
  Monus.write em_threadpool_load: load_percent,
20
- em_threadpool_run: run,
23
+ em_threadpool_free: free,
24
+ em_threadpool_busy: busy,
21
25
  em_threadpool_total: total,
22
- em_threadpool_broken: broken
26
+ em_threadpool_alive: alive,
27
+ em_threadpool_broken: broken,
28
+ em_threadqueue_length: threadqueue_length
23
29
  end
24
30
  end
25
31
  end
@@ -0,0 +1,82 @@
1
+ require 'resolv'
2
+
3
+ module Monus::Engine::EventMachine
4
+ def prepare
5
+ @dns_cache = {}
6
+ @prepared = true
7
+ end
8
+
9
+ def make_http_connection(host, port)
10
+ load_http
11
+ EM::HttpRequest.new("http://#{host}:#{port}")
12
+ end
13
+
14
+ def send_http_request(uri, method: :get, body: nil, connection: nil)
15
+ load_http
16
+ EM.schedule do
17
+ if connection
18
+ connection.setup_request(method, path: uri, keepalive: true)
19
+ else
20
+ EM::HttpRequest.new(uri).send(method)
21
+ end
22
+ end
23
+ end
24
+
25
+ def load_http
26
+ @http_loaded ||= begin
27
+ require 'em-http-request'
28
+ true
29
+ rescue LoadError
30
+ raise LoadError, 'in order to use HTTP requests in EventMachine engine you should add `em-http-request` gem to your Gemfile'
31
+ end
32
+ end
33
+
34
+ def send_udp_datagram(message, host, port)
35
+ EM.schedule do
36
+ @udp_socket ||= EM.open_datagram_socket '0.0.0.0', 0
37
+
38
+ ip = @dns_cache[host]
39
+
40
+ if ip.nil? and (host =~ Resolv::IPv4::Regex or host =~ Resolv::IPv6::Regex)
41
+ ip = host
42
+ @dns_cache[host] = ip
43
+ end
44
+
45
+ if ip
46
+ @udp_socket.send_datagram message, ip, port
47
+ else
48
+ EM::DNS::Resolver.resolve(host).callback do |ip_list|
49
+ ip = ip_list.first
50
+ @dns_cache[host] = ip
51
+ send_udp_datagram(message, host, port)
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ def invalidate_dns_cache!
58
+ keys = @dns_cache.keys.reject { |key| key =~ Resolv::IPv4::Regex || key =~ Resolv::IPv6::Regex }
59
+ keys.each { |key| @dns_cache.delete(key) }
60
+ end
61
+
62
+ def every(interval, fire_immediately: true, on_error: nil, &block)
63
+ EM.schedule do
64
+ block.call if fire_immediately
65
+
66
+ EM.add_periodic_timer(interval) do
67
+ begin
68
+ block.call
69
+ rescue => error
70
+ interval_binding = binding
71
+ Array(on_error).each do |handler|
72
+ handler.call error, :interval, interval_binding
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ extend self
80
+
81
+ Monus::Engine.register :eventmachine, self
82
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xanders
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-18 00:00:00.000000000 Z
11
+ date: 2019-05-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Monus is an in-process monitoring library for several backends and engines
14
14
  email: necropolis@inbox.ru
@@ -22,10 +22,12 @@ files:
22
22
  - lib/monus/backend.rb
23
23
  - lib/monus/backends/influxdb.rb
24
24
  - lib/monus/built_in_metric.rb
25
+ - lib/monus/built_in_metrics/em_callbacks.rb
26
+ - lib/monus/built_in_metrics/em_latency.rb
25
27
  - lib/monus/built_in_metrics/em_threadpool.rb
26
- - lib/monus/built_in_metrics/em_ticks.rb
27
28
  - lib/monus/built_in_metrics/works.rb
28
29
  - lib/monus/engine.rb
30
+ - lib/monus/engines/eventmachine.rb
29
31
  - lib/monus/engines/pure.rb
30
32
  homepage: https://github.com/xanders/monus.rb
31
33
  licenses:
@@ -47,8 +49,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
49
  version: '0'
48
50
  requirements: []
49
51
  rubyforge_project:
50
- rubygems_version: 2.6.13
52
+ rubygems_version: 2.4.5.1
51
53
  signing_key:
52
54
  specification_version: 4
53
55
  summary: God of Monitoring
54
56
  test_files: []
57
+ has_rdoc:
@@ -1,22 +0,0 @@
1
- module Monus::BuiltInMetric::EmTicks
2
- def activate
3
- @interval = Monus.options.dig(:em_ticks_metric_options, :interval) || 1
4
-
5
- counter, time = 0, Time.now
6
-
7
- @tick_loop = EM.tick_loop do
8
- counter += 1
9
- delta = Time.now - time
10
- if delta > @interval
11
- ticks = (counter.to_f / delta).round
12
- Fiber.new { Monus.set :em_ticks, ticks }.resume
13
- counter = 0
14
- time = Time.now
15
- end
16
- end
17
- end
18
-
19
- extend self
20
-
21
- Monus::BuiltInMetric.register :em_ticks, self
22
- end