yabeda 0.1.1

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: 8303dda88bd4a759800248c44901f651992a8d38a0d75bf1d974b169a0e3020a
4
+ data.tar.gz: 53247638e8aa2697ceac88c70ff6155490a6dbcda5550feee1d5243e12280085
5
+ SHA512:
6
+ metadata.gz: a4a8044919d940c2d1c4f4b779303e6424d68224aae578660cdd932630a57de4f4f41bd03b9f40aa1f285ecff0c09f1c6c760597ffe04f8a1a38d8e2e28c5f9a
7
+ data.tar.gz: 11d0aadd135fa73fa60d9764cc518abf0966b0d44d1f607ed4244e79d1166792d4de9c9857bcfc7ec62b1641f3b843a4b0fb63de02932c9eea1fb8a1435a408b
@@ -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 yabeda.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
+ # Yabeda
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 'yabeda'
13
+ # Then add monitoring system adapter, e.g.:
14
+ # gem 'yabeda-prometheus'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ ## Usage
22
+
23
+ 1. Declare your metrics:
24
+
25
+ ```ruby
26
+ Yabeda.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
+ Yabeda.your_app_bells_rang_count.increment({bell_size: bell.size}, by: 1)
42
+ end
43
+
44
+ def whistle!
45
+ Yabeda.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
+ Yabeda.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/yabeda-rb/yabeda.
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 "yabeda"
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,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "concurrent"
4
+
5
+ require "yabeda/version"
6
+ require "yabeda/dsl"
7
+
8
+ module Yabeda
9
+ include DSL
10
+
11
+ cattr_reader :metrics, default: Concurrent::Hash.new
12
+ cattr_reader :adapters, default: Concurrent::Hash.new
13
+ cattr_reader :collectors, default: Concurrent::Array.new
14
+
15
+ class << self
16
+ # @param [Symbol] name
17
+ # @param [BaseAdapter] instance
18
+ def register_adapter(name, instance)
19
+ adapters[name] = instance
20
+ # NOTE: Pretty sure there is race condition
21
+ metrics.each do |_, metric|
22
+ instance.register!(metric)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yabeda
4
+ class BaseAdapter
5
+ def register!(metric)
6
+ case metric
7
+ when Counter then register_counter!(metric)
8
+ when Gauge then register_gauge!(metric)
9
+ when Histogram then register_histogram!(metric)
10
+ else raise "#{metric.class} is unknown metric type"
11
+ end
12
+ end
13
+
14
+ def register_counter!(_metric)
15
+ raise NotImplementedError, "#{self.class} doesn't support counters as metric type!"
16
+ end
17
+
18
+ def perform_counter_increment!(_counter, _tags, _increment)
19
+ raise NotImplementedError, "#{self.class} doesn't support incrementing counters"
20
+ end
21
+
22
+ def register_gauge!(_metric)
23
+ raise NotImplementedError, "#{self.class} doesn't support gauges as metric type!"
24
+ end
25
+
26
+ def perform_gauge_set!(_metric, _tags, _value)
27
+ raise NotImplementedError, "#{self.class} doesn't support setting gauges"
28
+ end
29
+
30
+ def register_histogram!(_metric)
31
+ raise NotImplementedError, "#{self.class} doesn't support histograms as metric type!"
32
+ end
33
+
34
+ def perform_histogram_measure!(_metric, _tags, _value)
35
+ raise NotImplementedError, "#{self.class} doesn't support measuring histograms"
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yabeda
4
+ # Growing-only counter
5
+ class Counter < Metric
6
+ def increment(tags, by: 1)
7
+ values[tags] += by
8
+ ::Yabeda.adapters.each do |_, adapter|
9
+ adapter.perform_counter_increment!(self, tags, by)
10
+ end
11
+ values[tags]
12
+ end
13
+
14
+ def values
15
+ @values ||= Concurrent::Hash.new(0)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yabeda/metric"
4
+ require "yabeda/counter"
5
+ require "yabeda/gauge"
6
+ require "yabeda/histogram"
7
+
8
+ module Yabeda
9
+ module DSL
10
+ def self.included(base)
11
+ base.extend ClassMethods
12
+ end
13
+
14
+ module ClassMethods
15
+ def configure(&block)
16
+ class_exec(&block)
17
+ @group = nil
18
+ end
19
+
20
+ def collect(&block)
21
+ ::Yabeda.collectors.push(block)
22
+ end
23
+
24
+ def group(group_name)
25
+ @group = group_name
26
+ end
27
+
28
+ def counter(*args, **kwargs)
29
+ register(Counter.new(*args, **kwargs, group: @group))
30
+ end
31
+
32
+ def gauge(*args, **kwargs)
33
+ register(Gauge.new(*args, **kwargs, group: @group))
34
+ end
35
+
36
+ def histogram(*args, **kwargs)
37
+ register(Histogram.new(*args, **kwargs, group: @group))
38
+ end
39
+
40
+ private
41
+
42
+ def register(metric)
43
+ name = [metric.group, metric.name].compact.join("_")
44
+ ::Yabeda.define_singleton_method(name) { metric }
45
+ ::Yabeda.metrics[name] = metric
46
+ ::Yabeda.adapters.each do |_, adapter|
47
+ adapter.register!(metric)
48
+ end
49
+ metric
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yabeda
4
+ # Arbitrary value
5
+ class Gauge < Metric
6
+ def set(tags, value)
7
+ values[tags] = value
8
+ ::Yabeda.adapters.each do |_, adapter|
9
+ adapter.perform_gauge_set!(self, tags, value)
10
+ end
11
+ value
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yabeda
4
+ class Histogram < Metric
5
+ option :buckets
6
+
7
+ def measure(tags, value)
8
+ values[tags] = value
9
+ ::Yabeda.adapters.each do |_, adapter|
10
+ adapter.perform_histogram_measure!(self, tags, value)
11
+ end
12
+ value
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry-initializer"
4
+
5
+ module Yabeda
6
+ # Base class for all metrics
7
+ class Metric
8
+ extend Dry::Initializer
9
+
10
+ param :name, comment: "Metric name. Use snake_case. E.g. job_runtime"
11
+ option :comment, optional: true, comment: "Documentation string. Required by some adapters."
12
+ option :unit, optional: true, comment: "In which units it is measured. E.g. `seconds`"
13
+ option :per, optional: true, comment: "Per which unit is measured `unit`. E.g. `call` as in seconds per call"
14
+ option :group, optional: true, comment: "Category name for grouping metrics"
15
+
16
+ # Returns the value for the given label set
17
+ def get(labels = {})
18
+ values[labels]
19
+ end
20
+
21
+ def values
22
+ @values ||= Concurrent::Hash.new
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yabeda
4
+ VERSION = "0.1.1"
5
+ end
@@ -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 "yabeda/version"
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "yabeda"
9
+ spec.version = Yabeda::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/yabeda-rb/yabeda"
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
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yabeda
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrey Novikov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-10-17 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
+ - lib/yabeda.rb
133
+ - lib/yabeda/base_adapter.rb
134
+ - lib/yabeda/counter.rb
135
+ - lib/yabeda/dsl.rb
136
+ - lib/yabeda/gauge.rb
137
+ - lib/yabeda/histogram.rb
138
+ - lib/yabeda/metric.rb
139
+ - lib/yabeda/version.rb
140
+ - yabeda.gemspec
141
+ homepage: https://github.com/yabeda-rb/yabeda
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: []