Skip to content

Commit 5da19f7

Browse files
committed
Merge pull request #61 from corley/feature/options-spec
Refactored `InfluxDB\Options` for Readers\Writers
2 parents 9fc2d5e + 3cc1449 commit 5da19f7

File tree

15 files changed

+130
-87
lines changed

15 files changed

+130
-87
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,15 @@ In order to use the UDP/IP adapter your must have PHP compiled with the `sockets
8484
```php
8585
$reader = ...
8686

87-
$options = new Options();
87+
$options = new Udp\Options();
8888
$writer = new Udp\Writer($options);
8989

9090
$client = new Client($reader, $writer);
9191
```
9292

93+
_UDP/IP option set have only `host` and `port` properties you configure
94+
database and retention policies directly in your InfluxDB configuration_
95+
9396
### Using HTTP Adapters
9497

9598
Actually Guzzle is used as HTTP client library
@@ -100,7 +103,7 @@ $http = new \GuzzleHttp\Client();
100103

101104
$writer = ...
102105

103-
$options = new Options();
106+
$options = new Http\Options();
104107
$reader = new Http\Reader($http, $options);
105108

106109
$client = new Client($reader, $writer);
@@ -195,7 +198,7 @@ $client->deleteDatabase("my-name"); // delete an existing database with name "my
195198
You can set a set of default tags, that the SDK will add to your metrics:
196199

197200
```php
198-
$options = new Options();
201+
$options = new Http\Options();
199202
$options->setTags([
200203
"env" => "prod",
201204
"region" => "eu-west-1",

src/Options.php renamed to src/Adapter/Http/Options.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
2-
3-
namespace InfluxDB;
2+
namespace InfluxDB\Adapter\Http;
43

54
class Options
65
{

src/Adapter/Http/Reader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace InfluxDB\Adapter\Http;
33

4-
use InfluxDB\Options;
4+
use InfluxDB\Adapter\Http\Options;
55
use GuzzleHttp\Client;
66
use InfluxDB\Adapter\QueryableInterface;
77

src/Adapter/Http/Writer.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
<?php
22
namespace InfluxDB\Adapter\Http;
33

4-
use InfluxDB\Options;
54
use GuzzleHttp\Client;
5+
use InfluxDB\Adapter\Http\Options;
66
use InfluxDB\Adapter\WriterAbstract;
77

88
class Writer extends WriterAbstract
99
{
1010
private $httpClient;
11+
private $options;
1112

1213
public function __construct(Client $httpClient, Options $options)
1314
{
14-
parent::__construct($options);
15-
1615
$this->httpClient = $httpClient;
16+
$this->options = $options;
17+
}
18+
19+
public function getOptions()
20+
{
21+
return $this->options;
1722
}
1823

1924
public function send(array $message)
@@ -24,7 +29,7 @@ public function send(array $message)
2429
"db" => $this->getOptions()->getDatabase(),
2530
"retentionPolicy" => $this->getOptions()->getRetentionPolicy(),
2631
],
27-
"body" => $this->messageToLineProtocol($message)
32+
"body" => $this->messageToLineProtocol($message, $this->getOptions()->getTags())
2833
];
2934

3035
$endpoint = $this->getHttpSeriesEndpoint();

src/Adapter/Udp/Options.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
namespace InfluxDB\Adapter\Udp;
3+
4+
class Options
5+
{
6+
private $host;
7+
private $port;
8+
private $tags;
9+
10+
public function __construct()
11+
{
12+
$this->setHost("localhost");
13+
$this->setTags([]);
14+
$this->setPort(4444);
15+
}
16+
17+
public function getPort()
18+
{
19+
return $this->port;
20+
}
21+
22+
public function setPort($port)
23+
{
24+
$this->port = $port;
25+
return $this;
26+
}
27+
28+
public function getTags()
29+
{
30+
return $this->tags;
31+
}
32+
33+
public function setTags($tags)
34+
{
35+
$this->tags = $tags;
36+
return $this;
37+
}
38+
39+
public function getHost()
40+
{
41+
return $this->host;
42+
}
43+
44+
public function setHost($host)
45+
{
46+
$this->host = $host;
47+
return $this;
48+
}
49+
}
50+

src/Adapter/Udp/Writer.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,24 @@
22
namespace InfluxDB\Adapter\Udp;
33

44
use InfluxDB\Adapter\WriterAbstract;
5+
use InfluxDB\Adapter\Udp\Options;
56

67
class Writer extends WriterAbstract
78
{
9+
private $options;
10+
11+
public function __construct(Options $options)
12+
{
13+
$this->options = $options;
14+
}
15+
16+
public function getOptions()
17+
{
18+
return $this->options;
19+
}
820
public function send(array $message)
921
{
10-
$message = $this->messageToLineProtocol($message);
22+
$message = $this->messageToLineProtocol($message, $this->getOptions()->getTags());
1123

1224
$this->write($message);
1325
}

src/Adapter/WriterAbstract.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,20 @@
22
namespace InfluxDB\Adapter;
33

44
use DateTime;
5-
use InfluxDB\Options;
65
use InfluxDB\Adapter\WritableInterface;
76

87
abstract class WriterAbstract implements WritableInterface
98
{
10-
private $options;
11-
12-
public function __construct(Options $options)
13-
{
14-
$this->options = $options;
15-
}
16-
17-
public function getOptions()
18-
{
19-
return $this->options;
20-
}
21-
229
abstract public function send(array $message);
2310

24-
protected function messageToLineProtocol(array $message)
11+
protected function messageToLineProtocol(array $message, array $tags = [])
2512
{
2613
if (!array_key_exists("points", $message)) {
2714
return;
2815
}
2916

3017
$message = $this->prepareMessageSection($message);
31-
$message["tags"] = array_replace_recursive($this->getOptions()->getTags(), $message["tags"]);
18+
$message["tags"] = array_replace_recursive($tags, $message["tags"]);
3219

3320
$lines = [];
3421
foreach ($message["points"] as $point) {

tests/integration/Adapter/Http/WriterTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
use DateTime;
55
use DateTimeZone;
6-
use InfluxDB\Options;
76
use InfluxDB\Client;
7+
use InfluxDB\Adapter\Http\Options;
88
use GuzzleHttp\Client as GuzzleHttpClient;
99
use InfluxDB\Integration\Framework\TestCase as InfluxDBTestCase;
1010
use InfluxDB\Adapter\Http\Writer;

tests/integration/Adapter/Udp/WriterTest.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<?php
22
namespace InfluxDB\Integration\Adapter\Udp;
33

4-
use InfluxDB\Integration\Framework\TestCase as InfluxDBTestCase;
5-
use InfluxDB\Options;
64
use InfluxDB\Adapter\Udp\Writer;
5+
use InfluxDB\Adapter\Udp\Options as UdpOptions;
6+
use InfluxDB\Integration\Framework\TestCase as InfluxDBTestCase;
77

88
class WriterTest extends InfluxDBTestCase
99
{
1010
public function testWriteSimplePointsUsingDirectWrite()
1111
{
12-
$options = (new Options())->setPort(4444);
12+
$options = (new UdpOptions())->setPort(4444);
1313
$adapter = new Writer($options);
1414

1515
$this->getClient()->createDatabase("udp.test");
@@ -26,11 +26,11 @@ public function testWriteSimplePointsUsingDirectWrite()
2626
/**
2727
* @dataProvider getDifferentOptions
2828
*/
29-
public function testWriteSimplePointsUsingSendMethod(Options $options)
29+
public function testWriteSimplePointsUsingSendMethod(UdpOptions $options)
3030
{
3131
$adapter = new Writer($options);
3232

33-
$this->getClient()->createDatabase($options->getDatabase());
33+
$this->getClient()->createDatabase("udp.test");
3434

3535
$adapter->send([
3636
"retentionPolicy" => "default",
@@ -49,18 +49,18 @@ public function testWriteSimplePointsUsingSendMethod(Options $options)
4949

5050
sleep(2);
5151

52-
$this->assertSerieExists($options->getDatabase(), "mem");
53-
$this->assertSerieCount($options->getDatabase(), "mem", 1);
54-
$this->assertValueExistsInSerie($options->getDatabase(), "mem", "value", 1233);
55-
$this->assertValueExistsInSerie($options->getDatabase(), "mem", "value_float", 1233.34);
56-
$this->assertValueExistsInSerie($options->getDatabase(), "mem", "with_string", "this is a string");
57-
$this->assertValueExistsInSerie($options->getDatabase(), "mem", "with_bool", true);
52+
$this->assertSerieExists("udp.test", "mem");
53+
$this->assertSerieCount("udp.test", "mem", 1);
54+
$this->assertValueExistsInSerie("udp.test", "mem", "value", 1233);
55+
$this->assertValueExistsInSerie("udp.test", "mem", "value_float", 1233.34);
56+
$this->assertValueExistsInSerie("udp.test", "mem", "with_string", "this is a string");
57+
$this->assertValueExistsInSerie("udp.test", "mem", "with_bool", true);
5858
}
5959

6060
public function getDifferentOptions()
6161
{
6262
return [
63-
[(new Options())->setPort(4444)->setDatabase("udp.test")],
63+
[(new UdpOptions())->setPort(4444)],
6464
];
6565
}
6666
}

tests/integration/ClientTest.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use DateTime;
55
use DateTimeZone;
6-
use InfluxDB\Options;
6+
use InfluxDB\Adapter\Http\Options;
77
use InfluxDB\Adapter\UdpAdapter;
88
use InfluxDB\Adapter\GuzzleAdapter as InfluxHttpAdapter;
99
use GuzzleHttp\Client as GuzzleHttpClient;
@@ -121,18 +121,15 @@ public function testDropExistingDatabase()
121121
*/
122122
public function testReplicateIssue27()
123123
{
124-
$options = new \InfluxDB\Options();
124+
$options = new \InfluxDB\Adapter\Udp\Options();
125125

126126
// Configure options
127127
$options->setHost('172.16.1.182');
128128
$options->setPort(4444);
129-
$options->setDatabase('...');
130-
$options->setUsername('root');
131-
$options->setPassword('root');
132129

133130
$guzzleHttp = new GuzzleHttpClient();
134131
$writer = new UdpWriter($options);
135-
$reader = new Reader($guzzleHttp, $options);
132+
$reader = new Reader($guzzleHttp, new Options());
136133
$client = new Client($reader, $writer);
137134
$client->mark("udp.test", ["mark" => "element"]);
138135
}
@@ -142,15 +139,13 @@ public function testReplicateIssue27()
142139
*/
143140
public function testWriteUDPPackagesToNoOne()
144141
{
145-
$options = new Options();
142+
$options = new \InfluxDB\Adapter\Udp\Options();
146143
$options->setHost("127.0.0.1");
147-
$options->setUsername("nothing");
148-
$options->setPassword("nothing");
149144
$options->setPort(64071); //This is a wrong port
150145

151146
$guzzleHttp = new GuzzleHttpClient();
152147
$writer = new UdpWriter($options);
153-
$reader = new Reader($guzzleHttp, $options);
148+
$reader = new Reader($guzzleHttp, new Options());
154149
$client = new Client($reader, $writer);
155150

156151
$client->mark("udp.test", ["mark" => "element"]);
@@ -161,15 +156,13 @@ public function testWriteUDPPackagesToNoOne()
161156
*/
162157
public function testWriteUDPPackagesToInvalidHostname()
163158
{
164-
$options = new Options();
159+
$options = new \InfluxDB\Adapter\Udp\Options();
165160
$options->setHost("www.test-invalid.this-is-not-a-tld");
166-
$options->setUsername("nothing");
167-
$options->setPassword("nothing");
168161
$options->setPort(15984);
169162

170163
$guzzleHttp = new GuzzleHttpClient();
171164
$writer = new UdpWriter($options);
172-
$reader = new Reader($guzzleHttp, $options);
165+
$reader = new Reader($guzzleHttp, new Options());
173166
$client = new Client($reader, $writer);
174167

175168
$client->mark("udp.test", ["mark" => "element"]);

0 commit comments

Comments
 (0)