graphite_client 0.3.0 → 0.4.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.
- data/README.md +19 -2
- data/Rakefile +2 -1
- data/VERSION +1 -1
- data/graphite_client.gemspec +6 -4
- data/lib/graphite_client.rb +2 -0
- data/lib/graphite_client/event_reporter.rb +27 -0
- data/spec/event_reporter_spec.rb +67 -0
- metadata +7 -3
data/README.md
CHANGED
@@ -1,5 +1,22 @@
|
|
1
|
+
# graphite_client #
|
2
|
+
|
3
|
+
## About ##
|
4
|
+
|
1
5
|
Very simple ruby client for reporting metrics to [Graphite](http://graphite.wikidot.com/).
|
2
6
|
|
3
|
-
Original code taken from a Gist
|
4
|
-
|
7
|
+
[Original code](https://gist.github.com/1678399) taken from a Gist by [joakimk](https://github.com/joakimk/).
|
8
|
+
|
9
|
+
## Usage ##
|
10
|
+
|
11
|
+
### Reporting the standard way, over TCP ###
|
12
|
+
|
13
|
+
graphite = GraphiteClient.new('graphite_host')
|
14
|
+
graphite.report('metric name', value, time)
|
15
|
+
graphite.report('another metric name', another_value, another_time)
|
16
|
+
|
17
|
+
### Reporting [events](https://code.launchpad.net/~lucio.torre/graphite/add-events/+merge/69142) ###
|
18
|
+
|
19
|
+
graphite_event = GraphiteClient::EventReporter.new('http://graphite_host/events/')
|
20
|
+
graphite_event.report(:what => 'an event', :tags => ['some', 'tags'], :data => 'some info')
|
5
21
|
|
22
|
+
* When reporting an event, you can (optionally) pass `:basic_auth => { :username => 'un', :password => 'pw' }` to the `GraphiteClient::EventReporter` constructor.
|
data/Rakefile
CHANGED
@@ -20,7 +20,8 @@ Jeweler::Tasks.new do |gem|
|
|
20
20
|
gem.summary = %Q{Very simple ruby client for graphite.}
|
21
21
|
gem.description = %Q{Original code by https://github.com/joakimk.}
|
22
22
|
gem.email = "alex@crackpot.org"
|
23
|
-
gem.authors = ["Alex Dean"]
|
23
|
+
gem.authors = ["Alex Dean", "Matthew Trost"]
|
24
|
+
gem.require_paths = ["lib", "lib/graphite_client"]
|
24
25
|
# dependencies defined in Gemfile
|
25
26
|
end
|
26
27
|
Jeweler::RubygemsDotOrgTasks.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.1
|
data/graphite_client.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "graphite_client"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
-
s.authors = ["Alex Dean"]
|
12
|
-
s.date = "2012-
|
11
|
+
s.authors = ["Alex Dean", "Matthew Trost"]
|
12
|
+
s.date = "2012-12-14"
|
13
13
|
s.description = "Original code by https://github.com/joakimk."
|
14
14
|
s.email = "alex@crackpot.org"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -27,12 +27,14 @@ Gem::Specification.new do |s|
|
|
27
27
|
"VERSION",
|
28
28
|
"graphite_client.gemspec",
|
29
29
|
"lib/graphite_client.rb",
|
30
|
+
"lib/graphite_client/event_reporter.rb",
|
31
|
+
"spec/event_reporter_spec.rb",
|
30
32
|
"spec/graphite_client_spec.rb",
|
31
33
|
"spec/spec_helper.rb"
|
32
34
|
]
|
33
35
|
s.homepage = "http://github.com/tedconf/graphite_client"
|
34
36
|
s.licenses = ["MIT"]
|
35
|
-
s.require_paths = ["lib"]
|
37
|
+
s.require_paths = ["lib", "lib/graphite_client"]
|
36
38
|
s.rubygems_version = "1.8.24"
|
37
39
|
s.summary = "Very simple ruby client for graphite."
|
38
40
|
|
data/lib/graphite_client.rb
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'net/http'
|
3
|
+
require 'net/https'
|
4
|
+
|
5
|
+
class GraphiteClient
|
6
|
+
class EventReporter
|
7
|
+
def initialize(graphite_server_events_url, opts={})
|
8
|
+
uri = URI(graphite_server_events_url)
|
9
|
+
@http = Net::HTTP.new(uri.host, uri.port)
|
10
|
+
@req = Net::HTTP::Post.new(uri.request_uri)
|
11
|
+
|
12
|
+
@http.use_ssl = true if uri.scheme == 'https'
|
13
|
+
|
14
|
+
if opts[:basic_auth]
|
15
|
+
username = opts[:basic_auth][:username]
|
16
|
+
password = opts[:basic_auth][:password]
|
17
|
+
@req.basic_auth(username, password)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def report(event={})
|
22
|
+
event[:tags] = Array(event[:tags]).join(',')
|
23
|
+
@req.body = event.to_json
|
24
|
+
@http.request(@req)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require 'graphite_client'
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
|
+
|
4
|
+
describe "GraphiteClient::EventReporter" do
|
5
|
+
before do
|
6
|
+
@url = 'http://example.com/path'
|
7
|
+
@uri = URI(@url)
|
8
|
+
@https_url = 'https://example.com/path'
|
9
|
+
@https_uri = URI(@https_url)
|
10
|
+
@event = {:what => 'test', :tags => 'test', :data => 'test'}
|
11
|
+
@basic_auth = {:username => 'test', :password => 'test'}
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should create an HTTP connection and POST to it" do
|
15
|
+
Net::HTTP.should_receive(:new).with(@uri.host, @uri.port).and_return(http = mock())
|
16
|
+
Net::HTTP::Post.should_receive(:new).with(@uri.request_uri).and_return(req = mock())
|
17
|
+
req.should_receive(:body=).with(@event.to_json)
|
18
|
+
http.should_receive(:request).with(req)
|
19
|
+
event_reporter = GraphiteClient::EventReporter.new(@url)
|
20
|
+
event_reporter.report(@event)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should reuse the same HTTP connection" do
|
24
|
+
Net::HTTP.should_receive(:new).with(@uri.host, @uri.port).and_return(http = mock())
|
25
|
+
Net::HTTP::Post.should_receive(:new).with(@uri.request_uri).and_return(req = mock())
|
26
|
+
req.should_receive(:body=).twice.with(@event.to_json)
|
27
|
+
http.should_receive(:request).twice.with(req)
|
28
|
+
event_reporter = GraphiteClient::EventReporter.new(@url)
|
29
|
+
event_reporter.report(@event)
|
30
|
+
event_reporter.report(@event)
|
31
|
+
end
|
32
|
+
|
33
|
+
context "with basic auth" do
|
34
|
+
it "should create an HTTP connection and POST to it, using basic auth" do
|
35
|
+
Net::HTTP.should_receive(:new).with(@uri.host, @uri.port).and_return(http = mock())
|
36
|
+
Net::HTTP::Post.should_receive(:new).with(@uri.request_uri).and_return(req = mock())
|
37
|
+
req.should_receive(:basic_auth).with(@basic_auth[:username], @basic_auth[:password])
|
38
|
+
req.should_receive(:body=).with(@event.to_json)
|
39
|
+
http.should_receive(:request).with(req)
|
40
|
+
event_reporter = GraphiteClient::EventReporter.new(@url, basic_auth: @basic_auth)
|
41
|
+
event_reporter.report(@event)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "over SSL" do
|
46
|
+
it "should create an HTTPS connection and POST to it" do
|
47
|
+
Net::HTTP.should_receive(:new).with(@https_uri.host, @https_uri.port).and_return(http = mock())
|
48
|
+
Net::HTTP::Post.should_receive(:new).with(@https_uri.request_uri).and_return(req = mock())
|
49
|
+
http.should_receive(:use_ssl=).with(true)
|
50
|
+
req.should_receive(:body=).with(@event.to_json)
|
51
|
+
http.should_receive(:request).with(req)
|
52
|
+
event_reporter = GraphiteClient::EventReporter.new(@https_url)
|
53
|
+
event_reporter.report(@event)
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should create an HTTPS connection and POST to it, using basic auth" do
|
57
|
+
Net::HTTP.should_receive(:new).with(@https_uri.host, @https_uri.port).and_return(http = mock())
|
58
|
+
Net::HTTP::Post.should_receive(:new).with(@https_uri.request_uri).and_return(req = mock())
|
59
|
+
http.should_receive(:use_ssl=).with(true)
|
60
|
+
req.should_receive(:basic_auth).with(@basic_auth[:username], @basic_auth[:password])
|
61
|
+
req.should_receive(:body=).with(@event.to_json)
|
62
|
+
http.should_receive(:request).with(req)
|
63
|
+
event_reporter = GraphiteClient::EventReporter.new(@https_url, basic_auth: @basic_auth)
|
64
|
+
event_reporter.report(@event)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphite_client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Alex Dean
|
9
|
+
- Matthew Trost
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date: 2012-
|
13
|
+
date: 2012-12-14 00:00:00.000000000 Z
|
13
14
|
dependencies:
|
14
15
|
- !ruby/object:Gem::Dependency
|
15
16
|
name: rspec
|
@@ -93,6 +94,8 @@ files:
|
|
93
94
|
- VERSION
|
94
95
|
- graphite_client.gemspec
|
95
96
|
- lib/graphite_client.rb
|
97
|
+
- lib/graphite_client/event_reporter.rb
|
98
|
+
- spec/event_reporter_spec.rb
|
96
99
|
- spec/graphite_client_spec.rb
|
97
100
|
- spec/spec_helper.rb
|
98
101
|
homepage: http://github.com/tedconf/graphite_client
|
@@ -102,6 +105,7 @@ post_install_message:
|
|
102
105
|
rdoc_options: []
|
103
106
|
require_paths:
|
104
107
|
- lib
|
108
|
+
- lib/graphite_client
|
105
109
|
required_ruby_version: !ruby/object:Gem::Requirement
|
106
110
|
none: false
|
107
111
|
requirements:
|
@@ -110,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
110
114
|
version: '0'
|
111
115
|
segments:
|
112
116
|
- 0
|
113
|
-
hash:
|
117
|
+
hash: 654855312543279234
|
114
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
115
119
|
none: false
|
116
120
|
requirements:
|