-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration test to parse counter with openmetrics python parser, #3
- Loading branch information
1 parent
78bc5df
commit 775a935
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |