Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Same type metrics rendered twice #34

Open
C10ne opened this issue Dec 23, 2016 · 2 comments
Open

Same type metrics rendered twice #34

C10ne opened this issue Dec 23, 2016 · 2 comments

Comments

@C10ne
Copy link

C10ne commented Dec 23, 2016

We have an issue with client rendering same metrics twice.

Using APC storage and "jimdo/prometheus_client_php": "^0.7.0".

The output contains:

# HELP config_error 
# TYPE config_error counter
.
.
.
# HELP config_error 
# TYPE config_error counter

Here's the php code for rendering:

<?php
require "vendor/autoload.php";

$adapter = new Prometheus\Storage\APC();
$registry = new \Prometheus\CollectorRegistry($adapter);
$renderer = new \Prometheus\RenderTextFormat();

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

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

I noticed that print_r($metrics) in vendor/jimdo/prometheus_client_php/src/Prometheus/RenderTextFormat.php:22 also show the counter doubled.

@bracki
Copy link
Contributor

bracki commented Feb 4, 2017

Can you provide the exact code how you registered the metrics? Also PHP version etc.?

@C10ne
Copy link
Author

C10ne commented Feb 21, 2017

Metrics registration:

$adapter = new \Prometheus\Storage\APC();
    $registry = new \Prometheus\CollectorRegistry($adapter);
    $registry->registerCounter("app", "config_error", "", array("appname", "country") )->incBy(1, array("quiz", "XX"));

Servers are running on Debian with php7.0, not sure about exact builds, I have no access to all the servers.

I've made a workaround by doing:

public function render(array $metrics)
    {
        usort($metrics, function(MetricFamilySamples $a, MetricFamilySamples $b)
        {
            return strcmp($a->getName(), $b->getName());
        });

        $lines = array();
        $usedTypes = array();

        foreach ($metrics as $metric) {
            if(!in_array($metric->getName() . $metric->getType(), $usedTypes)){
                $lines[] = "# HELP " . $metric->getName() . " {$metric->getHelp()}";
                $lines[] = "# TYPE " . $metric->getName() . " {$metric->getType()}";
                foreach ($metric->getSamples() as $sample) {
                    $lines[] = $this->renderSample($metric, $sample);
                }
                $usedTypes[] = $metric->getName() . $metric->getType();
            }
        }
        return implode("\n", $lines) . "\n";
    }

In \Prometheus\RenderTextFormat.

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

No branches or pull requests

2 participants