evil-metrics 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 57989b99c00c0dd7cbdb6867dc01c4bd1769d3399e53d47c498e82efce5992c2
4
+ data.tar.gz: 4723e6fab2b8b7203693426bac3130f32187209ed094bfda0946ce2ce3da893c
5
+ SHA512:
6
+ metadata.gz: c8bf69f0e6ecd8a2b8f020b62751f85fdc8980f156042a4c1ad6a152aa235e6b9c9052698c3978ba58af371f49585b1b9db8873a3a6df231523561f451b65904
7
+ data.tar.gz: 0f414417e7dff665730c07527761e05dd7e0751bed0e102ff4f17278c4f1a0241adc42f49d54f40d163e42eab51e48ffa7053aa917ff43ba020c786392dd9b46
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ Gemfile.lock
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,46 @@
1
+ ---
2
+ require:
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.3
7
+
8
+ Metrics/BlockLength:
9
+ Exclude:
10
+ - "Gemfile"
11
+ - "spec/**/*"
12
+
13
+ Style/BracesAroundHashParameters:
14
+ EnforcedStyle: context_dependent
15
+
16
+ Style/StringLiterals:
17
+ EnforcedStyle: double_quotes
18
+
19
+ # Allow to use let!
20
+ RSpec/LetSetup:
21
+ Enabled: false
22
+
23
+ RSpec/MultipleExpectations:
24
+ Enabled: false
25
+
26
+ Bundler/OrderedGems:
27
+ Enabled: false
28
+
29
+ Style/TrailingCommaInArguments:
30
+ Description: 'Checks for trailing comma in argument lists.'
31
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-params-comma'
32
+ Enabled: true
33
+ EnforcedStyleForMultiline: consistent_comma
34
+
35
+ Style/TrailingCommaInArrayLiteral:
36
+ Description: 'Checks for trailing comma in array literals.'
37
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
38
+ Enabled: true
39
+ EnforcedStyleForMultiline: consistent_comma
40
+
41
+ Style/TrailingCommaInHashLiteral:
42
+ Description: 'Checks for trailing comma in hash literals.'
43
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
44
+ Enabled: true
45
+ EnforcedStyleForMultiline: consistent_comma
46
+
@@ -0,0 +1,9 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.1
7
+ - 2.4.4
8
+ - 2.3.7
9
+ before_install: gem install bundler -v 1.16.5
@@ -0,0 +1 @@
1
+ --plugin dry-initializer
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in evil-metrics.gemspec
8
+ gemspec
9
+
10
+ group :development, :test do
11
+ gem "pry"
12
+ gem "pry-byebug", platform: :mri
13
+
14
+ gem "rubocop"
15
+ gem "rubocop-rspec"
16
+ end
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Andrey Novikov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,100 @@
1
+ # Evil::Metrics
2
+
3
+ **This software is Work in Progress: features will appear and disappear, API will be changed, your feedback is always welcome!**
4
+
5
+ Extensible solution for easy setup of monitoring in your Ruby apps.
6
+
7
+ ## Installation
8
+
9
+ Most of the time you don't need to add this gem to your Gemfile directly (unless you're only collecting your custom metrics):
10
+
11
+ ```ruby
12
+ gem 'evil-metrics'
13
+ # Then add monitoring system adapter, e.g.:
14
+ # gem 'evil-metrics-prometheus'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ ## Usage
22
+
23
+ 1. Declare your metrics:
24
+
25
+ ```ruby
26
+ Evil::Metrics.configure do
27
+ group :your_app
28
+
29
+ counter :bells_rang_count, "Total number of bells being rang"
30
+ gauge :whistles_active, "Number of whistles ready to whistle"
31
+ histogram :whistle_runtime, "How long whistles are being active", unit: :seconds
32
+ end
33
+ ```
34
+
35
+ 2. Access metric in your app and use it!
36
+
37
+ ```ruby
38
+ def ring_the_bell(id)
39
+ bell = Bell.find(id)
40
+ bell.ring!
41
+ Evil::Metrics.your_app_bells_rang_count.increment({bell_size: bell.size}, by: 1)
42
+ end
43
+
44
+ def whistle!
45
+ Evil::Metrics.your_app_whistle_runtime.measure do
46
+ # Run your code
47
+ end
48
+ end
49
+ ```
50
+
51
+ 3. Setup collecting of metrics that do not tied to specific events in you application. E.g.: reporting your app's current state
52
+ ```ruby
53
+ Evil::Metrics.configure do
54
+ # This block will be executed periodically few times in a minute
55
+ # (by timer or external request depending on adapter you're using)
56
+ # Keep it fast and simple!
57
+ collect do
58
+ your_app_whistles_active.set({}, Whistle.where(state: :active).count
59
+ end
60
+ end
61
+ ```
62
+
63
+ 4. See the docs for the adapter you're using
64
+ 5. Enjoy!
65
+
66
+ ## Roadmap (aka TODO or Help wanted)
67
+
68
+ - Ability to change metric settings for individual adapters
69
+
70
+ ```rb
71
+ histogram :foo, comment: "say what?" do
72
+ adapter :prometheus do
73
+ buckets [0.01, 0.5, …, 60, 300, 3600]
74
+ end
75
+ end
76
+ ```
77
+
78
+ - Ability to route some metrics only for given adapter:
79
+
80
+ ```rb
81
+ adapter :prometheus do
82
+ include_group :sidekiq
83
+ end
84
+ ```
85
+
86
+
87
+
88
+ ## Development
89
+
90
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
91
+
92
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
93
+
94
+ ## Contributing
95
+
96
+ Bug reports and pull requests are welcome on GitHub at https://github.com/evil-metrics/evil-metrics.
97
+
98
+ ## License
99
+
100
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "evil/metrics"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ require "pry"
11
+ Pry.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path("lib", __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require "evil/metrics/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "evil-metrics"
9
+ spec.version = Evil::Metrics::VERSION
10
+ spec.authors = ["Andrey Novikov"]
11
+ spec.email = ["envek@envek.name"]
12
+
13
+ spec.summary = "Extensible framework for collecting metric for your Ruby application"
14
+ spec.description = <<~DESCRIPTION
15
+ Collect statistics about how your application is performing with ease. \
16
+ Export metrics to various monitoring systems.
17
+ DESCRIPTION
18
+ spec.homepage = "https://github.com/evil-metrics/evil-metrics"
19
+ spec.license = "MIT"
20
+
21
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
22
+ f.match(%r{^(test|spec|features)/})
23
+ end
24
+ spec.bindir = "exe"
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ["lib"]
27
+
28
+ spec.add_dependency "concurrent-ruby"
29
+ spec.add_dependency "dry-initializer"
30
+
31
+ spec.add_development_dependency "bundler", "~> 1.16"
32
+ spec.add_development_dependency "rake", "~> 12.0"
33
+ spec.add_development_dependency "rspec", "~> 3.0"
34
+ spec.add_development_dependency "yard"
35
+ spec.add_development_dependency "yard-dry-initializer"
36
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "concurrent"
4
+
5
+ require "evil/metrics/version"
6
+ require "evil/metrics/dsl"
7
+
8
+ module Evil
9
+ module Metrics
10
+ include DSL
11
+
12
+ cattr_reader :metrics, default: Concurrent::Hash.new
13
+ cattr_reader :adapters, default: Concurrent::Hash.new
14
+ cattr_reader :collectors, default: Concurrent::Array.new
15
+
16
+ class << self
17
+ # @param [Symbol] name
18
+ # @param [BaseAdapter] instance
19
+ def register_adapter(name, instance)
20
+ adapters[name] = instance
21
+ # NOTE: Pretty sure there is race condition
22
+ metrics.each do |_, metric|
23
+ instance.register!(metric)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Evil
4
+ module Metrics
5
+ class BaseAdapter
6
+ def register!(metric)
7
+ case metric
8
+ when Counter then register_counter!(metric)
9
+ when Gauge then register_gauge!(metric)
10
+ when Histogram then register_histogram!(metric)
11
+ else raise "#{metric.class} is unknown metric type"
12
+ end
13
+ end
14
+
15
+ def register_counter!(_metric)
16
+ raise NotImplementedError, "#{self.class} doesn't support counters as metric type!"
17
+ end
18
+
19
+ def perform_counter_increment!(_counter, _tags, _increment)
20
+ raise NotImplementedError, "#{self.class} doesn't support incrementing counters"
21
+ end
22
+
23
+ def register_gauge!(_metric)
24
+ raise NotImplementedError, "#{self.class} doesn't support gauges as metric type!"
25
+ end
26
+
27
+ def perform_gauge_set!(_metric, _tags, _value)
28
+ raise NotImplementedError, "#{self.class} doesn't support setting gauges"
29
+ end
30
+
31
+ def register_histogram!(_metric)
32
+ raise NotImplementedError, "#{self.class} doesn't support histograms as metric type!"
33
+ end
34
+
35
+ def perform_histogram_measure!(_metric, _tags, _value)
36
+ raise NotImplementedError, "#{self.class} doesn't support measuring histograms"
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Evil
4
+ module Metrics
5
+ # Growing-only counter
6
+ class Counter < Metric
7
+ def increment(tags, by: 1)
8
+ values[tags] += by
9
+ ::Evil::Metrics.adapters.each do |_, adapter|
10
+ adapter.perform_counter_increment!(self, tags, by)
11
+ end
12
+ values[tags]
13
+ end
14
+
15
+ def values
16
+ @values ||= Concurrent::Hash.new(0)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "evil/metrics/metric"
4
+ require "evil/metrics/counter"
5
+ require "evil/metrics/gauge"
6
+ require "evil/metrics/histogram"
7
+
8
+ module Evil
9
+ module Metrics
10
+ module DSL
11
+ def self.included(base)
12
+ base.extend ClassMethods
13
+ end
14
+
15
+ module ClassMethods
16
+ def configure(&block)
17
+ class_exec(&block)
18
+ @group = nil
19
+ end
20
+
21
+ def collect(&block)
22
+ ::Evil::Metrics.collectors.push(block)
23
+ end
24
+
25
+ def group(group_name)
26
+ @group = group_name
27
+ end
28
+
29
+ def counter(*args, **kwargs)
30
+ register(Counter.new(*args, **kwargs, group: @group))
31
+ end
32
+
33
+ def gauge(*args, **kwargs)
34
+ register(Gauge.new(*args, **kwargs, group: @group))
35
+ end
36
+
37
+ def histogram(*args, **kwargs)
38
+ register(Histogram.new(*args, **kwargs, group: @group))
39
+ end
40
+
41
+ private
42
+
43
+ def register(metric)
44
+ name = [metric.group, metric.name].compact.join("_")
45
+ ::Evil::Metrics.define_singleton_method(name) { metric }
46
+ ::Evil::Metrics.metrics[name] = metric
47
+ ::Evil::Metrics.adapters.each do |_, adapter|
48
+ adapter.register!(metric)
49
+ end
50
+ metric
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Evil
4
+ module Metrics
5
+ # Arbitrary value
6
+ class Gauge < Metric
7
+ def set(tags, value)
8
+ values[tags] = value
9
+ ::Evil::Metrics.adapters.each do |_, adapter|
10
+ adapter.perform_gauge_set!(self, tags, value)
11
+ end
12
+ value
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Evil
4
+ module Metrics
5
+ class Histogram < Metric
6
+ option :buckets
7
+
8
+ def measure(tags, value)
9
+ values[tags] = value
10
+ ::Evil::Metrics.adapters.each do |_, adapter|
11
+ adapter.perform_histogram_measure!(self, tags, value)
12
+ end
13
+ value
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry-initializer"
4
+
5
+ module Evil
6
+ module Metrics
7
+ # Base class for all metrics
8
+ class Metric
9
+ extend Dry::Initializer
10
+
11
+ param :name, comment: "Metric name. Use snake_case. E.g. job_runtime"
12
+ option :comment, optional: true, comment: "Documentation string. Required by some adapters."
13
+ option :unit, optional: true, comment: "In which units it is measured. E.g. `seconds`"
14
+ option :per, optional: true, comment: "Per which unit is measured `unit`. E.g. `call` as in seconds per call"
15
+ option :group, optional: true, comment: "Category name for grouping metrics"
16
+
17
+ # Returns the value for the given label set
18
+ def get(labels = {})
19
+ values[labels]
20
+ end
21
+
22
+ def values
23
+ @values ||= Concurrent::Hash.new
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Evil
4
+ module Metrics
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: evil-metrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrey Novikov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-10-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: concurrent-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: dry-initializer
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.16'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard-dry-initializer
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: 'Collect statistics about how your application is performing with ease. Export
112
+ metrics to various monitoring systems.
113
+
114
+ '
115
+ email:
116
+ - envek@envek.name
117
+ executables: []
118
+ extensions: []
119
+ extra_rdoc_files: []
120
+ files:
121
+ - ".gitignore"
122
+ - ".rspec"
123
+ - ".rubocop.yml"
124
+ - ".travis.yml"
125
+ - ".yardopts"
126
+ - Gemfile
127
+ - LICENSE.txt
128
+ - README.md
129
+ - Rakefile
130
+ - bin/console
131
+ - bin/setup
132
+ - evil-metrics.gemspec
133
+ - lib/evil/metrics.rb
134
+ - lib/evil/metrics/base_adapter.rb
135
+ - lib/evil/metrics/counter.rb
136
+ - lib/evil/metrics/dsl.rb
137
+ - lib/evil/metrics/gauge.rb
138
+ - lib/evil/metrics/histogram.rb
139
+ - lib/evil/metrics/metric.rb
140
+ - lib/evil/metrics/version.rb
141
+ homepage: https://github.com/evil-metrics/evil-metrics
142
+ licenses:
143
+ - MIT
144
+ metadata: {}
145
+ post_install_message:
146
+ rdoc_options: []
147
+ require_paths:
148
+ - lib
149
+ required_ruby_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ required_rubygems_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ requirements: []
160
+ rubyforge_project:
161
+ rubygems_version: 2.7.6
162
+ signing_key:
163
+ specification_version: 4
164
+ summary: Extensible framework for collecting metric for your Ruby application
165
+ test_files: []