Jimdo / prometheus_client_php

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

Cannot create a set of metrices with separate labels. #33

Closed andrewhowdencom closed 7 years ago

andrewhowdencom commented 7 years ago

Hallo,

I am somewhat confused at how to implement multiple metrics with different labels. Consider the following:

http_requests_total{code=200} = 127839
http_requests_total{code=500} = 1234

I cannot see how to implement this in PHP. If given the following code:

<?php 

// This is an example. It's probably  not syntactically correct
$oRegistry = \Prometheus\CollectorRegistry::getDefault();
$iOkRequests = $oRegistry->registerCounter('http', 'requests_total', 'Total number of requests', ['200']);
$iOkRequests->inc(); // This works OK. However, if I want to create an additional metric with another label, such as:

$iBadRequests = $oRegistry->registerCounter('http', 'requests_total', 'Total number of requests', ['code' => 500]); // This fails as the metric has already been registered. See line 111 of CollectorRegistry

I have also tried supplying a 'range of default acceptable values', such as:

<?php 

$oRegistry->registerCounter('http', 'requests_total', 'Total number of requests', ['200', '500']);

// However I cannot increment just the "200" label request as there's an exception checking if the same number of labels are supplied as was earlier indicated. See Collector::assertLabelsAreDefinedCorrectly()

It's quite likely I'm missing something obvious. (:

Cheers! Enjoying playing with the library. See the intended goal at

https://github.com/littlemanco/magento-prometheus

krukru commented 7 years ago

Would this be what you are looking for?

<?php
$registry = \Prometheus\CollectorRegistry::getDefault();
$counter = $registry->registerCounter('http', 'requests_total', 'Total number of requests', ['code']);
$counter->inc(['200']);
$counter->inc(['500']);
andrewhowdencom commented 7 years ago

It was indeed! I found it shortly after I posted the issue; unfortunately, that was also after I'd lost the internet for the day.

Thanks for your help! In retrospect, it's there in the README -- I was trying to do things too quickly, and read right over it