inst_statsd 3.1.0 → 3.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 57fa28e5cb8fa8bc63d35ca8233d41b3f79f58107c266b708f7d9c60e38d7f81
4
- data.tar.gz: 45e23f3284b62308099fb6ab7167ef7036d4bbc4a87fb32dcf70a1bd246674b6
3
+ metadata.gz: 70725525a856b43a9d37075d6bdf74fdeac3a7c3a306545bca25e41e3011ca9a
4
+ data.tar.gz: e7fcf1593bf7186c877be71fbd38f86c285fa852294ee08e00d90049076cb0b6
5
5
  SHA512:
6
- metadata.gz: 79d3b39e1de95726c07a623f2d317fa512f9d3fee346afbc26b1ec8435919783a46887292e54777a9e7ee19ca51314aa8e703729fa5bc5fc45bef121eebffe56
7
- data.tar.gz: 3de309abbcc308546567c280c98c910b34cece7dd27dad1cea4911e3b5f7fb263a997f26128bfa1541ae5d23e87c93f36da98b8c13790c42734980444446f42a
6
+ metadata.gz: '02963b466ff709d44a2e7ed69f8a060263d0a66b87cf856d1bf9a8f75814441dda10692fb7184096c36e7365865e94d2d322cc2a04863fe66183a21e97085e0e'
7
+ data.tar.gz: f24f390b747d2a86e81a45bde5e9160e86375dd565798da7e9bd037977b6be4cc71c4c29621fe3c87bade029c2884ee74b02382f2a100770563811edd3171ac8
@@ -0,0 +1,38 @@
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
+ # Non-Datadog clients don't support distribution metrics, so we use fall back to increment
33
+ return increment(metric, tags: tags) if instance && !data_dog?
34
+
35
+ distribution(metric, 1, tags: tags)
36
+ end
37
+ end
38
+ 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.3.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,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require "datadog/statsd"
5
+
6
+ RSpec.describe InstStatsd::Distribution do
7
+ let(:instance) { instance_double(Datadog::Statsd) }
8
+
9
+ before do
10
+ allow(InstStatsd::Statsd).to receive_messages(data_dog?: is_datadog, dog_tags: dog_tags, instance: instance)
11
+ end
12
+
13
+ describe ".distribution" do
14
+ subject { InstStatsd::Statsd.distribution(metric, value, tags: tags) }
15
+
16
+ let(:metric) { "client.request.failed" }
17
+ let(:value) { 23 }
18
+ let(:tags) { { status: "500" } }
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
+ let(:dog_tags) { { status: "401" } }
53
+ let(:is_datadog) { true }
54
+
55
+ before do
56
+ allow(InstStatsd::Statsd).to receive_messages(distribution: nil)
57
+ end
58
+
59
+ it 'invokes "distribution" on InstStatsd::Statsd with metric, 1, and tags' do
60
+ expect(InstStatsd::Statsd).to receive(:distribution).with(metric, 1, tags: tags)
61
+
62
+ subject
63
+ end
64
+
65
+ context "when the configured instance is not a DataDog instance" do
66
+ let(:dog_tags) { {} }
67
+ let(:is_datadog) { false }
68
+
69
+ it 'invokes "increment" on InstStatsd::Statsd with metric and tags' do
70
+ expect(InstStatsd::Statsd).to receive(:increment).with(metric, tags: tags)
71
+
72
+ subject
73
+ end
74
+ end
75
+ end
76
+ 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.3.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