influxdb-client 1.7.0.pre.1015 → 1.8.0.pre.1240

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8f7d3acebd47909037578d03e9c41a563328fc8a112019ef705417f4ce0b5dc7
4
- data.tar.gz: f5fd4db9b000fb952eedd3ef7cbdc93fec3b5806101d819fd5c9febc33b95a68
3
+ metadata.gz: e49b8841c9ce5b13c24595d8fec2b2ed6eb8fe6676b606b9a10ef851d8ea98a9
4
+ data.tar.gz: 328d60cf43f6d5c1a43e5ac22945a76175c2752b370a5ca155a7236aedbe79be
5
5
  SHA512:
6
- metadata.gz: 1da2d03a086ad92b8c7d82a29cdff3e228dd9140808df9b3ae1ed961670535ad8125d3345202b5dc6dad0f7d370057f24e8607f9bfc6479d756bbea61e04cebd
7
- data.tar.gz: 1fe5947810dff9c7d97ec8eefc4d2d54d8e67db663c42e2ae606747f444bd96b9083beb1439e27fb2d2fd1c1fc6bae055d3ffd9816f0819b85039eb47b37f7a8
6
+ metadata.gz: 5a20d45c4dad7031e96bedb7ac76c3f146a17d8bec00fad5eba2d6cba062bcef172b5c389dce7db589b8d5481256cc246d652c48019fb6087b4d49f88c45c7d6
7
+ data.tar.gz: 707f373b6a72790a4e34b40490077b2da5e2bd1b2fd703d48237a1a80a4b5fe1053bb2b4263fb8f586e37c85544ac3b292f22d9c8012f101e0db56f483ba617c
@@ -82,6 +82,8 @@ jobs:
82
82
  docker:
83
83
  - image: << parameters.ruby-image >>
84
84
  - image: &influx-image quay.io/influxdb/<< parameters.influxdb-image >>
85
+ environment:
86
+ INFLUXD_HTTP_BIND_ADDRESS: :8086
85
87
  steps:
86
88
  - prepare
87
89
  - test:
@@ -89,13 +91,8 @@ jobs:
89
91
  - storing-test-results
90
92
 
91
93
  deploy-preview:
92
- parameters:
93
- influxdb-image:
94
- type: string
95
- default: *default-influxdb-image
96
94
  docker:
97
95
  - image: *default-ruby-image
98
- - image: *influx-image
99
96
  steps:
100
97
  - run:
101
98
  name: Early return if this build is from a forked repository
@@ -39,3 +39,5 @@ Metrics/CyclomaticComplexity:
39
39
  Max: 15
40
40
  Metrics/PerceivedComplexity:
41
41
  Max: 15
42
+ Metrics/ParameterLists:
43
+ Max: 10
@@ -1,4 +1,18 @@
1
- ## 1.7.0 [unreleased]
1
+ ## 1.8.0 [unreleased]
2
+
3
+ ### Features
4
+ 1. [#36](https://github.com/influxdata/influxdb-client-ruby/issues/36): Added support for default tags
5
+
6
+ ### API
7
+ 1. [#50](https://github.com/influxdata/influxdb-client-ruby/pull/50): Default port changed from 9999 -> 8086
8
+
9
+ ### Bug Fixes
10
+ 1. [#52](https://github.com/influxdata/influxdb-client-ruby/pull/52): Fixed aborting of background threads
11
+
12
+ ## 1.7.0 [2020-08-14]
13
+
14
+ ### Features
15
+ 1. [#47](https://github.com/influxdata/influxdb-client-ruby/pull/47): Added max_retries, max_retry_delay and exponential_base to WriteApi
2
16
 
3
17
  ## 1.6.0 [2020-07-17]
4
18
 
data/README.md CHANGED
@@ -23,7 +23,7 @@ The client can be installed manually or with bundler.
23
23
  To install the client gem manually:
24
24
 
25
25
  ```
26
- gem install influxdb-client -v 1.6.0
26
+ gem install influxdb-client -v 1.7.0
27
27
  ```
28
28
 
29
29
  ## Usage
@@ -33,7 +33,7 @@ gem install influxdb-client -v 1.6.0
33
33
  Use **InfluxDB::Client** to create a client connected to a running InfluxDB 2 instance.
34
34
 
35
35
  ```ruby
36
- client = InfluxDB2::Client.new('https://localhost:9999', 'my-token')
36
+ client = InfluxDB2::Client.new('https://localhost:8086', 'my-token')
37
37
  ```
38
38
 
39
39
  #### Client Options
@@ -50,7 +50,7 @@ client = InfluxDB2::Client.new('https://localhost:9999', 'my-token')
50
50
  | use_ssl | Turn on/off SSL for HTTP communication | bool | true |
51
51
 
52
52
  ```ruby
53
- client = InfluxDB2::Client.new('https://localhost:9999', 'my-token',
53
+ client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
54
54
  bucket: 'my-bucket',
55
55
  org: 'my-org',
56
56
  precision: InfluxDB2::WritePrecision::NANOSECOND)
@@ -68,7 +68,7 @@ The result retrieved by [QueryApi](https://github.com/influxdata/influxdb-client
68
68
 
69
69
  Synchronously executes the Flux query and return result as unprocessed String
70
70
  ```ruby
71
- client = InfluxDB2::Client.new('https://localhost:9999', 'my-token',
71
+ client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
72
72
  bucket: 'my-bucket',
73
73
  org: 'my-org')
74
74
 
@@ -78,7 +78,7 @@ result = query_api.query_raw(query: 'from(bucket:"' + bucket + '") |> range(star
78
78
  #### Synchronous query
79
79
  Synchronously executes the Flux query and return result as a Array of [FluxTables](https://github.com/influxdata/influxdb-client-ruby/blob/master/lib/influxdb2/client/flux_table.rb)
80
80
  ```ruby
81
- client = InfluxDB2::Client.new('https://localhost:9999', 'my-token',
81
+ client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
82
82
  bucket: 'my-bucket',
83
83
  org: 'my-org')
84
84
 
@@ -89,7 +89,7 @@ result = query_api.query(query: 'from(bucket:"' + bucket + '") |> range(start: 1
89
89
  #### Query stream
90
90
  Synchronously executes the Flux query and return stream of [FluxRecord](https://github.com/influxdata/influxdb-client-ruby/blob/master/lib/influxdb2/client/flux_table.rb)
91
91
  ```ruby
92
- client = InfluxDB2::Client.new('https://localhost:9999', 'my-token',
92
+ client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
93
93
  bucket: 'my-bucket',
94
94
  org: 'my-org')
95
95
 
@@ -107,7 +107,7 @@ end
107
107
  The [WriteApi](https://github.com/influxdata/influxdb-client-ruby/blob/master/lib/influxdb2/client/write_api.rb) supports synchronous and batching writes into InfluxDB 2.0. In default api uses synchronous write. To enable batching you can use WriteOption.
108
108
 
109
109
  ```ruby
110
- client = InfluxDB2::Client.new('https://localhost:9999', 'my-token',
110
+ client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
111
111
  bucket: 'my-bucket',
112
112
  org: 'my-org',
113
113
  precision: InfluxDB2::WritePrecision::NANOSECOND)
@@ -123,13 +123,17 @@ The writes are processed in batches which are configurable by `WriteOptions`:
123
123
  | --- | --- | --- |
124
124
  | batchSize | the number of data point to collect in batch | 1000 |
125
125
  | flush_interval | the number of milliseconds before the batch is written | 1000 |
126
- | retry_interval | the number of milliseconds to retry unsuccessful write. The retry interval is used when the InfluxDB server does not specify "Retry-After" header. | 1000 |
126
+ | retry_interval | the number of milliseconds to retry unsuccessful write. The retry interval is used when the InfluxDB server does not specify "Retry-After" header. | 5000 |
127
127
  | jitter_interval | the number of milliseconds to increase the batch flush interval by a random amount | 0 |
128
-
128
+ | max_retries | the number of max retries when write fails | 5 |
129
+ | max_retry_delay | maximum delay when retrying write in milliseconds | 180000 |
130
+ | exponential_base | the base for the exponential retry delay, the next delay is computed as `retry_interval * exponential_base^(attempts - 1) + random(jitter_interval)` | 5 |
129
131
  ```ruby
130
132
  write_options = InfluxDB2::WriteOptions.new(write_type: InfluxDB2::WriteType::BATCHING,
131
- batch_size: 10, flush_interval: 5_000)
132
- client = InfluxDB2::Client.new('http://localhost:9999',
133
+ batch_size: 10, flush_interval: 5_000,
134
+ max_retries: 3, max_retry_delay: 15_000,
135
+ exponential_base: 2)
136
+ client = InfluxDB2::Client.new('http://localhost:8086',
133
137
  'my-token',
134
138
  bucket: 'my-bucket',
135
139
  org: 'my-org',
@@ -144,7 +148,7 @@ write_api.write(data: 'h2o,location=west value=33i 15')
144
148
 
145
149
  Configure default time precision:
146
150
  ```ruby
147
- client = InfluxDB2::Client.new('https://localhost:9999', 'my-token',
151
+ client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
148
152
  bucket: 'my-bucket',
149
153
  org: 'my-org',
150
154
  precision: InfluxDB2::WritePrecision::NANOSECOND)
@@ -152,7 +156,7 @@ client = InfluxDB2::Client.new('https://localhost:9999', 'my-token',
152
156
 
153
157
  Configure precision per write:
154
158
  ```ruby
155
- client = InfluxDB2::Client.new('https://localhost:9999', 'my-token',
159
+ client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
156
160
  bucket: 'my-bucket',
157
161
  org: 'my-org')
158
162
 
@@ -169,7 +173,7 @@ Allowed values for precision are:
169
173
 
170
174
  Default `bucket` and `organization` destination are configured via `InfluxDB::Client`:
171
175
  ```ruby
172
- client = InfluxDB2::Client.new('https://localhost:9999', 'my-token',
176
+ client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
173
177
  bucket: 'my-bucket',
174
178
  org: 'my-org')
175
179
  ```
@@ -177,7 +181,7 @@ client = InfluxDB2::Client.new('https://localhost:9999', 'my-token',
177
181
  but there is also possibility to override configuration per write:
178
182
 
179
183
  ```ruby
180
- client = InfluxDB2::Client.new('https://localhost:9999', 'my-token')
184
+ client = InfluxDB2::Client.new('https://localhost:8086', 'my-token')
181
185
 
182
186
  write_api = client.create_write_api
183
187
  write_api.write(data: 'h2o,location=west value=33i 15', bucket: 'production-data', org: 'customer-1')
@@ -193,7 +197,7 @@ The data could be written as:
193
197
  1. `Array` of above items
194
198
 
195
199
  ```ruby
196
- client = InfluxDB2::Client.new('https://localhost:9999', 'my-token',
200
+ client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
197
201
  bucket: 'my-bucket',
198
202
  org: 'my-org',
199
203
  precision: InfluxDB2::WritePrecision::NANOSECOND)
@@ -210,12 +214,42 @@ write_api = client.create_write_api
210
214
  write_api.write(data: ['h2o,location=west value=33i 15', point, hash])
211
215
  ```
212
216
 
217
+ #### Default Tags
218
+
219
+ Sometimes is useful to store same information in every measurement e.g. `hostname`, `location`, `customer`.
220
+ The client is able to use static value, app settings or env variable as a tag value.
221
+
222
+ The expressions:
223
+ - `California Miner` - static value
224
+ - `${env.hostname}` - environment property
225
+
226
+ ##### Via API
227
+
228
+ ```ruby
229
+ client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
230
+ bucket: 'my-bucket',
231
+ org: 'my-org',
232
+ precision: InfluxDB2::WritePrecision::NANOSECOND,
233
+ use_ssl: false,
234
+ tags: { id: '132-987-655' })
235
+
236
+ point_settings = InfluxDB2::PointSettings.new(default_tags: { customer: 'California Miner' })
237
+ point_settings.add_default_tag('data_center', '${env.data_center}')
238
+
239
+ write_api = client.create_write_api(write_options: InfluxDB2::SYNCHRONOUS,
240
+ point_settings: point_settings)
241
+
242
+ write_api.write(data: InfluxDB2::Point.new(name: 'h2o')
243
+ .add_tag('location', 'europe')
244
+ .add_field('level', 2))
245
+ ```
246
+
213
247
  ### Delete data
214
248
 
215
249
  The [DeleteApi](https://github.com/influxdata/influxdb-client-ruby/blob/master/lib/influxdb2/client/delete_api.rb) supports deletes [points](https://v2.docs.influxdata.com/v2.0/reference/glossary/#point) from an InfluxDB bucket.
216
250
 
217
251
  ```ruby
218
- client = InfluxDB2::Client.new('http://localhost:9999', 'my-token',
252
+ client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
219
253
  bucket: 'my-bucket',
220
254
  org: 'my-org',
221
255
  precision: InfluxDB2::WritePrecision::NANOSECOND)
@@ -24,12 +24,12 @@
24
24
  set -e
25
25
 
26
26
  echo "Wait to start InfluxDB 2.0"
27
- wget -S --spider --tries=20 --retry-connrefused --waitretry=5 http://localhost:9999/metrics
27
+ wget -S --spider --tries=20 --retry-connrefused --waitretry=5 http://localhost:8086/metrics
28
28
 
29
29
  echo
30
30
  echo "Post onBoarding request, to setup initial user (my-user@my-password), org (my-org) and bucketSetup (my-bucket)"
31
31
  echo
32
- curl -i -X POST http://localhost:9999/api/v2/setup -H 'accept: application/json' \
32
+ curl -i -X POST http://localhost:8086/api/v2/setup -H 'accept: application/json' \
33
33
  -d '{
34
34
  "username": "my-user",
35
35
  "password": "my-password",
@@ -49,9 +49,10 @@ echo
49
49
  docker pull "${INFLUXDB_V2_IMAGE}" || true
50
50
  docker run \
51
51
  --detach \
52
+ --env INFLUXD_HTTP_BIND_ADDRESS=:8086 \
52
53
  --name influxdb_v2 \
53
54
  --network influx_network \
54
- --publish 9999:9999 \
55
+ --publish 8086:8086 \
55
56
  "${INFLUXDB_V2_IMAGE}"
56
57
 
57
58
  #
@@ -29,10 +29,10 @@ module InfluxDB2
29
29
  # Instantiate a new InfluxDB client.
30
30
  #
31
31
  # @example Instantiate a client.
32
- # InfluxDBClient::Client.new(url: 'https://localhost:9999', token: 'my-token')
32
+ # InfluxDBClient::Client.new(url: 'https://localhost:8086', token: 'my-token')
33
33
  #
34
34
  # @param [Hash] options The options to be used by the client.
35
- # @param [String] url InfluxDB URL to connect to (ex. https://localhost:9999).
35
+ # @param [String] url InfluxDB URL to connect to (ex. https://localhost:8086).
36
36
  # @param [String] token Access Token used for authenticating/authorizing the InfluxDB request sent by client.
37
37
  #
38
38
  # @option options [String] :bucket the default destination bucket for writes
@@ -43,6 +43,7 @@ module InfluxDB2
43
43
  # @option options [Integer] :read_timeout Number of seconds to wait for one block of data to be read
44
44
  # @option options [Integer] :max_redirect_count Maximal number of followed HTTP redirects
45
45
  # @option options [bool] :use_ssl Turn on/off SSL for HTTP communication
46
+ # @option options [Hash] :tags Default tags which will be added to each point written by api.
46
47
  # the body line-protocol
47
48
  def initialize(url, token, options = nil)
48
49
  @auto_closeable = []
@@ -57,8 +58,8 @@ module InfluxDB2
57
58
  # Write time series data into InfluxDB thought WriteApi.
58
59
  #
59
60
  # @return [WriteApi] New instance of WriteApi.
60
- def create_write_api(write_options: InfluxDB2::SYNCHRONOUS)
61
- write_api = WriteApi.new(options: @options, write_options: write_options)
61
+ def create_write_api(write_options: InfluxDB2::SYNCHRONOUS, point_settings: InfluxDB2::DEFAULT_POINT_SETTINGS)
62
+ write_api = WriteApi.new(options: @options, write_options: write_options, point_settings: point_settings)
62
63
  @auto_closeable.push(write_api)
63
64
  write_api
64
65
  end
@@ -83,6 +83,8 @@ module InfluxDB2
83
83
  else
84
84
  raise InfluxError.from_response(response)
85
85
  end
86
+ rescue *InfluxError::HTTP_ERRORS => error
87
+ raise InfluxError.from_error(error)
86
88
  ensure
87
89
  http.finish if http.started?
88
90
  end
@@ -1,19 +1,33 @@
1
1
  module InfluxDB2
2
2
  # InfluxError that is raised during HTTP communication.
3
3
  class InfluxError < StandardError
4
+ HTTP_ERRORS = [
5
+ EOFError,
6
+ Errno::ECONNREFUSED,
7
+ Errno::ECONNRESET,
8
+ Errno::EINVAL,
9
+ Net::HTTPBadResponse,
10
+ Net::HTTPHeaderSyntaxError,
11
+ Net::ProtocolError,
12
+ Timeout::Error
13
+ ].freeze
14
+
4
15
  # HTTP status code
5
16
  attr_reader :code
6
17
  # Reference code unique to the error type
7
18
  attr_reader :reference
8
19
  # The Retry-After header describes when to try the request again.
9
20
  attr_reader :retry_after
21
+ # original error
22
+ attr_reader :original
10
23
 
11
- def initialize(message:, code:, reference:, retry_after:)
24
+ def initialize(original = nil, message:, code:, reference:, retry_after:)
12
25
  super(message)
13
26
 
14
27
  @code = code
15
28
  @reference = reference
16
29
  @retry_after = retry_after
30
+ @original = original
17
31
  end
18
32
 
19
33
  def self.from_response(response)
@@ -27,5 +41,10 @@ module InfluxDB2
27
41
  obj = new(message: message, code: '', reference: '', retry_after: '')
28
42
  obj
29
43
  end
44
+
45
+ def self.from_error(error)
46
+ obj = new(error, message: error.message, code: '', reference: '', retry_after: '')
47
+ obj
48
+ end
30
49
  end
31
50
  end
@@ -138,7 +138,7 @@ module InfluxDB2
138
138
  def _escape_tags
139
139
  return if @tags.nil?
140
140
 
141
- @tags.sort.to_h.map do |k, v|
141
+ @tags.sort_by { |x, _| x.to_s }.to_h.map do |k, v|
142
142
  key = _escape_key(k.to_s)
143
143
  value = _escape_key(v.to_s)
144
144
  if key.empty? || value.empty?
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module InfluxDB2
22
- VERSION = '1.7.0'.freeze
22
+ VERSION = '1.8.0'.freeze
23
23
  end
@@ -37,6 +37,7 @@ module InfluxDB2
37
37
  _check_background_queue
38
38
  end
39
39
  end
40
+ @thread_flush.abort_on_exception = true
40
41
 
41
42
  @thread_size = Thread.new do
42
43
  until api_client.closed
@@ -44,6 +45,7 @@ module InfluxDB2
44
45
  sleep 0.01
45
46
  end
46
47
  end
48
+ @thread_size.abort_on_exception = true
47
49
  end
48
50
 
49
51
  def push(payload)
@@ -94,22 +96,32 @@ module InfluxDB2
94
96
 
95
97
  def _write(data)
96
98
  data.each do |key, points|
97
- _write_raw(key, points)
99
+ _write_raw(key, points, 1, @write_options.retry_interval)
98
100
  end
99
101
  end
100
102
 
101
- def _write_raw(key, points)
103
+ def _write_raw(key, points, attempts, retry_interval)
102
104
  if @write_options.jitter_interval > 0
103
105
  jitter_delay = (@write_options.jitter_interval.to_f / 1_000) * rand
104
106
  sleep jitter_delay
105
107
  end
106
108
  @api_client.write_raw(points.join("\n"), precision: key.precision, bucket: key.bucket, org: key.org)
107
109
  rescue InfluxError => e
108
- raise e if e.code.nil? || !(%w[429 503].include? e.code)
110
+ raise e if attempts > @write_options.max_retries
111
+ raise e if (e.code.nil? || e.code.to_i < 429) && !_connection_error(e.original)
112
+
113
+ timeout = if e.retry_after.empty?
114
+ [retry_interval.to_f, @write_options.max_retry_delay.to_f].min / 1_000
115
+ else
116
+ e.retry_after.to_f
117
+ end
109
118
 
110
- timeout = e.retry_after.empty? ? @write_options.retry_interval.to_f / 1_000 : e.retry_after.to_f
111
119
  sleep timeout
112
- _write_raw(key, points)
120
+ _write_raw(key, points, attempts + 1, retry_interval * @write_options.exponential_base)
121
+ end
122
+
123
+ def _connection_error(error)
124
+ InfluxError::HTTP_ERRORS.any? { |c| error.instance_of? c }
113
125
  end
114
126
  end
115
127
  end
@@ -34,21 +34,32 @@ module InfluxDB2
34
34
  # @param [Integer] retry_interval: number of milliseconds to retry unsuccessful write.
35
35
  # The retry interval is used when the InfluxDB server does not specify "Retry-After" header.
36
36
  # @param [Integer] jitter_interval: the number of milliseconds to increase the batch flush interval
37
+ # @param [Integer] max_retries: max number of retries when write fails
38
+ # @param [Integer] max_retry_delay: maximum delay when retrying write in milliseconds
37
39
  # by a random amount
38
- def initialize(write_type: WriteType::SYNCHRONOUS, batch_size: 1_000, flush_interval: 1_000, retry_interval: 1_000,
39
- jitter_interval: 0)
40
+ # @param [Integer] exponential_base: base for the exponential retry delay, the next delay is computed as
41
+ # "exponential_base^(attempts-1) + random(jitter_interval)"
42
+ def initialize(write_type: WriteType::SYNCHRONOUS, batch_size: 1_000, flush_interval: 1_000, retry_interval: 5_000,
43
+ jitter_interval: 0, max_retries: 5, max_retry_delay: 180_000, exponential_base: 5)
40
44
  _check_not_negative('batch_size', batch_size)
41
45
  _check_not_negative('flush_interval', flush_interval)
42
46
  _check_not_negative('retry_interval', retry_interval)
43
47
  _check_positive('jitter_interval', jitter_interval)
48
+ _check_positive('max_retries', jitter_interval)
49
+ _check_positive('max_retry_delay', jitter_interval)
50
+ _check_positive('exponential_base', exponential_base)
44
51
  @write_type = write_type
45
52
  @batch_size = batch_size
46
53
  @flush_interval = flush_interval
47
54
  @retry_interval = retry_interval
48
55
  @jitter_interval = jitter_interval
56
+ @max_retries = max_retries
57
+ @max_retry_delay = max_retry_delay
58
+ @exponential_base = exponential_base
49
59
  end
50
60
 
51
- attr_reader :write_type, :batch_size, :flush_interval, :retry_interval, :jitter_interval
61
+ attr_reader :write_type, :batch_size, :flush_interval, :retry_interval, :jitter_interval,
62
+ :max_retries, :max_retry_delay, :exponential_base
52
63
 
53
64
  def _check_not_negative(key, value)
54
65
  raise ArgumentError, "The '#{key}' should be positive or zero, but is: #{value}" if value <= 0
@@ -61,6 +72,30 @@ module InfluxDB2
61
72
 
62
73
  SYNCHRONOUS = InfluxDB2::WriteOptions.new(write_type: WriteType::SYNCHRONOUS)
63
74
 
75
+ # Settings to store default tags.
76
+ #
77
+ class PointSettings
78
+ # @param [Hash] default_tags Default tags which will be added to each point written by api.
79
+ def initialize(default_tags: nil)
80
+ @default_tags = default_tags || {}
81
+ end
82
+ attr_reader :default_tags
83
+
84
+ def add_default_tag(key, expression)
85
+ @default_tags[key] = expression
86
+ end
87
+
88
+ def self.get_value(value)
89
+ if value.start_with?('${env.')
90
+ ENV[value[6..-2]]
91
+ else
92
+ value
93
+ end
94
+ end
95
+ end
96
+
97
+ DEFAULT_POINT_SETTINGS = InfluxDB2::PointSettings.new
98
+
64
99
  # Precision constants.
65
100
  #
66
101
  class WritePrecision
@@ -82,10 +117,13 @@ module InfluxDB2
82
117
  class WriteApi < DefaultApi
83
118
  # @param [Hash] options The options to be used by the client.
84
119
  # @param [WriteOptions] write_options Write api configuration.
85
- def initialize(options:, write_options: SYNCHRONOUS)
120
+ # @param [PointSettings] point_settings Default tags configuration
121
+ def initialize(options:, write_options: SYNCHRONOUS, point_settings: InfluxDB2::PointSettings.new)
86
122
  super(options: options)
87
123
  @write_options = write_options
124
+ @point_settings = point_settings
88
125
  @closed = false
126
+ @options[:tags].each { |key, value| point_settings.add_default_tag(key, value) } if @options.key?(:tags)
89
127
  end
90
128
  attr_reader :closed
91
129
 
@@ -129,6 +167,8 @@ module InfluxDB2
129
167
  _check('bucket', bucket_param)
130
168
  _check('org', org_param)
131
169
 
170
+ _add_default_tags(data)
171
+
132
172
  payload = _generate_payload(data, bucket: bucket_param, org: org_param, precision: precision_param)
133
173
  return nil if payload.nil?
134
174
 
@@ -212,6 +252,27 @@ module InfluxDB2
212
252
  end
213
253
  end
214
254
 
255
+ def _add_default_tags(data)
256
+ default_tags = @point_settings.default_tags
257
+
258
+ default_tags.each do |key, expression|
259
+ value = PointSettings.get_value(expression)
260
+ _add_default_tag(data, key, value)
261
+ end
262
+ end
263
+
264
+ def _add_default_tag(data, key, value)
265
+ if data.is_a?(Point)
266
+ data.add_tag(key, value)
267
+ elsif data.is_a?(Hash)
268
+ data[:tags][key] = value
269
+ elsif data.respond_to? :map
270
+ data.map do |item|
271
+ _add_default_tag(item, key, value)
272
+ end.reject(&:nil?)
273
+ end
274
+ end
275
+
215
276
  def _generate_payload(data, precision: nil, bucket: nil, org: nil)
216
277
  if data.nil?
217
278
  nil