stackdriver-ruby 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +88 -0
- data/Rakefile +7 -0
- data/lib/stackdriver.rb +24 -0
- data/lib/stackdriver/client.rb +58 -0
- data/lib/stackdriver/client/annotation_event.rb +28 -0
- data/lib/stackdriver/client/custom.rb +34 -0
- data/lib/stackdriver/client/deploy_event.rb +26 -0
- data/lib/stackdriver/version.rb +3 -0
- data/stackdriver-ruby.gemspec +24 -0
- data/test/helper.rb +3 -0
- data/test/stackdriver/client/custom_test.rb +11 -0
- data/test/stackdriver_test.rb +5 -0
- metadata +118 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 533a9f71ae4edea5f911cccabed44c57d995835c
|
4
|
+
data.tar.gz: 01e22c7b6dc3076574959bfa0c331e4fb2dfc831
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 310c5eeeee1c05e092300d05233e8ec642636e343c3615998ab7ee56ab9358ecd31227b94c3a1db77b74c9585ec0df8eee5397c87d2ceee0c8c33ae62e35a1be
|
7
|
+
data.tar.gz: ae45d9fe4f9db5f8580bf3d10a920e8048f4710422eeb64b68d4d7fc4f6a7d0a07ef1ed27f1a6d11afb6e7af4fc87c34c8968ec9c99df754a77953fa84509d2a
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 balser
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# stackdriver-ruby
|
2
|
+
|
3
|
+
A simple ruby toolkit for reporting metrics and events to [Stackdriver] endpoints.
|
4
|
+
|
5
|
+
## installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'stackdriver-ruby'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install stackdriver-ruby
|
18
|
+
|
19
|
+
## usage
|
20
|
+
|
21
|
+
Set `STACKDRIVER_API_KEY` in your environment. Errors are rescued by default in order to not break the application if reporting fails. For debugging responses, you can call `Stackdriver.last_response` to inspect the previous response.
|
22
|
+
|
23
|
+
**Reporting a [custom metric]**
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
Stackdriver.custom(metric_name, value, collected_at, aws_instance_id)
|
27
|
+
# => true
|
28
|
+
```
|
29
|
+
|
30
|
+
**Sending a [deploy event]**
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
options = {
|
34
|
+
deployed_by: 'whodis',
|
35
|
+
deployed_to: 'production',
|
36
|
+
repository: 'foo'
|
37
|
+
}
|
38
|
+
|
39
|
+
Stackdriver.deploy_event('0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', options)
|
40
|
+
# => true
|
41
|
+
```
|
42
|
+
|
43
|
+
**Displaying an [annotation event]**
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
options = {
|
47
|
+
level: 'WARN' # INFO/WARN/ERROR (default INFO)
|
48
|
+
annotated_by: 'whodis'
|
49
|
+
instance_id: 'i-1234abcd',
|
50
|
+
event_epoch: Time.now.to_i
|
51
|
+
}
|
52
|
+
|
53
|
+
Stackdriver.annotation_event('That just happened!', options)
|
54
|
+
# => true
|
55
|
+
```
|
56
|
+
|
57
|
+
## examples
|
58
|
+
|
59
|
+
* [Using with delayed jobs.](https://github.com/balser/stackdriver-ruby/wiki/Example-using-with-delayed-jobs)
|
60
|
+
|
61
|
+
## tk
|
62
|
+
|
63
|
+
* configureables (logger, agent, etc)
|
64
|
+
* tests
|
65
|
+
|
66
|
+
## thank you
|
67
|
+
|
68
|
+
The code structure an conventions are heavily influenced by the following gems:
|
69
|
+
* [octokit]
|
70
|
+
* [twitter]
|
71
|
+
* [instagram-ruby-gem]
|
72
|
+
|
73
|
+
## contributing
|
74
|
+
|
75
|
+
1. Fork it ( http://github.com/balser/stackdriver-ruby/fork )
|
76
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
77
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
78
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
79
|
+
5. Create new Pull Request
|
80
|
+
|
81
|
+
[stackdriver]: http://www.stackdriver.com/
|
82
|
+
[custom metric]: http://feedback.stackdriver.com/knowledgebase/articles/181488-sending-custom-application-metrics-to-the-stackdri
|
83
|
+
[deploy event]: http://feedback.stackdriver.com/knowledgebase/articles/212917-sending-code-deploy-events-to-stackdriver
|
84
|
+
[annotation event]: http://feedback.stackdriver.com/knowledgebase/articles/260455-sending-annotation-events-to-stackdriver
|
85
|
+
|
86
|
+
[octokit]: https://github.com/octokit/octokit.rb
|
87
|
+
[instagram-ruby-gem]: https://github.com/Instagram/instagram-ruby-gem
|
88
|
+
[twitter]: https://github.com/sferik/twitter
|
data/Rakefile
ADDED
data/lib/stackdriver.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'stackdriver/client'
|
2
|
+
|
3
|
+
#
|
4
|
+
# Ruby toolkit for the reporting Stackdriver events and metrics.
|
5
|
+
#
|
6
|
+
module Stackdriver
|
7
|
+
|
8
|
+
class << self
|
9
|
+
|
10
|
+
def client
|
11
|
+
@client ||= Stackdriver::Client.new
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
#
|
17
|
+
# Delegate to the Stackdriver::Client.
|
18
|
+
#
|
19
|
+
def method_missing(method_name, *args, &block)
|
20
|
+
return super unless client.respond_to?(method_name)
|
21
|
+
client.send(method_name, *args, &block)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'stackdriver/client/custom'
|
2
|
+
require 'stackdriver/client/deploy_event'
|
3
|
+
require 'stackdriver/client/annotation_event'
|
4
|
+
|
5
|
+
module Stackdriver
|
6
|
+
#
|
7
|
+
# Client for Stackdriver API
|
8
|
+
#
|
9
|
+
class Client
|
10
|
+
include Stackdriver::Client::Custom
|
11
|
+
include Stackdriver::Client::DeployEvent
|
12
|
+
include Stackdriver::Client::AnnotationEvent
|
13
|
+
|
14
|
+
STACKDRIVER_HOST = 'https://custom-gateway.stackdriver.com'.freeze
|
15
|
+
|
16
|
+
def last_response
|
17
|
+
@last_response
|
18
|
+
end
|
19
|
+
|
20
|
+
def clear
|
21
|
+
@last_response = nil
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def conn
|
27
|
+
@conn ||= Faraday.new(url: STACKDRIVER_HOST) do |faraday|
|
28
|
+
faraday.adapter Faraday.default_adapter
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
#
|
33
|
+
# Send the metrics/event and return success boolean.
|
34
|
+
#
|
35
|
+
def request(method, endpoint, data)
|
36
|
+
begin
|
37
|
+
@last_response = conn.send(method) do |req|
|
38
|
+
req.url endpoint
|
39
|
+
req.body = MultiJson.dump(data)
|
40
|
+
req.headers['Content-Type'] = 'application/json'
|
41
|
+
req.headers['x-stackdriver-apikey'] = api_key
|
42
|
+
req.options[:timeout] = 2
|
43
|
+
req.options[:open_timeout] = 2
|
44
|
+
end
|
45
|
+
|
46
|
+
# TODO: Confirm whether 201 is the only success response.
|
47
|
+
@last_response.status == 201
|
48
|
+
rescue
|
49
|
+
false
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def api_key
|
54
|
+
@api_key ||= ENV.fetch('STACKDRIVER_API_KEY', 'NullApiKey')
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
#
|
2
|
+
# See:
|
3
|
+
# http://feedback.stackdriver.com/knowledgebase/articles/260455-sending-annotation-events-to-stackdriver
|
4
|
+
#
|
5
|
+
module Stackdriver
|
6
|
+
class Client
|
7
|
+
module AnnotationEvent
|
8
|
+
|
9
|
+
ENDPOINT = 'v1/annotationevent'.freeze
|
10
|
+
|
11
|
+
#
|
12
|
+
# Options:
|
13
|
+
# :level (INFO/WARN/ERROR, default INFO)
|
14
|
+
# :annotated_by
|
15
|
+
# :instance_id
|
16
|
+
# :event_epoch
|
17
|
+
#
|
18
|
+
def annotation_event(message, options={})
|
19
|
+
data = {
|
20
|
+
message: message,
|
21
|
+
}.merge(options)
|
22
|
+
|
23
|
+
request(:post, ENDPOINT, data)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
#
|
2
|
+
# See: http://feedback.stackdriver.com/knowledgebase/articles/181488-sending-custom-application-metrics-to-the-stackdri
|
3
|
+
#
|
4
|
+
module Stackdriver
|
5
|
+
class Client
|
6
|
+
module Custom
|
7
|
+
|
8
|
+
ENDPOINT = 'v1/custom'.freeze
|
9
|
+
|
10
|
+
def custom(name, value, collected_at=Time.now.to_i, instance=nil)
|
11
|
+
data_point = {
|
12
|
+
name: name,
|
13
|
+
value: value,
|
14
|
+
collected_at: collected_at,
|
15
|
+
instance: instance,
|
16
|
+
}
|
17
|
+
|
18
|
+
request(:post, ENDPOINT, gateway_message(data_point))
|
19
|
+
end
|
20
|
+
|
21
|
+
#
|
22
|
+
# Wraps the datapoint per doc.
|
23
|
+
#
|
24
|
+
def gateway_message(data)
|
25
|
+
{
|
26
|
+
data: data,
|
27
|
+
timestamp: Time.now.to_i,
|
28
|
+
proto_version: 1,
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
#
|
2
|
+
# http://feedback.stackdriver.com/knowledgebase/articles/212917-sending-code-deploy-events-to-stackdriver
|
3
|
+
#
|
4
|
+
module Stackdriver
|
5
|
+
class Client
|
6
|
+
module DeployEvent
|
7
|
+
|
8
|
+
ENDPOINT = 'v1/deployevent'
|
9
|
+
|
10
|
+
#
|
11
|
+
# Options:
|
12
|
+
# :deployed_by
|
13
|
+
# :deployed_to
|
14
|
+
# :repository
|
15
|
+
#
|
16
|
+
def deploy_event(revision_id, options={})
|
17
|
+
data = {
|
18
|
+
revision_id: revision_id,
|
19
|
+
}.merge(options)
|
20
|
+
|
21
|
+
request(:post, ENDPOINT, data)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'stackdriver/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'stackdriver-ruby'
|
8
|
+
spec.version = Stackdriver::VERSION
|
9
|
+
spec.authors = ['Brian Balser']
|
10
|
+
spec.email = ['balser.brian@gmail.com']
|
11
|
+
spec.summary = %q{A ruby client for Stackdriver metrics.}
|
12
|
+
spec.homepage = ''
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
21
|
+
spec.add_development_dependency 'rake'
|
22
|
+
spec.add_runtime_dependency 'multi_json', '~> 1.8'
|
23
|
+
spec.add_runtime_dependency 'faraday', '~> 0.9'
|
24
|
+
end
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stackdriver-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brian Balser
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-03 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: '1.5'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: multi_json
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.8'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.8'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: faraday
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0.9'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0.9'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- balser.brian@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- lib/stackdriver.rb
|
82
|
+
- lib/stackdriver/client.rb
|
83
|
+
- lib/stackdriver/client/annotation_event.rb
|
84
|
+
- lib/stackdriver/client/custom.rb
|
85
|
+
- lib/stackdriver/client/deploy_event.rb
|
86
|
+
- lib/stackdriver/version.rb
|
87
|
+
- stackdriver-ruby.gemspec
|
88
|
+
- test/helper.rb
|
89
|
+
- test/stackdriver/client/custom_test.rb
|
90
|
+
- test/stackdriver_test.rb
|
91
|
+
homepage: ''
|
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
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.2.0
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: A ruby client for Stackdriver metrics.
|
115
|
+
test_files:
|
116
|
+
- test/helper.rb
|
117
|
+
- test/stackdriver/client/custom_test.rb
|
118
|
+
- test/stackdriver_test.rb
|