algometrics 0.1.0
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/.gitignore +9 -0
- data/.rspec +2 -0
- data/.rubocop.yml +10 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +58 -0
- data/Rakefile +6 -0
- data/algometrics.gemspec +27 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/algometrics/client.rb +114 -0
- data/lib/algometrics/middleware/response_handler.rb +18 -0
- data/lib/algometrics/version.rb +3 -0
- data/lib/algometrics.rb +11 -0
- metadata +114 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 49e202d1206158d5ab83bbc82603fed6420b49c8
|
4
|
+
data.tar.gz: 478425703e7a60280376b610d2b5d21be2a92c88
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2e8c44cb9885b502a57dabea34be3694836b3d4d45e08c3cb75aa6c485acf8fa111ea49a1577c32db4dffb6f0ea9919ae6c35906db5429cbc20d742a72e040b4
|
7
|
+
data.tar.gz: befffb7174dc1fc30ada19eaea24b013a1344baedc4d97473fb46f7c5b2d74dc3f8b1f6ce65ed5252acd0b6e26346569d5dc95cd1848874610447ef5c32bfcb8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Alex Kurkin
|
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,58 @@
|
|
1
|
+
# algometrics.io ruby client
|
2
|
+
|
3
|
+
algometrics-gem is an official ruby client for event-tracking and real-time workflow monitoring and alerting platform [algometrics.io](https://algometrics.io/)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'algometrics'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install algometrics
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Quickest possible way to send events is the following:
|
24
|
+
|
25
|
+
```
|
26
|
+
client = Algometrics::Client.new(api_key: '...')
|
27
|
+
client.track(event: 'Job - Started', actor: {type: 'Job', id: 123})
|
28
|
+
client.track(event: 'Job - Completed', actor: {type: 'Job', id: 123})
|
29
|
+
```
|
30
|
+
|
31
|
+
This way, we'll be able to track events for actor `Job#123`.
|
32
|
+
|
33
|
+
### Send events with status
|
34
|
+
|
35
|
+
By default all events are sent with status `success`.
|
36
|
+
|
37
|
+
You can explicitly send events with status `failure`, in this case any workflows associated with events will be immediately marked as failed and notifications will be sent.
|
38
|
+
|
39
|
+
```
|
40
|
+
client = Algometrics::Client.new(api_key: '...')
|
41
|
+
client.track(event: 'Order Placed', actor: {type: 'Order', id: 123}, status: Algometrics::FAILURE)
|
42
|
+
```
|
43
|
+
|
44
|
+
|
45
|
+
## Development
|
46
|
+
|
47
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
48
|
+
|
49
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/algometricsio/algometrics-gem
|
54
|
+
|
55
|
+
|
56
|
+
## License
|
57
|
+
|
58
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/algometrics.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'algometrics/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "algometrics"
|
8
|
+
spec.version = Algometrics::VERSION
|
9
|
+
spec.authors = ["Alex Kurkin"]
|
10
|
+
spec.email = ["alex@algometrics.io"]
|
11
|
+
|
12
|
+
spec.summary = "Algometrics API client for sending events to algometrics.io"
|
13
|
+
spec.description = "Send events to algometrics.io and monitor your workflows in real-time."
|
14
|
+
spec.homepage = "https://github.com/algometricsio/algometrics-gem"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_dependency 'faraday', '~> 0.9.2'
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "algometrics"
|
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,114 @@
|
|
1
|
+
require 'faraday'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module Algometrics
|
5
|
+
class Client
|
6
|
+
extend Forwardable
|
7
|
+
|
8
|
+
def_delegators :connection,
|
9
|
+
:get,
|
10
|
+
:post,
|
11
|
+
:head,
|
12
|
+
:put,
|
13
|
+
:delete,
|
14
|
+
:build_url
|
15
|
+
|
16
|
+
DEFAULT_CONFIG = {
|
17
|
+
url: "https://algometrics.io",
|
18
|
+
api_version: "/v1"
|
19
|
+
}
|
20
|
+
|
21
|
+
attr_reader :api_key,
|
22
|
+
:url,
|
23
|
+
:api_version,
|
24
|
+
:adapter
|
25
|
+
|
26
|
+
def initialize(opts = {})
|
27
|
+
@api_key = opts[:api_key]
|
28
|
+
|
29
|
+
@api_version = opts[:api_version] || DEFAULT_CONFIG[:api_version]
|
30
|
+
@url = URI.join((opts[:url] || DEFAULT_CONFIG[:url]), @api_version).to_s
|
31
|
+
|
32
|
+
@adapter = opts[:adapter] || Faraday.default_adapter
|
33
|
+
end
|
34
|
+
|
35
|
+
def connection
|
36
|
+
@connection ||= Faraday::Connection.new(
|
37
|
+
url: url,
|
38
|
+
request: {
|
39
|
+
open_timeout: 30,
|
40
|
+
timeout: 30
|
41
|
+
}
|
42
|
+
) do |f|
|
43
|
+
f.adapter adapter
|
44
|
+
f.use Algometrics::Middleware::ResponseHandler
|
45
|
+
end.tap do |transport|
|
46
|
+
transport.headers[:user_agent] = user_agent
|
47
|
+
transport.headers[:content_type] = 'application/json'
|
48
|
+
transport.headers[:x_api_key] = api_key
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def user_agent
|
53
|
+
"algometrics-gem #{Algometrics::VERSION}"
|
54
|
+
end
|
55
|
+
|
56
|
+
def track(event:, actor:, status: Algometrics::SUCCESS)
|
57
|
+
unless valid_actor?(actor)
|
58
|
+
Algometrics::Client.logger.error("Incorrect method execution: invalid actor: '#{actor}'")
|
59
|
+
return
|
60
|
+
end
|
61
|
+
|
62
|
+
actor = parse_actor(actor)
|
63
|
+
|
64
|
+
data = {
|
65
|
+
event: event,
|
66
|
+
actor: actor,
|
67
|
+
status: [Algometrics::SUCCESS, Algometrics::FAILURE].include?(status) ? status : Algometrics::SUCCESS
|
68
|
+
}
|
69
|
+
|
70
|
+
connection.post("#{api_version}/events", data.to_json)
|
71
|
+
end
|
72
|
+
|
73
|
+
def self.logger
|
74
|
+
@logger ||= Logger.new(STDOUT)
|
75
|
+
end
|
76
|
+
|
77
|
+
def self.logger=(new_logger)
|
78
|
+
@logger = new_logger
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def parse_actor(actor)
|
84
|
+
case actor
|
85
|
+
when String
|
86
|
+
type, id = actor.split("#")
|
87
|
+
{ type: type, id: id }
|
88
|
+
when Hash
|
89
|
+
actor
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def validate_actor_string(str)
|
94
|
+
!(str =~ /\A\w+#\w+\z/).nil?
|
95
|
+
end
|
96
|
+
|
97
|
+
def validate_actor_hash(hash)
|
98
|
+
return false unless ([:type, :id] & hash.keys).count > 0
|
99
|
+
validate_actor_string("#{hash[:type]}##{hash[:id]}")
|
100
|
+
end
|
101
|
+
|
102
|
+
def valid_actor?(actor)
|
103
|
+
case actor
|
104
|
+
when String
|
105
|
+
validate_actor_string(actor)
|
106
|
+
when Hash
|
107
|
+
validate_actor_hash(actor)
|
108
|
+
else
|
109
|
+
false
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Algometrics
|
2
|
+
module Middleware
|
3
|
+
class ResponseHandler < Faraday::Middleware
|
4
|
+
|
5
|
+
def call(request_env)
|
6
|
+
@app.call(request_env).on_complete do |response_env|
|
7
|
+
status = response_env.status
|
8
|
+
|
9
|
+
case status
|
10
|
+
when 401
|
11
|
+
Algometrics::Client.logger.error('Invalid Algometrics API key')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
end # class ResponseHandler
|
17
|
+
end # module Middleware
|
18
|
+
end # module Algometrics
|
data/lib/algometrics.rb
ADDED
metadata
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: algometrics
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Kurkin
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.9.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.9.2
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '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'
|
69
|
+
description: Send events to algometrics.io and monitor your workflows in real-time.
|
70
|
+
email:
|
71
|
+
- alex@algometrics.io
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- algometrics.gemspec
|
84
|
+
- bin/console
|
85
|
+
- bin/setup
|
86
|
+
- lib/algometrics.rb
|
87
|
+
- lib/algometrics/client.rb
|
88
|
+
- lib/algometrics/middleware/response_handler.rb
|
89
|
+
- lib/algometrics/version.rb
|
90
|
+
homepage: https://github.com/algometricsio/algometrics-gem
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.4.5
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Algometrics API client for sending events to algometrics.io
|
114
|
+
test_files: []
|