ruby_script_exporter 0.0.2 → 0.0.4

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: c519baa82ed0393d7b038669bf035a4bc05018a0ecb8da173ead0ad9d2b58d70
4
- data.tar.gz: '0876858a6b675cbc71f0c52f39584dfe3fd5b21850ec54080694416339780a20'
3
+ metadata.gz: 91137cebd5e7ca4dcfeabeeb3994e7d4564baa586d3764106b1cb651c17e7cca
4
+ data.tar.gz: 3c8283bd2612f3386c1532dcf9bfefb95d6a54a6cab144eb78466743197b3aa1
5
5
  SHA512:
6
- metadata.gz: e97359edfa445da493e7cf9a3fe75716aed59ecef67b387b2b9b733f2696d709e030e9b51fa56433ea1acddbb65abb0e3afeaae167ccb943b64b8f6214c598d8
7
- data.tar.gz: ab958039e3494ef2141bb10da35596368fd63957d92e4ce65013ba1ea9c18f9dd94a44106806404d009ac194bb9362ab0a6a19a577e8afc8789aa0a956b054a1
6
+ metadata.gz: cc734f579a2707398aa24faecac8bde966820ce4ece8072984c256095d5e8ed7ce375e7e162f65882667098f566a12f62c5002f865cbfdca1fbaa2408f00fcb3
7
+ data.tar.gz: 20781aa8bf90e705e6c6a96f625033a0b5cc7415afdb72fd766bb9e75a2805863c858e1291b9aeabbbf76a5ba6d5d20e31e51a590589dcf5aa3229733bd2938c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 0.0.4
2
+
3
+ * Add HTTP observer
4
+
5
+ ## 0.0.3
6
+
7
+ * Add MIT license
8
+
1
9
  ## 0.0.2
2
10
 
3
11
  * Added specs
data/Gemfile.lock CHANGED
@@ -1,18 +1,44 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ruby_script_exporter (0.0.2)
4
+ ruby_script_exporter (0.0.4)
5
+ http (~> 5.2.0)
5
6
  rackup (~> 2.1.0)
6
7
  sinatra (~> 4.0.0)
7
8
 
8
9
  GEM
9
10
  remote: https://rubygems.org/
10
11
  specs:
12
+ addressable (2.8.7)
13
+ public_suffix (>= 2.0.2, < 7.0)
11
14
  base64 (0.2.0)
15
+ bigdecimal (3.1.8)
16
+ byebug (11.1.3)
17
+ crack (1.0.0)
18
+ bigdecimal
19
+ rexml
12
20
  diff-lcs (1.5.0)
13
- mqtt (0.6.0)
21
+ domain_name (0.6.20240107)
22
+ ffi (1.17.0-x86_64-linux-gnu)
23
+ ffi-compiler (1.3.2)
24
+ ffi (>= 1.15.5)
25
+ rake
26
+ hashdiff (1.1.0)
27
+ http (5.2.0)
28
+ addressable (~> 2.8)
29
+ base64 (~> 0.1)
30
+ http-cookie (~> 1.0)
31
+ http-form_data (~> 2.2)
32
+ llhttp-ffi (~> 0.5.0)
33
+ http-cookie (1.0.6)
34
+ domain_name (~> 0.5)
35
+ http-form_data (2.3.0)
36
+ llhttp-ffi (0.5.0)
37
+ ffi-compiler (~> 1.0)
38
+ rake (~> 13.0)
14
39
  mustermann (3.0.0)
15
40
  ruby2_keywords (~> 0.0.1)
41
+ public_suffix (6.0.0)
16
42
  rack (3.0.8)
17
43
  rack-protection (4.0.0)
18
44
  base64 (>= 0.1.0)
@@ -23,6 +49,8 @@ GEM
23
49
  rack (>= 3)
24
50
  webrick (~> 1.8)
25
51
  rake (13.0.6)
52
+ rexml (3.3.1)
53
+ strscan
26
54
  rspec (3.12.0)
27
55
  rspec-core (~> 3.12.0)
28
56
  rspec-expectations (~> 3.12.0)
@@ -43,19 +71,25 @@ GEM
43
71
  rack-protection (= 4.0.0)
44
72
  rack-session (>= 2.0.0, < 3)
45
73
  tilt (~> 2.0)
74
+ strscan (3.1.0)
46
75
  tilt (2.3.0)
47
76
  timecop (0.9.8)
77
+ webmock (3.23.1)
78
+ addressable (>= 2.8.0)
79
+ crack (>= 0.3.2)
80
+ hashdiff (>= 0.4.0, < 2.0.0)
48
81
  webrick (1.8.1)
49
82
 
50
83
  PLATFORMS
51
84
  x86_64-linux
52
85
 
53
86
  DEPENDENCIES
54
- mqtt
87
+ byebug (~> 11.1.3)
55
88
  rake (~> 13.0)
56
89
  rspec (~> 3.6)
57
90
  ruby_script_exporter!
58
91
  timecop (~> 0.9.8)
92
+ webmock (~> 3.23.1)
59
93
 
60
94
  BUNDLED WITH
61
95
  2.4.10
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2024 Martin Schaflitzl
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -12,6 +12,7 @@ ruby_script_exporter is a small framework to expose metrics produced by ruby sni
12
12
  Usage: ruby_script_exporter [options]
13
13
  -s SERVICE_DIR, --script-directory Specify where to look for service definitions
14
14
  -r, --reload-on-request Reload service definitions for every request, useful for developing probes
15
+ --raise-errors Raise on errors, dont just report the probe as failed
15
16
  ```
16
17
 
17
18
  ## Example Probe
@@ -62,3 +63,23 @@ total_probe_count 1
62
63
  ```
63
64
 
64
65
  Next to `some_metric` there are also a number of internal metrics exposed.
66
+
67
+ ## Build in observers
68
+
69
+ ### HTTP
70
+
71
+ Used to check a HTTP endpoint.
72
+
73
+ ```
74
+ service 'some_http_service' do
75
+ probe 'some probe' do
76
+ label :some_label, 'Foo'
77
+
78
+ run do
79
+ observe_http("https://example.com",
80
+ expected_body: /Example/,
81
+ )
82
+ end
83
+ end
84
+ end
85
+ ```
@@ -5,7 +5,7 @@ service 'dummy service' do
5
5
 
6
6
  probe 'dummy probe' do
7
7
  run do
8
- observe :time, Time.now
8
+ observe :time, Time.now.to_i
9
9
  end
10
10
  end
11
11
 
@@ -13,7 +13,7 @@ service 'dummy service' do
13
13
  cache_for 30
14
14
 
15
15
  run do
16
- observe :time, Time.now
16
+ observe :time, Time.now.to_i
17
17
  end
18
18
  end
19
19
  end
@@ -5,27 +5,8 @@ require "bundler/setup"
5
5
  require 'optparse'
6
6
  require "ruby_script_exporter"
7
7
 
8
- options = {
9
- service_dir: 'example_services/',
10
- reload_on_request: false,
11
- port: 9100,
12
- host: '0.0.0.0',
13
- }
8
+ options = RubyScriptExporter::Util.parse_options
14
9
 
15
- OptionParser.new do |opts|
16
- opts.banner = "Usage: ruby_script_exporter [options]"
17
-
18
- opts.on("-s SERVICE_DIR", "--script-directory SERVICE_DIR", "Specify where to look for service definitions") do |service_dir|
19
- options[:service_dir] = service_dir
20
- end
21
-
22
- opts.on("-r", "--reload-on-request", "Reload service definitions for every request, useful for developing probes") do
23
- options[:reload_on_request] = true
24
- end
25
- end.parse!
26
-
27
- RubyScriptExporter::Server.service_directory = options[:service_dir]
28
- RubyScriptExporter::Server.reload_on_request = options[:reload_on_request]
29
10
  RubyScriptExporter::Server.set :port, options[:port]
30
11
  RubyScriptExporter::Server.set :bind , options[:host]
31
12
  RubyScriptExporter::Server.run!
@@ -1,6 +1,14 @@
1
1
  module RubyScriptExporter
2
2
  class Executor
3
3
 
4
+ Type.register_type(:cached_probe_count, :gauge, 'Count of probes which returned a cached result', global: true)
5
+ Type.register_type(:error_probe_count, :gauge, 'Count probes witch threw an error while executing', global: true)
6
+ Type.register_type(:successful_probe_count, :gauge, 'Count of probes which ran successfully', global: true)
7
+ Type.register_type(:timeout_probe_count, :gauge, 'Count of probes which timed out', global: true)
8
+ Type.register_type(:total_probe_count, :gauge, 'Total probe count', global: true)
9
+ Type.register_type(:probe_execution_time, :gauge, 'Execution time per probe', global: true)
10
+ Type.register_type(:total_execution_time, :gauge, 'Total execution time', global: true)
11
+
4
12
  def initialize(services, report_execution_time: false, report_counts: false)
5
13
  @services = services
6
14
  @report_execution_time = report_execution_time
@@ -26,10 +34,12 @@ module RubyScriptExporter
26
34
 
27
35
  begin
28
36
  probe_measurements, execution_time = probe.run
29
- rescue Probe::ScriptError
37
+ rescue Probe::ScriptError => e
38
+ STDERR.puts "Error while executing #{probe.inspect}: #{e.inspect}"
30
39
  error_count += 1
31
40
  next
32
41
  rescue Probe::ScriptTimeout
42
+ STDERR.puts "Timeout while executing #{probe.inspect}"
33
43
  timeout_count += 1
34
44
  next
35
45
  end
@@ -0,0 +1,44 @@
1
+ require 'http'
2
+
3
+ module RubyScriptExporter
4
+ module Observers
5
+ module Http
6
+ Type.register_type(:http_status, :gauge, "HTTP response status: 0:good, 1:refused, 2:timeout, 3:name_not_resolved, 4:connection_failed, 5:certificate_error, 6:response_code_invalid, 7:response_body_invalid")
7
+ Type.register_type(:http_response_time, :gauge, "HTTP response time")
8
+
9
+ def observe_http(url, method: :get, expected_response_code: 200, expected_body: nil, timeout: 0.9)
10
+ start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
11
+ response = HTTP.timeout(timeout).public_send(method, url)
12
+ end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
13
+
14
+ observe :http_response_time, end_time - start_time
15
+
16
+ if expected_response_code && expected_response_code != response.code
17
+ observe :http_status, 6
18
+ return
19
+ end
20
+
21
+ if expected_body && !response.body.to_s.match?(expected_body)
22
+ observe :http_status, 7
23
+ return
24
+ end
25
+
26
+ observe :http_status, 0
27
+ rescue HTTP::ConnectionError => e
28
+ case e.message
29
+ when /Name or service not known/
30
+ observe :http_status, 3 # Name not resolved
31
+ when /Connection refused/
32
+ observe :http_status, 1 # Connection refused
33
+ else
34
+ observe :http_status, 4 # Unknown connection error
35
+ end
36
+ rescue HTTP::ConnectTimeoutError, HTTP::TimeoutError => e
37
+ observe :http_status, 2 # Connection timeout
38
+ rescue OpenSSL::SSL::SSLError
39
+ observe :http_status, 5 # Certificate invalid
40
+ end
41
+
42
+ end
43
+ end
44
+ end
@@ -13,6 +13,14 @@ module RubyScriptExporter
13
13
  attr_writer :runner_proc
14
14
  attr_reader :service
15
15
 
16
+ def self.raise_errors=(raise_errors)
17
+ @raise_errors = raise_errors
18
+ end
19
+
20
+ def self.raise_errors
21
+ @raise_errors
22
+ end
23
+
16
24
  def initialize(name, service)
17
25
  @name = name
18
26
  @service = service
@@ -51,8 +59,12 @@ module RubyScriptExporter
51
59
  end
52
60
  rescue Timeout::Error
53
61
  raise ScriptTimeout
54
- rescue StandardError
55
- raise ScriptError
62
+ rescue StandardError => e
63
+ if self.class.raise_errors
64
+ raise e
65
+ end
66
+
67
+ raise ScriptError, e.inspect
56
68
  end
57
69
 
58
70
  execution_time = end_time - start_time
@@ -1,6 +1,8 @@
1
1
  module RubyScriptExporter
2
2
  class Runner
3
3
 
4
+ include Observers::Http
5
+
4
6
  attr_reader :measurements
5
7
 
6
8
  def initialize(probe)
@@ -1,4 +1,5 @@
1
1
  require 'sinatra'
2
+ require 'logger'
2
3
 
3
4
  module RubyScriptExporter
4
5
  class Server < Sinatra::Base
@@ -17,11 +18,16 @@ module RubyScriptExporter
17
18
  @services = RubyScriptExporter::ScriptLoader.load_directory(@service_directory)
18
19
  end
19
20
 
21
+ def self.run
22
+ measurements = Executor.new(services, report_execution_time: true, report_counts: true).run
23
+ Formatter.new(measurements).format
24
+ end
25
+
20
26
  set :default_content_type, 'text'
27
+ set :logging, Logger::DEBUG
21
28
 
22
29
  get '/metrics' do
23
- measurements = Executor.new(self.class.services, report_execution_time: true, report_counts: true).run
24
- Formatter.new(measurements).format
30
+ self.class.run
25
31
  end
26
32
  end
27
33
  end
@@ -1,18 +1,21 @@
1
1
  module RubyScriptExporter
2
2
  class Type
3
3
 
4
+ attr_reader :global
5
+
4
6
  @types = {}
5
7
 
6
- def initialize(name, type, help = nil)
8
+ def initialize(name, type, help = nil, global: false)
7
9
  @name = name
8
10
  @type = type
9
11
  @help = help
12
+ @global = global
10
13
  end
11
14
 
12
- def self.register_type(name, type, help = nil)
15
+ def self.register_type(name, type, help = nil, global: false)
13
16
  raise ArgumentError, "Type for measurement '#{name}' already registered." if @types.key?(name)
14
17
 
15
- @types[name.to_sym] = Type.new(name, type, help)
18
+ @types[name.to_sym] = Type.new(name, type, help, global: global)
16
19
  end
17
20
 
18
21
  def self.from_name(name)
@@ -26,15 +29,12 @@ module RubyScriptExporter
26
29
  type
27
30
  end
28
31
 
32
+ def self.types
33
+ @types
34
+ end
35
+
29
36
  def self.reset_types
30
- @types = {}
31
- register_type(:cached_probe_count, :gauge, 'Count of probes which returned a cached result')
32
- register_type(:error_probe_count, :gauge, 'Count probes witch threw an error while executing')
33
- register_type(:successful_probe_count, :gauge, 'Count of probes which ran successfully')
34
- register_type(:timeout_probe_count, :gauge, 'Count of probes which timed out')
35
- register_type(:total_probe_count, :gauge, 'Total probe count')
36
- register_type(:probe_execution_time, :gauge, 'Execution time per probe')
37
- register_type(:total_execution_time, :gauge, 'Total execution time')
37
+ @types = @types.select { |_, v| v.global }
38
38
  end
39
39
 
40
40
  def format_for_open_metrics
@@ -7,5 +7,38 @@ module RubyScriptExporter
7
7
  "#{count} #{string}"
8
8
  end
9
9
 
10
+ def self.parse_options
11
+ options = {
12
+ service_dir: 'example_services/',
13
+ reload_on_request: false,
14
+ port: 9100,
15
+ host: '0.0.0.0',
16
+ raise_errors: false,
17
+ }
18
+
19
+ OptionParser.new do |opts|
20
+ opts.banner = "Usage: ruby_script_exporter [options]"
21
+
22
+ opts.on("-s SERVICE_DIR", "--script-directory SERVICE_DIR", "Specify where to look for service definitions") do |service_dir|
23
+ options[:service_dir] = service_dir
24
+ end
25
+
26
+ opts.on("-r", "--reload-on-request", "Reload service definitions for every request, useful for developing probes") do
27
+ options[:reload_on_request] = true
28
+ end
29
+
30
+ opts.on("--raise-errors", "Stop and print errors instead of raising the error count") do
31
+ options[:raise_errors] = true
32
+ end
33
+
34
+ end.parse!
35
+
36
+ RubyScriptExporter::Server.service_directory = options[:service_dir]
37
+ RubyScriptExporter::Server.reload_on_request = options[:reload_on_request]
38
+ RubyScriptExporter::Probe.raise_errors = options[:raise_errors]
39
+
40
+ options
41
+ end
42
+
10
43
  end
11
44
  end
@@ -1,3 +1,3 @@
1
1
  module RubyScriptExporter
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "ruby_script_exporter/version"
4
+ require_relative "ruby_script_exporter/type"
5
+ require_relative "ruby_script_exporter/observers/http"
4
6
  require_relative "ruby_script_exporter/probe"
5
7
  require_relative "ruby_script_exporter/service"
6
8
  require_relative "ruby_script_exporter/server"
@@ -9,5 +11,4 @@ require_relative "ruby_script_exporter/runner"
9
11
  require_relative "ruby_script_exporter/measurement"
10
12
  require_relative "ruby_script_exporter/formatter"
11
13
  require_relative "ruby_script_exporter/script_loader"
12
- require_relative "ruby_script_exporter/util"
13
- require_relative "ruby_script_exporter/type"
14
+ require_relative "ruby_script_exporter/util"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby_script_exporter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Schaflitzl
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-01-30 00:00:00.000000000 Z
11
+ date: 2024-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sinatra
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 2.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: http
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 5.2.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 5.2.0
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rspec
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -80,6 +94,34 @@ dependencies:
80
94
  - - "~>"
81
95
  - !ruby/object:Gem::Version
82
96
  version: '13.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 11.1.3
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 11.1.3
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 3.23.1
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 3.23.1
83
125
  description:
84
126
  email:
85
127
  - gems@martin-sc.de
@@ -93,6 +135,7 @@ files:
93
135
  - CHANGELOG.md
94
136
  - Gemfile
95
137
  - Gemfile.lock
138
+ - LICENSE
96
139
  - README.md
97
140
  - Rakefile
98
141
  - example_services/dummy.rb
@@ -101,6 +144,7 @@ files:
101
144
  - lib/ruby_script_exporter/executor.rb
102
145
  - lib/ruby_script_exporter/formatter.rb
103
146
  - lib/ruby_script_exporter/measurement.rb
147
+ - lib/ruby_script_exporter/observers/http.rb
104
148
  - lib/ruby_script_exporter/probe.rb
105
149
  - lib/ruby_script_exporter/runner.rb
106
150
  - lib/ruby_script_exporter/script_loader.rb
@@ -110,10 +154,12 @@ files:
110
154
  - lib/ruby_script_exporter/util.rb
111
155
  - lib/ruby_script_exporter/version.rb
112
156
  homepage: https://rubygems.org/gems/ruby_script_exporter
113
- licenses: []
157
+ licenses:
158
+ - MIT
114
159
  metadata:
115
160
  homepage_uri: https://rubygems.org/gems/ruby_script_exporter
116
161
  source_code_uri: https://github.com/mschaf/ruby_script_exporter
162
+ changelog_uri: https://github.com/mschaf/ruby_script_exporter/blob/main/CHANGELOG.md
117
163
  post_install_message:
118
164
  rdoc_options: []
119
165
  require_paths: