influxdb-async_queue 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +194 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/influxdb-async_queue.gemspec +31 -0
- data/lib/influxdb/async_queue/adapters/redis.rb +38 -0
- data/lib/influxdb/async_queue/adapters/test_adapter.rb +21 -0
- data/lib/influxdb/async_queue/config.rb +70 -0
- data/lib/influxdb/async_queue/queue.rb +43 -0
- data/lib/influxdb/async_queue/server.rb +58 -0
- data/lib/influxdb/async_queue/version.rb +5 -0
- data/lib/influxdb/async_queue.rb +33 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 144067720dc33c090446d5f38eca137100e1c84a
|
4
|
+
data.tar.gz: bd1faa76de45e8ec1d5be6f6b88385643062140c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 927facca10a3f073bf8ebb58ee3cfacdfe19d1844efd6d08353196103b95b188d83908c69e0272c396601c1f9591e2927d25e843c3a5f425f19405772551a234
|
7
|
+
data.tar.gz: a01c42d6cf55a4c03622ddcb6fe5f22400c91a68c7fff33bc9a4b9acd88c4b19985698c258384715899eaa31335dd32ad53b8ff271d0c757226da50342df9399
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at n.babushkin@fun-box.ru. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 n.babushkin
|
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,194 @@
|
|
1
|
+
# InfluxDB::AsyncQueue
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/mugimaru/influxdb-async_queue.svg?branch=master)](https://travis-ci.org/mugimaru/influxdb-async_queue)
|
4
|
+
|
5
|
+
`influxdb-async_queue` is an attempt to optimize influxdb interactions by aggregating metrics into batches through redis queue.
|
6
|
+
|
7
|
+
The gem consists of 2 major components:
|
8
|
+
1. `InflunxDB::AsyncQueue::Queue` responds to `write_point`/`write_points` and could be used instead of `InfluxDB::Client`
|
9
|
+
3. `InfluxDB::AsyncQueue::Server` grabs a batch of points from a queue and sends them to influxdb in an infinite loop.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'influxdb-async_queue'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install influxdb-async_queue
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
### Configuration
|
30
|
+
|
31
|
+
Configure manually
|
32
|
+
```ruby
|
33
|
+
InfluxDB::AsyncQueue.configure do |config|
|
34
|
+
config.influxdb_database = 'test'
|
35
|
+
config.influxdb_hosts = ['localhost']
|
36
|
+
config.adapter_name = 'redis'
|
37
|
+
end
|
38
|
+
```
|
39
|
+
load configuration from file
|
40
|
+
```ruby
|
41
|
+
InfluxDB::AsyncQueue.load_config('path/to/file.yml')
|
42
|
+
```
|
43
|
+
|
44
|
+
```yml
|
45
|
+
batch_size: 100
|
46
|
+
sleep_timeout: 0
|
47
|
+
|
48
|
+
influxdb:
|
49
|
+
database: example
|
50
|
+
username: root
|
51
|
+
password: root
|
52
|
+
port: 8086
|
53
|
+
precision: ms
|
54
|
+
hosts:
|
55
|
+
- localhost
|
56
|
+
|
57
|
+
adapter:
|
58
|
+
name: redis
|
59
|
+
config:
|
60
|
+
-
|
61
|
+
host: localhost
|
62
|
+
port: 6379
|
63
|
+
db: 0
|
64
|
+
```
|
65
|
+
|
66
|
+
or combine both options
|
67
|
+
```ruby
|
68
|
+
InfluxDB::AsyncQueue.load_config('path/to/file.yml') do |config|
|
69
|
+
config.logger = Logger.new
|
70
|
+
config.influxdb_database = 'test'
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
#### Writing points
|
75
|
+
|
76
|
+
Use `InfluxDB::AsyncQueue::Queue` instead of `InfluxDB::Client` to push points into queue
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
client = InfluxDB::AsyncQueue::Client.queue
|
80
|
+
# or
|
81
|
+
client = InfluxDB::AsyncQueue::Client::Queue.new(
|
82
|
+
InfluxDB::AsyncQueue::Adapters::Redis.new('redis://localhost:6379/0', 'redis-keys')
|
83
|
+
)
|
84
|
+
|
85
|
+
client.write_point('series_name', tags: { tag: 1 }, values: { value: 10 })
|
86
|
+
client.write_points([
|
87
|
+
{ series: 'series_name', tags: { tag: 1 }, values: { value: 10 } },
|
88
|
+
{ series: 'series_name', tags: { tag: 2 }, values: { value: 15 } }
|
89
|
+
])
|
90
|
+
```
|
91
|
+
|
92
|
+
Points will be serialized into line protocole and pushed into redis queue.
|
93
|
+
|
94
|
+
#### Sending points to influxdb
|
95
|
+
|
96
|
+
Start the server in a separate process
|
97
|
+
```
|
98
|
+
InfluxDB::AsyncQueue.load_config('path/to/file.yml')
|
99
|
+
InfluxDB::AsyncQueue::Server.run
|
100
|
+
```
|
101
|
+
|
102
|
+
#### Complete example (rails app)
|
103
|
+
|
104
|
+
```ruby
|
105
|
+
# bin/influxdb-async_queue
|
106
|
+
|
107
|
+
#!/usr/bin/env ruby
|
108
|
+
# frozen_string_literal: true
|
109
|
+
|
110
|
+
env = ENV['RUN_ENV'] || 'development'
|
111
|
+
puts "env - #{env}"
|
112
|
+
|
113
|
+
require 'influxdb/async_queue'
|
114
|
+
require 'influxdb/async_queue/server'
|
115
|
+
|
116
|
+
require 'active_support/logger'
|
117
|
+
|
118
|
+
config_path = File.expand_path("../config/influxdb-async_queue/#{env}.yml", __dir__)
|
119
|
+
$stdout.sync = true
|
120
|
+
InfluxDB::AsyncQueue.configure(config_path) do |c|
|
121
|
+
c.logger = ActiveSupport::Logger.new($stdout)
|
122
|
+
end
|
123
|
+
|
124
|
+
InfluxDB::AsyncQueue::Server.run
|
125
|
+
```
|
126
|
+
|
127
|
+
```
|
128
|
+
# config/upstart/Procfile.production
|
129
|
+
|
130
|
+
influxdb_async_queue: cd /srv/projects/mtsrbt_cms/current && env RUN_ENV=production bundle exec ruby bin/influxdb-async_queue.rb
|
131
|
+
```
|
132
|
+
|
133
|
+
```ruby
|
134
|
+
# config/initializers/metrics.rb
|
135
|
+
|
136
|
+
ActiveSupport::Notifications.subscribe "process_action.action_controller" do |_name, start, finish, id, payload|
|
137
|
+
controller_runtime = ((finish - start)*1000).ceil
|
138
|
+
view_runtime = (payload[:view_runtime] || 0).ceil
|
139
|
+
db_runtime = (payload[:db_runtime] || 0).ceil
|
140
|
+
|
141
|
+
client = InfluxDB::AsyncQueue.queue
|
142
|
+
tags = { method: "#{payload[:controller]}##{payload[:action]}", server: Socket.gethostname }
|
143
|
+
client.write_point 'rails.controller', values: { value: controller_runtime }, tags: tags
|
144
|
+
client.write_point 'rails.view', values: { value: view_runtime }, tags: tags
|
145
|
+
client.write_point 'rails.db', values: { value: db_runtime }, tags: tags
|
146
|
+
end
|
147
|
+
```
|
148
|
+
|
149
|
+
#### Define an adapter for your favorite storage
|
150
|
+
|
151
|
+
```ruby
|
152
|
+
class InfxluDB::AsyncQueue::Adapters::MyAdapter
|
153
|
+
|
154
|
+
def initialize(arg1, arg2)
|
155
|
+
end
|
156
|
+
|
157
|
+
def push(array_of_points)
|
158
|
+
# ...
|
159
|
+
end
|
160
|
+
|
161
|
+
def pop(limit)
|
162
|
+
# ...
|
163
|
+
end
|
164
|
+
end
|
165
|
+
```
|
166
|
+
|
167
|
+
configure `influxdb-asynq_queue` to use it instead of redis:
|
168
|
+
|
169
|
+
```ruby
|
170
|
+
InfluxDB::AsyncQueue.configure do |config|
|
171
|
+
config.adapter = InfluxDB::AsyncQueue::Adapters::MyAdapter.new('foo', 'bar')
|
172
|
+
end
|
173
|
+
```
|
174
|
+
or
|
175
|
+
```
|
176
|
+
adapter:
|
177
|
+
name: my_adapter
|
178
|
+
config: ['foo', 'bar']
|
179
|
+
```
|
180
|
+
|
181
|
+
## Development
|
182
|
+
|
183
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
184
|
+
|
185
|
+
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).
|
186
|
+
|
187
|
+
## Contributing
|
188
|
+
|
189
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/mugimaru/influxdb-async_queue. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
190
|
+
|
191
|
+
|
192
|
+
## License
|
193
|
+
|
194
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "influxdb/async_queue"
|
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,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'influxdb/async_queue/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "influxdb-async_queue"
|
8
|
+
spec.version = InfluxDB::AsyncQueue::VERSION
|
9
|
+
spec.authors = ["mugimaru"]
|
10
|
+
spec.email = ["pochi.73@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Asynchronously write influxdb metrics with redis}
|
13
|
+
spec.description = %q{influxdb-async_queue is an attempt to optimize influxdb interactions by aggregating metrics into batches through redis queue.}
|
14
|
+
spec.homepage = "https://github.com/mugimaru/influxdb-async_queue"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_runtime_dependency 'activesupport'
|
25
|
+
spec.add_runtime_dependency 'influxdb'
|
26
|
+
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
30
|
+
spec.add_development_dependency "redis"
|
31
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'redis'
|
2
|
+
|
3
|
+
module InfluxDB
|
4
|
+
module AsyncQueue
|
5
|
+
module Adapters
|
6
|
+
class Redis
|
7
|
+
DEFAULT_QUEUE_NAME = 'influxdb.async_queue'.freeze
|
8
|
+
|
9
|
+
def initialize(redis, queue_name = nil)
|
10
|
+
@redis =
|
11
|
+
if redis.is_a?(::Redis)
|
12
|
+
redis
|
13
|
+
elsif redis.is_a?(String)
|
14
|
+
::Redis.new(url: redis)
|
15
|
+
elsif redis.is_a?(Hash)
|
16
|
+
::Redis.new(redis)
|
17
|
+
else
|
18
|
+
raise ArgumentError
|
19
|
+
end
|
20
|
+
|
21
|
+
@queue_name = queue_name || DEFAULT_QUEUE_NAME
|
22
|
+
end
|
23
|
+
attr_reader :redis, :queue_name
|
24
|
+
|
25
|
+
def pop(batch_size)
|
26
|
+
redis.multi do |r|
|
27
|
+
r.lrange(queue_name, 0, batch_size - 1)
|
28
|
+
r.ltrim(queue_name, batch_size, -1)
|
29
|
+
end.first
|
30
|
+
end
|
31
|
+
|
32
|
+
def push(*points)
|
33
|
+
redis.rpush(queue_name, points)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module InfluxDB
|
2
|
+
module AsyncQueue
|
3
|
+
module Adapters
|
4
|
+
class TestAdapter
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@array = []
|
8
|
+
end
|
9
|
+
attr_reader :array
|
10
|
+
|
11
|
+
def push(*points)
|
12
|
+
array.push(*points)
|
13
|
+
end
|
14
|
+
|
15
|
+
def pop(batch_size)
|
16
|
+
array.shift(batch_size)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
module InfluxDB
|
5
|
+
module AsyncQueue
|
6
|
+
class Config
|
7
|
+
ATTRIBUTES = {
|
8
|
+
'batch_size' => 1000,
|
9
|
+
'sleep_timeout' => 5,
|
10
|
+
'influxdb/database' => nil,
|
11
|
+
'influxdb/username' => 'root',
|
12
|
+
'influxdb/password' => 'root',
|
13
|
+
'influxdb/port' => 8086,
|
14
|
+
'influxdb/precision' => 'ms',
|
15
|
+
'influxdb/retention_policy' => nil,
|
16
|
+
'influxdb/hosts' => ['localhost'],
|
17
|
+
'adapter/name' => 'redis',
|
18
|
+
'adapter/config' => [{ host: 'localhost', port: 6379, db: 0 }]
|
19
|
+
}.freeze
|
20
|
+
|
21
|
+
def self.load_from_file!(path)
|
22
|
+
new(
|
23
|
+
YAML.load(File.open(path))
|
24
|
+
)
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize(config = {})
|
28
|
+
@config = Hash[config.map { |k,v| [k.to_s, v] }]
|
29
|
+
@config['adapter'] ||= {}
|
30
|
+
@config['influxdb'] ||= {}
|
31
|
+
end
|
32
|
+
attr_writer :adapter, :logger, :influxdb_client
|
33
|
+
|
34
|
+
ATTRIBUTES.each do |attr, default|
|
35
|
+
attr_name = attr.gsub('/', '_')
|
36
|
+
hash_keys = attr.split('/')
|
37
|
+
|
38
|
+
define_method(attr_name) do
|
39
|
+
@config.dig(*hash_keys) || default
|
40
|
+
end
|
41
|
+
|
42
|
+
define_method("#{attr_name}=") do |v|
|
43
|
+
hash_keys[0..-2].inject(@config, :fetch)[hash_keys.last] = v
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def adapter
|
48
|
+
@adapter ||= ::InfluxDB::AsyncQueue::Adapters.const_get(adapter_name.camelize).new(
|
49
|
+
*(adapter_config || [])
|
50
|
+
)
|
51
|
+
end
|
52
|
+
|
53
|
+
def logger
|
54
|
+
@logger ||= Logger.new($stdout)
|
55
|
+
end
|
56
|
+
|
57
|
+
def influxdb_client
|
58
|
+
@influxdb_client ||= ::InfluxDB::Client.new(
|
59
|
+
username: influxdb_username,
|
60
|
+
password: influxdb_password,
|
61
|
+
hosts: influxdb_hosts,
|
62
|
+
port: influxdb_port,
|
63
|
+
async: false,
|
64
|
+
retry: true,
|
65
|
+
time_precision: influxdb_precision
|
66
|
+
)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'influxdb'
|
2
|
+
|
3
|
+
module InfluxDB
|
4
|
+
module AsyncQueue
|
5
|
+
class Queue
|
6
|
+
|
7
|
+
def initialize(adapter)
|
8
|
+
@adapter = adapter
|
9
|
+
end
|
10
|
+
attr_reader :adapter
|
11
|
+
|
12
|
+
def write_point(series_name, data)
|
13
|
+
push(
|
14
|
+
serialize_point(
|
15
|
+
data.merge(series: series_name)
|
16
|
+
)
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
def write_points(data)
|
21
|
+
push(
|
22
|
+
*data.map { |point| serialize_point(point) }
|
23
|
+
)
|
24
|
+
end
|
25
|
+
|
26
|
+
def push(*args)
|
27
|
+
adapter.push(*args)
|
28
|
+
end
|
29
|
+
|
30
|
+
def pop(*args)
|
31
|
+
adapter.pop(*args)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def serialize_point(point)
|
37
|
+
p = point.dup
|
38
|
+
p[:timestamp] = (Time.now.to_f * 1000).ceil unless p[:timestamp]
|
39
|
+
::InfluxDB::PointValue.new(p).dump
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'influxdb'
|
2
|
+
|
3
|
+
module InfluxDB
|
4
|
+
module AsyncQueue
|
5
|
+
class Server
|
6
|
+
|
7
|
+
def self.run(*args)
|
8
|
+
new(*args).run
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(config = nil)
|
12
|
+
@config = config || ::InfluxDB::AsyncQueue.config
|
13
|
+
@queue = ::InfluxDB::AsyncQueue::Queue.new(@config.adapter)
|
14
|
+
end
|
15
|
+
attr_reader :config, :queue
|
16
|
+
|
17
|
+
def run
|
18
|
+
config.logger.info("Start #{self.inspect}")
|
19
|
+
points = nil
|
20
|
+
|
21
|
+
loop do
|
22
|
+
next unless iterate(points)
|
23
|
+
points = nil
|
24
|
+
end
|
25
|
+
rescue Interrupt
|
26
|
+
config.logger.info('Interrupted; exit(0)'.freeze)
|
27
|
+
queue.push(points) if points
|
28
|
+
exit(0)
|
29
|
+
end
|
30
|
+
|
31
|
+
def iterate(points)
|
32
|
+
points = queue.pop(config.batch_size)
|
33
|
+
if points.empty?
|
34
|
+
config.logger.debug 'Queue is empty; sleep'.freeze
|
35
|
+
Kernel.sleep(config.sleep_timeout)
|
36
|
+
return false
|
37
|
+
end
|
38
|
+
|
39
|
+
config.logger.info("Going to write #{points.size} points")
|
40
|
+
config.influxdb_client.write(
|
41
|
+
points.join("\n"),
|
42
|
+
config.influxdb_precision,
|
43
|
+
config.influxdb_retention_policy.presence,
|
44
|
+
config.influxdb_database
|
45
|
+
)
|
46
|
+
true
|
47
|
+
rescue InfluxDB::Error => e
|
48
|
+
config.logger.error(e.inspect)
|
49
|
+
queue.push(points) if points
|
50
|
+
true
|
51
|
+
rescue => e
|
52
|
+
config.logger.error(e.inspect)
|
53
|
+
raise
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'active_support/inflector'
|
2
|
+
require 'active_support/core_ext/object/blank'
|
3
|
+
|
4
|
+
require "influxdb/async_queue/version"
|
5
|
+
require "influxdb/async_queue/config"
|
6
|
+
require "influxdb/async_queue/queue"
|
7
|
+
require "influxdb/async_queue/adapters/redis"
|
8
|
+
|
9
|
+
module InfluxDB
|
10
|
+
module AsyncQueue
|
11
|
+
|
12
|
+
def self.load_config(path)
|
13
|
+
@config = ::InfluxDB::AsyncQueue::Config.load_from_file! path
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.configure(file = nil)
|
17
|
+
load_config(file) if file
|
18
|
+
yield config
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.config
|
22
|
+
@config ||= ::InfluxDB::AsyncQueue::Config.new
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.config=(config)
|
26
|
+
@config = config
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.queue
|
30
|
+
@queue ||= ::InfluxDB::AsyncQueue::Queue.new(config.adapter)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: influxdb-async_queue
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- mugimaru
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: influxdb
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
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: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.13'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.13'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: redis
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: influxdb-async_queue is an attempt to optimize influxdb interactions
|
98
|
+
by aggregating metrics into batches through redis queue.
|
99
|
+
email:
|
100
|
+
- pochi.73@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rspec"
|
107
|
+
- ".travis.yml"
|
108
|
+
- CODE_OF_CONDUCT.md
|
109
|
+
- Gemfile
|
110
|
+
- LICENSE.txt
|
111
|
+
- README.md
|
112
|
+
- Rakefile
|
113
|
+
- bin/console
|
114
|
+
- bin/setup
|
115
|
+
- influxdb-async_queue.gemspec
|
116
|
+
- lib/influxdb/async_queue.rb
|
117
|
+
- lib/influxdb/async_queue/adapters/redis.rb
|
118
|
+
- lib/influxdb/async_queue/adapters/test_adapter.rb
|
119
|
+
- lib/influxdb/async_queue/config.rb
|
120
|
+
- lib/influxdb/async_queue/queue.rb
|
121
|
+
- lib/influxdb/async_queue/server.rb
|
122
|
+
- lib/influxdb/async_queue/version.rb
|
123
|
+
homepage: https://github.com/mugimaru/influxdb-async_queue
|
124
|
+
licenses:
|
125
|
+
- MIT
|
126
|
+
metadata: {}
|
127
|
+
post_install_message:
|
128
|
+
rdoc_options: []
|
129
|
+
require_paths:
|
130
|
+
- lib
|
131
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - ">="
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
141
|
+
requirements: []
|
142
|
+
rubyforge_project:
|
143
|
+
rubygems_version: 2.5.1
|
144
|
+
signing_key:
|
145
|
+
specification_version: 4
|
146
|
+
summary: Asynchronously write influxdb metrics with redis
|
147
|
+
test_files: []
|
148
|
+
has_rdoc:
|