cloudinsight-sdk 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/examples/example.rb +29 -0
- data/lib/cloud_insight/statsd.rb +119 -0
- data/lib/cloud_insight/version.rb +5 -0
- data/lib/cloudinsight-sdk.rb +4 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZDgxMTQxOTFiZTU2MDgxN2M0NGY2MjRmNjRmM2NiZDA2NzM2MTE3NA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
N2Q0YjBlMGQzZWI5Mzg2M2UyNTA3ZGQzNTRlNmFmZDBjNzQ1YzZiYQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZTVjOTkwZDY5NzAwMmE5MGVkNGY0ZWQxNDJkY2M2Y2NlYTk2YTZhNTNhMWI5
|
10
|
+
ZDUxODgwZDc4MDcyYTc0ZDQwZGYzMzBjY2UzZWM2OGIwNDAyYTU2ZDRhYTJl
|
11
|
+
NWE5ZDc2YjUyNjA4MTg1MDI1ZThjYzU1YzRjZTg5NjJmZmQxYzA=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
MWI5NjhlMjNhYzgyZWFhYjM0NDcyNGM4Yzk0NDYxNzg2ZDViMTcwZTA2Y2Jl
|
14
|
+
M2Y4ZGVjNzdmMzU5MjE1NWU3ZmUwOTEzY2E4Mzg2MDRlODZlYTFhMjFkMGNk
|
15
|
+
ZDUzOGNlNWZkMjM4YWE5ZmNhMTY1OWU4OWM1NGFkMjQ0YzVmMjE=
|
data/examples/example.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
#! /usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'cloudinsight-sdk'
|
4
|
+
|
5
|
+
statsd = CloudInsight::Statsd.new
|
6
|
+
|
7
|
+
# Increment a counter.(default: 1)
|
8
|
+
statsd.increment('page.views')
|
9
|
+
|
10
|
+
# Increment a counter by 5 steps.
|
11
|
+
statsd.increment('page.views', 5)
|
12
|
+
|
13
|
+
# Increment a counter by 5, 50% sample
|
14
|
+
statsd.increment('page.views', 5, ['page.tag'], 0.5)
|
15
|
+
|
16
|
+
# Decrement a counter.
|
17
|
+
statsd.decrement('page.views')
|
18
|
+
|
19
|
+
# Decrement a counter by 5 steps.
|
20
|
+
statsd.decrement('page.views', 5)
|
21
|
+
|
22
|
+
# Decrement a counter by 5, 50% sample
|
23
|
+
statsd.decrement('page.views', 5, ['page.tag'], 0.5)
|
24
|
+
|
25
|
+
# Record a gauge 100 of replies
|
26
|
+
statsd.gauge('blogs.replies', 100)
|
27
|
+
|
28
|
+
# Record a gauge by 100, 50% sample
|
29
|
+
statsd.gauge('users.online', 100, ['users.cloudinsight'], 0.5)
|
@@ -0,0 +1,119 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'socket'
|
4
|
+
require 'logger'
|
5
|
+
|
6
|
+
module CloudInsight
|
7
|
+
class Statsd
|
8
|
+
attr_reader :socket, :buffer
|
9
|
+
|
10
|
+
ENCODING = 'utf-8'.freeze
|
11
|
+
|
12
|
+
# Init Statsd
|
13
|
+
# @statsd = CloudInsight::Statsd.new
|
14
|
+
# <String> host: the host of the OneStatsd server (default: 'localhost')
|
15
|
+
# <Fixnum> port: the port of the OneStatsd server (default: 8251)
|
16
|
+
# <Hash> option:
|
17
|
+
# <Fixnum> option[:max_buffer_size]: Maximum number of metrics to buffer before sending to the server (default: 50)
|
18
|
+
# <Array> option[:constant_tags]: Tags to attach to every metric reported by this client (default: [])
|
19
|
+
# <Boolean> option[:use_ms]: Report timed values in milliseconds instead of seconds (default: false)
|
20
|
+
def initialize(host = 'localhost', port = 8251, option = {})
|
21
|
+
@host = host
|
22
|
+
@port = port
|
23
|
+
@buffer = []
|
24
|
+
@socket = nil
|
25
|
+
@max_buffer_size = option[:max_buffer_size] || 50
|
26
|
+
@constant_tags = option[:constant_tags] || []
|
27
|
+
@use_ms = option[:use_ms] || false
|
28
|
+
exit_handler
|
29
|
+
end
|
30
|
+
|
31
|
+
# Record the value of a gauge, optionally setting a list of tags and a sample rate.
|
32
|
+
# @statsd.gauge('test.online', 8080)
|
33
|
+
# @statsd.gauge('test.online', 8080, ['test:online'], 0.8)
|
34
|
+
def gauge(metric, value = 1, tags = [], sample_rate = 1)
|
35
|
+
report(metric, 'g', value, tags, sample_rate)
|
36
|
+
end
|
37
|
+
|
38
|
+
# Increment a counter, optionally setting a value, tags and a sample rate.
|
39
|
+
# @statsd.increment('test.online', 1)
|
40
|
+
# @statsd.increment('test.online', 1, ['test:online'], 0.8)
|
41
|
+
def increment(metric, value = 1, tags = [], sample_rate = 1)
|
42
|
+
report(metric, 'c', value, tags, sample_rate)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Decrement a counter, optionally setting a value, tags and a sample rate.
|
46
|
+
# @statsd.decrement('test.online', 1)
|
47
|
+
# @statsd.decrement('test.online', 1, ['test:online'], 0.8)
|
48
|
+
def decrement(metric, value = 1, tags = [], sample_rate = 1)
|
49
|
+
report(metric, 'c', -value, tags, sample_rate)
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def report(metric, mtype, value, tags, sample_rate)
|
55
|
+
return if sample_rate.to_f != 1.0 && rand > sample_rate.to_f
|
56
|
+
data = convert(metric, mtype, value, tags, sample_rate)
|
57
|
+
store_to_buffer data if data
|
58
|
+
end
|
59
|
+
|
60
|
+
def convert(metric, mtype, value, tags, sample_rate)
|
61
|
+
data = []
|
62
|
+
data << "#{metric}:#{value}"
|
63
|
+
data << mtype.to_s
|
64
|
+
data << "@#{sample_rate.to_f}"
|
65
|
+
data << "##{tags.join(',')}" unless (tags += @constant_tags).empty?
|
66
|
+
begin
|
67
|
+
data.join('|').encode(ENCODING)
|
68
|
+
rescue
|
69
|
+
nil
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def store_to_buffer(data)
|
74
|
+
@buffer << data
|
75
|
+
flush_buffer if @buffer.size >= @max_buffer_size
|
76
|
+
end
|
77
|
+
|
78
|
+
def flush_buffer
|
79
|
+
buffer_old = @buffer
|
80
|
+
@buffer = []
|
81
|
+
send_to_agent { |socket| socket.send(buffer_old.join("\n"), 0) } unless buffer_old.empty?
|
82
|
+
end
|
83
|
+
|
84
|
+
def send_to_agent
|
85
|
+
yield socket
|
86
|
+
rescue => e
|
87
|
+
logger.error "send to agent server error: #{e.inspect}"
|
88
|
+
end
|
89
|
+
|
90
|
+
def socket
|
91
|
+
@socket ||= begin
|
92
|
+
UDPSocket.new.tap do |udp_socket|
|
93
|
+
udp_socket.connect @host, @port
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def logger
|
99
|
+
@logger ||= begin
|
100
|
+
Logger.new(STDOUT).tap do |log|
|
101
|
+
log.level = Logger::INFO
|
102
|
+
log.datetime_format = '%Y-%m-%d %H:%M:%S'
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# flush buffer on exit
|
108
|
+
def exit_handler
|
109
|
+
at_exit do
|
110
|
+
begin
|
111
|
+
flush_buffer
|
112
|
+
socket.close
|
113
|
+
rescue => e
|
114
|
+
logger.error "close sokcet error: #{e.inspect}"
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cloudinsight-sdk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- cloudinsight
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-04-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '10.1'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '10.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest-reporters
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: mocha
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.13'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.13'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.9'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.9'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.39'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.39'
|
97
|
+
description: Cloud Insight SDK. (http://www.oneapm.com/ci/feature.html)
|
98
|
+
email: support@oneapm.com
|
99
|
+
executables: []
|
100
|
+
extensions: []
|
101
|
+
extra_rdoc_files: []
|
102
|
+
files:
|
103
|
+
- examples/example.rb
|
104
|
+
- lib/cloud_insight/statsd.rb
|
105
|
+
- lib/cloud_insight/version.rb
|
106
|
+
- lib/cloudinsight-sdk.rb
|
107
|
+
homepage: http://www.oneapm.com/ci/feature.html
|
108
|
+
licenses:
|
109
|
+
- CloudInsight
|
110
|
+
- MIT
|
111
|
+
- Ruby
|
112
|
+
metadata: {}
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ! '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '1.9'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ! '>='
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 2.4.5
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: Cloud Insight SDK
|
133
|
+
test_files: []
|