statsby 0.0.1
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 +7 -0
- data/lib/statsby.rb +7 -0
- data/lib/statsby/client.rb +57 -0
- data/lib/statsby/context.rb +46 -0
- data/lib/statsby/tag_set.rb +15 -0
- data/lib/statsby/version.rb +3 -0
- metadata +90 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 11f56a68495e9b665acba59afb614a5f1890ebe7
|
|
4
|
+
data.tar.gz: 1eec668de77fc3f75c2a431cb69359dd90e58428
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: a22c323e3287425010be626a355030ab542217e44ad9d5fc1759a829e5c82af9db6703511d1de092522ec957796f1866e108c35664368158e12cb19ff6de16be
|
|
7
|
+
data.tar.gz: 6cbfabe3de262592b4da807a702aba24418986bd79dfaa7552620c9ef48841fc0521a21d3a424ca2fb9d59b5dd304ca757fc8254831bb4bd8adfce8e3c4817fd
|
data/lib/statsby.rb
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
require 'socket'
|
|
2
|
+
|
|
3
|
+
module Statsby
|
|
4
|
+
# Use a Statsby::Client to send metrics over UDP to a StatsD server
|
|
5
|
+
class Client
|
|
6
|
+
DEFAULT_HOST = 'localhost'.freeze
|
|
7
|
+
DEFAULT_PORT = 8125
|
|
8
|
+
|
|
9
|
+
attr_reader :socket, :host, :port, :tags, :tags_enabled
|
|
10
|
+
|
|
11
|
+
def initialize(
|
|
12
|
+
host: DEFAULT_HOST,
|
|
13
|
+
port: DEFAULT_PORT,
|
|
14
|
+
tags: {},
|
|
15
|
+
tags_enabled: true
|
|
16
|
+
)
|
|
17
|
+
@socket = UDPSocket.new
|
|
18
|
+
@host = host
|
|
19
|
+
@port = port
|
|
20
|
+
@tags = Statsby::TagSet.from_hash(tags)
|
|
21
|
+
@tags_enabled = tags_enabled
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def send_message(message)
|
|
25
|
+
socket.send(message, 0, host, port)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def counter(metric_name, value, local_tags = {})
|
|
29
|
+
send_message(format_message(metric_name, value, 'c', local_tags))
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def gauge(metric_name, value, local_tags = {})
|
|
33
|
+
send_message(format_message(metric_name, value, 'g', local_tags))
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def timing(metric_name, value, local_tags = {})
|
|
37
|
+
send_message(format_message(metric_name, value, 'ms', local_tags))
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def set(metric_name, value, local_tags = {})
|
|
41
|
+
send_message(format_message(metric_name, value, 's', local_tags))
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def format_tags(message_tags = {})
|
|
45
|
+
combined_tags = tags.merge(message_tags)
|
|
46
|
+
",#{combined_tags}" if tags_enabled && !combined_tags.empty?
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def format_message(metric_name, value, type, message_tags = {})
|
|
50
|
+
"#{metric_name}#{format_tags(message_tags)}:#{value}|#{type}"
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def subcontext(tags = {})
|
|
54
|
+
Statsby::Context.new(self, tags)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require 'statsby/tag_set'
|
|
2
|
+
module Statsby
|
|
3
|
+
# This is meant to be used as a thin layer over a
|
|
4
|
+
# client or another context as a way to organize tags.
|
|
5
|
+
class Context
|
|
6
|
+
attr_reader :super_context, :tags
|
|
7
|
+
|
|
8
|
+
def initialize(super_context, tags = {})
|
|
9
|
+
@super_context = super_context
|
|
10
|
+
@tags = Statsby::TagSet.from_hash(tags)
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def counter(metric_name, value, local_tags = {})
|
|
14
|
+
combined_tags = tags.merge(local_tags)
|
|
15
|
+
super_context.counter(metric_name, value, combined_tags)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def gauge(metric_name, value, local_tags = {})
|
|
19
|
+
combined_tags = tags.merge(local_tags)
|
|
20
|
+
super_context.counter(metric_name, value, combined_tags)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def timing(metric_name, value, local_tags = {})
|
|
24
|
+
combined_tags = tags.merge(local_tags)
|
|
25
|
+
super_context.counter(metric_name, value, combined_tags)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def set(metric_name, value, local_tags = {})
|
|
29
|
+
combined_tags = tags.merge(local_tags)
|
|
30
|
+
super_context.counter(metric_name, value, combined_tags)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def format_tags(message_tags = {})
|
|
34
|
+
super_context.format_tags(tags.merge(message_tags))
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def format_message(metric_name, value, type, message_tags = {})
|
|
38
|
+
combined_tags = tags.merge(message_tags)
|
|
39
|
+
super_context.format_message(metric_name, value, type, combined_tags)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def subcontext(tags = {})
|
|
43
|
+
Statsby::Context.new(self, tags)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module Statsby
|
|
2
|
+
# A set of tags is really just some key value pairs,
|
|
3
|
+
# so we can leverage Ruby's built-in Hash class, simply
|
|
4
|
+
# overriding `#to_s` to get the format that we need.
|
|
5
|
+
# Maybe we can add some validation in the future.
|
|
6
|
+
class TagSet < Hash
|
|
7
|
+
def self.from_hash(hash)
|
|
8
|
+
TagSet.new.merge(hash)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def to_s
|
|
12
|
+
map { |key, value| "#{key}=#{value}" }.join(',')
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: statsby
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- tastybacon
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2017-11-02 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: pry
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0.11'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0.11'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rspec
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '3.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '3.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rubocop
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0.50'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0.50'
|
|
55
|
+
description: A toy implementation of a StatsD client in Ruby
|
|
56
|
+
email: tastycuredpork@gmail.com
|
|
57
|
+
executables: []
|
|
58
|
+
extensions: []
|
|
59
|
+
extra_rdoc_files: []
|
|
60
|
+
files:
|
|
61
|
+
- lib/statsby.rb
|
|
62
|
+
- lib/statsby/client.rb
|
|
63
|
+
- lib/statsby/context.rb
|
|
64
|
+
- lib/statsby/tag_set.rb
|
|
65
|
+
- lib/statsby/version.rb
|
|
66
|
+
homepage: https://github.com/tastybacon/statsby
|
|
67
|
+
licenses:
|
|
68
|
+
- MIT
|
|
69
|
+
metadata: {}
|
|
70
|
+
post_install_message:
|
|
71
|
+
rdoc_options: []
|
|
72
|
+
require_paths:
|
|
73
|
+
- lib
|
|
74
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
75
|
+
requirements:
|
|
76
|
+
- - ">="
|
|
77
|
+
- !ruby/object:Gem::Version
|
|
78
|
+
version: '0'
|
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
|
+
requirements:
|
|
81
|
+
- - ">="
|
|
82
|
+
- !ruby/object:Gem::Version
|
|
83
|
+
version: '0'
|
|
84
|
+
requirements: []
|
|
85
|
+
rubyforge_project:
|
|
86
|
+
rubygems_version: 2.6.14
|
|
87
|
+
signing_key:
|
|
88
|
+
specification_version: 4
|
|
89
|
+
summary: StatsD Ruby Client
|
|
90
|
+
test_files: []
|