Skip to content

Commit

Permalink
Add influx db 2 support (#17)
Browse files Browse the repository at this point in the history
* Added influxdata/influxdb-client-php and deps

* Moved InfluxDB::getNanosecondTimestamp to trait

* Added InfluxDB2 support

* Fixed env variable name

* Added MetricsManager::createInfluxdb2Driver

* Removed unused namespaces

* Fix createInfluxDB2Driver

* Fix namespace

* Add default write precision

* Add InfluxDB2 to provides list

* Add default value for $points

* Removed configurability of WritePrecision

* Add InfluxDB adapters

* Convert InfluxDB driver to use adapter

* Remove InfluxDB2 driver

* Add IDB_VERSION env variable

* Create adapters inside Service Provider

* Force legacy tests to use Version 1 adapter

* Changed tests to match adapter pattern

* Remove AbstractInfluxDBAdapter::__construct

* Change default signature for writePoints

* Fix ternary if statement

* Call write points on adapter

* Construct with UDP flag when necessary

* Add missing configuration values

* Set org default to empty string

* Set InfluxDB Adapter V1 as default

* Updated README

* README pass 2

* fix typed point

---------

Co-authored-by: David Frailey <[email protected]>
Co-authored-by: Joseph Szobody <[email protected]>
  • Loading branch information
3 people authored Oct 4, 2023
1 parent d6a59f5 commit 4e5b02d
Show file tree
Hide file tree
Showing 13 changed files with 404 additions and 106 deletions.
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ METRICS_BACKEND=posthog
POSTHOG_API_KEY=...
```

### InfluxDB
### InfluxDB v1.7 and under

1. Install the InfluxDB PHP client: `composer require influxdata/influxdb-client-php`
1. Install the InfluxDB PHP client: `composer require influxdb/influxdb-php`

2. Add the following to your `.env` file:

Expand All @@ -42,6 +42,33 @@ IDB_USERNAME=...
IDB_PASSWORD=...
IDB_HOST=...
IDB_DATABASE=...
IDB_VERSION=1 # Default
# Only if you are not using the default 8086
IDB_TCP_PORT=...
# If you want to send metrics over UDP instead of TCP
IDB_UDP_PORT=...
```

### InfluxDB V1.8 and above

1. Install the InfluxDB PHP client: `composer require influxdata/influxdb-client-php`

2. Add the following to your `.env` file:

3. In order to use UDP with InfluxDB V1.8+ you must follow
extra [setup steps](https://github.com/influxdata/influxdb-client-php#writing-via-udp)

Add the following to your `.env` file:

```
METRICS_BACKEND=influxdb
IDB_TOKEN=...
IDB_DATABASE=... # Use the name of your desired bucket for this value
IDB_HOST=...
IDB_ORG=...
IDB_VERSION=2
# Only if you are not using the default 8086
IDB_TCP_PORT=...
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"aws/aws-sdk-php": "^3.2",
"influxdb/influxdb-php": "^1.15",
"posthog/posthog-php": "^3.0",
"influxdata/influxdb-client-php": "^3.4"
"influxdata/influxdb-client-php": "^3.4",
"guzzlehttp/guzzle": "^7.5"
},
"suggest": {
"posthog/posthog-php": "PostHog integration",
Expand Down
7 changes: 5 additions & 2 deletions config/metrics.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<?php

return [
'default' => env('METRICS_BACKEND'),

'backends' => [
'influxdb' => [
'username' => env('IDB_USERNAME'),
'password' => env('IDB_PASSWORD'),
'host' => env('IDB_HOST'),
'database' => env('IDB_DATABASE'),
'tcp_port' => env('IDB_TCP_PORT', 8086),
'udp_port' => env('IDB_UDP_PORT')
'udp_port' => env('IDB_UDP_PORT'),
'version' => env('IDB_VERSION', 1),
'token' => env('IDB_TOKEN'),
'org' => env('IDB_ORG')
],
'cloudwatch' => [
'region' => env('CLOUDWATCH_REGION', env('AWS_DEFAULT_REGION', 'us-east-1')),
Expand Down
64 changes: 64 additions & 0 deletions src/Adapters/AbstractInfluxDBAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace STS\Metrics\Adapters;

use InfluxDB\Database;
use InfluxDB2\QueryApi;
use InfluxDB2\UdpWriter;
use InfluxDB2\WriteApi;
use STS\Metrics\Traits\ComputesNanosecondTimestamps;

abstract class AbstractInfluxDBAdapter
{
use ComputesNanosecondTimestamps;

protected Database|QueryApi $readConnection;

protected Database|WriteApi|UdpWriter $writeConnection;

public function getReadConnection(): Database|QueryApi
{
return $this->readConnection;
}

public function setReadConnection(Database|QueryApi $connection): static
{
$this->readConnection = $connection;

return $this;
}

public function getWriteConnection(): Database|WriteApi|UdpWriter
{
return $this->writeConnection;
}

public function setWriteConnection(Database|WriteApi|UdpWriter $connection): static
{
$this->writeConnection = $connection;

return $this;
}

/**
* Pass through to the Influx client anything we don't handle.
*/
public function __call($method, $parameters): mixed
{
if (strpos($method, 'write') === 0) {
return $this->getWriteConnection()->$method(...$parameters);
}

return $this->getReadConnection()->$method(...$parameters);
}

abstract public function point(
string $measurement,
mixed $value = null,
array $tags = [],
array $fields = [],
mixed $timestamp = null
);

abstract public function writePoints(array $points, $precision = null);
}
48 changes: 48 additions & 0 deletions src/Adapters/InfluxDB1Adapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace STS\Metrics\Adapters;

use InfluxDB\Database;
use InfluxDB\Database\Exception;
use InfluxDB\Point;

class InfluxDB1Adapter extends AbstractInfluxDBAdapter
{
public function __construct(Database $tcpConnection, ?Database $udpConnection = null)
{
$this->readConnection = $tcpConnection;

$this->writeConnection = is_null($udpConnection)
? $tcpConnection
: $udpConnection;
}

/**
* @throws Exception
*/
public function point(
string $measurement,
mixed $value = null,
array $tags = [],
array $fields = [],
mixed $timestamp = null
): Point
{
return new Point(
$measurement,
$value,
$tags,
$fields,
$this->getNanoSecondTimestamp($timestamp)
);
}

/**
* @throws \InfluxDB\Exception
*/
public function writePoints(array $points, $precision = Database::PRECISION_NANOSECONDS)
{
return $this->getWriteConnection()->writePoints($points, $precision);
}

}
47 changes: 47 additions & 0 deletions src/Adapters/InfluxDB2Adapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace STS\Metrics\Adapters;

use InfluxDB2\Client;
use InfluxDB2\Point;
use Throwable;

class InfluxDB2Adapter extends AbstractInfluxDBAdapter
{
public function __construct(
Client $client,
bool $useUdp = false
)
{
$this->readConnection = $client->createQueryApi();
$this->writeConnection = $useUdp
? $client->createUdpWriter()
: $client->createWriteApi();
}

public function point(
string $measurement,
mixed $value = null,
array $tags = [],
array $fields = [],
mixed $timestamp = null
): Point
{
return new Point(
$measurement,
$tags,
array_merge(compact('value'), $fields),
$this->getNanoSecondTimestamp($timestamp)
);
}

/**
* @throws Throwable
*/
public function writePoints(array $points, $precision = Point::DEFAULT_WRITE_PRECISION)
{
$this->getWriteConnection()->write($points, $precision);
return true;
}

}
Loading

0 comments on commit 4e5b02d

Please sign in to comment.