yabeda 0.6.2 → 0.10.0

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
  SHA256:
3
- metadata.gz: fbc7c44d3d257c23cdbce5b423db52c4f2591c21737d939d9dce8df53c557f04
4
- data.tar.gz: 9c68a889b0f978a4471b0458e4d48903ab019ae7dfc6948b55cb5a5147060127
3
+ metadata.gz: df5873bc79b044f45361239ef207d63f8a97776d39908652b11629dbb67fd317
4
+ data.tar.gz: d6d170503bfbc5ea7a2e1b2ce4415710efc1e68487350a67e270ae4b2f77c102
5
5
  SHA512:
6
- metadata.gz: 2cd9007fda7bd41d90a20238ac81dd4d2efe46e39f5473e7e0c318f5efb984f6b3e0843a06fb5c325514f0e4282fae98b2fcfc9f9920bdfbccfca946de32b910
7
- data.tar.gz: 0d429f2971ff62ec68d36bedf089ef5f203a8c63cabaddd917704529d6aaf307999ac3d2716207abb9c37be23e42d5dcdc3115d5a6eb8551eb23ef940aefb80d
6
+ metadata.gz: a8bde67925e08a45425963e778c1e6cd5dcc9a24c46cce96abeb84d56adc99529ecc58556cb930652be64d7cea604563181b69abc1157388e896d9eb76af53a8
7
+ data.tar.gz: 77d9400f003c06e387b3b75cf42d95a52546c2b240021905e649198fe78f0ff2b502da771dbce50c95e837608218026bbea5e310334371cc417567ee67c60392
@@ -0,0 +1,82 @@
1
+ name: Build and release gem to RubyGems
2
+
3
+ on:
4
+ push:
5
+ tags:
6
+ - v*
7
+
8
+ jobs:
9
+ release:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - uses: actions/checkout@v2
13
+ with:
14
+ fetch-depth: 0 # Fetch current tag as annotated. See https://github.com/actions/checkout/issues/290
15
+ - uses: ruby/setup-ruby@v1
16
+ with:
17
+ ruby-version: 2.7
18
+ - name: "Extract data from tag: version, message, body"
19
+ id: tag
20
+ run: |
21
+ git fetch --tags --force # Really fetch annotated tag. See https://github.com/actions/checkout/issues/290#issuecomment-680260080
22
+ echo ::set-output name=version::${GITHUB_REF#refs/tags/v}
23
+ echo ::set-output name=subject::$(git for-each-ref $GITHUB_REF --format='%(contents:subject)')
24
+ BODY="$(git for-each-ref $GITHUB_REF --format='%(contents:body)')"
25
+ # Extract changelog entries between this and previous version headers
26
+ escaped_version=$(echo ${GITHUB_REF#refs/tags/v} | sed -e 's/[]\/$*.^[]/\\&/g')
27
+ changelog=$(awk "BEGIN{inrelease=0} /## ${escaped_version}/{inrelease=1;next} /## [0-9]+\.[0-9]+\.[0-9]+/{inrelease=0;exit} {if (inrelease) print}" CHANGELOG.md)
28
+ # Multiline body for release. See https://github.community/t/set-output-truncates-multiline-strings/16852/5
29
+ BODY="${BODY}"$'\n'"${changelog}"
30
+ BODY="${BODY//'%'/'%25'}"
31
+ BODY="${BODY//$'\n'/'%0A'}"
32
+ BODY="${BODY//$'\r'/'%0D'}"
33
+ echo "::set-output name=body::$BODY"
34
+ # Add pre-release option if tag name has any suffix after vMAJOR.MINOR.PATCH
35
+ if [[ ${GITHUB_REF#refs/tags/} =~ ^v[0-9]+\.[0-9]+\.[0-9]+.+ ]]; then
36
+ echo ::set-output name=prerelease::true
37
+ fi
38
+ - name: Build gem
39
+ run: gem build
40
+ - name: Calculate checksums
41
+ run: sha256sum yabeda-${{ steps.tag.outputs.version }}.gem > SHA256SUM
42
+ - name: Check version
43
+ run: ls -l yabeda-${{ steps.tag.outputs.version }}.gem
44
+ - name: Create Release
45
+ id: create_release
46
+ uses: actions/create-release@v1
47
+ env:
48
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
49
+ with:
50
+ tag_name: ${{ github.ref }}
51
+ release_name: ${{ steps.tag.outputs.subject }}
52
+ body: ${{ steps.tag.outputs.body }}
53
+ draft: false
54
+ prerelease: ${{ steps.tag.outputs.prerelease }}
55
+ - name: Upload built gem as release asset
56
+ uses: actions/upload-release-asset@v1
57
+ env:
58
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
59
+ with:
60
+ upload_url: ${{ steps.create_release.outputs.upload_url }}
61
+ asset_path: yabeda-${{ steps.tag.outputs.version }}.gem
62
+ asset_name: yabeda-${{ steps.tag.outputs.version }}.gem
63
+ asset_content_type: application/x-tar
64
+ - name: Upload checksums as release asset
65
+ uses: actions/upload-release-asset@v1
66
+ env:
67
+ GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
68
+ with:
69
+ upload_url: ${{ steps.create_release.outputs.upload_url }}
70
+ asset_path: SHA256SUM
71
+ asset_name: SHA256SUM
72
+ asset_content_type: text/plain
73
+ - name: Publish to GitHub packages
74
+ env:
75
+ GEM_HOST_API_KEY: Bearer ${{ secrets.GITHUB_TOKEN }}
76
+ run: |
77
+ gem push yabeda-${{ steps.tag.outputs.version }}.gem --host https://rubygems.pkg.github.com/${{ github.repository_owner }}
78
+ - name: Publish to RubyGems
79
+ env:
80
+ GEM_HOST_API_KEY: "${{ secrets.RUBYGEMS_API_KEY }}"
81
+ run: |
82
+ gem push yabeda-${{ steps.tag.outputs.version }}.gem
@@ -0,0 +1,46 @@
1
+ name: Run tests
2
+
3
+ on:
4
+ pull_request:
5
+ push:
6
+ branches:
7
+ - '**'
8
+ tags-ignore:
9
+ - 'v*'
10
+
11
+ jobs:
12
+ test:
13
+ name: "Run tests"
14
+ runs-on: ubuntu-latest
15
+ strategy:
16
+ fail-fast: false
17
+ matrix:
18
+ include:
19
+ - ruby: 3.0
20
+ - ruby: 2.7
21
+ - ruby: 2.6
22
+ - ruby: 2.5
23
+ container:
24
+ image: ruby:${{ matrix.ruby }}
25
+ env:
26
+ CI: true
27
+ steps:
28
+ - uses: actions/checkout@v2
29
+ - uses: actions/cache@v2
30
+ with:
31
+ path: vendor/bundle
32
+ key: bundle-${{ matrix.ruby }}-${{ hashFiles('**/*.gemspec') }}-${{ hashFiles('**/Gemfile') }}
33
+ restore-keys: |
34
+ bundle-${{ matrix.ruby }}-${{ hashFiles('**/*.gemspec') }}-${{ hashFiles('**/Gemfile') }}
35
+ bundle-${{ matrix.ruby }}-
36
+ - name: Upgrade Bundler to 2.0 (for older Rubies)
37
+ run: gem install bundler -v '~> 2.0'
38
+ - name: Bundle install
39
+ run: |
40
+ bundle config path vendor/bundle
41
+ bundle install
42
+ bundle update
43
+ - name: Run Rubocop
44
+ run: bundle exec rubocop
45
+ - name: Run RSpec
46
+ run: bundle exec rspec
data/CHANGELOG.md CHANGED
@@ -5,6 +5,43 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
6
6
  and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## Unreleased
9
+
10
+ ## 0.10.0 - 2021-07-21
11
+
12
+ ### Added
13
+
14
+ - Ability to pass a block to `Yabeda::Histogram#measure` to automatically measure its runtime in seconds using [monotonic time](https://blog.dnsimple.com/2018/03/elapsed-time-with-ruby-the-right-way/).
15
+ - Debug mode that will enable some additional metrics to help debug performance issues with your usage of Yabeda (or Yabeda itself). Use environment variable `YABEDA_DEBUG` to enable it or call `Yabeda.debug!`.
16
+ - Debugging histogram `yabeda_collect_duration` that measures duration of every collect block, as they are used for collecting metrics of application state and usually makes some potentially slow queries to databases, network requests, etc.
17
+
18
+ ### Changed
19
+
20
+ - Adapters now should use method `Yabeda.collect!` instead of manual calling of every collector block.
21
+
22
+ ## 0.9.0 - 2021-05-07
23
+
24
+ ### Added
25
+
26
+ - Ability to set global metric tags only for a specific group [#19](https://github.com/yabeda-rb/yabeda/pull/19) by [@liaden]
27
+
28
+ ## 0.8.0 - 2020-08-21
29
+
30
+ ### Added
31
+
32
+ - Added railtie to automatically configure Yabeda on Rails: moved from [yabeda-rails](https://github.com/yabeda-rb/yabeda-rails) gem. [@Envek]
33
+
34
+ ## 0.7.0 - 2020-08-07
35
+
36
+ ### Added
37
+
38
+ - `#increment` and `#decrement` convenience methods for `Yabeda::Gauge`. [#13](https://github.com/yabeda-rb/yabeda/pull/13) by [@dsalahutdinov]
39
+ - Ability to use custom step in `#increment` and `#decrement` for gauges. [@Envek]
40
+
41
+ ### Fixed
42
+
43
+ - Account for default tags in `Yabeda::Metric#get`. [@Envek]
44
+
8
45
  ## 0.6.2 - 2020-08-04
9
46
 
10
47
  ### Fixed
@@ -67,7 +104,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
67
104
 
68
105
  ### Fixed
69
106
 
70
- - Removed accidental dependency from Rails. @dsalahutdinov
107
+ - Removed accidental dependency from Rails. [@dsalahutdinov]
71
108
 
72
109
  ## 0.1.1 - 2018-10-17
73
110
 
@@ -80,4 +117,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
80
117
  - Initial release of evil-metrics gem. @Envek
81
118
 
82
119
  [@Envek]: https://github.com/Envek "Andrey Novikov"
120
+ [@dsalahutdinov]: https://github.com/dsalahutdinov "Dmitry Salahutdinov"
83
121
  [@asusikov]: https://github.com/asusikov "Alexander Susikov"
122
+ [@liaden]: https://github.com/liaden "Joel Johnson"
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # Yabeda
1
+ # ![Yabeda](./yabeda-logo.png)
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/yabeda.svg)](https://rubygems.org/gems/yabeda) [![Build Status](https://travis-ci.org/yabeda-rb/yabeda.svg?branch=master)](https://travis-ci.org/yabeda-rb/yabeda)
3
+ [![Gem Version](https://badge.fury.io/rb/yabeda.svg)](https://rubygems.org/gems/yabeda)
4
4
 
5
5
  **This software is Work in Progress: features will appear and disappear, API will be changed, your feedback is always welcome!**
6
6
 
@@ -49,7 +49,7 @@ And then execute:
49
49
  Yabeda.configure!
50
50
  ```
51
51
 
52
- _But [yabeda-rails] will do this for you automatically._
52
+ _If you're using Ruby on Rails then it will be configured automatically!_
53
53
 
54
54
  3. Access metric in your app and use it!
55
55
 
@@ -79,10 +79,14 @@ And then execute:
79
79
  end
80
80
  ```
81
81
 
82
- 5. _Optionally_ setup default tags that will be added to all metrics
82
+ 5. _Optionally_ setup default tags for all appropriate metrics
83
83
  ```ruby
84
84
  Yabeda.configure do
85
+ # matches all metrics in all groups
85
86
  default_tag :rails_environment, 'production'
87
+
88
+ # matches all metrics in the :your_app group
89
+ default_tag :tag_name, 'override', group: :your_app
86
90
  end
87
91
 
88
92
  # You can redefine them for limited amount of time
@@ -91,17 +95,72 @@ And then execute:
91
95
  end
92
96
  ```
93
97
 
94
- 6. See the docs for the adapter you're using
95
- 7. Enjoy!
98
+ **Note**: any usage of `with_tags` **must** have all those tags defined on all metrics that are generated in the block.
99
+
100
+ 6. _Optionally_ override default tags using precedence:
101
+
102
+ The tag precedence from high to low is:
103
+
104
+ * Manually specified tags
105
+ * Thread local tags (specified by `Yabeda.with_tags`)
106
+ * Group specific tags
107
+ * Global tags
108
+
109
+ 7. See the docs for the adapter you're using
110
+ 8. Enjoy!
96
111
 
97
112
  ## Available monitoring system adapters
98
113
 
99
- - [Prometheus](https://github.com/yabeda-rb/yabeda-prometheus)
114
+ ### Maintained by Yabeda
115
+
116
+ - Prometheus:
117
+ - [yabeda-prometheus](https://github.com/yabeda-rb/yabeda-prometheus) — wraps [official Ruby client for Prometheus](https://github.com/prometheus/client_ruby).
118
+ - [yabeda-prometheus-mmap](https://github.com/yabeda-rb/yabeda-prometheus-mmap) — wraps [GitLab's fork of Prometheus Ruby client](https://gitlab.com/gitlab-org/prometheus-client-mmap) which may work better for multi-process application servers.
100
119
  - [Datadog](https://github.com/yabeda-rb/yabeda-datadog)
101
120
  - [NewRelic](https://github.com/yabeda-rb/yabeda-newrelic)
121
+
122
+ ### Third-party adapters
123
+
124
+ These are developed and maintained by other awesome folks:
125
+
102
126
  - [Statsd](https://github.com/asusikov/yabeda-statsd)
103
127
  - _…and more! You can write your own adapter and open a pull request to add it into this list._
104
128
 
129
+ ## Available plugins to collect metrics
130
+
131
+ ### Maintained by Yabeda
132
+
133
+ - [yabeda-rails] — basic request metrics for [Ruby on Rails](https://rubyonrails.org/) applications.
134
+ - [yabeda-sidekiq] — comprehensive set of metrics for monitoring [Sidekiq](https://sidekiq.org/) jobs execution and queues.
135
+ - [yabeda-faktory] — metrics for monitoring jobs execution by Ruby workers of [Faktory](https://contribsys.com/faktory/).
136
+ - [yabeda-graphql] — metrics to query and field-level monitoring for apps using [GraphQL-Ruby](https://graphql-ruby.org/).
137
+ - [yabeda-puma-plugin] — metrics for internal state and performance of [Puma](https://puma.io/) application server.
138
+ - [yabeda-http_requests] — monitor how many outgoing HTTP calls your application does (uses [Sniffer](https://github.com/aderyabin/sniffer)).
139
+ - [yabeda-schked] — monitor number and duration of Cron jobs executed by [Schked](https://github.com/bibendi/schked).
140
+
141
+ ### Third-party plugins
142
+
143
+ These are developed and maintained by other awesome folks:
144
+
145
+ - [yabeda-grape](https://github.com/efigence/yabeda-grape) — metrics for [Grape](https://github.com/ruby-grape/grape) framework.
146
+ - [yabeda-gruf](https://github.com/Placewise/yabeda-gruf) — metrics for [gRPC Ruby Framework](https://github.com/bigcommerce/gruf)
147
+ - [yabeda-gc](https://github.com/ianks/yabeda-gc) — metrics for Ruby garbage collection.
148
+ - _…and more! You can write your own adapter and open a pull request to add it into this list._
149
+
150
+ ## Configuration
151
+
152
+ Configuration is handled by [anyway_config] gem. With it you can load settings from environment variables (which names are constructed from config key upcased and prefixed with `YABEDA_`), YAML files, and other sources. See [anyway_config] docs for details.
153
+
154
+ Config key | Type | Default | Description |
155
+ ---------- | -------- | ------- | ----------- |
156
+ `debug` | boolean | `false` | Collects metrics measuring Yabeda performance |
157
+
158
+ ## Debugging metrics
159
+
160
+ - Time of collector block run: `yabeda_collect_duration` (segmented by block source location). Collector blocks are used for collecting metrics of application state and usually makes some potentially slow queries to databases, network requests, etc.
161
+
162
+ These are only enabled in debug mode. To enable it either set `debug` config key to `true` (e.g. by specifying `YABEDA_DEBUG=true` in your environment variables or executing `Yabeda.debug!` in your code).
163
+
105
164
  ## Roadmap (aka TODO or Help wanted)
106
165
 
107
166
  - Ability to change metric settings for individual adapters
@@ -130,6 +189,36 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
130
189
 
131
190
  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).
132
191
 
192
+ ### Releasing
193
+
194
+ 1. Bump version number in `lib/yabeda/version.rb`
195
+
196
+ In case of pre-releases keep in mind [rubygems/rubygems#3086](https://github.com/rubygems/rubygems/issues/3086) and check version with command like `Gem::Version.new(Yabeda::VERSION).to_s`
197
+
198
+ 2. Fill `CHANGELOG.md` with missing changes, add header with version and date.
199
+
200
+ 3. Make a commit:
201
+
202
+ ```sh
203
+ git add lib/yabeda/version.rb CHANGELOG.md
204
+ version=$(ruby -r ./lib/yabeda/version.rb -e "puts Gem::Version.new(Yabeda::VERSION)")
205
+ git commit --message="${version}: " --edit
206
+ ```
207
+
208
+ 3. Create annotated tag:
209
+
210
+ ```sh
211
+ git tag v${version} --annotate --message="${version}: " --edit --sign
212
+ ```
213
+
214
+ 4. Fill version name into subject line and (optionally) some description (changes will be taken from changelog and appended automatically)
215
+
216
+ 5. Push it:
217
+
218
+ ```sh
219
+ git push --follow-tags
220
+ ```
221
+
133
222
  ## Contributing
134
223
 
135
224
  Bug reports and pull requests are welcome on GitHub at https://github.com/yabeda-rb/yabeda.
@@ -139,3 +228,10 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/yabeda
139
228
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
140
229
 
141
230
  [yabeda-rails]: https://github.com/yabeda-rb/yabeda-rails/ "Yabeda plugin for collecting and exporting basic metrics for Rails applications"
231
+ [yabeda-sidekiq]: https://github.com/yabeda-rb/yabeda-sidekiq/ "Yabeda plugin for complete monitoring of Sidekiq metrics"
232
+ [yabeda-faktory]: https://github.com/yabeda-rb/yabeda-faktory/ "Yabeda plugin for complete monitoring of Faktory Ruby Workers"
233
+ [yabeda-graphql]: https://github.com/yabeda-rb/yabeda-graphql/ "Measure and understand how good your GraphQL-Ruby application works"
234
+ [yabeda-puma-plugin]: https://github.com/yabeda-rb/yabeda-puma-plugin/ "Collects Puma web-server metrics from puma control application"
235
+ [yabeda-http_requests]: https://github.com/yabeda-rb/yabeda-http_requests/ "Builtin metrics to monitor external HTTP requests"
236
+ [yabeda-schked]: https://github.com/yabeda-rb/yabeda-schked/ "Built-in metrics for monitoring Schked recurring jobs out of the box"
237
+ [anyway_config]: https://github.com/palkan/anyway_config "Configuration library for Ruby gems and applications"
data/lib/yabeda.rb CHANGED
@@ -1,17 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "concurrent"
4
+ require "forwardable"
4
5
 
5
6
  require "yabeda/version"
7
+ require "yabeda/config"
6
8
  require "yabeda/dsl"
7
9
  require "yabeda/tags"
8
10
  require "yabeda/errors"
11
+ require "yabeda/railtie" if defined?(Rails)
9
12
 
10
13
  # Extendable framework for collecting and exporting metrics from Ruby apps
11
14
  module Yabeda
12
15
  include DSL
13
16
 
14
17
  class << self
18
+ extend Forwardable
19
+
15
20
  # @return [Hash<String, Yabeda::Metric>] All registered metrics
16
21
  def metrics
17
22
  @metrics ||= Concurrent::Hash.new
@@ -19,7 +24,9 @@ module Yabeda
19
24
 
20
25
  # @return [Hash<String, Yabeda::Group>] All registered metrics
21
26
  def groups
22
- @groups ||= Concurrent::Hash.new
27
+ @groups ||= Concurrent::Hash.new.tap do |hash|
28
+ hash[nil] = Yabeda::GlobalGroup.new(nil)
29
+ end
23
30
  end
24
31
 
25
32
  # @return [Hash<String, Yabeda::BaseAdapter>] All loaded adapters
@@ -32,7 +39,26 @@ module Yabeda
32
39
  @collectors ||= Concurrent::Array.new
33
40
  end
34
41
 
35
- # @return [Hash<Symbol, Symbol>] All added default tags
42
+ def config
43
+ @config ||= Config.new
44
+ end
45
+
46
+ def_delegators :config, :debug?
47
+
48
+ # Execute all collector blocks for periodical retrieval of metrics
49
+ #
50
+ # This method is intended to be used by monitoring systems adapters
51
+ def collect!
52
+ collectors.each do |collector|
53
+ if config.debug?
54
+ yabeda.collect_duration.measure({ location: collector.source_location.join(":") }, &collector)
55
+ else
56
+ collector.call
57
+ end
58
+ end
59
+ end
60
+
61
+ # @return [Hash<Symbol, Symbol>] All added global default tags
36
62
  def default_tags
37
63
  @default_tags ||= Concurrent::Hash.new
38
64
  end
@@ -64,6 +90,8 @@ module Yabeda
64
90
  def configure!
65
91
  raise(AlreadyConfiguredError, @configured_by) if already_configured?
66
92
 
93
+ debug! if config.debug?
94
+
67
95
  configurators.each do |(group, block)|
68
96
  group group
69
97
  class_eval(&block)
@@ -80,19 +108,45 @@ module Yabeda
80
108
 
81
109
  @configured_by = caller_locations(1, 1)[0].to_s
82
110
  end
111
+
112
+ # Enable and setup service metrics to monitor yabeda performance
113
+ def debug!
114
+ return false if @debug_was_enabled_by # Prevent multiple calls
115
+
116
+ config.debug ||= true # Enable debug mode in config if it wasn't enabled from other sources
117
+ @debug_was_enabled_by = caller_locations(1, 1)[0].to_s
118
+
119
+ configure do
120
+ group :yabeda
121
+
122
+ histogram :collect_duration,
123
+ tags: %i[location], unit: :seconds,
124
+ buckets: [0.0001, 0.001, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, 30, 60].freeze,
125
+ comment: "A histogram for the time required to evaluate collect blocks"
126
+ end
127
+
128
+ adapters.each_value(&:debug!)
129
+
130
+ true
131
+ end
83
132
  # rubocop: enable Metrics/MethodLength, Metrics/AbcSize
84
133
 
85
134
  # Forget all the configuration.
86
135
  # For testing purposes as it doesn't rollback changes in adapters.
87
136
  # @api private
137
+ # rubocop: disable Metrics/AbcSize
88
138
  def reset!
89
139
  default_tags.clear
90
140
  adapters.clear
91
- groups.clear
92
- metrics.clear
141
+ groups.each_key { |group| singleton_class.send(:remove_method, group) if group && respond_to?(group) }
142
+ @groups = nil
143
+ metrics.each_key { |metric| singleton_class.send(:remove_method, metric) if respond_to?(metric) }
144
+ @metrics = nil
93
145
  collectors.clear
94
146
  configurators.clear
95
147
  instance_variable_set(:@configured_by, nil)
148
+ instance_variable_set(:@debug_was_enabled_by, nil)
96
149
  end
150
+ # rubocop: enable Metrics/AbcSize
97
151
  end
98
152
  end
@@ -35,5 +35,8 @@ module Yabeda
35
35
  def perform_histogram_measure!(_metric, _tags, _value)
36
36
  raise NotImplementedError, "#{self.class} doesn't support measuring histograms"
37
37
  end
38
+
39
+ # Hook to enable debug mode in adapters when it is enabled in Yabeda itself
40
+ def debug!; end
38
41
  end
39
42
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "anyway"
4
+
5
+ module Yabeda
6
+ # Runtime configuration for the main yabeda gem
7
+ class Config < ::Anyway::Config
8
+ config_name :yabeda
9
+
10
+ # Declare and collect metrics about Yabeda performance
11
+ attr_config debug: false
12
+ end
13
+ end
@@ -4,7 +4,7 @@ module Yabeda
4
4
  # Growing-only counter
5
5
  class Counter < Metric
6
6
  def increment(tags, by: 1)
7
- all_tags = ::Yabeda::Tags.build(tags)
7
+ all_tags = ::Yabeda::Tags.build(tags, group)
8
8
  values[all_tags] += by
9
9
  ::Yabeda.adapters.each do |_, adapter|
10
10
  adapter.perform_counter_increment!(self, all_tags, by)
@@ -5,6 +5,7 @@ require "yabeda/counter"
5
5
  require "yabeda/gauge"
6
6
  require "yabeda/histogram"
7
7
  require "yabeda/group"
8
+ require "yabeda/global_group"
8
9
  require "yabeda/dsl/metric_builder"
9
10
 
10
11
  module Yabeda
@@ -30,6 +31,7 @@ module Yabeda
30
31
  # (like NewRelic) it is treated individually and has a special meaning.
31
32
  def group(group_name)
32
33
  @group = group_name
34
+ Yabeda.groups[@group] ||= Yabeda::Group.new(@group)
33
35
  return unless block_given?
34
36
 
35
37
  yield
@@ -58,24 +60,30 @@ module Yabeda
58
60
  #
59
61
  # @param name [Symbol] Name of default tag
60
62
  # @param value [String] Value of default tag
61
- def default_tag(name, value)
62
- ::Yabeda.default_tags[name] = value
63
+ def default_tag(name, value, group: @group)
64
+ if group
65
+ Yabeda.groups[group] ||= Yabeda::Group.new(group)
66
+ Yabeda.groups[group].default_tag(name, value)
67
+ else
68
+ Yabeda.default_tags[name] = value
69
+ end
63
70
  end
64
71
 
65
72
  # Redefine default tags for a limited amount of time
66
73
  # @param tags Hash{Symbol=>#to_s}
67
74
  def with_tags(**tags)
68
- Thread.current[:yabeda_temporary_tags] = tags
75
+ previous_temp_tags = temporary_tags
76
+ Thread.current[:yabeda_temporary_tags] = Thread.current[:yabeda_temporary_tags].merge(tags)
69
77
  yield
70
78
  ensure
71
- Thread.current[:yabeda_temporary_tags] = {}
79
+ Thread.current[:yabeda_temporary_tags] = previous_temp_tags
72
80
  end
73
81
 
74
82
  # Get tags set by +with_tags+
75
83
  # @api private
76
84
  # @return Hash
77
85
  def temporary_tags
78
- Thread.current[:yabeda_temporary_tags] || {}
86
+ Thread.current[:yabeda_temporary_tags] ||= {}
79
87
  end
80
88
 
81
89
  private
@@ -95,9 +103,10 @@ module Yabeda
95
103
  if group.nil?
96
104
  group = Group.new(metric.group)
97
105
  ::Yabeda.groups[metric.group] = group
98
- ::Yabeda.define_singleton_method(metric.group) { group }
99
106
  end
100
107
 
108
+ ::Yabeda.define_singleton_method(metric.group) { group } unless ::Yabeda.respond_to?(metric.group)
109
+
101
110
  group.register_metric(metric)
102
111
  end
103
112
  end
data/lib/yabeda/gauge.rb CHANGED
@@ -4,12 +4,20 @@ module Yabeda
4
4
  # Arbitrary value, can be changed in both sides
5
5
  class Gauge < Metric
6
6
  def set(tags, value)
7
- all_tags = ::Yabeda::Tags.build(tags)
7
+ all_tags = ::Yabeda::Tags.build(tags, group)
8
8
  values[all_tags] = value
9
9
  ::Yabeda.adapters.each do |_, adapter|
10
10
  adapter.perform_gauge_set!(self, all_tags, value)
11
11
  end
12
12
  value
13
13
  end
14
+
15
+ def increment(tags, by: 1)
16
+ set(tags, get(tags).to_i + by)
17
+ end
18
+
19
+ def decrement(tags, by: 1)
20
+ set(tags, get(tags).to_i - by)
21
+ end
14
22
  end
15
23
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "forwardable"
4
+ require_relative "./group"
5
+
6
+ module Yabeda
7
+ # Represents implicit global group
8
+ class GlobalGroup < Group
9
+ extend Forwardable
10
+
11
+ def_delegators ::Yabeda, :default_tags, :default_tag
12
+ end
13
+ end
data/lib/yabeda/group.rb CHANGED
@@ -9,6 +9,16 @@ module Yabeda
9
9
 
10
10
  param :name
11
11
 
12
+ def default_tags
13
+ @default_tags ||= Concurrent::Hash.new
14
+ ::Yabeda.default_tags.merge(@default_tags)
15
+ end
16
+
17
+ def default_tag(key, value)
18
+ @default_tags ||= Concurrent::Hash.new
19
+ @default_tags[key] = value
20
+ end
21
+
12
22
  def register_metric(metric)
13
23
  define_singleton_method(metric.name) { metric }
14
24
  end
@@ -6,13 +6,25 @@ module Yabeda
6
6
  class Histogram < Metric
7
7
  option :buckets
8
8
 
9
- def measure(tags, value)
10
- all_tags = ::Yabeda::Tags.build(tags)
9
+ # rubocop: disable Metrics/MethodLength
10
+ def measure(tags, value = nil)
11
+ if value.nil? ^ block_given?
12
+ raise ArgumentError, "You must provide either numeric value or block for Yabeda::Histogram#measure!"
13
+ end
14
+
15
+ if block_given?
16
+ starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
17
+ yield
18
+ value = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - starting)
19
+ end
20
+
21
+ all_tags = ::Yabeda::Tags.build(tags, group)
11
22
  values[all_tags] = value
12
23
  ::Yabeda.adapters.each do |_, adapter|
13
24
  adapter.perform_histogram_measure!(self, all_tags, value)
14
25
  end
15
26
  value
16
27
  end
28
+ # rubocop: enable Metrics/MethodLength
17
29
  end
18
30
  end
data/lib/yabeda/metric.rb CHANGED
@@ -17,15 +17,17 @@ module Yabeda
17
17
 
18
18
  # Returns the value for the given label set
19
19
  def get(labels = {})
20
- values[labels]
20
+ values[::Yabeda::Tags.build(labels, group)]
21
21
  end
22
22
 
23
23
  def values
24
24
  @values ||= Concurrent::Hash.new
25
25
  end
26
26
 
27
+ # Returns allowed tags for metric (with account for global and group-level +default_tags+)
28
+ # @return Array<Symbol>
27
29
  def tags
28
- (Yabeda.default_tags.keys + Array(super)).uniq
30
+ (Yabeda.groups[group].default_tags.keys + Array(super)).uniq
29
31
  end
30
32
  end
31
33
  end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yabeda
4
+ module Rails
5
+ class Railtie < ::Rails::Railtie # :nodoc:
6
+ config.after_initialize do
7
+ Yabeda.configure! unless Yabeda.already_configured?
8
+ end
9
+ end
10
+ end
11
+ end
data/lib/yabeda/tags.rb CHANGED
@@ -3,8 +3,12 @@
3
3
  module Yabeda
4
4
  # Class to merge tags
5
5
  class Tags
6
- def self.build(tags)
7
- ::Yabeda.default_tags.merge(Yabeda.temporary_tags).merge(tags)
6
+ def self.build(tags, group_name = nil)
7
+ Yabeda.default_tags.dup.tap do |result|
8
+ result.merge!(Yabeda.groups[group_name].default_tags) if group_name
9
+ result.merge!(Yabeda.temporary_tags)
10
+ result.merge!(tags)
11
+ end
8
12
  end
9
13
  end
10
14
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Yabeda
4
- VERSION = "0.6.2"
4
+ VERSION = "0.10.0"
5
5
  end
data/yabeda-logo.png ADDED
Binary file
data/yabeda.gemspec CHANGED
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
26
  spec.require_paths = ["lib"]
27
27
 
28
+ spec.add_dependency "anyway_config", ">= 1.3", "< 3"
28
29
  spec.add_dependency "concurrent-ruby"
29
30
  spec.add_dependency "dry-initializer"
30
31
 
metadata CHANGED
@@ -1,15 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yabeda
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrey Novikov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-04 00:00:00.000000000 Z
11
+ date: 2021-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: anyway_config
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3'
13
33
  - !ruby/object:Gem::Dependency
14
34
  name: concurrent-ruby
15
35
  requirement: !ruby/object:Gem::Requirement
@@ -111,17 +131,18 @@ dependencies:
111
131
  description: 'Collect statistics about how your application is performing with ease.
112
132
  Export metrics to various monitoring systems.
113
133
 
114
- '
134
+ '
115
135
  email:
116
136
  - envek@envek.name
117
137
  executables: []
118
138
  extensions: []
119
139
  extra_rdoc_files: []
120
140
  files:
141
+ - ".github/workflows/build-release.yml"
142
+ - ".github/workflows/test.yml"
121
143
  - ".gitignore"
122
144
  - ".rspec"
123
145
  - ".rubocop.yml"
124
- - ".travis.yml"
125
146
  - ".yardopts"
126
147
  - CHANGELOG.md
127
148
  - Gemfile
@@ -132,6 +153,7 @@ files:
132
153
  - bin/setup
133
154
  - lib/yabeda.rb
134
155
  - lib/yabeda/base_adapter.rb
156
+ - lib/yabeda/config.rb
135
157
  - lib/yabeda/counter.rb
136
158
  - lib/yabeda/dsl.rb
137
159
  - lib/yabeda/dsl/class_methods.rb
@@ -139,11 +161,14 @@ files:
139
161
  - lib/yabeda/dsl/option_builder.rb
140
162
  - lib/yabeda/errors.rb
141
163
  - lib/yabeda/gauge.rb
164
+ - lib/yabeda/global_group.rb
142
165
  - lib/yabeda/group.rb
143
166
  - lib/yabeda/histogram.rb
144
167
  - lib/yabeda/metric.rb
168
+ - lib/yabeda/railtie.rb
145
169
  - lib/yabeda/tags.rb
146
170
  - lib/yabeda/version.rb
171
+ - yabeda-logo.png
147
172
  - yabeda.gemspec
148
173
  homepage: https://github.com/yabeda-rb/yabeda
149
174
  licenses:
@@ -164,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
164
189
  - !ruby/object:Gem::Version
165
190
  version: '0'
166
191
  requirements: []
167
- rubygems_version: 3.1.2
192
+ rubygems_version: 3.1.6
168
193
  signing_key:
169
194
  specification_version: 4
170
195
  summary: Extensible framework for collecting metric for your Ruby application
data/.travis.yml DELETED
@@ -1,9 +0,0 @@
1
- ---
2
- language: ruby
3
- cache: bundler
4
- rvm:
5
- - 2.7.0
6
- - 2.6.5
7
- - 2.5.7
8
- - 2.4.9
9
- before_install: gem install bundler -v "~> 2.0"