Skip to content

Commit b9626a8

Browse files
committed
Initial commit: API preview for metrics and adapters
0 parents  commit b9626a8

22 files changed

+508
-0
lines changed

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -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

.rspec

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--format documentation
2+
--color
3+
--require spec_helper

.rubocop.yml

+46
Original file line numberDiff line numberDiff line change
@@ -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+

.travis.yml

+9
Original file line numberDiff line numberDiff line change
@@ -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

.yardopts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--plugin dry-initializer

Gemfile

+16
Original file line numberDiff line numberDiff line change
@@ -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

LICENSE.txt

+21
Original file line numberDiff line numberDiff line change
@@ -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.

README.md

+100
Original file line numberDiff line numberDiff line change
@@ -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).

Rakefile

+8
Original file line numberDiff line numberDiff line change
@@ -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

bin/console

+11
Original file line numberDiff line numberDiff line change
@@ -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

bin/setup

+8
Original file line numberDiff line numberDiff line change
@@ -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

evil-metrics.gemspec

+36
Original file line numberDiff line numberDiff line change
@@ -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 = ["[email protected]"]
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

lib/evil/metrics.rb

+28
Original file line numberDiff line numberDiff line change
@@ -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

lib/evil/metrics/base_adapter.rb

+40
Original file line numberDiff line numberDiff line change
@@ -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

lib/evil/metrics/counter.rb

+20
Original file line numberDiff line numberDiff line change
@@ -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 commit comments

Comments
 (0)