cloudwatch-metrics-resque 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d70d5795af66b45d5f34a4a1e433c1bf43259ef6
4
+ data.tar.gz: 3c2f9b76245208d75dd3f2f677ca66d1ee00f0a9
5
+ SHA512:
6
+ metadata.gz: 776ed5fe053d3de4412eb2e47047f7511d278dd4641781e74a9a2d0d8867f412b99dd223b7c46a10317559785a274dbc510a55735fa6bf32e94633978716d907
7
+ data.tar.gz: aac6778d90517e5a43a230c0f14632624611f3afbfdd6079cc901f1df2d9a752c81337a70afa15aa7ace5d8a79b91fa7ddb2e87a59f70108bde8951ff60d3d48
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,72 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.3
3
+
4
+ ### customize
5
+
6
+ Metrics/ParameterLists:
7
+ CountKeywordArgs: false
8
+
9
+ Style/Lambda:
10
+ EnforcedStyle: literal
11
+
12
+ Style/LambdaCall:
13
+ EnforcedStyle: braces
14
+
15
+ Style/MultilineMethodCallIndentation:
16
+ EnforcedStyle: indented
17
+
18
+ Style/TrailingCommaInArguments:
19
+ EnforcedStyleForMultiline: comma
20
+
21
+ Style/TrailingCommaInLiteral:
22
+ EnforcedStyleForMultiline: comma
23
+
24
+ ### disabled
25
+
26
+ Style/Documentation:
27
+ Enabled: false
28
+
29
+ ### enabled
30
+
31
+ Style/AutoResourceCleanup:
32
+ Enabled: true
33
+
34
+ Style/CollectionMethods:
35
+ Enabled: true
36
+
37
+ Style/Encoding:
38
+ Enabled: true
39
+
40
+ Style/FirstArrayElementLineBreak:
41
+ Enabled: true
42
+
43
+ Style/FirstHashElementLineBreak:
44
+ Enabled: true
45
+
46
+ Style/FirstMethodArgumentLineBreak:
47
+ Enabled: true
48
+
49
+ Style/FirstMethodParameterLineBreak:
50
+ Enabled: true
51
+
52
+ Style/ImplicitRuntimeError:
53
+ Enabled: true
54
+
55
+ Style/InlineComment:
56
+ Enabled: true
57
+
58
+ Style/MultilineAssignmentLayout:
59
+ Enabled: true
60
+ EnforcedStyle: same_line
61
+
62
+ Style/OptionHash:
63
+ Enabled: true
64
+
65
+ Style/Send:
66
+ Enabled: true
67
+
68
+ Style/StringMethods:
69
+ Enabled: true
70
+
71
+ Style/SymbolArray:
72
+ Enabled: true
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at m.ishihara@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in cloudwatch-metrics-resque.gemspec
6
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Masaki Takeuchi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,66 @@
1
+ # CloudWatchMetrics::Resque
2
+
3
+ Send Resque.info to CloudWatch Metrics
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/cloudwatch-metrics-resque.svg)](https://badge.fury.io/rb/cloudwatch-metrics-resque)
6
+ [![Code Climate](https://codeclimate.com/github/m4i/cloudwatch-metrics-resque/badges/gpa.svg)](https://codeclimate.com/github/m4i/cloudwatch-metrics-resque)
7
+ [![Dependency Status](https://gemnasium.com/badges/github.com/m4i/cloudwatch-metrics-resque.svg)](https://gemnasium.com/github.com/m4i/cloudwatch-metrics-resque)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'cloudwatch-metrics-resque'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install cloudwatch-metrics-resque
24
+
25
+ Or use Docker:
26
+
27
+ $ docker run --rm m4i0/cloudwatch-metrics-resque --help
28
+
29
+ ## Usage
30
+
31
+ ```
32
+ Usage: cloudwatch-metrics-resque [options]
33
+ --namespace <namespace>
34
+ --interval <seconds>
35
+ --dryrun
36
+ -h, --host <host>
37
+ -p, --port <port>
38
+ -s, --socket <socket>
39
+ -a, --password <password>
40
+ -n, --db <db>
41
+ --url <url>
42
+ --redis-namespace <namespace>
43
+ --[no-]pending
44
+ --[no-]processed
45
+ --[no-]failed
46
+ --[no-]queues
47
+ --[no-]workers
48
+ --[no-]working
49
+ --[no-]pending-per-queue
50
+ --[no-]not-working
51
+ --[no-]processing
52
+ ```
53
+
54
+ ## Development
55
+
56
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
57
+
58
+ 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).
59
+
60
+ ## Contributing
61
+
62
+ Bug reports and pull requests are welcome on GitHub at https://github.com/m4i/cloudwatch-metrics-resque. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ task default: :spec
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'cloudwatch/metrics/resque'
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
@@ -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,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'cloud_watch_metrics/resque/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'cloudwatch-metrics-resque'
9
+ spec.version = CloudWatchMetrics::Resque::VERSION
10
+ spec.authors = ['Masaki Takeuchi']
11
+ spec.email = ['m.ishihara@gmail.com']
12
+
13
+ spec.summary = 'Send Resque.info to CloudWatch Metrics'
14
+ spec.homepage = 'https://github.com/m4i/cloudwatch-metrics-resque'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = 'exe'
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ['lib']
23
+
24
+ spec.required_ruby_version = '>= 2.4'
25
+
26
+ spec.add_dependency 'aws-sdk-core', '~> 2'
27
+ spec.add_dependency 'resque', '~> 1'
28
+
29
+ spec.add_development_dependency 'bundler', '~> 1.13'
30
+ spec.add_development_dependency 'rake', '~> 12.0'
31
+ spec.add_development_dependency 'rubocop', '>= 0.46'
32
+ end
@@ -0,0 +1 @@
1
+ /.bundle/
@@ -0,0 +1,15 @@
1
+ FROM ruby:2.4-alpine
2
+
3
+ RUN set -eux \
4
+ && apk add --no-cache \
5
+ openssl \
6
+ # dumb-init
7
+ && VERSION=1.2.0 \
8
+ && wget -O /usr/local/bin/dumb-init https://github.com/Yelp/dumb-init/releases/download/v$VERSION/dumb-init_${VERSION}_amd64 \
9
+ && chmod +x /usr/local/bin/dumb-init
10
+
11
+ ENTRYPOINT ["dumb-init", "cloudwatch-metrics-resque"]
12
+ WORKDIR /app
13
+
14
+ COPY Gemfile Gemfile.lock /app/
15
+ RUN bundle install -j4 --frozen
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'cloudwatch-metrics-resque'
@@ -0,0 +1,41 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ aws-sdk-core (2.6.44)
5
+ aws-sigv4 (~> 1.0)
6
+ jmespath (~> 1.0)
7
+ aws-sigv4 (1.0.0)
8
+ jmespath (1.3.1)
9
+ mono_logger (1.1.0)
10
+ multi_json (1.12.1)
11
+ rack (1.6.5)
12
+ rack-protection (1.5.3)
13
+ rack
14
+ redis (3.3.2)
15
+ redis-namespace (1.5.2)
16
+ redis (~> 3.0, >= 3.0.4)
17
+ resque (1.26.0)
18
+ mono_logger (~> 1.0)
19
+ multi_json (~> 1.0)
20
+ redis-namespace (~> 1.3)
21
+ sinatra (>= 0.9.2)
22
+ vegas (~> 0.1.2)
23
+ cloudwatch-metrics-resque (0.5.0)
24
+ aws-sdk-core (~> 2)
25
+ resque (~> 1)
26
+ sinatra (1.4.7)
27
+ rack (~> 1.5)
28
+ rack-protection (~> 1.4)
29
+ tilt (>= 1.3, < 3)
30
+ tilt (2.0.5)
31
+ vegas (0.1.11)
32
+ rack (>= 1.0.0)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ cloudwatch-metrics-resque
39
+
40
+ BUNDLED WITH
41
+ 1.13.7
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'cloud_watch_metrics/resque'
5
+
6
+ Version = CloudWatchMetrics::Resque::VERSION
7
+ CloudWatchMetrics::Resque.run(ARGV)
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CloudWatchMetrics
4
+ autoload :Base, 'cloud_watch_metrics/base'
5
+ autoload :Dimensions, 'cloud_watch_metrics/dimensions'
6
+ autoload :MetaData, 'cloud_watch_metrics/meta_data'
7
+ autoload :Util, 'cloud_watch_metrics/util'
8
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CloudWatchMetrics
4
+ module Base
5
+ def self.included(mod)
6
+ mod.extend(ClassMethods)
7
+ end
8
+
9
+ module ClassMethods
10
+ def run(args)
11
+ new(parse_arguments(args)).run
12
+ end
13
+ end
14
+
15
+ def run
16
+ if @interval
17
+ loop do
18
+ start = Time.now
19
+ Timeout.timeout(@interval * 2 - 1) { run_once }
20
+ sleep((start - Time.now) % @interval)
21
+ end
22
+ else
23
+ run_once
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CloudWatchMetrics
4
+ class Dimensions < Hash
5
+ def initialize(hash)
6
+ super()
7
+ replace(hash)
8
+ end
9
+
10
+ def to_cloudwatch
11
+ map { |name, value| { name: name, value: value } }
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'open-uri'
4
+
5
+ module CloudWatchMetrics
6
+ module MetaData
7
+ class << self
8
+ def instance_id
9
+ open('http://169.254.169.254/latest/meta-data/instance-id', &:read)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,126 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'optparse'
4
+
5
+ require 'resque'
6
+
7
+ require 'cloud_watch_metrics'
8
+ require 'cloud_watch_metrics/resque/builder'
9
+ require 'cloud_watch_metrics/resque/info'
10
+ require 'cloud_watch_metrics/resque/version'
11
+
12
+ module CloudWatchMetrics
13
+ class Resque
14
+ include Base
15
+
16
+ DEFAULT_NAMESPACE = 'Resque'
17
+ DEFAULT_METRICS = {
18
+ pending: true,
19
+ processed: true,
20
+ failed: true,
21
+ queues: true,
22
+ workers: true,
23
+ working: true,
24
+ pending_per_queue: true,
25
+ not_working: false,
26
+ processing: false,
27
+ }.freeze
28
+
29
+ class << self
30
+ private
31
+
32
+ def parse_arguments(args)
33
+ {}.tap do |options|
34
+ option_parser.parse(args, into: options)
35
+
36
+ convert_symbol_keys_from_dash_to_underscore(options)
37
+
38
+ redis = delete_keys(options, %i(host port socket password db))
39
+ redis = options.delete(:url) if options.key?(:url)
40
+ options[:redis] = redis unless redis.empty?
41
+
42
+ options[:metrics] = delete_keys(options, DEFAULT_METRICS.keys)
43
+ end
44
+ end
45
+
46
+ def option_parser
47
+ OptionParser.new do |opt|
48
+ opt.on('--namespace <namespace>', String)
49
+ opt.on('--interval <seconds>', Float)
50
+ opt.on('--dryrun', TrueClass)
51
+
52
+ opt.on('-h', '--host <host>', String)
53
+ opt.on('-p', '--port <port>', Integer)
54
+ opt.on('-s', '--socket <socket>', String)
55
+ opt.on('-a', '--password <password>', String)
56
+ opt.on('-n', '--db <db>', String)
57
+ opt.on('--url <url>', String)
58
+ opt.on('--redis-namespace <namespace>', String)
59
+
60
+ DEFAULT_METRICS.each_key do |key|
61
+ opt.on("--[no-]#{key.to_s.tr('_', '-')}", TrueClass)
62
+ end
63
+ end
64
+ end
65
+
66
+ def convert_symbol_keys_from_dash_to_underscore(hash)
67
+ hash.keys.each do |key|
68
+ if key.match?('-')
69
+ hash[key.to_s.tr('-', '_').to_sym] = hash.delete(key)
70
+ end
71
+ end
72
+ end
73
+
74
+ def delete_keys(hash, keys)
75
+ hash
76
+ .select { |key,| keys.include?(key) }
77
+ .each_key { |key| hash.delete(key) }
78
+ end
79
+ end
80
+
81
+ def initialize(
82
+ namespace: DEFAULT_NAMESPACE,
83
+ interval: nil,
84
+ dryrun: false,
85
+ redis: nil,
86
+ redis_namespace: nil,
87
+ metrics: {}
88
+ )
89
+ @namespace = namespace
90
+ @interval = interval
91
+ @dryrun = dryrun
92
+ ::Resque.redis = redis if redis
93
+ @redis_namespace = redis_namespace
94
+ @metrics = DEFAULT_METRICS.merge(metrics)
95
+ end
96
+
97
+ private
98
+
99
+ def run_once
100
+ metric_data = redis_namespaces.flat_map do |redis_namespace|
101
+ builder.build(info(redis_namespace).update)
102
+ end
103
+
104
+ Util.put_metric_data(@namespace, metric_data, dryrun: @dryrun)
105
+ end
106
+
107
+ def redis_namespaces
108
+ return [::Resque.redis.namespace] unless @redis_namespace
109
+ return [@redis_namespace.to_sym] unless @redis_namespace.include?('*')
110
+
111
+ suffix = ':queues'
112
+ ::Resque.redis.redis.keys(@redis_namespace + suffix).map do |key|
113
+ key[0...-suffix.length].to_sym
114
+ end
115
+ end
116
+
117
+ def builder
118
+ @_builder ||= Builder.new(@metrics)
119
+ end
120
+
121
+ def info(namespace)
122
+ @_infos ||= {}
123
+ @_infos[namespace] ||= Info.new(namespace, @metrics[:pending_per_queue])
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CloudWatchMetrics
4
+ class Resque
5
+ class Builder
6
+ def initialize(metrics)
7
+ @metric_names = metrics.select do |key, value|
8
+ value && key != :pending_per_queue
9
+ end.keys
10
+ end
11
+
12
+ def build(info)
13
+ @info = info
14
+
15
+ metric_data = @metric_names.map do |key|
16
+ build_datum(camelize(key.to_s), @info.public_send(key))
17
+ end
18
+
19
+ metric_data.concat(build_per_queue) if @info.queue_sizes
20
+
21
+ metric_data
22
+ end
23
+
24
+ private
25
+
26
+ def build_per_queue
27
+ @info.queue_sizes.map do |name, size|
28
+ build_datum('Pending', size, Queue: name)
29
+ end
30
+ end
31
+
32
+ def build_datum(metric_name, value, dimensions = {})
33
+ {
34
+ metric_name: metric_name,
35
+ dimensions: default_dimensions.merge(dimensions).to_cloudwatch,
36
+ timestamp: @info.time,
37
+ value: value,
38
+ unit: 'Count',
39
+ }
40
+ end
41
+
42
+ def default_dimensions
43
+ Dimensions.new(Namespace: @info.namespace.to_s)
44
+ end
45
+
46
+ def camelize(string)
47
+ string.gsub(/(?:^|_)(.)/) { Regexp.last_match(1).upcase }
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'resque'
4
+
5
+ module CloudWatchMetrics
6
+ class Resque
7
+ class Info
8
+ attr_reader :namespace, :time, :queue_sizes
9
+
10
+ def initialize(namespace, queue_sizes_enabled)
11
+ @namespace = namespace
12
+ @queue_sizes_enabled = queue_sizes_enabled
13
+ end
14
+
15
+ def update
16
+ @previous_processed = processed if @info
17
+ ::Resque.redis.namespace = @namespace
18
+ @time = Time.now
19
+ @info = ::Resque.info
20
+ @queue_sizes = fetch_queue_sizes if @queue_sizes_enabled
21
+ self
22
+ end
23
+
24
+ def pending
25
+ @info.fetch(:pending)
26
+ end
27
+
28
+ def processed
29
+ @info.fetch(:processed)
30
+ end
31
+
32
+ def failed
33
+ @info.fetch(:failed)
34
+ end
35
+
36
+ def queues
37
+ @info.fetch(:queues)
38
+ end
39
+
40
+ def workers
41
+ @info.fetch(:workers)
42
+ end
43
+
44
+ def working
45
+ @info.fetch(:working)
46
+ end
47
+
48
+ def not_working
49
+ [0, workers - working].max
50
+ end
51
+
52
+ def processing
53
+ incremental_size_of_processed + working
54
+ end
55
+
56
+ private
57
+
58
+ def fetch_queue_sizes
59
+ ::Resque.queues.map { |name| [name, ::Resque.size(name)] }
60
+ end
61
+
62
+ def incremental_size_of_processed
63
+ if @previous_processed.nil?
64
+ 0
65
+ else
66
+ [0, processed - @previous_processed].max
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CloudWatchMetrics
4
+ class Resque
5
+ VERSION = '0.5.0'
6
+ end
7
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ require 'aws-sdk-core'
6
+
7
+ module CloudWatchMetrics
8
+ module Util
9
+ # http://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_limits.html
10
+ MAX_METRIC_DATA_PER_PUT = 20
11
+
12
+ class << self
13
+ def accept_hash(option_parser)
14
+ option_parser.accept(Hash) do |s,|
15
+ break s unless s
16
+ s
17
+ .split(',').reject(&:empty?)
18
+ .map { |kv| kv.include?('=') ? kv.split('=', 2) : [kv, true] }
19
+ .to_h
20
+ end
21
+ end
22
+
23
+ # @return [void]
24
+ def put_metric_data(namespace, metric_data, dryrun: false)
25
+ return dump_metric_data(namespace, metric_data) if dryrun
26
+
27
+ metric_data.each_slice(MAX_METRIC_DATA_PER_PUT).map do |data|
28
+ Thread.start(data, cloudwatch) do |data_, cloudwatch_|
29
+ cloudwatch_.put_metric_data(
30
+ namespace: namespace,
31
+ metric_data: data_,
32
+ )
33
+ end
34
+ end.each(&:join)
35
+ end
36
+
37
+ private
38
+
39
+ def cloudwatch
40
+ @_cloudwatch ||= Aws::CloudWatch::Client.new
41
+ end
42
+
43
+ def dump_metric_data(namespace, metric_data)
44
+ json = { namespace: namespace, metric_data: metric_data }.to_json(
45
+ indent: ' ' * 2,
46
+ space: ' ',
47
+ object_nl: "\n",
48
+ array_nl: "\n",
49
+ )
50
+ puts json
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cloud_watch_metrics/resque'
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloudwatch-metrics-resque
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: ruby
6
+ authors:
7
+ - Masaki Takeuchi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-01-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk-core
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: resque
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0.46'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0.46'
83
+ description:
84
+ email:
85
+ - m.ishihara@gmail.com
86
+ executables:
87
+ - cloudwatch-metrics-resque
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rubocop.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - cloudwatch-metrics-resque.gemspec
101
+ - docker/.gitignore
102
+ - docker/Dockerfile
103
+ - docker/Gemfile
104
+ - docker/Gemfile.lock
105
+ - exe/cloudwatch-metrics-resque
106
+ - lib/cloud_watch_metrics.rb
107
+ - lib/cloud_watch_metrics/base.rb
108
+ - lib/cloud_watch_metrics/dimensions.rb
109
+ - lib/cloud_watch_metrics/meta_data.rb
110
+ - lib/cloud_watch_metrics/resque.rb
111
+ - lib/cloud_watch_metrics/resque/builder.rb
112
+ - lib/cloud_watch_metrics/resque/info.rb
113
+ - lib/cloud_watch_metrics/resque/version.rb
114
+ - lib/cloud_watch_metrics/util.rb
115
+ - lib/cloudwatch/metrics/resque.rb
116
+ homepage: https://github.com/m4i/cloudwatch-metrics-resque
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '2.4'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.6.8
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Send Resque.info to CloudWatch Metrics
140
+ test_files: []