datadog_metrics 1.0.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: 7c4854b755cf8dfd6d6cb3c0ddc83c368a8733efb6d99ad6dbfddcd83e706677
4
+ data.tar.gz: 1b0168cb2249f4b2191b0d68b3cd52cd311d860f848e0e03e607094c12cbb9a0
5
+ SHA512:
6
+ metadata.gz: 4c82ea521485affcfc97f9ba8845716b3260aabf20c861ef356d88f2b0b45fd79b9d5262185b167fd391e1c66ceaef0f4d24369c05037d1027228bc1f282fcae
7
+ data.tar.gz: a651fb8a9d560c8aea596ae534ed9da1138e7a9280f4668a919f235e43bf512b178193370a21e8c4ff2d4e99b3bb630e94acaecd439f6e4db418b744549f55bb
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
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # 1.0.0
2
+
3
+ Initial release
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in datadog_metrics.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ datadog_metrics (1.0.0)
5
+ dogstatsd-ruby (~> 4.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ coderay (1.1.2)
11
+ diff-lcs (1.3)
12
+ dogstatsd-ruby (4.6.0)
13
+ method_source (0.9.2)
14
+ pry (0.12.2)
15
+ coderay (~> 1.1.0)
16
+ method_source (~> 0.9.0)
17
+ rake (10.5.0)
18
+ rspec (3.9.0)
19
+ rspec-core (~> 3.9.0)
20
+ rspec-expectations (~> 3.9.0)
21
+ rspec-mocks (~> 3.9.0)
22
+ rspec-core (3.9.1)
23
+ rspec-support (~> 3.9.1)
24
+ rspec-expectations (3.9.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.9.0)
27
+ rspec-mocks (3.9.1)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.9.0)
30
+ rspec-support (3.9.2)
31
+
32
+ PLATFORMS
33
+ ruby
34
+
35
+ DEPENDENCIES
36
+ bundler (~> 1.17)
37
+ datadog_metrics!
38
+ pry
39
+ rake (~> 10.0)
40
+ rspec (~> 3.0)
41
+
42
+ BUNDLED WITH
43
+ 1.17.3
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2020 Will Jessop
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # DatadogMetrics
2
+
3
+ DatadogMetrics provides a convenient wrapper around sending metrics to Datadog in your Ruby application.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'datadog_metrics'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install datadog_metrics
20
+
21
+ ## Usage
22
+
23
+ For Rails, create an initialiser, eg. `config/initializers/datadog_metrics.rb` containing something like:
24
+
25
+ ```ruby
26
+ Rails.configuration.datadog_metrics = if ["development", "test"].include? Rails.env
27
+ require "datadog_metrics/logger"
28
+ DatadogMetrics.new(DatadogLogger.new(Datadog::Statsd.new))
29
+ else
30
+ DatadogMetrics.new
31
+ end
32
+ ```
33
+
34
+ The development/test branch will use the DatadogMetrics built-in logger to log the metrics that would have been sent, rather than logging them to Datadog.
35
+
36
+ ## Development
37
+
38
+ 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.
39
+
40
+ 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).
41
+
42
+ ## Updating the gem version
43
+
44
+ To update it, first bump the version appropriately in the `version.rb` file. Afterwards build the gem by running:
45
+ ```bash
46
+ gem build datadog_metrics.gemspec
47
+ ```
48
+
49
+ Then either use the ```rake push``` rake task to push the gem to rubygems.
50
+
51
+ ## Versioning / Changes
52
+
53
+ DatadogMetrics uses [semantic versioning](https://semver.org/), please respect that when making changes. When you make changes please log these in the CHANGELOG.md file.
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wjessop/datadog_metrics.
58
+
59
+ ## License
60
+
61
+ MIT, see LICENSE file.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "datadog_metrics"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ 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,43 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "datadog_metrics/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "datadog_metrics"
8
+ spec.version = DatadogMetrics::VERSION
9
+ spec.authors = ["Will Jessop"]
10
+ spec.email = ["will@willj.net"]
11
+
12
+ spec.summary = %q{Send metrics to Datadog}
13
+ spec.description = %q{Send metrics to Datadog with this one simple trick}
14
+ spec.homepage = "https://github.com/wjessop/datadog_metrics"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/wjessop/datadog_metrics"
23
+ spec.metadata["changelog_uri"] = "https://github.com/wjessop/datadog_metrics/blob/master/CHANGELOG.md"
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib"]
36
+
37
+ spec.add_dependency "dogstatsd-ruby", "~> 4.0"
38
+
39
+ spec.add_development_dependency "bundler", "~> 1.17"
40
+ spec.add_development_dependency "rake", "~> 10.0"
41
+ spec.add_development_dependency "rspec", "~> 3.0"
42
+ spec.add_development_dependency "pry"
43
+ end
@@ -0,0 +1,100 @@
1
+ require "datadog_metrics/version"
2
+
3
+ require 'datadog/statsd'
4
+
5
+ # The default endpoint is the local dogstatsd agent which we can
6
+ # talk to using the ruby client:
7
+ #
8
+ # https://github.com/DataDog/dogstatsd-ruby
9
+ #
10
+ # In development we will pass an object that just logs what it
11
+ # would have reported to DataDog to the local log file, and in
12
+ # test we pass an object we can test with.
13
+
14
+ class DatadogMetrics
15
+ class << self
16
+ def default_tags=(tags)
17
+ @default_tags = tags.transform_keys(&:to_s).freeze
18
+ end
19
+
20
+ def default_tags
21
+ @default_tags ||= {}
22
+ end
23
+ end
24
+
25
+ attr_reader :endpoint
26
+
27
+ def initialize(endpoint = Datadog::Statsd.new(ENV.fetch("STATS_HOST", "localhost"), 8125))
28
+ @endpoint = endpoint
29
+ end
30
+
31
+ def time(stat, opt={}, &block)
32
+ opt[:tags] = format_tags(opt[:tags]) if opt.key?(:tags)
33
+
34
+ @endpoint.time(stat, opt) do
35
+ yield
36
+ end
37
+ end
38
+
39
+ def timing(metric, ms, tags: {})
40
+ @endpoint.timing(metric, ms, tags: format_tags(tags))
41
+ end
42
+
43
+ def increment(metric, tags: {})
44
+ @endpoint.increment(metric, tags: format_tags(tags))
45
+ end
46
+
47
+ def decrement(metric, tags: {})
48
+ @endpoint.decrement(metric, tags: format_tags(tags))
49
+ end
50
+
51
+ def count(metric, count, tags: {})
52
+ @endpoint.count(metric, count, tags: format_tags(tags))
53
+ end
54
+
55
+ def increment_with_tags(metric, tags)
56
+ @endpoint.increment(metric, tags: format_tags(tags))
57
+ end
58
+
59
+ def count_with_tags(metric, count, tags: {})
60
+ @endpoint.count(metric, count, tags: format_tags(tags))
61
+ end
62
+
63
+ def gauge(metric, value, tags=[], sample_rate=1, **opts)
64
+ tags = opts[:tags] if opts.key?(:tags)
65
+ @endpoint.gauge(metric, value, tags: format_tags(tags), sample_rate: sample_rate)
66
+ end
67
+
68
+ def histogram(metric, value, tags: {})
69
+ @endpoint.histogram(metric, value, tags: format_tags(tags))
70
+ end
71
+
72
+ def histogram_with_tags(metric, value, tags)
73
+ @endpoint.histogram(metric, value, tags: format_tags(tags))
74
+ end
75
+
76
+ def event(metric, value, tags: {})
77
+ @endpoint.event(metric, value, tags: format_tags(tags))
78
+ end
79
+
80
+ def batch
81
+ @endpoint.batch(&proc { yield(endpoint)})
82
+ end
83
+
84
+ private
85
+
86
+ def format_tags(tags)
87
+ return [] if tags.nil?
88
+
89
+ if tags.is_a?(Array)
90
+ tags = tags.each_with_object({}) do |tag, hash|
91
+ key, rest = tag.split(':', 2)
92
+ hash[key] = rest
93
+ end
94
+ end
95
+
96
+ self.class.default_tags.
97
+ merge(tags).
98
+ map { |key, value| [key, value].compact.join(':') }
99
+ end
100
+ end
@@ -0,0 +1,15 @@
1
+ # Datadoglogger is designed to be used in Development mode and will log the
2
+ # metrics that would have been sent to datadog without the need to run dogstatsd
3
+ # locally.
4
+ class DatadogLogger
5
+ (Datadog::Statsd.instance_methods - Object.instance_methods).each do |method|
6
+ define_method(method) do |*args, &block|
7
+ Rails.logger.info "Datadog: '#{method}' called on the Datadog logger with args: #{args.inspect}"
8
+ block.call if block
9
+ end
10
+ end
11
+
12
+ def initialize(target)
13
+ @target = target
14
+ end
15
+ end
@@ -0,0 +1,30 @@
1
+ # Mock statsd class to record data during the specs.
2
+ class FakeDatadogMetrics
3
+ attr_accessor :metrics
4
+
5
+ delegate :empty?, to: :metrics
6
+
7
+ def initialize
8
+ self.metrics = []
9
+ end
10
+
11
+ def increment(*arguments)
12
+ metrics << [:increment, *arguments]
13
+ end
14
+
15
+ def increment_with_tags(*arguments)
16
+ metrics << [:increment, *arguments]
17
+ end
18
+
19
+ def histogram_with_tags(*arguments)
20
+ metrics << [:histogram, *arguments]
21
+ end
22
+
23
+ def count_with_tags(*arguments)
24
+ metrics << [:count, *arguments]
25
+ end
26
+
27
+ def gauge(*arguments)
28
+ metrics << [:guage, *arguments]
29
+ end
30
+ end
@@ -0,0 +1,67 @@
1
+ RSpec::Matchers.define :have_logged do |expected|
2
+ # The string to display for the expected value.
3
+ # NOTE Regexp#inspect is nicer than Regexp#to_s but we don't want to inspect
4
+ # the String and show the surrounding quotes.
5
+ expected_string = expected.is_a?(Regexp) ? expected.inspect : expected.to_s
6
+
7
+ # Whether the line matches.
8
+ matching_line = lambda do |line|
9
+ case expected
10
+ when String
11
+ line.include? expected
12
+ when Regexp
13
+ line.match expected
14
+ else
15
+ raise 'Must provide a String or Regexp'
16
+ end
17
+ end
18
+
19
+
20
+ # # Whether the severity matches.
21
+ # # NOTE This matches the conventional Logger format, which has the severity tag
22
+ # # followed by ' -- :'. (Technically, the logger's progname goes between
23
+ # # the '--' and the ':', but it is unset for the Rails logger.) If this
24
+ # # format is changed, it may cause false reports and need to be changed
25
+ # # here as well.
26
+ # matching_severity = ->(line, severity) { severity ? line.include?("#{severity} -- :") : true }
27
+
28
+ match do |actual|
29
+ # # NOTE: Only the new StringIO-backed Logger object will respond to logdev.
30
+ # raise 'Logger has not been faked. Make sure you include the shared context.' \
31
+ # unless actual.respond_to? :logdev
32
+
33
+ # # The logger has a log device which has a device -- this is our StringIO.
34
+ # io = actual.logdev.dev
35
+
36
+ # # Writing to the StringIO advances its seek cursor so we must rewind.
37
+ # io.rewind
38
+
39
+ # io.each_line.any? do |line|
40
+ # matching_line.call(line) && matching_severity.call(line, @severity)
41
+ # end
42
+ end
43
+
44
+ description { "logs a metric matching #{expected_string}" }
45
+
46
+ # # See http://ruby-doc.org/stdlib-2.5.3/libdoc/logger/rdoc/Logger/Severity.html
47
+ # # NOTE The defined? check is needed because of the way this block is
48
+ # # metaprogrammatically evaluated.
49
+ # SEVERITIES = %i[debug error fatal info unknown warn].freeze unless defined? SEVERITIES
50
+
51
+ # chain(:with_severity) do |severity|
52
+ # raise "unknown severity #{severity.inspect}, severities are #{SEVERITIES.to_sentence}" \
53
+ # unless SEVERITIES.include? severity
54
+
55
+ # # NOTE The use of a matcher instantiates an object so we can use instance
56
+ # # variables to pass information around.
57
+ # @severity = severity == :unknown ? 'ANY' : severity.to_s.upcase
58
+ # end
59
+
60
+ failure_message do |actual|
61
+ message = 'Expected that DatadogMetrics would have logged a metric'
62
+ # message += " of severity #{@severity}" if @severity
63
+ message += " matching:\n #{expected_string}"
64
+ message += "\n\nLogged:\n#{actual.logdev.dev.string}"
65
+ message
66
+ end
67
+ end
@@ -0,0 +1,3 @@
1
+ class DatadogMetrics
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: datadog_metrics
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Will Jessop
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-02-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dogstatsd-ruby
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Send metrics to Datadog with this one simple trick
84
+ email:
85
+ - will@willj.net
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - CHANGELOG.md
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - LICENSE
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - datadog_metrics.gemspec
101
+ - lib/datadog_metrics.rb
102
+ - lib/datadog_metrics/logger.rb
103
+ - lib/datadog_metrics/testing/fake_datadog_metrics.rb
104
+ - lib/datadog_metrics/testing/rspec_matchers.rb
105
+ - lib/datadog_metrics/version.rb
106
+ homepage: https://github.com/wjessop/datadog_metrics
107
+ licenses: []
108
+ metadata:
109
+ allowed_push_host: https://rubygems.org
110
+ homepage_uri: https://github.com/wjessop/datadog_metrics
111
+ source_code_uri: https://github.com/wjessop/datadog_metrics
112
+ changelog_uri: https://github.com/wjessop/datadog_metrics/blob/master/CHANGELOG.md
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ requirements: []
128
+ rubygems_version: 3.1.2
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: Send metrics to Datadog
132
+ test_files: []