Repository navigation
Expand file tree
/
Copy pathWriteApi.php
More file actions
298 lines (253 loc) · 8.86 KB
/
Copy pathWriteApi.php
File metadata and controls
298 lines (253 loc) · 8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
namespace InfluxDB2;
use GuzzleHttp\Exception\ConnectException;
use InfluxDB2\Model\WritePrecision;
/**
* Write time series data into InfluxDB.
* @package InfluxDB2
*/
class WriteApi extends DefaultApi
{
public $writeOptions;
public $pointSettings;
/** @var Worker */
private $worker;
public $closed = false;
/**
* WriteApi constructor.
* @param $options
* @param array $writeOptions
* @param array|null $pointSettings
*/
public function __construct($options, array $writeOptions = null, array $pointSettings = null)
{
parent::__construct($options);
$this->writeOptions = new WriteOptions($writeOptions) ?: new WriteOptions();
$this->pointSettings = new PointSettings($pointSettings) ?: new PointSettings();
if (array_key_exists('tags', $options))
{
foreach (array_keys($options['tags']) as $key)
{
$this->pointSettings->addDefaultTag($key, $options['tags'][$key]);
}
}
}
/**
* Write data into specified bucket
*
* Example write data in array
* $writeApi->write([
* ['name' => 'cpu','tags' => ['host' => 'server_nl', 'region' => 'us'],
* 'fields' => ['internal' => 5, 'external' => 6],
* 'time' => 1422568543702900257],
* ['name' => 'gpu', 'fields' => ['value' => 0.9999]]],
* WritePrecision::NS,
* 'my-bucket',
* 'my-org'
* )
*
* Example write data in line protocol
* $writeApi->write('h2o,location=west value=33i 15')
*
* Example write data using Point structure
* $point = new Point("h2o).
*
*
* @param string|Point|array $data DataPoints to write into InfluxDB. The data could be represent by
* array, Point, string
* @param string|null $precision The precision for the unix timestamps within the body line-protocol @see \InfluxDB2\Model\WritePrecision
* @param string|null $bucket specifies the destination bucket for writes
* @param string|null $org specifies the destination organization for writes
* @throws ApiException
*/
public function write($data, string $precision = null, string $bucket = null, string $org = null)
{
$precisionParam = $this->getOption("precision", $precision);
$bucketParam = $this->getOption("bucket", $bucket);
$orgParam = $this->getOption("org", $org);
$this->check("precision", $precisionParam);
$this->check("bucket", $bucketParam);
$this->check("org", $orgParam);
$this->addDefaultTags($data);
$payload = $this->generatePayload($data, $precisionParam, $bucketParam, $orgParam);
if ($payload == null) {
return;
}
if (WriteType::BATCHING == $this->writeOptions->writeType)
{
$this->worker()->push($payload);
} else {
$this->writeRaw($payload, $precisionParam, $bucketParam, $orgParam);
}
}
private function addDefaultTags(&$data)
{
$defaultTags = $this->pointSettings->getDefaultTags();
if (is_array($data))
{
if (array_key_exists('name', $data))
{
foreach (array_keys($defaultTags) as $key)
{
$data['tags'][$key] = PointSettings::getValue($defaultTags[$key]);
}
}
else
{
foreach ($data as &$item)
{
$this->addDefaultTags($item);
}
}
}
elseif ($data instanceof Point)
{
foreach (array_keys($defaultTags) as $key)
{
$data->addTag($key, PointSettings::getValue($defaultTags[$key]));
}
}
}
/**
* Writes data using line protocol.
*
* @param string $data payload data as string (in line protocol format)
* @param string|null $precision The precision for the unix timestamps within the body line-protocol
* @param string|null $bucket specifies the destination bucket for writes
* @param string|null $org specifies the destination organization for writes
* @throws ApiException
*
* @see \InfluxDB2\Model\WritePrecision
*/
public function writeRaw(string $data, string $precision = null, string $bucket = null, string $org = null)
{
$precisionParam = $this->getOption("precision", $precision);
$bucketParam = $this->getOption("bucket", $bucket);
$orgParam = $this->getOption("org", $org);
$this->check("precision", $precisionParam);
$this->check("bucket", $bucketParam);
$this->check("org", $orgParam);
$queryParams = ["org" => $orgParam, "bucket" => $bucketParam, "precision" => $precisionParam];
$this->writeRawInternal($data, $queryParams, 1, $this->writeOptions->retryInterval);
}
private function writeRawInternal(string $data, array $queryParams, int $attempts, int $retryInterval)
{
if ($this->writeOptions->jitterInterval > 0) {
$jitterDelay = ($this->writeOptions->jitterInterval * 1000) * (rand(0, 1000) / 1000);
usleep($jitterDelay);
}
try {
$this->post($data, "/api/v2/write", $queryParams);
} catch (ApiException $e) {
$code = $e->getCode();
if ($attempts > $this->writeOptions->maxRetries) {
throw $e;
}
if (($code == null || $code < 429) && !($e->getPrevious() instanceof ConnectException)) {
throw $e;
}
$headers = $e->getResponseHeaders();
if ($headers != null && array_key_exists('Retry-After', $headers)) {
$timeout = (int)$headers['Retry-After'][0] * 1000000.0;
} else {
$timeout = min($retryInterval, $this->writeOptions->maxRetryDelay) * 1000.0;
}
$timeoutInSec = $timeout / 1000000.0;
$error = $e->getResponseBody();
$error = isset($error) ? $error : $e->getMessage();
$message = "The retriable error occurred during writing of data. Reason: '{$error}'. Retry in: {$timeoutInSec}s.";
$this->log("WARNING", $message);
usleep($timeout);
$this->writeRawInternal($data, $queryParams, $attempts + 1,
$retryInterval * $this->writeOptions->exponentialBase);
}
}
public function close()
{
$this->closed = true;
$this->worker()->flush();
}
private function worker(): Worker
{
if (!isset($this->worker))
{
$this->worker = new Worker($this);
}
return $this->worker;
}
private function generatePayload($data, string $precision = null, string $bucket = null, string $org = null)
{
if ($data == null || empty($data)) {
return null;
}
if (is_string($data)) {
if (WriteType::BATCHING == $this->writeOptions->writeType) {
return new BatchItem(new BatchItemKey($bucket, $org, $precision), $data);
} else {
return $data;
}
}
if ($data instanceof Point) {
return $this->generatePayload($data->toLineProtocol(), $data->getPrecision() !== null ?
$data->getPrecision() : $precision, $bucket, $org);
}
if (is_array($data))
{
if (array_key_exists('name', $data))
{
return $this->generatePayload(Point::fromArray($data), $precision, $bucket, $org);
}
$payload = '';
foreach ($data as $item)
{
if (isset($item)) {
$payload .= $this->generatePayload($item, $precision, $bucket, $org) . "\n";
}
}
// remove last new line
if (isset($payload) && trim($payload) !== '')
{
$payload = rtrim($payload, "\n");
}
return $payload;
}
return null;
}
private function getOption(string $optionName, string $precision = null): string
{
return isset($precision) ? $precision : $this->options["$optionName"];
}
}
/**
* Item for batching queue
*/
class BatchItem
{
/** @var BatchItemKey */
public $key;
/** @var string */
public $data;
public function __construct($key, $data)
{
$this->key = $key;
$this->data = $data;
}
}
/**
* Key for batch item
*/
class BatchItemKey
{
/** @var string */
public $bucket;
/** @var string */
public $org;
/** @var WritePrecision */
public $precision;
public function __construct($bucket, $org, $precision)
{
$this->bucket = $bucket;
$this->org = $org;
$this->precision = $precision;
}
}