yabeda 0.7.0 → 0.10.1

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: 8e30282089c3784919bbdfef129e36f9898bbe30376dec4148ab45551835e95a
4
- data.tar.gz: 9abb1750e05b17f4f97f5d3c91d9eaf6f638c735b82d46baa1618500edf373da
3
+ metadata.gz: f5aed70338bdfe24c4505a3cbe51cb4853c7c0e0c1842ef1cee93480ef517c02
4
+ data.tar.gz: 95dc76b40980f227abe24c7bf70339d47d81eaa91e6bdfa354c8b6e6b93c8170
5
5
  SHA512:
6
- metadata.gz: b8fb61bea63f36d9f8ea98985c12d5247cefe7463ad2129f3bbda1ffbff34cc8faa4b71d797f44e02cc957a2159cdca3b53996fbf641e496476ab04e47489f3d
7
- data.tar.gz: 66f06b7e63832eda3d43e0266fc4aa38726b261215a5308930bc9757a344dd60f43844a3d9b5f74a62069e1c090664248505d8a2408f0d6801685ccd926ea8b4
6
+ metadata.gz: 77e865d851f2e9f2a168426983df6693c9e655e0349107aafd25594eea12f9f83c14da519abbdfc5826f8c0e208d68b2a0616735771ba5ec82bde7acb3e71085
7
+ data.tar.gz: aa7596a3de80b063845d552136c882cbdf46e772a1f53998058ba894dc8428290fdbae391ef241f2264dd5e76bd41be8cfab0b8afad654152b50d3b47fd0ab3b
@@ -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,48 @@
1
+ name: Tests
2
+
3
+ on:
4
+ pull_request:
5
+ push:
6
+ branches:
7
+ - '**'
8
+ tags-ignore:
9
+ - 'v*'
10
+
11
+ jobs:
12
+ test:
13
+ name: "Ruby ${{ matrix.ruby }}"
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
+ - ruby: "2.4"
24
+ - ruby: "2.3"
25
+ container:
26
+ image: ruby:${{ matrix.ruby }}
27
+ env:
28
+ CI: true
29
+ steps:
30
+ - uses: actions/checkout@v2
31
+ - uses: actions/cache@v2
32
+ with:
33
+ path: vendor/bundle
34
+ key: bundle-${{ matrix.ruby }}-${{ hashFiles('**/*.gemspec') }}-${{ hashFiles('**/Gemfile') }}
35
+ restore-keys: |
36
+ bundle-${{ matrix.ruby }}-${{ hashFiles('**/*.gemspec') }}-${{ hashFiles('**/Gemfile') }}
37
+ bundle-${{ matrix.ruby }}-
38
+ - name: Upgrade Bundler to 2.0 (for older Rubies)
39
+ run: gem install bundler -v '~> 2.0'
40
+ - name: Bundle install
41
+ run: |
42
+ bundle config path vendor/bundle
43
+ bundle install
44
+ bundle update
45
+ - name: Run Rubocop
46
+ run: bundle exec rubocop
47
+ - name: Run RSpec
48
+ run: bundle exec rspec
data/CHANGELOG.md CHANGED
@@ -5,6 +5,38 @@ 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.1 - 2021-08-30
11
+
12
+ ### Fixed
13
+
14
+ - Compatibility with anyway_config 1.x gem (which is automatically used on older Rubies, older then minimal Ruby 2.5 for anyway_config 2.x)
15
+
16
+ ## 0.10.0 - 2021-07-21
17
+
18
+ ### Added
19
+
20
+ - 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/).
21
+ - 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!`.
22
+ - 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.
23
+
24
+ ### Changed
25
+
26
+ - Adapters now should use method `Yabeda.collect!` instead of manual calling of every collector block.
27
+
28
+ ## 0.9.0 - 2021-05-07
29
+
30
+ ### Added
31
+
32
+ - Ability to set global metric tags only for a specific group [#19](https://github.com/yabeda-rb/yabeda/pull/19) by [@liaden]
33
+
34
+ ## 0.8.0 - 2020-08-21
35
+
36
+ ### Added
37
+
38
+ - Added railtie to automatically configure Yabeda on Rails: moved from [yabeda-rails](https://github.com/yabeda-rb/yabeda-rails) gem. [@Envek]
39
+
8
40
  ## 0.7.0 - 2020-08-07
9
41
 
10
42
  ### Added
@@ -93,3 +125,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
93
125
  [@Envek]: https://github.com/Envek "Andrey Novikov"
94
126
  [@dsalahutdinov]: https://github.com/dsalahutdinov "Dmitry Salahutdinov"
95
127
  [@asusikov]: https://github.com/asusikov "Alexander Susikov"
128
+ [@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,73 @@ 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
+ - [yabeda-anycable] — monitor number, duration, and status of [AnyCable](https://anycable.io/) RPC calls.
141
+
142
+ ### Third-party plugins
143
+
144
+ These are developed and maintained by other awesome folks:
145
+
146
+ - [yabeda-grape](https://github.com/efigence/yabeda-grape) — metrics for [Grape](https://github.com/ruby-grape/grape) framework.
147
+ - [yabeda-gruf](https://github.com/Placewise/yabeda-gruf) — metrics for [gRPC Ruby Framework](https://github.com/bigcommerce/gruf)
148
+ - [yabeda-gc](https://github.com/ianks/yabeda-gc) — metrics for Ruby garbage collection.
149
+ - _…and more! You can write your own adapter and open a pull request to add it into this list._
150
+
151
+ ## Configuration
152
+
153
+ 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.
154
+
155
+ Config key | Type | Default | Description |
156
+ ---------- | -------- | ------- | ----------- |
157
+ `debug` | boolean | `false` | Collects metrics measuring Yabeda performance |
158
+
159
+ ## Debugging metrics
160
+
161
+ - 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.
162
+
163
+ 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).
164
+
105
165
  ## Roadmap (aka TODO or Help wanted)
106
166
 
107
167
  - Ability to change metric settings for individual adapters
@@ -130,6 +190,36 @@ After checking out the repo, run `bin/setup` to install dependencies. Then, run
130
190
 
131
191
  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
192
 
193
+ ### Releasing
194
+
195
+ 1. Bump version number in `lib/yabeda/version.rb`
196
+
197
+ 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`
198
+
199
+ 2. Fill `CHANGELOG.md` with missing changes, add header with version and date.
200
+
201
+ 3. Make a commit:
202
+
203
+ ```sh
204
+ git add lib/yabeda/version.rb CHANGELOG.md
205
+ version=$(ruby -r ./lib/yabeda/version.rb -e "puts Gem::Version.new(Yabeda::VERSION)")
206
+ git commit --message="${version}: " --edit
207
+ ```
208
+
209
+ 3. Create annotated tag:
210
+
211
+ ```sh
212
+ git tag v${version} --annotate --message="${version}: " --edit --sign
213
+ ```
214
+
215
+ 4. Fill version name into subject line and (optionally) some description (changes will be taken from changelog and appended automatically)
216
+
217
+ 5. Push it:
218
+
219
+ ```sh
220
+ git push --follow-tags
221
+ ```
222
+
133
223
  ## Contributing
134
224
 
135
225
  Bug reports and pull requests are welcome on GitHub at https://github.com/yabeda-rb/yabeda.
@@ -139,3 +229,11 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/yabeda
139
229
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
140
230
 
141
231
  [yabeda-rails]: https://github.com/yabeda-rb/yabeda-rails/ "Yabeda plugin for collecting and exporting basic metrics for Rails applications"
232
+ [yabeda-sidekiq]: https://github.com/yabeda-rb/yabeda-sidekiq/ "Yabeda plugin for complete monitoring of Sidekiq metrics"
233
+ [yabeda-faktory]: https://github.com/yabeda-rb/yabeda-faktory/ "Yabeda plugin for complete monitoring of Faktory Ruby Workers"
234
+ [yabeda-graphql]: https://github.com/yabeda-rb/yabeda-graphql/ "Measure and understand how good your GraphQL-Ruby application works"
235
+ [yabeda-puma-plugin]: https://github.com/yabeda-rb/yabeda-puma-plugin/ "Collects Puma web-server metrics from puma control application"
236
+ [yabeda-http_requests]: https://github.com/yabeda-rb/yabeda-http_requests/ "Builtin metrics to monitor external HTTP requests"
237
+ [yabeda-schked]: https://github.com/yabeda-rb/yabeda-schked/ "Built-in metrics for monitoring Schked recurring jobs out of the box"
238
+ [yabeda-anycable]: https://github.com/yabeda-rb/yabeda-anycable "Collect performance metrics for AnyCable RPC server"
239
+ [anyway_config]: https://github.com/palkan/anyway_config "Configuration library for Ruby gems and applications"
@@ -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,16 @@
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
+
13
+ # Implement predicate method from AnywayConfig 2.x to support AnywayConfig 1.x users
14
+ alias debug? debug unless instance_methods.include?(:debug?)
15
+ end
16
+ 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,7 +4,7 @@ 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)
@@ -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[::Yabeda::Tags.build(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.7.0"
4
+ VERSION = "0.10.1"
5
5
  end
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
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.0", "< 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.7.0
4
+ version: 0.10.1
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-07 00:00:00.000000000 Z
11
+ date: 2021-08-30 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.0'
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.0'
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"