Skip to content

Commit 8e70e7a

Browse files
ChrisBrhennevogel
authored andcommitted
Support InfluxDB 2.0
1 parent db23bb0 commit 8e70e7a

27 files changed

+271
-193
lines changed

influxdb-rails.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,13 @@ Gem::Specification.new do |spec|
2424

2525
spec.required_ruby_version = ">= 3.0"
2626

27-
spec.add_runtime_dependency "influxdb", "~> 0.6", ">= 0.6.4"
27+
spec.add_runtime_dependency "influxdb-client"
2828
spec.add_runtime_dependency "railties", ">= 5.0"
2929

3030
spec.add_development_dependency "actionmailer"
3131
spec.add_development_dependency "activejob"
3232
spec.add_development_dependency "activerecord"
3333
spec.add_development_dependency "bundler", ">= 1.0.0"
34-
spec.add_development_dependency "fakeweb"
3534
spec.add_development_dependency "launchy"
3635
spec.add_development_dependency "pry"
3736
spec.add_development_dependency "rake"
@@ -43,4 +42,5 @@ Gem::Specification.new do |spec|
4342
spec.add_development_dependency "rubocop-rspec"
4443
spec.add_development_dependency "sqlite3"
4544
spec.add_development_dependency "tzinfo"
45+
spec.add_development_dependency "webmock"
4646
end

lib/influxdb-rails.rb

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
require "net/https"
33
require "rubygems"
44
require "socket"
5+
require "influxdb-client"
56
require "influxdb/rails/middleware/block_instrumentation_subscriber"
67
require "influxdb/rails/middleware/render_subscriber"
78
require "influxdb/rails/middleware/request_subscriber"
@@ -21,41 +22,48 @@ module InfluxDB
2122
# InfluxDB and Rails. This is a singleton class.
2223
module Rails
2324
class << self
24-
attr_writer :configuration, :client
25+
attr_writer :configuration, :client, :write_api
2526

2627
def configure
27-
return configuration unless block_given?
28+
yield configuration if block_given?
2829

29-
yield configuration
30-
self.client = nil # if we change configuration, reload the client
30+
# if we change configuration, reload the client
31+
self.client = nil
32+
self.write_api = nil
33+
34+
configuration
35+
end
36+
37+
def configuration
38+
@configuration ||= InfluxDB::Rails::Configuration.new
3139
end
3240

3341
# rubocop:disable Metrics/MethodLength
3442

3543
def client
3644
@client ||= begin
3745
cfg = configuration.client
38-
InfluxDB::Client.new \
39-
database: cfg.database,
40-
username: cfg.username,
41-
password: cfg.password,
42-
auth_method: cfg.auth_method,
43-
hosts: cfg.hosts,
44-
port: cfg.port,
45-
async: cfg.async,
46-
use_ssl: cfg.use_ssl,
47-
retry: cfg.retry,
48-
open_timeout: cfg.open_timeout,
49-
read_timeout: cfg.read_timeout,
50-
max_delay: cfg.max_delay,
51-
time_precision: cfg.time_precision
46+
InfluxDB2::Client.new(
47+
cfg.url,
48+
cfg.token,
49+
org: cfg.org,
50+
bucket: cfg.bucket,
51+
precision: cfg.precision,
52+
open_timeout: cfg.open_timeout,
53+
write_timeout: cfg.write_timeout,
54+
read_timeout: cfg.read_timeout,
55+
use_ssl: cfg.use_ssl,
56+
logger: ::Rails.logger,
57+
async: cfg.async,
58+
retries: cfg.retries
59+
)
5260
end
5361
end
5462

5563
# rubocop:enable Metrics/MethodLength
5664

57-
def configuration
58-
@configuration ||= InfluxDB::Rails::Configuration.new
65+
def write_api
66+
@write_api ||= client.create_write_api(write_options: configuration.client.write_options)
5967
end
6068

6169
def current

lib/influxdb/rails/configuration.rb

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,37 @@ def load_defaults
2828
class ClientConfig
2929
include Configurable
3030

31+
WRITE_TYPE = {
32+
true => ::InfluxDB2::WriteType::BATCHING,
33+
false => ::InfluxDB2::WriteType::SYNCHRONOUS,
34+
}.freeze
35+
private_constant :WRITE_TYPE
36+
3137
set_defaults(
32-
hosts: ["localhost"].freeze,
33-
port: 8086,
34-
username: "root".freeze,
35-
password: "root".freeze,
36-
database: nil,
37-
auth_method: "params".freeze,
38-
async: true,
39-
use_ssl: false,
40-
retry: nil,
41-
open_timeout: 5,
42-
read_timeout: 300,
43-
max_delay: 30,
44-
time_precision: "s".freeze
38+
url: "http://localhost:8086".freeze,
39+
token: nil,
40+
org: nil,
41+
bucket: nil,
42+
use_ssl: true,
43+
open_timeout: 5.seconds,
44+
write_timeout: 5.seconds,
45+
read_timeout: 60.seconds,
46+
precision: ::InfluxDB2::WritePrecision::MILLISECOND,
47+
retries: 0,
48+
async: true,
49+
max_retry_delay_ms: 10.seconds.to_i * 1000
4550
)
4651

4752
def initialize
4853
load_defaults
4954
end
55+
56+
def write_options
57+
InfluxDB2::WriteOptions.new(
58+
write_type: WRITE_TYPE[async],
59+
max_retries: retries
60+
)
61+
end
5062
end
5163
private_constant :ClientConfig
5264

@@ -69,36 +81,6 @@ class Configuration
6981
# configuration passed to InfluxDB::Client
7082
attr_reader :client
7183

72-
# FIXME: Old configuration options, remove this in 1.0.1
73-
attr_writer \
74-
:series_name_for_controller_runtimes,
75-
:series_name_for_view_runtimes,
76-
:series_name_for_db_runtimes,
77-
:series_name_for_render_template,
78-
:series_name_for_render_partial,
79-
:series_name_for_render_collection,
80-
:series_name_for_sql,
81-
:series_name_for_exceptions,
82-
:series_name_for_instrumentation,
83-
:ignored_exceptions,
84-
:ignored_exception_messages,
85-
:ignored_user_agents,
86-
:application_root,
87-
:environment_variable_filters,
88-
:backtrace_filters,
89-
:influxdb_database,
90-
:influxdb_username,
91-
:influxdb_password,
92-
:influxdb_hosts,
93-
:influxdb_port,
94-
:async,
95-
:use_ssl,
96-
:retry,
97-
:open_timeout,
98-
:read_timeout,
99-
:max_delay,
100-
:time_precision
101-
10284
def initialize
10385
@client = ClientConfig.new
10486
load_defaults

lib/influxdb/rails/helpers/rspec_matchers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def metrics
3434
InfluxDB::Rails.instance_variable_set :@configuration, nil
3535
InfluxDB::Rails.configure
3636

37-
allow(InfluxDB::Rails).to receive(:client).and_return(InfluxDB::Rails::TestClient.new)
37+
InfluxDB::Rails.client = InfluxDB::Rails::TestClient.new
3838
allow_any_instance_of(InfluxDB::Rails::Configuration)
3939
.to receive(:ignored_environments).and_return(%w[development])
4040

lib/influxdb/rails/metric.rb

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
require "influxdb/rails/values"
21
require "influxdb/rails/tags"
32

43
module InfluxDB
@@ -12,27 +11,24 @@ def initialize(configuration:, timestamp:, tags: {}, values: {})
1211
end
1312

1413
def write
15-
client.write_point configuration.measurement_name, options
14+
write_api.write(data: data)
1615
end
1716

1817
private
1918

2019
attr_reader :configuration, :tags, :values, :timestamp
2120

22-
def options
21+
def data
2322
{
24-
values: Values.new(values: values).to_h,
25-
tags: Tags.new(tags: tags, config: configuration).to_h,
26-
timestamp: timestamp_with_precision,
23+
fields: values.merge(InfluxDB::Rails.current.values),
24+
tags: Tags.new(tags: tags, config: configuration).to_h,
25+
name: configuration.measurement_name,
26+
time: timestamp.utc,
2727
}
2828
end
2929

30-
def timestamp_with_precision
31-
InfluxDB.convert_timestamp(timestamp.utc, configuration.client.time_precision)
32-
end
33-
34-
def client
35-
InfluxDB::Rails.client
30+
def write_api
31+
InfluxDB::Rails.write_api
3632
end
3733
end
3834
end

lib/influxdb/rails/middleware/request_subscriber.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def values
2828
controller: duration,
2929
view: (payload[:view_runtime] || 0).ceil,
3030
db: (payload[:db_runtime] || 0).ceil,
31-
started: started,
31+
started: start.utc,
3232
}
3333
end
3434

lib/influxdb/rails/railtie.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require "influxdb"
1+
require "influxdb-client"
22
require "rails"
33

44
module InfluxDB

lib/influxdb/rails/tags.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,13 @@ def initialize(config:, tags: {}, additional_tags: InfluxDB::Rails.current.tags)
88
end
99

1010
def to_h
11-
expanded_tags.reject do |_, value|
12-
value.to_s.blank?
13-
end
11+
config.tags_middleware.call(tags.merge(default_tags))
1412
end
1513

1614
private
1715

1816
attr_reader :additional_tags, :tags, :config
1917

20-
def expanded_tags
21-
config.tags_middleware.call(tags.merge(default_tags))
22-
end
23-
2418
def default_tags
2519
{
2620
server: Socket.gethostname,

lib/influxdb/rails/test_client.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ class TestClient
55
[]
66
end
77

8-
def write_point(name, options = {})
9-
metrics << options.merge(name: name)
8+
def write(options = {})
9+
metrics << options[:data]
10+
end
11+
12+
def create_write_api(*)
13+
self
1014
end
1115
end
1216
end

lib/rails/generators/influxdb/templates/initializer.rb

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,24 @@
11
InfluxDB::Rails.configure do |config|
2-
## The only setting you actually need to update is the name of the
3-
## database within the InfluxDB server instance. Don't forget to
4-
## create this database as well.
5-
config.client.database = "rails"
6-
# config.client.hosts = ["localhost"]
7-
# config.client.port = 8086
8-
9-
## If you've setup user authentication (and activated it in the server
10-
## config), you need to configure the credentials here.
11-
# config.client.username = "root"
12-
# config.client.password = "root"
13-
14-
## If your InfluxDB service requires an HTTPS connection then you can
15-
## enable it here.
16-
# config.client.use_ssl = true
2+
## The only settings you actually need to set are org, bucket and token.
3+
config.client.bucket = "bucket"
4+
config.client.org = "org"
5+
config.client.token = "token"
6+
# config.client.url = "localhost:8086",
177

188
## Various other client and connection options. These are used to create
19-
## an `InfluxDB::Client` instance (i.e. `InfluxDB::Rails.client`).
9+
## an `InfluxDB2::Client` instance (i.e. `InfluxDB::Rails.client`).
2010
##
21-
## See docs for the influxdb gem for the canonical list of options:
22-
## https://github.com/influxdata/influxdb-ruby#list-of-configuration-options
11+
## See docs for the influxdb-client gem for the canonical list of options:
12+
## https://github.com/influxdata/influxdb-client-ruby#creating-a-client
2313
##
24-
## These defaults for the influxdb-rails gem deviate from the default
25-
## for the influxdb gem:
26-
# config.client.async = true # false
27-
# config.client.read_timeout = 30 # 300
28-
# config.client.max_delay = 300 # 30
29-
# config.client.time_precision = "ms" # "s"
30-
31-
## Prior to 1.0.0, this gem has written all data points in different
32-
## measurements (the config options were named `series_name_for_*`).
33-
## Since 1.0.0.beta3, we're now using a single measurements
34-
# config.measurement_name = "rails"
14+
# config.client.use_ssql = true
15+
# config.client.open_timeout = 5.seconds
16+
# config.client.write_timeout = 5.seconds
17+
# config.client.read_timeout = 60.seconds
18+
# config.client.time_precisions = InfluxDB2::WritePrecision::MILLISECOND
19+
# config.client.retries = 0
20+
# config.client.max_retry_delay_ms = 10_000
21+
# config.client.async = true
3522

3623
## Disable rails framework hooks.
3724
# config.ignored_hooks = ['sql.active_record', 'render_template.action_view']
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require "#{File.dirname(__FILE__)}/../spec_helper"
2+
3+
RSpec.describe "InfluxDB Rails sends metrics" do
4+
it "does a HTTP request to InfluxDB server with correct tags and fields" do
5+
travel_to Time.zone.local(2018, 1, 1, 9, 0, 0)
6+
configure("bucket", "org")
7+
stub_hostname("hostname")
8+
request = write_request.with(
9+
query: hash_including(bucket: "bucket", org: "org", precision: "ms"),
10+
body: "rails,hook=block_instrumentation,location=raw,name=name,server=hostname value=1i 1514797200000000000"
11+
)
12+
13+
InfluxDB::Rails.instrument "name", values: { value: 1 }
14+
15+
assert_requested(request, times: 1)
16+
end
17+
18+
private
19+
20+
def configure(bucket, org)
21+
InfluxDB::Rails.configure do |config|
22+
config.client.token = "my-token"
23+
config.client.bucket = bucket
24+
config.client.org = org
25+
config.client.async = false
26+
end
27+
end
28+
29+
def write_request
30+
stub_request(:post, "https://localhost:8086/api/v2/write")
31+
end
32+
33+
def stub_hostname(name)
34+
allow(Socket).to receive(:gethostname).and_return(name)
35+
end
36+
end

0 commit comments

Comments
 (0)