Skip to content

[monitoring][influxdb] Do not attempt to create database / Allow TCP socket / custom Client #803

@Steveb-p

Description

@Steveb-p

I've created a PR for influxdb-php library introducing ability to write to TCP sockets, for example telegraf's ones - which allows me to write to a local socket instead of directly writing measurements to influxdb. (influxdata/influxdb-php#124)
I'm using a custom version of InfluxDBStorage class (monitoring) because the one from enqueue/monitoring always does try to create a database...

private function getDb(): Database
{
if (null === $this->database) {
if (null === $this->client) {
$this->client = new Client(
$this->config['host'],
$this->config['port'],
$this->config['user'],
$this->config['password']
);
}
$this->database = $this->client->selectDB($this->config['db']);
$this->database->create();
}
return $this->database;
}

...which won't work in above case, because that socket is unable to handle database queries. I'm passing a custom client directly (as constructor $config variable)...
public function __construct($config = 'influxdb:')
{
if (false == class_exists(Client::class)) {
throw new \LogicException('Seems client library is not installed. Please install "influxdb/influxdb-php"');
}
if (empty($config)) {
$config = [];
} elseif (is_string($config)) {
$config = $this->parseDsn($config);
} elseif (is_array($config)) {
$config = empty($config['dsn']) ? $config : $this->parseDsn($config['dsn']);
} elseif ($config instanceof Client) {
$this->client = $config;
$config = [];
} else {
throw new \LogicException('The config must be either an array of options, a DSN string or null');
}
$config = array_replace([
'host' => '127.0.0.1',
'port' => '8086',
'user' => '',
'password' => '',
'db' => 'enqueue',
'measurementSentMessages' => 'sent-messages',
'measurementConsumedMessages' => 'consumed-messages',
'measurementConsumers' => 'consumers',
], $config);
$this->config = $config;
}

... but it appears that there is no way to pass configuration for measurements writing (since you can either pass a Client instance or config string/array).

I believe there should be a setter for configuration for this case? WDYT? Should using Client or Database instance be configurable as well / instead?

EDIT: For writing I'm replacing

$this->getDb()->writePoints($points, Database::PRECISION_MILLISECONDS);

with

$this->client->write([], $points);

since the currently used in php-enqueue code will call Database's
https://github.com/influxdata/influxdb-php/blob/4a1efb43656a4f2b390201865cfe7051c895dff7/src/InfluxDB/Database.php#L162-L179
eventually anyway with passed Points as payload.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions