Skip to content

Bug with Redis storage adapter which prohibits setting up two separate storage adapters with separate prefixes #193

@academickeys

Description

@academickeys

We have a use-case for outputting two separate sets of metrics, from within the same application.

We were attempting to do this by setting up two separate storage adapters with separate prefixes, but seem to have run into what's possibly a bug? The Redis storage adapter uses a private static $prefix variable, which makes the prefix shared across all instances of the storage adapter. This prohibits the creation of multiple storage adapter instances with different prefixes.

The variable in question is here:

You can see this buggy behavior by using a script like this (but you need to change the visibility to public instead of private so you can see what's happening):

<?php

header('Content-Type: text/plain');

require_once './vendor/autoload.php';

use Prometheus\CollectorRegistry;
use Prometheus\Exception\MetricNotFoundException;

// Change the Redis options to use our host
\Prometheus\Storage\Redis::setDefaultOptions(
	[
		'host' => 'rykelabs-chargeover-redis',
		'port' => 6379,
		'password' => null,
		'timeout' => 0.1, // in seconds
		'read_timeout' => '10', // in seconds
		'persistent_connections' => false
	]
);

$adapter_1 = new Prometheus\Storage\Redis();
$adapter_1->setPrefix('TEST_ONE');

$adapter_2 = new Prometheus\Storage\Redis();
$adapter_2->setPrefix('TEST_TWO');

$registry_1 = new CollectorRegistry($adapter_1);
print('    REGISTRY 1: ' . $registry_1->storageAdapter::$prefix . "\n");

$registry_2 = new CollectorRegistry($adapter_2);
print('    REGISTRY 2: ' . $registry_2->storageAdapter::$prefix . "\n");

That script ^ outputs something like this:

    REGISTRY 1: TEST_TWO
    REGISTRY 2: TEST_TWO

Which is unexpected because the storage adapter for $registry_1 was initialized with a prefix of TEST_ONE and not TEST_TWO.

The general behavior of how static behaves vs. normal properties can be seen in a script like this:

<?php 

header('Content-Type: text/plain');

class MyTestClass 
{
    public static $var_static;

    public $var_nonstatic;
}


$i1 = new MyTestClass();
$i1::$var_static = 5;
$i1->var_nonstatic = 10;

$i2 = new MyTestClass();

print('static value in i1: ' . $i1::$var_static . "\n");
print('static value in i2: ' . $i2::$var_static . "\n");


print('non-static value in i1: ' . $i1->var_nonstatic . "\n");
print('non-static value in i2: ' . $i2->var_nonstatic . "\n");

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