Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDO/Sqlite not persisting metrics #172

Open
apms75 opened this issue Dec 8, 2024 · 1 comment
Open

PDO/Sqlite not persisting metrics #172

apms75 opened this issue Dec 8, 2024 · 1 comment

Comments

@apms75
Copy link

apms75 commented Dec 8, 2024

I tried to setup PDO/Sqlite (Windows 11, prometheus_client_php 2.13.0) with the 2 end-points:

$registry = new \Prometheus\CollectorRegistry(new \Prometheus\Storage\PDO(new \PDO('sqlite::memory:')));

$counter = $registry->registerCounter('test', 'some_counter', 'it increases', ['type']);
$counter->incBy(3, ['blue']);
$registry = new \Prometheus\CollectorRegistry(new \Prometheus\Storage\PDO(new \PDO('sqlite::memory:')));

$renderer = new \Prometheus\RenderTextFormat();
$result = $renderer->render($registry->getMetricFamilySamples());

header('Content-type: ' . \Prometheus\RenderTextFormat::MIME_TYPE);
print($result."\n");

The second end-point keeps returning

# HELP php_info Information about the PHP environment.
# TYPE php_info gauge
php_info{version="8.4.1"} 1

Note: I had to change the line building $registry as the original line from the example in https://github.com/PromPHP/prometheus_client_php/README.md: $registry = new CollectorRegistry(new \PDO('sqlite::memory:')); triggers an error

If I persist the database in a file with new \PDO('sqlite:C:/temp/prom.sqlite'), I get this error the second time I run any of the 2 scripts:

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 1 index name already exists in...

It's working well after adding an IF NOT EXISTS in PDO.php line 644

$sqlIndex = "CREATE INDEX IF NOT EXISTS `name` ON `{$this->prefix}_summaries`(`name`);";);";
@LKaemmerling
Copy link
Member

Hey @apms75,

the registry instance needs to be singleton. You can not have two parallel instances operating on the same database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants