inst_statsd 2.3.0 → 3.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/inst_statsd/event.rb +64 -0
- data/lib/inst_statsd/statsd.rb +3 -1
- data/lib/inst_statsd/version.rb +5 -0
- data/lib/inst_statsd.rb +1 -0
- data/spec/inst_statsd/event_spec.rb +159 -0
- data/spec/inst_statsd/statsd_spec.rb +22 -0
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f261bcd23b66b67672a3e580233639f242393b26faa9060d69ce1876679766c
|
4
|
+
data.tar.gz: b79c3e4285dd13e3f0ed06bb78eb4dbde10229ad90e3b5be94afaa75b88493d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c4e8b9f646337330232bac47c35fb06d1c72d3eaf530be50c6ef271d256240b154be933f85d52f6fda6203f03d4a1e62fb3e93862c7ec8c1b7cfd309c4b97bc2
|
7
|
+
data.tar.gz: 88a89d55b88069d1011c01037f5602af4315de9c95b5b34298263a73c9eda0cec2d6e048aafcdfcfa83a93d87b52ef017d6e96a2863ac4dee864da807105d749
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module InstStatsd
|
4
|
+
# Mix-in methods for supporting DataDog events
|
5
|
+
# See https://docs.datadoghq.com/service_management/events/
|
6
|
+
module Event
|
7
|
+
SUPPORTED_TYPES = %i[
|
8
|
+
deploy
|
9
|
+
feature_disabled
|
10
|
+
feature_enabled
|
11
|
+
provision
|
12
|
+
refresh
|
13
|
+
].freeze
|
14
|
+
|
15
|
+
def instance; end
|
16
|
+
def data_dog?; end
|
17
|
+
def dog_tags; end
|
18
|
+
|
19
|
+
# This end point allows you to post events to the DatDog event stream.
|
20
|
+
#
|
21
|
+
# @param [String] title Event title
|
22
|
+
# @param [String] text Event text. Supports newlines (+\n+)
|
23
|
+
# @param [String, nil] :type Can be "deploy", "feature_disabled",
|
24
|
+
# "feature_enabled", "provision", or "refresh"
|
25
|
+
# @param [Hash] :tags tags to be added to event. Note that the
|
26
|
+
# environment, service, and other data are automatically
|
27
|
+
# added as tags for you without specifying them here.
|
28
|
+
# @param [Integer, String, nil] :date_happened (nil) Assign a timestamp
|
29
|
+
# to the event. Default is now when none
|
30
|
+
# @param [String, nil] :priority ('normal') Can be "normal" or "low"
|
31
|
+
# @param [String, nil] :alert_type ('info') Can be "error", "warning", "info" or "success".
|
32
|
+
#
|
33
|
+
# @example Report an event:
|
34
|
+
# InstStatsd::Statsd.event(
|
35
|
+
# "Quiz API Deploy",
|
36
|
+
# "<release> was deployed to Quiz API",
|
37
|
+
# tags: {foo: 'bar'},
|
38
|
+
# type: 'deploy',
|
39
|
+
# alert_type: :success
|
40
|
+
# )
|
41
|
+
def event(title, text, type: nil, tags: {}, alert_type: nil, priority: nil, date_happened: nil)
|
42
|
+
return unless instance && data_dog?
|
43
|
+
|
44
|
+
instance.event(
|
45
|
+
title,
|
46
|
+
text,
|
47
|
+
{
|
48
|
+
alert_type: alert_type,
|
49
|
+
priority: priority,
|
50
|
+
date_happened: date_happened,
|
51
|
+
tags: tags_from_opts(tags, type, dog_tags)
|
52
|
+
}.compact
|
53
|
+
)
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def tags_from_opts(tags, type, dd_tags)
|
59
|
+
custom_tags = tags.merge(dd_tags)
|
60
|
+
custom_tags[:type] = type if SUPPORTED_TYPES.include? type
|
61
|
+
custom_tags.compact
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/lib/inst_statsd/statsd.rb
CHANGED
@@ -24,6 +24,8 @@
|
|
24
24
|
|
25
25
|
module InstStatsd
|
26
26
|
module Statsd
|
27
|
+
extend InstStatsd::Event
|
28
|
+
|
27
29
|
# replace "." in key names with another character to avoid creating spurious sub-folders in graphite
|
28
30
|
def self.escape(str, replacement = '_')
|
29
31
|
str.respond_to?(:gsub) ? str.gsub('.', replacement) : str
|
@@ -43,7 +45,7 @@ module InstStatsd
|
|
43
45
|
if self.instance
|
44
46
|
if Array === stat
|
45
47
|
stat.each do |st|
|
46
|
-
self.#{method}(st, *args, tags:
|
48
|
+
self.#{method}(st, *args, tags: tags, short_stat: nil)
|
47
49
|
end
|
48
50
|
return
|
49
51
|
end
|
data/lib/inst_statsd.rb
CHANGED
@@ -0,0 +1,159 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
RSpec.describe InstStatsd::Event do
|
6
|
+
include described_class
|
7
|
+
|
8
|
+
let(:title) { "Title" }
|
9
|
+
let(:text) {"Some text."}
|
10
|
+
|
11
|
+
let(:instance) { double }
|
12
|
+
let(:data_dog?) { true }
|
13
|
+
let(:dog_tags) { {} }
|
14
|
+
let(:opts) { {} }
|
15
|
+
|
16
|
+
describe '#event' do
|
17
|
+
subject { event(title, text, opts) }
|
18
|
+
|
19
|
+
context 'with title and text only' do
|
20
|
+
let(:opts) { {} }
|
21
|
+
|
22
|
+
it 'invokes "event" on the instance with title and text' do
|
23
|
+
expect(instance).to receive(:event).with(
|
24
|
+
title,
|
25
|
+
text,
|
26
|
+
tags: {}
|
27
|
+
)
|
28
|
+
|
29
|
+
subject
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'with alert_type set' do
|
34
|
+
let(:opts) { { alert_type: :error } }
|
35
|
+
|
36
|
+
it 'invokes "event" on the instance with expected arguments' do
|
37
|
+
expect(instance).to receive(:event).with(
|
38
|
+
title,
|
39
|
+
text,
|
40
|
+
alert_type: :error,
|
41
|
+
tags: {}
|
42
|
+
)
|
43
|
+
|
44
|
+
subject
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'with priority set' do
|
49
|
+
let(:opts) { { priority: :low } }
|
50
|
+
|
51
|
+
it 'invokes "event" on the instance with expected arguments' do
|
52
|
+
expect(instance).to receive(:event).with(
|
53
|
+
title,
|
54
|
+
text,
|
55
|
+
priority: :low,
|
56
|
+
tags: {}
|
57
|
+
)
|
58
|
+
|
59
|
+
subject
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context 'with date_happened set' do
|
64
|
+
let(:opts) { { date_happened: date_happened } }
|
65
|
+
let(:date_happened) { Time.now.to_i }
|
66
|
+
|
67
|
+
it 'invokes "event" on the instance with expected arguments' do
|
68
|
+
expect(instance).to receive(:event).with(
|
69
|
+
title,
|
70
|
+
text,
|
71
|
+
date_happened: date_happened,
|
72
|
+
tags: {}
|
73
|
+
)
|
74
|
+
|
75
|
+
subject
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context 'with an invalid type set' do
|
80
|
+
let(:opts) { { type: :banana } }
|
81
|
+
|
82
|
+
it 'does not sent the invalid aggregation key' do
|
83
|
+
expect(instance).to receive(:event).with(
|
84
|
+
title,
|
85
|
+
text,
|
86
|
+
tags: {}
|
87
|
+
)
|
88
|
+
|
89
|
+
subject
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
context 'with a valid type set' do
|
94
|
+
let(:opts) { { type: :deploy } }
|
95
|
+
|
96
|
+
it 'sets the valid aggregation key' do
|
97
|
+
expect(instance).to receive(:event).with(
|
98
|
+
title,
|
99
|
+
text,
|
100
|
+
tags: {type: :deploy}
|
101
|
+
)
|
102
|
+
|
103
|
+
subject
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'with custom tags' do
|
108
|
+
let(:opts) { { tags: { project: 'cool-project'} } }
|
109
|
+
|
110
|
+
it 'invokes "event" on the instance with expected arguments' do
|
111
|
+
expect(instance).to receive(:event).with(
|
112
|
+
title,
|
113
|
+
text,
|
114
|
+
tags: { project: 'cool-project' }
|
115
|
+
)
|
116
|
+
|
117
|
+
subject
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
context 'with all opts set' do
|
122
|
+
let(:date_happened) { Time.now.to_i }
|
123
|
+
let(:opts) do
|
124
|
+
{
|
125
|
+
type: :deploy,
|
126
|
+
alert_type: :warning,
|
127
|
+
priority: :low,
|
128
|
+
date_happened: date_happened,
|
129
|
+
tags: { foo: 'bar' }
|
130
|
+
}
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'sets all arguments' do
|
134
|
+
expect(instance).to receive(:event).with(
|
135
|
+
title,
|
136
|
+
text,
|
137
|
+
alert_type: :warning,
|
138
|
+
priority: :low,
|
139
|
+
date_happened: date_happened,
|
140
|
+
tags: { foo: 'bar', type: :deploy }
|
141
|
+
)
|
142
|
+
|
143
|
+
subject
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
context 'when the instance is not DataDog' do
|
148
|
+
let(:data_dog?) { false }
|
149
|
+
|
150
|
+
it { is_expected.to eq nil }
|
151
|
+
|
152
|
+
it 'does not invoke "event" on the instance' do
|
153
|
+
expect(instance).not_to receive(:event)
|
154
|
+
|
155
|
+
subject
|
156
|
+
end
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
@@ -10,6 +10,10 @@ describe InstStatsd::Statsd do
|
|
10
10
|
InstStatsd::Statsd.reset_instance
|
11
11
|
end
|
12
12
|
|
13
|
+
it 'includes the events module' do
|
14
|
+
expect(described_class).to respond_to :event
|
15
|
+
end
|
16
|
+
|
13
17
|
it 'appends the hostname to stat names by default' do
|
14
18
|
allow(InstStatsd::Statsd).to receive(:hostname).and_return('testhost')
|
15
19
|
statsd = double
|
@@ -91,6 +95,24 @@ describe InstStatsd::Statsd do
|
|
91
95
|
expect(InstStatsd::Statsd.time('test.name') { 'test' }).to eq 'test'
|
92
96
|
end
|
93
97
|
|
98
|
+
context 'with datadog enabled' do
|
99
|
+
it 'handles being called with an array of stat names' do
|
100
|
+
converted_tags = %w[tag:value host:]
|
101
|
+
statsd = double
|
102
|
+
allow(InstStatsd::Statsd).to receive(:instance).and_return(statsd)
|
103
|
+
allow(InstStatsd::Statsd).to receive(:append_hostname?).and_return(false)
|
104
|
+
allow(InstStatsd::Statsd).to receive(:data_dog?).and_return(true)
|
105
|
+
METHODS.each do |method|
|
106
|
+
expect(statsd).to receive(method).once.with('test.one', 'value', tags: converted_tags)
|
107
|
+
expect(statsd).to receive(method).once.with('test.two', 'value', tags: converted_tags)
|
108
|
+
InstStatsd::Statsd.send(method, %w[test.one test.two], 'value', tags: {tag: 'value'}, short_stat: 'short_stat')
|
109
|
+
end
|
110
|
+
expect(statsd).to receive('timing').once.with('test.one', anything, tags: converted_tags, sample_rate: anything)
|
111
|
+
expect(statsd).to receive('timing').once.with('test.two', anything, tags: converted_tags, sample_rate: anything)
|
112
|
+
expect(InstStatsd::Statsd.time(%w[test.one test.two], tags: {tag: 'value'}, short_stat: 'short_stat') { 'test' }).to eq 'test'
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
94
116
|
it "ignores all calls if statsd isn't enabled" do
|
95
117
|
allow(InstStatsd::Statsd).to receive(:instance).and_return(nil)
|
96
118
|
METHODS.each do |method|
|
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:
|
4
|
+
version: 3.0.1
|
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:
|
12
|
+
date: 2023-07-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: dogstatsd-ruby
|
@@ -136,15 +136,18 @@ files:
|
|
136
136
|
- lib/inst_statsd/block_tracking.rb
|
137
137
|
- lib/inst_statsd/counter.rb
|
138
138
|
- lib/inst_statsd/default_tracking.rb
|
139
|
+
- lib/inst_statsd/event.rb
|
139
140
|
- lib/inst_statsd/null_logger.rb
|
140
141
|
- lib/inst_statsd/request_logger.rb
|
141
142
|
- lib/inst_statsd/request_stat.rb
|
142
143
|
- lib/inst_statsd/request_tracking.rb
|
143
144
|
- lib/inst_statsd/sql_tracker.rb
|
144
145
|
- lib/inst_statsd/statsd.rb
|
146
|
+
- lib/inst_statsd/version.rb
|
145
147
|
- spec/inst_statsd/block_stat_spec.rb
|
146
148
|
- spec/inst_statsd/block_tracking_spec.rb
|
147
149
|
- spec/inst_statsd/counter_spec.rb
|
150
|
+
- spec/inst_statsd/event_spec.rb
|
148
151
|
- spec/inst_statsd/inst_statsd_spec.rb
|
149
152
|
- spec/inst_statsd/null_logger_spec.rb
|
150
153
|
- spec/inst_statsd/request_logger_spec.rb
|
@@ -167,14 +170,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
167
170
|
requirements:
|
168
171
|
- - ">="
|
169
172
|
- !ruby/object:Gem::Version
|
170
|
-
version: '2.
|
173
|
+
version: '2.7'
|
171
174
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
172
175
|
requirements:
|
173
176
|
- - ">="
|
174
177
|
- !ruby/object:Gem::Version
|
175
178
|
version: '0'
|
176
179
|
requirements: []
|
177
|
-
rubygems_version: 3.
|
180
|
+
rubygems_version: 3.2.6
|
178
181
|
signing_key:
|
179
182
|
specification_version: 4
|
180
183
|
summary: Statsd for Instructure
|
@@ -182,6 +185,7 @@ test_files:
|
|
182
185
|
- spec/inst_statsd/block_stat_spec.rb
|
183
186
|
- spec/inst_statsd/block_tracking_spec.rb
|
184
187
|
- spec/inst_statsd/counter_spec.rb
|
188
|
+
- spec/inst_statsd/event_spec.rb
|
185
189
|
- spec/inst_statsd/inst_statsd_spec.rb
|
186
190
|
- spec/inst_statsd/null_logger_spec.rb
|
187
191
|
- spec/inst_statsd/request_logger_spec.rb
|