yabeda-shoryuken 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '053931d7159c46e6add4652c91a6c8e110b731c920250bcd4c709c04140738a3'
4
+ data.tar.gz: 9dc0365fb7ace0ddf80337291940c733fcafac479458a6dae68070d1ea87b36b
5
+ SHA512:
6
+ metadata.gz: 42c3fdcdefa9e5b3277d1e844aeed86fdb05b7baf8f0dede1b5f329cadd34e027ab76b5ac005d39dbb0516d031425dd349a66306917ccca34ca09c6d732842af
7
+ data.tar.gz: bfa06257e30369837d521de20e396bd983e77ef559c1e3c12fa01a4b90280072a6fd10488fb950a6d8ac67609cf21480e9f273e99b1f62fb1fb5da610461cd67
@@ -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-shoryuken-${{ steps.tag.outputs.version }}.gem > SHA256SUM
42
+ - name: Check version
43
+ run: ls -l yabeda-shoryuken-${{ 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-shoryuken-${{ steps.tag.outputs.version }}.gem
62
+ asset_name: yabeda-shoryuken-${{ 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-shoryuken-${{ 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-shoryuken-${{ 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
+ if: "!contains(github.event.head_commit.message, '[ci skip]')"
15
+ runs-on: ubuntu-latest
16
+ strategy:
17
+ fail-fast: false
18
+ matrix:
19
+ include:
20
+ - ruby: 3.0
21
+ - ruby: 2.7
22
+ - ruby: 2.6
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/.gitignore ADDED
@@ -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
data/.rubocop.yml ADDED
@@ -0,0 +1,52 @@
1
+ ---
2
+ require:
3
+ - rubocop-rspec
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 2.3
7
+
8
+ Metrics/BlockLength:
9
+ Max: 50
10
+ Exclude:
11
+ - "Gemfile"
12
+ - "spec/**/*"
13
+
14
+ Metrics/MethodLength:
15
+ Max: 25
16
+
17
+ Layout/LineLength:
18
+ Enabled: 150
19
+
20
+ # Allow to use let!
21
+ RSpec/LetSetup:
22
+ Enabled: false
23
+
24
+ RSpec/MultipleExpectations:
25
+ Enabled: false
26
+
27
+ RSpec/ExampleLength:
28
+ Enabled: false
29
+
30
+ Bundler/OrderedGems:
31
+ Enabled: false
32
+
33
+ Style/ClassAndModuleChildren:
34
+ Enabled: false
35
+
36
+ Style/TrailingCommaInArguments:
37
+ Description: 'Checks for trailing comma in argument lists.'
38
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-params-comma'
39
+ Enabled: true
40
+ EnforcedStyleForMultiline: consistent_comma
41
+
42
+ Style/TrailingCommaInArrayLiteral:
43
+ Description: 'Checks for trailing comma in array literals.'
44
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
45
+ Enabled: true
46
+ EnforcedStyleForMultiline: consistent_comma
47
+
48
+ Style/TrailingCommaInHashLiteral:
49
+ Description: 'Checks for trailing comma in hash literals.'
50
+ StyleGuide: 'https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas'
51
+ Enabled: true
52
+ EnforcedStyleForMultiline: consistent_comma
data/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
6
+ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
7
+
8
+ ## 0.1.1 - 2023-01-30
9
+
10
+ - Fix Shoryuken::ClientMiddleware implementation. @retsef
11
+ - Add missing test. @retsef
12
+
13
+ ## 0.1.0 - 2023-01-25
14
+
15
+ - Add basic usage of the gem in README. @retsef
16
+ - Initial release of yabeda-shoryuken gem. @retsef
17
+
18
+ [@retsef]: https://github.com/retsef "Roberto Scinocca"
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at scinocca@extendi.it. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,15 @@
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-cloudwatch.gemspec
8
+ gemspec
9
+
10
+ group :development, :test do
11
+ gem 'rake', '~> 13.0'
12
+
13
+ gem 'rubocop'
14
+ gem 'rubocop-rspec'
15
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Roberto
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.
data/README.md ADDED
@@ -0,0 +1,119 @@
1
+ # Yabeda::Shoryuken
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/yabeda-shoryuken.svg)](https://rubygems.org/gems/yabeda-cloudwatch)
4
+
5
+ Yabeda plugin for easy [Shoryuken] gem collect metrics from your application
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'yabeda-shoryuken'
13
+ # Then add monitoring system adapter, e.g.:
14
+ # gem 'yabeda-cloudwatch'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ If you're not on Rails then configure Yabeda after your application was initialized:
22
+
23
+ ```ruby
24
+ Yabeda.configure!
25
+ ```
26
+
27
+ _If you're using Ruby on Rails then Yabeda will configure itself automatically!_
28
+
29
+ **And that is it!** Shoryuken metrics are being collected!
30
+
31
+ ## Metrics
32
+
33
+ ### Local per-process metrics
34
+
35
+ Metrics representing state of current Shoryuken worker process and stats of executed or executing jobs:
36
+
37
+ - Total number of executed jobs: `shoryuken_jobs_executed_total` - (segmented by queue and class name)
38
+ - Number of jobs have been finished successfully: `shoryuken_jobs_success_total` (segmented by queue and class name)
39
+ - Number of jobs have been failed: `shoryuken_jobs_failed_total` (segmented by queue and class name)
40
+ - Time of job run: `shoryuken_job_runtime` (seconds per job execution, segmented by queue and class name)
41
+ - Maximum runtime of currently executing jobs: `shoryuken_running_job_runtime` (useful for detection of hung jobs, segmented by queue and class name)
42
+
43
+ ### Client metrics
44
+
45
+ Metrics collected where jobs are being pushed to queues (everywhere):
46
+
47
+ - Total number of enqueued messages: `shoryuken_messages_enqueued_total_count` (segmented by `queue` and `worker` class name)
48
+
49
+ ## Custom tags
50
+
51
+ You can add additional tags to these metrics by declaring `yabeda_tags` method in your worker.
52
+
53
+ ```ruby
54
+ # This block is optional but some adapters (like Prometheus) requires that all tags should be declared in advance
55
+ Yabeda.configure do
56
+ default_tag :importance, nil
57
+ end
58
+ class MyWorker
59
+ include Shoryuken::Worker
60
+ def yabeda_tags(*params) # This method will be called first, before +perform+
61
+ { importance: extract_importance(params) }
62
+ end
63
+ def perform(*params)
64
+ # Your logic here
65
+ end
66
+ end
67
+ ```
68
+ ## Configuration
69
+ Configuration is handled by [anyway_config] gem. With it you can load settings from environment variables (upcased and prefixed with `YABEDA_SIDEKIQ_`), YAML files, and other sources. See [anyway_config] docs for details.
70
+ Config key | Type | Default | Description |
71
+ ------------------------- | -------- | ------------------------------------------------------- |----------------------------------------------------------------------------------------------------------------------------------------------------|
72
+ `collect_cluster_metrics` | boolean | Enabled in Shoryuken worker processes, disabled otherwise | Defines whether this Ruby process should collect and expose metrics representing state of the whole Shoryuken installation (queues, processes, etc). |
73
+ `declare_process_metrics` | boolean | Enabled in Shoryuken worker processes, disabled otherwise | Declare metrics that are only tracked inside worker process even outside of them. Useful for multiprocess metric collection. |
74
+
75
+
76
+ ## Contributing
77
+
78
+ Bug reports and pull requests are welcome on GitHub at https://github.com/yabeda-rb/yabeda-shoryuken.
79
+
80
+ ### Releasing
81
+
82
+ 1. Bump version number in `lib/yabeda/shoryuken/version.rb`
83
+
84
+ 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::Shoryuken::VERSION).to_s`
85
+
86
+ 2. Fill `CHANGELOG.md` with missing changes, add header with version and date.
87
+
88
+ 3. Make a commit:
89
+
90
+ ```sh
91
+ git add lib/yabeda/shoryuken/version.rb CHANGELOG.md
92
+ version=$(ruby -r ./lib/yabeda/shoryuken/version.rb -e "puts Gem::Version.new(Yabeda::Shoryuken::VERSION)")
93
+ git commit --message="${version}: " --edit
94
+ ```
95
+
96
+ 4. Create annotated tag:
97
+
98
+ ```sh
99
+ git tag v${version} --annotate --message="${version}: " --edit --sign
100
+ ```
101
+
102
+ 5. Fill version name into subject line and (optionally) some description (list of changes will be taken from changelog and appended automatically)
103
+
104
+ 6. Push it:
105
+
106
+ ```sh
107
+ git push --follow-tags
108
+ ```
109
+
110
+ 7. GitHub Actions will create a new release, build and push gem into RubyGems! You're done!
111
+
112
+ ## License
113
+
114
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
115
+
116
+ [Shoryuken]: https://github.com/ruby-shoryuken/shoryuken "A super efficient Amazon SQS thread based message processor for Ruby"
117
+ [yabeda]: https://github.com/yabeda-rb/yabeda
118
+ [yabeda-prometheus]: https://github.com/yabeda-rb/yabeda-prometheus
119
+ [anyway_config]: https://github.com/palkan/anyway_config "Configuration library for Ruby gems and applications"
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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
+ require 'rubocop/rake_task'
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'yabeda/shoryuken'
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
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yabeda
4
+ module Shoryuken
5
+ # Client middleware to count number of enqueued jobs
6
+ class ClientMiddleware
7
+
8
+ def call(options = {})
9
+ queue_url = options[:queue_url]
10
+
11
+ Yabeda.shoryuken_messages_enqueued_total.increment({ queue: queue_url })
12
+
13
+ yield
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'anyway'
4
+
5
+ module Yabeda
6
+ module Shoryuken
7
+ # Yabeda Shoryuken config
8
+ class Config < ::Anyway::Config
9
+ config_name :yabeda_sidekiq
10
+
11
+ # By default all sidekiq worker processes (servers) collects global metrics about whole Sidekiq installation.
12
+ # Client processes (everything else that is not Sidekiq worker) by default doesn't.
13
+ # With this config you can override this behavior:
14
+ # - force disable if you don't want multiple Sidekiq workers to report the same numbers (that causes excess load to both Redis and monitoring)
15
+ # - force enable if you want non-Sidekiq process to collect them (like dedicated metric exporter process)
16
+ attr_config collect_cluster_metrics: ::Shoryuken.server?
17
+
18
+ # Declare metrics that are only tracked inside worker process even outside them
19
+ attr_config declare_process_metrics: ::Shoryuken.server?
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yabeda
4
+ module Shoryuken
5
+ # Shoryuken worker middleware
6
+ class ServerMiddleware
7
+ # rubocop: disable Metrics/AbcSize, :
8
+ def call(worker, queue, sqs_msg, body)
9
+ jid = SecureRandom.uuid
10
+
11
+ custom_tags = Yabeda::Shoryuken.custom_tags(worker, sqs_msg).to_h
12
+ labels = Yabeda::Shoryuken.labelize(worker.class, sqs_msg, queue, body).merge(custom_tags)
13
+ start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
14
+ begin
15
+ Yabeda::Shoryuken.jobs_started_at[labels][jid] = start
16
+ Yabeda.with_tags(**custom_tags) do
17
+ yield
18
+ end
19
+
20
+ Yabeda.shoryuken_jobs_success_total.increment(labels)
21
+ rescue Exception # rubocop: disable Lint/RescueException
22
+ Yabeda.shoryuken_jobs_failed_total.increment(labels)
23
+ raise
24
+ ensure
25
+ Yabeda.shoryuken_job_runtime.measure(labels, elapsed(start))
26
+ Yabeda.shoryuken_jobs_executed_total.increment(labels)
27
+ Yabeda::Shoryuken.jobs_started_at[labels].delete(jid)
28
+ end
29
+ end
30
+ # rubocop: enable Metrics/AbcSize:
31
+
32
+ private
33
+
34
+ def elapsed(start)
35
+ (Process.clock_gettime(Process::CLOCK_MONOTONIC) - start).round(3)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Yabeda
4
+ module Shoryuken
5
+ VERSION = '0.1.1'
6
+ end
7
+ end
@@ -0,0 +1,111 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'shoryuken'
4
+
5
+ require 'yabeda'
6
+ require 'yabeda/shoryuken/version'
7
+ require 'yabeda/shoryuken/client_middleware'
8
+ require 'yabeda/shoryuken/server_middleware'
9
+ require 'yabeda/shoryuken/config'
10
+
11
+ module Yabeda
12
+ # Yabeda Shoryuken integration
13
+ module Shoryuken
14
+ LONG_RUNNING_JOB_RUNTIME_BUCKETS = [
15
+ 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10, # standard (from Prometheus)
16
+ 30, 60, 120, 300, 1800, 3600, 21_600, # Sidekiq tasks may be very long-running
17
+ ].freeze
18
+
19
+ def self.config
20
+ @config ||= Config.new
21
+ end
22
+
23
+ Yabeda.configure do
24
+ config = ::Yabeda::Shoryuken.config
25
+
26
+ group :shoryuken
27
+
28
+ counter :messages_enqueued_total, tags: %i[queue],
29
+ comment: 'A counter of the total number of message shoryuken enqueued.'
30
+
31
+ if config.declare_process_metrics # defaults to +::Shoryuken.server?+
32
+ counter :jobs_executed_total, tags: %i[queue worker],
33
+ comment: 'A counter of the total number of jobs shoryuken executed.'
34
+ counter :jobs_success_total, tags: %i[queue worker],
35
+ comment: 'A counter of the total number of jobs successfully processed by shoryuken.'
36
+ counter :jobs_failed_total, tags: %i[queue worker],
37
+ comment: 'A counter of the total number of jobs failed in shoryuken.'
38
+
39
+ gauge :running_job_runtime, tags: %i[queue worker], aggregation: :max, unit: :seconds,
40
+ comment: 'How long currently running jobs are running (useful for detection of hung jobs)'
41
+
42
+ histogram :job_runtime, comment: 'A histogram of the job execution time.',
43
+ unit: :seconds, per: :job,
44
+ tags: %i[queue worker],
45
+ buckets: LONG_RUNNING_JOB_RUNTIME_BUCKETS
46
+ end
47
+
48
+ collect do
49
+ Yabeda::Shoryuken.track_max_job_runtime if ::Shoryuken.server?
50
+ end
51
+ end
52
+
53
+ ::Shoryuken.configure_server do |config|
54
+ config.server_middleware do |chain|
55
+ chain.add ServerMiddleware
56
+ end
57
+ config.client_middleware do |chain|
58
+ chain.add ClientMiddleware
59
+ end
60
+ end
61
+
62
+ ::Shoryuken.configure_client do |config|
63
+ config.client_middleware do |chain|
64
+ chain.add ClientMiddleware
65
+ end
66
+ end
67
+
68
+ class << self
69
+ def labelize(worker_class, sqs_msg, queue, body = nil)
70
+ { queue: queue, worker: worker_name(worker_class, sqs_msg, body) }
71
+ end
72
+
73
+ def worker_name(worker_class, sqs_msg, body = nil)
74
+ if ::Shoryuken.active_job? \
75
+ && !sqs_msg.is_a?(Array) \
76
+ && sqs_msg.message_attributes \
77
+ && sqs_msg.message_attributes['shoryuken_class'] \
78
+ && sqs_msg.message_attributes['shoryuken_class'][:string_value] \
79
+ == ActiveJob::QueueAdapters::ShoryukenAdapter::JobWrapper.to_s \
80
+ && body
81
+
82
+ "ActiveJob/#{body['job_class']}"
83
+ else
84
+ worker_class.to_s
85
+ end
86
+ end
87
+
88
+ def custom_tags(worker, sqs_msg)
89
+ return {} unless worker.respond_to?(:yabeda_tags)
90
+
91
+ worker.method(:yabeda_tags).arity.zero? ? worker.yabeda_tags : worker.yabeda_tags(*sqs_msg.attributes)
92
+ end
93
+
94
+ # Hash of hashes containing all currently running jobs' start timestamps
95
+ # to calculate maximum durations of currently running not yet completed jobs
96
+ # { { queue: "default", worker: "SomeJob" } => { "jid1" => 100500, "jid2" => 424242 } }
97
+ attr_accessor :jobs_started_at
98
+
99
+ def track_max_job_runtime
100
+ now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
101
+ ::Yabeda::Shoryuken.jobs_started_at.each do |labels, jobs|
102
+ oldest_job_started_at = jobs.values.min
103
+ oldest_job_duration = oldest_job_started_at ? (now - oldest_job_started_at).round(3) : 0
104
+ Yabeda.shoryuken.running_job_runtime.set(labels, oldest_job_duration)
105
+ end
106
+ end
107
+ end
108
+
109
+ self.jobs_started_at = Concurrent::Map.new { |hash, key| hash[key] = Concurrent::Map.new }
110
+ end
111
+ end
@@ -0,0 +1,6 @@
1
+ module Yabeda
2
+ module Shoryuken
3
+ VERSION: String
4
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
+ end
6
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/yabeda/shoryuken/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'yabeda-shoryuken'
7
+ spec.version = Yabeda::Shoryuken::VERSION
8
+ spec.authors = ['Roberto Scinocca']
9
+ spec.email = ['roberto.scinocca@hey.com']
10
+
11
+ spec.summary = 'Yabeda extension for Shoryuken'
12
+ spec.homepage = 'https://github.com/retsef/yabeda-shoryuken'
13
+ spec.license = 'MIT'
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = 'https://github.com/retsef/yabeda-shoryuken'
17
+ spec.metadata['changelog_uri'] = 'https://github.com/retsef/yabeda-shoryuken/blob/master/CHANGELOG.md'
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.required_ruby_version = '>= 2.3'
27
+
28
+ spec.add_dependency 'anyway_config', '>= 1.3', '< 3'
29
+ spec.add_dependency 'rack'
30
+ spec.add_dependency 'shoryuken'
31
+ spec.add_dependency 'yabeda', '~> 0.10'
32
+
33
+ spec.add_development_dependency 'aws-sdk-sqs', '>= 0'
34
+ spec.add_development_dependency 'bundler', '~> 2.0'
35
+ spec.add_development_dependency 'rake', '~> 13.0'
36
+ spec.add_development_dependency 'rspec', '~> 3.0'
37
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yabeda-shoryuken
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Roberto Scinocca
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-01-30 00:00:00.000000000 Z
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'
33
+ - !ruby/object:Gem::Dependency
34
+ name: rack
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: shoryuken
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: yabeda
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '0.10'
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '0.10'
75
+ - !ruby/object:Gem::Dependency
76
+ name: aws-sdk-sqs
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: bundler
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '2.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '2.0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rake
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '13.0'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '13.0'
117
+ - !ruby/object:Gem::Dependency
118
+ name: rspec
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - "~>"
122
+ - !ruby/object:Gem::Version
123
+ version: '3.0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - "~>"
129
+ - !ruby/object:Gem::Version
130
+ version: '3.0'
131
+ description:
132
+ email:
133
+ - roberto.scinocca@hey.com
134
+ executables: []
135
+ extensions: []
136
+ extra_rdoc_files: []
137
+ files:
138
+ - ".github/workflows/build-release.yml"
139
+ - ".github/workflows/test.yml"
140
+ - ".gitignore"
141
+ - ".rspec"
142
+ - ".rubocop.yml"
143
+ - CHANGELOG.md
144
+ - CODE_OF_CONDUCT.md
145
+ - Gemfile
146
+ - LICENSE.txt
147
+ - README.md
148
+ - Rakefile
149
+ - bin/console
150
+ - bin/setup
151
+ - lib/yabeda/shoryuken.rb
152
+ - lib/yabeda/shoryuken/client_middleware.rb
153
+ - lib/yabeda/shoryuken/config.rb
154
+ - lib/yabeda/shoryuken/server_middleware.rb
155
+ - lib/yabeda/shoryuken/version.rb
156
+ - sig/yabeda/shoryuken.rbs
157
+ - yabeda-shoryuken.gemspec
158
+ homepage: https://github.com/retsef/yabeda-shoryuken
159
+ licenses:
160
+ - MIT
161
+ metadata:
162
+ homepage_uri: https://github.com/retsef/yabeda-shoryuken
163
+ source_code_uri: https://github.com/retsef/yabeda-shoryuken
164
+ changelog_uri: https://github.com/retsef/yabeda-shoryuken/blob/master/CHANGELOG.md
165
+ post_install_message:
166
+ rdoc_options: []
167
+ require_paths:
168
+ - lib
169
+ required_ruby_version: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '2.3'
174
+ required_rubygems_version: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ requirements: []
180
+ rubygems_version: 3.3.7
181
+ signing_key:
182
+ specification_version: 4
183
+ summary: Yabeda extension for Shoryuken
184
+ test_files: []