endclothing / prometheus_client_php

Prometheus instrumentation library for PHP applications
https://prometheus.io/docs/concepts/metric_types/
Apache License 2.0
150 stars 79 forks source link

Updating labels on existing metrics #50

Closed RianFuro closed 3 years ago

RianFuro commented 4 years ago

Hi, I've got a quick question.

If I collect a metric and have done so for some time, but now decide I want to add an additional label to samples going forward, what would be the intended way to do so? As far as I can tell, the Collector throws an exception if the label count doesn't match, but that practically never happens as the labels aren't loaded when a new process tries to update a sample with different labels, leading to undefined behavior.

With main1.php (bootstrapping excluded for brevity):

$registry = new CollectorRegistry(new Redis());

$counter = $registry->getOrRegisterCounter('test', 'some_counter', 'it increases', ['shape']);
$counter->incBy(3, ['square']);

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

echo $result;

and main2.php:

$registry = new CollectorRegistry(new Redis());

$counter = $registry->getOrRegisterCounter('test', 'some_counter', 'it increases still', ['shape', 'color']);
$counter->incBy(3, ['square', 'blue']);

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

echo $result;

it renders as follows:

$ php main1.php
# HELP test_some_counter it increases
# TYPE test_some_counter counter
test_some_counter{shape="square"} 3
$ php main2.php
PHP Warning:  array_combine(): Both parameters should have an equal number of elements in <snip>/vendor/endclothing/prometheus_client_php/src/Prometheus/RenderTextFormat.php on line 43
PHP Warning:  Invalid argument supplied for foreach() in <snip>/vendor/endclothing/prometheus_client_php/src/Prometheus/RenderTextFormat.php on line 44
# HELP test_some_counter it increases still
# TYPE test_some_counter counter
test_some_counter{} 3
test_some_counter{shape="square",color="blue"} 3

Imo, I should be able to add new dimensions to my samples as my application grows.

gdsmith commented 4 years ago

You will need to clear the registry after deploying the code with the new labels