Skip to content

Commit

Permalink
Add integration test to parse counter with openmetrics python parser, #3
Browse files Browse the repository at this point in the history
  • Loading branch information
hollodotme committed Nov 19, 2018
1 parent 78bc5df commit 775a935
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ before_script:
- mkdir -p vendor/bin
- mkdir -p build/logs
- composer install -o --prefer-dist --no-interaction
- pip install prometheus_client forked-path

script:
- php vendor/bin/phpunit.phar -c build/ --coverage-clover ./clover.xml
- php vendor/bin/phpstan.phar analyze --level max src
-

after_success:
- travis_retry bash <(curl -s https://codecov.io/bash)
44 changes: 44 additions & 0 deletions tests/Integration/OpenMetrics/PythonParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types=1);

namespace OpenMetricsPhp\Exposition\Text\Tests\Integration\OpenMetrics;

use OpenMetricsPhp\Exposition\Text\Collections\CounterCollection;
use OpenMetricsPhp\Exposition\Text\Metrics\Counter;
use OpenMetricsPhp\Exposition\Text\Types\Label;
use OpenMetricsPhp\Exposition\Text\Types\MetricName;
use PHPUnit\Framework\TestCase;
use function dirname;
use function file_put_contents;
use function shell_exec;
use function unlink;

final class PythonParserTest extends TestCase
{
/**
* @throws \OpenMetricsPhp\Exposition\Text\Exceptions\InvalidArgumentException
* @throws \PHPUnit\Framework\ExpectationFailedException
* @throws \SebastianBergmann\RecursionContext\InvalidArgumentException
*/
public function testCanParseCounterMetricsWithPythonParser() : void
{
$filename = dirname( __DIR__, 3 ) . '/build/counter_metrics.txt';
$command = sprintf( 'python %s/parseFile.py %s', __DIR__, $filename );
$expectedOutput = "Name: a_total Labels: {u'foo': u'4'} Value: 1.0 Timestamp: 1234567.000000000\n";

$collection = CounterCollection::fromCounters(
MetricName::fromString( 'a' ),
Counter::fromValueAndTimestamp( 1, 1234567 )
->withLabels(
Label::fromNameAndValue( 'foo', '4' )
)
);

file_put_contents( $filename, $collection->getMetricsString() . "\n# EOF" );

$output = shell_exec( $command );

$this->assertSame( $expectedOutput, $output );

@unlink( $filename );
}
}
9 changes: 9 additions & 0 deletions tests/Integration/OpenMetrics/parseFile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from prometheus_client.openmetrics.parser import text_string_to_metric_families
from path import path
import sys

metrics = path(sys.argv[1]).bytes()

for family in text_string_to_metric_families(metrics):
for sample in family.samples:
print("Name: {0} Labels: {1} Value: {2} Timestamp: {3}".format(*sample))

0 comments on commit 775a935

Please sign in to comment.