null_statsd 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/rspec.yml +23 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +168 -0
- data/Rakefile +6 -0
- data/UserTesting.png +0 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/null_statsd/statsd.rb +104 -0
- data/lib/null_statsd/version.rb +3 -0
- data/lib/null_statsd.rb +6 -0
- data/null_statsd.gemspec +24 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bdcda9d6bbce5d1ed55cac57ca33e769c6bfc9b2e18b4a3b852d183f37258d90
|
4
|
+
data.tar.gz: 9cb8dd40167d522dd1eb21dfce10e7e1318b2b20492465d5e110e6ae9c043769
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4501b0cfc958c6c872ed08dcc2ed0ef0e2c0903f9211a067970fc0d6b3f2821dd2e8061958e12275860afb29ffdd99f114ac4bc064cb48914b1f577f8ac4f2e8
|
7
|
+
data.tar.gz: 5ce718a612489f86b4f2c5502c9eedf2e14f465696a2f09860c0403f05d82faef948465057c0d5c71b8c2785972568696a47d6d50c5d9b1488eab23b9d066989
|
@@ -0,0 +1,23 @@
|
|
1
|
+
name: RSpec
|
2
|
+
|
3
|
+
on: [push]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
RSpec:
|
7
|
+
runs-on: ubuntu-latest
|
8
|
+
strategy:
|
9
|
+
fail-fast: false
|
10
|
+
matrix:
|
11
|
+
ruby-version: ['2.7', '3.0', '3.1', '3.2', 'head']
|
12
|
+
|
13
|
+
steps:
|
14
|
+
- uses: actions/checkout@v4
|
15
|
+
|
16
|
+
- name: Set up ruby
|
17
|
+
uses: ruby/setup-ruby@v1
|
18
|
+
with:
|
19
|
+
ruby-version: ${{ matrix.ruby-version }}
|
20
|
+
bundler-cache: true
|
21
|
+
|
22
|
+
- name: Run RSpec
|
23
|
+
run: bundle exec rspec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Imad Mouaddine
|
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,168 @@
|
|
1
|
+
# NullStatsd
|
2
|
+
|
3
|
+
NullStatsd is a [Statsd](https://github.com/statsd/statsd) implementation which utilizes the
|
4
|
+
[Null Object Pattern](https://en.wikipedia.org/wiki/Null_object_pattern), allowing for a fully
|
5
|
+
stubbed Statsd object in your development and testing environments.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'null_statsd'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install null_statsd
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Create a thin wrapper around your Statsd implementation:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
module MyStatsd
|
29
|
+
def self.new
|
30
|
+
if ENV["STATSD_HOST"] # OR if Rails.development || Rails.staging ...
|
31
|
+
Statsd.new(statsd_host, statsd_port, *additional_params)
|
32
|
+
else
|
33
|
+
NullStatsd::Statsd.new(host: statsd_host, port: statsd_port, logger: Rails.logger)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
Create an instance and use it as normal:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
MyStatsd.new.increment(...)
|
43
|
+
```
|
44
|
+
|
45
|
+
Notice that your `statsd` endpoint is _not_ receiving data. Also notice that your _logs_ are
|
46
|
+
receiving data.
|
47
|
+
|
48
|
+
```
|
49
|
+
[NullStatsD host:42] Incrementing media.book.consumed with opts {"genre":"science_fiction"}
|
50
|
+
[NullStatsD host:42] Decrementing media.book.on_hand
|
51
|
+
[NullStatsD host:42] Recording timing info in book.checkout -> 0.512917 sec
|
52
|
+
```
|
53
|
+
|
54
|
+
### Supported API
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
instance = NullStatsd::Statsd.new(host: "fake.com", port: 4242, logger: Logger.new($stdout))
|
58
|
+
```
|
59
|
+
|
60
|
+
> [NullStatsD :-] Connecting to fake Statsd, pretending to be on fake.com:4242
|
61
|
+
|
62
|
+
#### increment(stat, opts = {})
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
instance.increment "media.book.consumed", genre: "horror"
|
66
|
+
```
|
67
|
+
|
68
|
+
> [NullStatsD :-] Incrementing media.book.consumed with opts genre:horror
|
69
|
+
|
70
|
+
#### decrement(stat, opts = {})
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
instance.decrement "media.book.on_hand", genre: "science fiction"
|
74
|
+
```
|
75
|
+
|
76
|
+
> [NullStatsD :-] Decrementing media.book.on_hand with opts genre:science fiction
|
77
|
+
|
78
|
+
#### count(stat, opts = {})
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
instance.count "responses", 3
|
82
|
+
```
|
83
|
+
|
84
|
+
> [NullStatsD :-] Increasing responses by 3
|
85
|
+
|
86
|
+
#### guage(stat, opts = {})
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
instance.guage "media.book.return_time", 12, measurement: "days"
|
90
|
+
```
|
91
|
+
|
92
|
+
> [NullStatsD :-] Setting guage media.book.return_time to 12 with opts measurement:days
|
93
|
+
|
94
|
+
#### histogram(stat, opts = {})
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
instance.histogram "media.book.lent.hour", 42
|
98
|
+
```
|
99
|
+
|
100
|
+
> [NullStatsD :-] Logging histogram media.book.lent.hour -> 42
|
101
|
+
|
102
|
+
#### timing(stat, ms, opts = {})
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
instance.timing "book checkout", 94, tags: "speedy"
|
106
|
+
```
|
107
|
+
|
108
|
+
> [NullStatsD :-] Timing book checkout at 94 ms with opts tags:speedy
|
109
|
+
|
110
|
+
#### set(stat, opts = {})
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
instance.set "media.book.lent", 10_000_000
|
114
|
+
```
|
115
|
+
|
116
|
+
> [NullStatsD :-] Setting media.book.lent to 10000000
|
117
|
+
|
118
|
+
#### service_check(stat, opts = {})
|
119
|
+
|
120
|
+
```ruby
|
121
|
+
instance.service_check "door.locked", "ok"
|
122
|
+
```
|
123
|
+
|
124
|
+
> [NullStatsD :-] Service check door.locked: ok
|
125
|
+
|
126
|
+
#### event(stat, opts = {})
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
instance.event "Leak", "The library roof has a leak on the west end. Please take care"
|
130
|
+
```
|
131
|
+
|
132
|
+
> [NullStatsD :-] Event Leak: The library roof has a leak on the west end. Please take care
|
133
|
+
|
134
|
+
#### time(stat, opts = {})
|
135
|
+
|
136
|
+
```ruby
|
137
|
+
instance.time("media.movie.consume") do
|
138
|
+
Movie.new().watch
|
139
|
+
end
|
140
|
+
```
|
141
|
+
|
142
|
+
> [NullStatsD :-] Recording timing info in media.movie.consumed -> 12323 sec
|
143
|
+
|
144
|
+
#### close(stat, opts = {})
|
145
|
+
|
146
|
+
```ruby
|
147
|
+
instance.close
|
148
|
+
```
|
149
|
+
|
150
|
+
> [NullStatsD :-] Close called
|
151
|
+
|
152
|
+
## Testing
|
153
|
+
|
154
|
+
`rake spec`
|
155
|
+
|
156
|
+
## License
|
157
|
+
|
158
|
+
The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
|
159
|
+
|
160
|
+
## Contributing
|
161
|
+
|
162
|
+
1. Fork it
|
163
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
164
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
165
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
166
|
+
5. Create a new Pull Request
|
167
|
+
|
168
|
+
Bug reports and pull requests are welcome on GitHub.
|
data/Rakefile
ADDED
data/UserTesting.png
ADDED
Binary file
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "null_statsd"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
require "json"
|
2
|
+
|
3
|
+
module NullStatsd
|
4
|
+
class Statsd
|
5
|
+
attr_accessor :namespace, :host, :port, :logger
|
6
|
+
|
7
|
+
def initialize(host:, port:, logger:)
|
8
|
+
@host = host
|
9
|
+
@port = port
|
10
|
+
@namespace = ""
|
11
|
+
@logger = logger
|
12
|
+
notify "Connecting to fake Statsd, pretending to be on #{host}:#{port}"
|
13
|
+
end
|
14
|
+
|
15
|
+
def increment(stat, opts = {})
|
16
|
+
notify "Incrementing #{stat}#{opts_string(opts)}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def decrement(stat, opts = {})
|
20
|
+
notify "Decrementing #{stat}#{opts_string(opts)}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def count(stat, count, opts = {})
|
24
|
+
notify "Increasing #{stat} by #{count}#{opts_string(opts)}"
|
25
|
+
end
|
26
|
+
|
27
|
+
def gauge(stat, value, opts = {})
|
28
|
+
notify "Setting gauge #{stat} to #{value}#{opts_string(opts)}"
|
29
|
+
end
|
30
|
+
|
31
|
+
def histogram(stat, value, opts = {})
|
32
|
+
notify "Logging histogram #{stat} -> #{value}#{opts_string(opts)}"
|
33
|
+
end
|
34
|
+
|
35
|
+
def timing(stat, ms, _sample_rate = 1, opts = {})
|
36
|
+
notify "Timing #{stat} at #{ms} ms#{opts_string(opts)}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def set(stat, value, opts = {})
|
40
|
+
notify "Setting #{stat} to #{value}#{opts_string(opts)}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def service_check(name, status, opts = {})
|
44
|
+
notify "Service check #{name}: #{status}#{opts_string(opts)}"
|
45
|
+
end
|
46
|
+
|
47
|
+
def event(title, text, opts = {})
|
48
|
+
notify "Event #{title}: #{text}#{opts_string(opts)}"
|
49
|
+
end
|
50
|
+
|
51
|
+
def close
|
52
|
+
notify "Close called"
|
53
|
+
end
|
54
|
+
|
55
|
+
def batch
|
56
|
+
yield self
|
57
|
+
end
|
58
|
+
|
59
|
+
def time(stat, opts = {})
|
60
|
+
time_in_sec, result = benchmark { yield }
|
61
|
+
notify "Recording timing info in #{stat} -> #{time_in_sec} sec#{opts_string(opts)}"
|
62
|
+
result
|
63
|
+
end
|
64
|
+
|
65
|
+
def with_namespace(namespace)
|
66
|
+
new_ns = dup
|
67
|
+
if @namespace == "" || @namespace == nil
|
68
|
+
new_ns.namespace = namespace
|
69
|
+
else
|
70
|
+
new_ns.namespace = "#{@namespace}.#{namespace}"
|
71
|
+
end
|
72
|
+
if block_given?
|
73
|
+
yield new_ns
|
74
|
+
else
|
75
|
+
new_ns
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
def identifier_string
|
82
|
+
if @namespace && @namespace != ""
|
83
|
+
"[NullStatsD #{@host}:#{@port}-#{@namespace}]"
|
84
|
+
else
|
85
|
+
"[NullStatsD #{@host}:#{@port}]"
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def benchmark(&block)
|
90
|
+
start = Time.now
|
91
|
+
result = block.call
|
92
|
+
elapsed_time = Time.now - start
|
93
|
+
return elapsed_time, result
|
94
|
+
end
|
95
|
+
|
96
|
+
def notify(msg)
|
97
|
+
logger.debug "#{identifier_string} #{msg}"
|
98
|
+
end
|
99
|
+
|
100
|
+
def opts_string(opts)
|
101
|
+
opts.empty? ? nil : " with opts #{opts.to_json}"
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
data/lib/null_statsd.rb
ADDED
data/null_statsd.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "null_statsd/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "null_statsd"
|
8
|
+
spec.version = NullStatsd::VERSION
|
9
|
+
spec.authors = ["Imad Mouaddine"]
|
10
|
+
spec.email = ["imouaddine@usertesting.com"]
|
11
|
+
spec.summary = %q{Implements null pattern for Statsd client}
|
12
|
+
spec.description = %q{Implements null pattern for Statsd client}
|
13
|
+
spec.homepage = "https://github.com/usertesting/null_statsd"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|UserTesting.png)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 2.2"
|
21
|
+
spec.add_development_dependency "rake", "~> 13.1"
|
22
|
+
spec.add_development_dependency "rspec", "~> 3.12"
|
23
|
+
spec.add_development_dependency "rspec-cover_it", "~> 0.1.0"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: null_statsd
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Imad Mouaddine
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-12-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.2'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '13.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '13.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.12'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.12'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-cover_it
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.1.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.1.0
|
69
|
+
description: Implements null pattern for Statsd client
|
70
|
+
email:
|
71
|
+
- imouaddine@usertesting.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".github/workflows/rspec.yml"
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- UserTesting.png
|
85
|
+
- bin/console
|
86
|
+
- bin/setup
|
87
|
+
- lib/null_statsd.rb
|
88
|
+
- lib/null_statsd/statsd.rb
|
89
|
+
- lib/null_statsd/version.rb
|
90
|
+
- null_statsd.gemspec
|
91
|
+
homepage: https://github.com/usertesting/null_statsd
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubygems_version: 3.4.10
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Implements null pattern for Statsd client
|
114
|
+
test_files: []
|