datadog-compound-metrics 0.1.0

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: 81e2cccbdfe1ede252799a12fb7b335f7bb69f8a62910788a4d31a3312230f82
4
+ data.tar.gz: dda6d1593b416f625f9f7b8e82ed63ea218060d37c01ba4ed24ff82b8c2ae99f
5
+ SHA512:
6
+ metadata.gz: 91550f3fffc294d83900ca69f72daec85f6514dff04e522449d661f1d29949a73f0098a5729c4bff2a5c30120c19f98792a392bac52992415c922abb31ae22ae
7
+ data.tar.gz: ce5587f36b1a6d0bb0eb2215aaa512ab73dbf74c7e5326a7c144e7f651d4a87172cd8cc5217bb1b4078257575a645b3b7e2e2c61b1d3d1fa356c403174dd5518
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,132 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+ - rubocop-rake
5
+
6
+ inherit_mode:
7
+ merge:
8
+ - Exclude
9
+
10
+ AllCops:
11
+ NewCops: enable
12
+ TargetRubyVersion: 3.0
13
+ Exclude:
14
+ - "db/**/*"
15
+ - "bin/**/*"
16
+ - "tmp/**/*"
17
+ - "log/**/*"
18
+ - "vendor/**/*"
19
+ - "spec/rails_helper.rb"
20
+
21
+ Layout/ArgumentAlignment:
22
+ EnforcedStyle: with_fixed_indentation
23
+
24
+ Layout/ArrayAlignment:
25
+ EnforcedStyle: with_fixed_indentation
26
+
27
+ Layout/EmptyLineBetweenDefs:
28
+ AllowAdjacentOneLineDefs: true
29
+
30
+ Layout/EndAlignment:
31
+ EnforcedStyleAlignWith: variable
32
+
33
+ Layout/FirstArgumentIndentation:
34
+ EnforcedStyle: consistent
35
+
36
+ Layout/FirstArrayElementIndentation:
37
+ EnforcedStyle: consistent
38
+
39
+ Layout/FirstHashElementIndentation:
40
+ EnforcedStyle: consistent
41
+
42
+ Layout/MultilineMethodCallIndentation:
43
+ EnforcedStyle: indented
44
+
45
+ Layout/MultilineOperationIndentation:
46
+ EnforcedStyle: indented
47
+
48
+ Layout/ParameterAlignment:
49
+ EnforcedStyle: with_fixed_indentation
50
+
51
+ Layout/SpaceBeforeBrackets:
52
+ Enabled: false
53
+
54
+ Lint/UnusedMethodArgument:
55
+ AllowUnusedKeywordArguments: true
56
+
57
+ Metrics/ParameterLists:
58
+ MaxOptionalParameters: 4
59
+
60
+ RSpec/ExpectChange:
61
+ EnforcedStyle: block
62
+
63
+ RSpec/LetSetup:
64
+ Enabled: false
65
+
66
+ RSpec/MultipleExpectations:
67
+ Max: 20
68
+
69
+ Style/Alias:
70
+ EnforcedStyle: prefer_alias_method
71
+
72
+ Style/ClassAndModuleChildren:
73
+ EnforcedStyle: nested
74
+
75
+ Style/CommandLiteral:
76
+ EnforcedStyle: percent_x
77
+
78
+ Style/RescueStandardError:
79
+ EnforcedStyle: implicit
80
+
81
+ Style/StringLiterals:
82
+ EnforcedStyle: double_quotes
83
+ ConsistentQuotesInMultiline: true
84
+
85
+ Style/StringLiteralsInInterpolation:
86
+ EnforcedStyle: double_quotes
87
+
88
+ Style/RaiseArgs:
89
+ EnforcedStyle: compact
90
+
91
+ Style/RegexpLiteral:
92
+ EnforcedStyle: percent_r
93
+
94
+ Style/Documentation:
95
+ Enabled: false
96
+
97
+ RSpec/NestedGroups:
98
+ Max: 10
99
+
100
+ RSpec/MultipleMemoizedHelpers:
101
+ Enabled: false
102
+
103
+ Lint/AmbiguousBlockAssociation:
104
+ Exclude:
105
+ - 'spec/**/*'
106
+
107
+ Naming/FileName:
108
+ Exclude:
109
+ - 'lib/datadog-compound-metrics.rb'
110
+
111
+ RSpec/ExampleLength:
112
+ Max: 15
113
+
114
+
115
+ Metrics/MethodLength:
116
+ Max: 20
117
+
118
+
119
+ RSpec/DescribeClass:
120
+ Exclude:
121
+ - 'spec/integration_spec.rb'
122
+
123
+ Naming/VariableNumber:
124
+ EnforcedStyle: snake_case
125
+
126
+ Layout/LineLength:
127
+ Exclude:
128
+ - 'datadog-compound-metrics.gemspec'
129
+
130
+ Lint/ConstantDefinitionInBlock:
131
+ Exclude:
132
+ - 'spec/spec_helper.rb'
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0]
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in datadog-compound-metrics.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "dogstatsd-ruby"
11
+ gem "rspec", "~> 3.0"
12
+ gem "rubocop", require: false
13
+ gem "rubocop-performance"
14
+ gem "rubocop-rake"
15
+ gem "rubocop-rspec"
16
+ gem "sidekiq", "~> 6.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,109 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ datadog-compound-metrics (0.1.0)
5
+ activesupport (>= 5)
6
+ sidekiq (>= 5)
7
+ sidekiq-cron
8
+ zeitwerk
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ activesupport (7.0.4.3)
14
+ concurrent-ruby (~> 1.0, >= 1.0.2)
15
+ i18n (>= 1.6, < 2)
16
+ minitest (>= 5.1)
17
+ tzinfo (~> 2.0)
18
+ ast (2.4.2)
19
+ concurrent-ruby (1.2.2)
20
+ connection_pool (2.4.0)
21
+ diff-lcs (1.5.0)
22
+ dogstatsd-ruby (5.5.0)
23
+ et-orbi (1.2.7)
24
+ tzinfo
25
+ fugit (1.8.1)
26
+ et-orbi (~> 1, >= 1.2.7)
27
+ raabro (~> 1.4)
28
+ globalid (1.1.0)
29
+ activesupport (>= 5.0)
30
+ i18n (1.12.0)
31
+ concurrent-ruby (~> 1.0)
32
+ json (2.6.3)
33
+ minitest (5.18.0)
34
+ parallel (1.22.1)
35
+ parser (3.2.2.0)
36
+ ast (~> 2.4.1)
37
+ raabro (1.4.0)
38
+ rack (2.2.6.4)
39
+ rainbow (3.1.1)
40
+ rake (13.0.6)
41
+ redis (4.8.1)
42
+ regexp_parser (2.7.0)
43
+ rexml (3.2.5)
44
+ rspec (3.12.0)
45
+ rspec-core (~> 3.12.0)
46
+ rspec-expectations (~> 3.12.0)
47
+ rspec-mocks (~> 3.12.0)
48
+ rspec-core (3.12.1)
49
+ rspec-support (~> 3.12.0)
50
+ rspec-expectations (3.12.2)
51
+ diff-lcs (>= 1.2.0, < 2.0)
52
+ rspec-support (~> 3.12.0)
53
+ rspec-mocks (3.12.5)
54
+ diff-lcs (>= 1.2.0, < 2.0)
55
+ rspec-support (~> 3.12.0)
56
+ rspec-support (3.12.0)
57
+ rubocop (1.49.0)
58
+ json (~> 2.3)
59
+ parallel (~> 1.10)
60
+ parser (>= 3.2.0.0)
61
+ rainbow (>= 2.2.2, < 4.0)
62
+ regexp_parser (>= 1.8, < 3.0)
63
+ rexml (>= 3.2.5, < 4.0)
64
+ rubocop-ast (>= 1.28.0, < 2.0)
65
+ ruby-progressbar (~> 1.7)
66
+ unicode-display_width (>= 2.4.0, < 3.0)
67
+ rubocop-ast (1.28.0)
68
+ parser (>= 3.2.1.0)
69
+ rubocop-capybara (2.17.1)
70
+ rubocop (~> 1.41)
71
+ rubocop-performance (1.16.0)
72
+ rubocop (>= 1.7.0, < 2.0)
73
+ rubocop-ast (>= 0.4.0)
74
+ rubocop-rake (0.6.0)
75
+ rubocop (~> 1.0)
76
+ rubocop-rspec (2.19.0)
77
+ rubocop (~> 1.33)
78
+ rubocop-capybara (~> 2.17)
79
+ ruby-progressbar (1.13.0)
80
+ sidekiq (6.5.8)
81
+ connection_pool (>= 2.2.5, < 3)
82
+ rack (~> 2.0)
83
+ redis (>= 4.5.0, < 5)
84
+ sidekiq-cron (1.10.0)
85
+ fugit (~> 1.8)
86
+ globalid (>= 1.0.1)
87
+ sidekiq (>= 6)
88
+ tzinfo (2.0.6)
89
+ concurrent-ruby (~> 1.0)
90
+ unicode-display_width (2.4.2)
91
+ zeitwerk (2.6.7)
92
+
93
+ PLATFORMS
94
+ x86_64-darwin-21
95
+ x86_64-linux
96
+
97
+ DEPENDENCIES
98
+ datadog-compound-metrics!
99
+ dogstatsd-ruby
100
+ rake (~> 13.0)
101
+ rspec (~> 3.0)
102
+ rubocop
103
+ rubocop-performance
104
+ rubocop-rake
105
+ rubocop-rspec
106
+ sidekiq (~> 6.0)
107
+
108
+ BUNDLED WITH
109
+ 2.4.10
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Karol Galanciak
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,56 @@
1
+ # DatadogCompoundMetrics
2
+
3
+ A gem for building compound metric (a single metric from multiple ones). Mostly to have a single metric do Horizontal Pod Autoscaling for workers consuming from multiple queues.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add datadog-compound-metrics
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install datadog-compound-metrics
14
+
15
+ ## Usage
16
+
17
+
18
+ In the initializer:
19
+
20
+
21
+ ``` rb
22
+ Rails.application.config.to_prepare do
23
+ DatadogCompoundMetrics.configure do |config|
24
+ config.datadog_statsd_client = Datadog::Statsd.new(ENV.fetch("DD_AGENT_HOST"), ENV.fetch("DATADOG_PORT"), namespace: "app_name.production", tags: ["host:disabled"]) # required
25
+ config.sidekiq_queue = :critical # required
26
+ config.sidekiq_cron_schedule = "*/10 * * * * *" # required, you can also use extended syntax covering seconds
27
+ end
28
+
29
+ DatadogCompoundMetrics.add_compound_metric("sidekiq.autoscaling.high_concurrency_worker") do |compound_metric|
30
+ compound_metric.add_calculation(-> { Sidekiq::Queue.new("queue_1").latency })
31
+ compound_metric.add_calculation(-> { Sidekiq::Queue.new("queue_2").latency })
32
+ # to have a single metric taking the maximum from these latencies:
33
+ compound_metric.calculation_strategy = :max # :min/:max, in the end it's going to be a method call on the Array
34
+ end
35
+
36
+ DatadogCompoundMetrics.schedule_job # add it to cron jobs
37
+ end
38
+
39
+ # remember also to tweak cron poll interval if you are planning to use extended syntax covering seconds and schedule jobs more often
40
+ Sidekiq::Options[:cron_poll_interval] = 10
41
+ ```
42
+
43
+
44
+ ## Development
45
+
46
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
47
+
48
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
49
+
50
+ ## Contributing
51
+
52
+ Bug reports and pull requests are welcome on GitHub at https://github.com/BookingSync/datadog-compound-metrics.
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "datadog_compound_metrics"
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DatadogCompoundMetrics
4
+ class CompoundMetric
5
+ attr_reader :name, :calculations
6
+ attr_accessor :calculation_strategy
7
+
8
+ def initialize(name)
9
+ @name = name
10
+ @calculations = []
11
+ end
12
+
13
+ def add_calculation(calculation)
14
+ calculations << calculation
15
+ end
16
+
17
+ def compute
18
+ calculations
19
+ .map(&:call)
20
+ .public_send(calculation_strategy)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DatadogCompoundMetrics
4
+ class CompoundMetricsWorker
5
+ include Sidekiq::Worker
6
+
7
+ sidekiq_options queue: :default
8
+
9
+ delegate :configuration, :compound_metrics, to: DatadogCompoundMetrics
10
+ delegate :datadog_statsd_client, to: :configuration
11
+
12
+ def perform
13
+ compound_metrics.each do |metric|
14
+ datadog_statsd_client.gauge(metric.name.to_s, metric.compute)
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DatadogCompoundMetrics
4
+ class Configuration
5
+ attr_accessor :datadog_statsd_client, :sidekiq_queue, :sidekiq_cron_schedule
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DatadogCompoundMetrics
4
+ module Version
5
+ end
6
+ VERSION = "0.1.0"
7
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require "active_support/core_ext"
5
+ require "sidekiq"
6
+ require "sidekiq-cron"
7
+ require "zeitwerk"
8
+
9
+ loader = Zeitwerk::Loader.for_gem
10
+ loader.ignore("#{__dir__}/datadog-compound-metrics.rb")
11
+ loader.setup
12
+
13
+ class DatadogCompoundMetrics
14
+ def self.loader
15
+ @loader ||= Zeitwerk::Loader.for_gem.tap do |loader|
16
+ loader.ignore("#{__dir__}/datadog-compound-metrics.rb")
17
+ end
18
+ end
19
+
20
+ def self.configuration
21
+ @configuration ||= DatadogCompoundMetrics::Configuration.new
22
+ end
23
+
24
+ def self.configure
25
+ yield configuration
26
+ end
27
+
28
+ def self.add_compound_metric(metric_name)
29
+ compound_metric = DatadogCompoundMetrics::CompoundMetric.new(metric_name)
30
+ yield compound_metric
31
+ compound_metrics << compound_metric
32
+ end
33
+
34
+ def self.compound_metrics
35
+ @compound_metrics ||= []
36
+ end
37
+
38
+ def self.schedule_job
39
+ Sidekiq::Cron::Job.create(
40
+ name: "DatadogCompoundMetrics::CompoundMetricsWorker",
41
+ cron: configuration.sidekiq_cron_schedule,
42
+ class: "DatadogCompoundMetrics::CompoundMetricsWorker",
43
+ queue: configuration.sidekiq_queue,
44
+ args: [],
45
+ active_job: false
46
+ )
47
+ end
48
+
49
+ def self.reset_config
50
+ @configuration = nil
51
+ end
52
+
53
+ def self.reset_metrics
54
+ @compound_metrics = []
55
+ end
56
+ end
57
+
58
+ DatadogCompoundMetrics.loader.setup
@@ -0,0 +1,8 @@
1
+ module Datadog
2
+ module Compound
3
+ module Metrics
4
+ VERSION: String
5
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
6
+ end
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: datadog-compound-metrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Karol Galanciak
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: sidekiq
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sidekiq-cron
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: zeitwerk
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: A gem for building compound metric (a single metric from multiple ones).
70
+ Mostly to have a single metric do Horizontal Pod Autoscaling for workers consuming
71
+ from multiple queues.
72
+ email:
73
+ - karol@bookingsync.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".rspec"
79
+ - ".rubocop.yml"
80
+ - CHANGELOG.md
81
+ - Gemfile
82
+ - Gemfile.lock
83
+ - LICENSE.txt
84
+ - README.md
85
+ - Rakefile
86
+ - lib/datadog-compound-metrics.rb
87
+ - lib/datadog_compound_metrics.rb
88
+ - lib/datadog_compound_metrics/compound_metric.rb
89
+ - lib/datadog_compound_metrics/compound_metrics_worker.rb
90
+ - lib/datadog_compound_metrics/configuration.rb
91
+ - lib/datadog_compound_metrics/version.rb
92
+ - sig/datadog/compound/metrics.rbs
93
+ homepage: https://github.com/BookingSync/datadog-compound-metrics
94
+ licenses:
95
+ - MIT
96
+ metadata:
97
+ homepage_uri: https://github.com/BookingSync/datadog-compound-metrics
98
+ source_code_uri: https://github.com/BookingSync/datadog-compound-metrics
99
+ changelog_uri: https://github.com/BookingSync/datadog-compound-metrics/blob/master/CHANGELOG.md
100
+ rubygems_mfa_required: 'true'
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: 3.0.0
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubygems_version: 3.4.10
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: A gem for building compound metric (a single metric from multiple ones).
120
+ test_files: []