fluent-plugin-timescaledb 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,17 +13,21 @@ module Fluent
13
13
  super
14
14
 
15
15
  @conn = PG.connect(@db_conn_string)
16
- @conn.prepare('insert_record', "INSERT INTO \"#{@db_table_name}\" (time, tag, record) VALUES ($1, $2, $3::jsonb)")
17
16
  end
18
17
 
19
18
  def close
20
- @conn.close if @conn
19
+ @conn.close if @conn and !@conn.finished?
21
20
  end
22
21
 
23
22
  def write(chunk)
23
+ reconnect_if_connection_bad!
24
+
25
+ values = []
24
26
  chunk.msgpack_each do | tag, time, record |
25
- @conn.exec_prepared('insert_record', [Time.at(time.to_f), tag, record.to_json])
27
+ values << "('#{@conn.escape_string(format_time(time))}','#{@conn.escape_string(tag)}','#{@conn.escape_string(record.to_json)}'::jsonb)"
26
28
  end
29
+
30
+ @conn.exec("INSERT INTO #{@conn.escape_identifier(@db_table_name)} (time, tag, record) VALUES #{values.join(',')}")
27
31
  end
28
32
 
29
33
  def format(tag, time, record)
@@ -33,6 +37,20 @@ module Fluent
33
37
  def formatted_to_msgpack_binary
34
38
  true
35
39
  end
40
+
41
+ private
42
+
43
+ TIME_FORMAT = "%Y-%m-%d %H:%M:%S.%N".freeze
44
+
45
+ def format_time(time)
46
+ Time.at(time.to_f).utc.strftime(TIME_FORMAT)
47
+ end
48
+
49
+ def reconnect_if_connection_bad!
50
+ if @conn.status == PG::CONNECTION_BAD
51
+ @conn.reset
52
+ end
53
+ end
36
54
  end
37
55
  end
38
56
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-timescaledb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-04-14 00:00:00.000000000 Z
12
+ date: 2019-04-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pg