inst_statsd 3.1.0 → 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 57fa28e5cb8fa8bc63d35ca8233d41b3f79f58107c266b708f7d9c60e38d7f81
4
- data.tar.gz: 45e23f3284b62308099fb6ab7167ef7036d4bbc4a87fb32dcf70a1bd246674b6
3
+ metadata.gz: 705acd8e1a6b843e6f4e54ec0e1525f1165022547c22649d4b0c60c2c1fc83fa
4
+ data.tar.gz: 1d0daad7330f761a6ea4642bd52c85a0a4bf5a6856e7bd967750670a7f08832e
5
5
  SHA512:
6
- metadata.gz: 79d3b39e1de95726c07a623f2d317fa512f9d3fee346afbc26b1ec8435919783a46887292e54777a9e7ee19ca51314aa8e703729fa5bc5fc45bef121eebffe56
7
- data.tar.gz: 3de309abbcc308546567c280c98c910b34cece7dd27dad1cea4911e3b5f7fb263a997f26128bfa1541ae5d23e87c93f36da98b8c13790c42734980444446f42a
6
+ metadata.gz: 1a23712f36838b2fd9afd2fe15430c9239e14b212aa6a05ed3358c81ff9799a2d11345dc1170915de903d6a73eed0a0dce05c183c75661693296ab5511b2cdb8
7
+ data.tar.gz: d5a2406ba15f33196527a9d24cb7d52d6eb5d0aea387f7a80d9382e389777e0c375473d50282af0841f9832adc8f27d8c89b3f07c249764de3170aa8b033303e
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module InstStatsd
4
+ # Mix-in methods for supporting DataDog events
5
+ # See https://docs.datadoghq.com/metrics/types/?tab=distribution#metric-types
6
+ module Distribution
7
+ # Sends a distribution metric to DataDog if the instance and DataDog are configured.
8
+ #
9
+ # Distributions are aggregated globally and are appropriate when a metric sourced from
10
+ # multiple hosts need to be considered in a global statistical distribution.
11
+ #
12
+ # @param metric [String] The name of the metric to send.
13
+ # @param value [Numeric] The value of the metric.
14
+ # @param tags [Hash] Optional tags to associate with the metric. Defaults to an empty hash.
15
+ #
16
+ # @example Record an error occurrence:
17
+ # InstStatsd::Statsd.distribution('client.request.failed', 1, tags: { status: '500' })
18
+ def distribution(metric, value, tags: {})
19
+ return unless instance && data_dog?
20
+
21
+ instance.distribution(metric, value, { tags: tags.merge(dog_tags) }.compact)
22
+ end
23
+
24
+ # Increments the specified distribution metric by 1.
25
+ #
26
+ # @param metric [String] The name of the metric to increment.
27
+ # @param tags [Hash] Optional tags to associate with the metric.
28
+ #
29
+ # @example Increment the error count:
30
+ # InstStatsd::Statsd.distributed_increment('client.request.failed', tags: { status: '500' })
31
+ def distributed_increment(metric, tags: {})
32
+ distribution(metric, 1, tags: tags)
33
+ end
34
+ end
35
+ end
@@ -29,6 +29,7 @@
29
29
  module InstStatsd
30
30
  module Statsd
31
31
  extend InstStatsd::Event
32
+ extend InstStatsd::Distribution
32
33
 
33
34
  # replace "." in key names with another character to avoid creating spurious sub-folders in graphite
34
35
  def self.escape(str, replacement = "_")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module InstStatsd
4
- VERSION = "3.1.0"
4
+ VERSION = "3.2.0"
5
5
  end
data/lib/inst_statsd.rb CHANGED
@@ -16,6 +16,7 @@ module InstStatsd
16
16
 
17
17
  class ConfigurationError < StandardError; end
18
18
 
19
+ require "inst_statsd/distribution"
19
20
  require "inst_statsd/event"
20
21
  require "inst_statsd/statsd"
21
22
  require "inst_statsd/block_stat"
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require "datadog/statsd"
5
+
6
+ RSpec.describe InstStatsd::Distribution do
7
+ describe ".distribution" do
8
+ subject { InstStatsd::Statsd.distribution(metric, value, tags: tags) }
9
+
10
+ let(:instance) { instance_double(Datadog::Statsd) }
11
+
12
+ let(:metric) { "client.request.failed" }
13
+ let(:value) { 23 }
14
+ let(:tags) { { status: "500" } }
15
+
16
+ before do
17
+ allow(InstStatsd::Statsd).to receive_messages(data_dog?: is_datadog, dog_tags: dog_tags, instance: instance)
18
+ end
19
+
20
+ context "when instance and data_dog? are configured" do
21
+ let(:dog_tags) { { environment: "production" } }
22
+ let(:is_datadog) { true }
23
+
24
+ it 'invokes "distribution" on the instance with metric, value, and tags' do
25
+ expect(instance).to receive(:distribution).with(
26
+ metric,
27
+ value,
28
+ { tags: { status: "500", environment: "production" } }
29
+ )
30
+
31
+ subject
32
+ end
33
+ end
34
+
35
+ context "when instance and data_dog? are not configured" do
36
+ let(:dog_tags) { {} }
37
+ let(:is_datadog) { false }
38
+
39
+ it "does nothing" do
40
+ expect(instance).not_to receive(:distribution)
41
+
42
+ subject
43
+ end
44
+ end
45
+ end
46
+
47
+ describe ".distributed_increment" do
48
+ subject { InstStatsd::Statsd.distributed_increment(metric, tags: tags) }
49
+
50
+ let(:metric) { "client.request.failed" }
51
+ let(:tags) { { status: "500" } }
52
+
53
+ before do
54
+ allow(InstStatsd::Statsd).to receive_messages(distribution: nil)
55
+ end
56
+
57
+ it 'invokes "distribution" on InstStatsd::Statsd with metric, 1, and tags' do
58
+ expect(InstStatsd::Statsd).to receive(:distribution).with(metric, 1, tags: tags)
59
+
60
+ subject
61
+ end
62
+ end
63
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inst_statsd
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Cloward
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2024-11-12 00:00:00.000000000 Z
12
+ date: 2024-12-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aroi
@@ -176,6 +176,7 @@ files:
176
176
  - lib/inst_statsd/block_tracking.rb
177
177
  - lib/inst_statsd/counter.rb
178
178
  - lib/inst_statsd/default_tracking.rb
179
+ - lib/inst_statsd/distribution.rb
179
180
  - lib/inst_statsd/event.rb
180
181
  - lib/inst_statsd/null_logger.rb
181
182
  - lib/inst_statsd/request_logger.rb
@@ -187,6 +188,7 @@ files:
187
188
  - spec/inst_statsd/block_stat_spec.rb
188
189
  - spec/inst_statsd/block_tracking_spec.rb
189
190
  - spec/inst_statsd/counter_spec.rb
191
+ - spec/inst_statsd/distribution_spec.rb
190
192
  - spec/inst_statsd/event_spec.rb
191
193
  - spec/inst_statsd/inst_statsd_spec.rb
192
194
  - spec/inst_statsd/null_logger_spec.rb