1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#!/usr/bin/perl
use v5.14;
use warnings;
use Test2::V0;
use Net::Prometheus;
use Net::Prometheus::Metric;
my $sample;
no warnings 'redefine';
local *Net::Prometheus::Metric::_type = sub { "untyped" };
local *Net::Prometheus::Metric::samples = sub {
return $sample;
};
my $client = Net::Prometheus->new(
disable_process_collector => 1,
disable_perl_collector => 1,
);
{
my $metric = $client->register( Net::Prometheus::Metric->new(
name => "metric",
labels => [ "lab" ],
help => "",
) );
sub test_label
{
my ( $label, $str, $name ) = @_;
$sample = $metric->make_sample(
"", $metric->labels( $label )->labelkey, 0
);
is( ( split m/\n/, $client->render )[2],
qq(metric{lab="$str"} 0),
"render for $name"
);
}
test_label( "a", "a",
'basic label',
);
test_label( "val here", "val here",
'label with whitespace',
);
test_label( q("quoted"), q(\"quoted\"),
'label with quotes',
);
test_label( 'ab\cd', "ab\\\\cd",
'label with backslash',
);
test_label( "line\nfeed", "line\\nfeed",
'label with linefeed',
);
test_label( "with\0null", "with\0null",
'label with NUL byte',
);
$client->unregister( $metric );
is( $client->render, "",
'render after ->unregister'
);
}
{
my $metric = $client->register( Net::Prometheus::Metric->new(
name => "metric",
labels => [ "x", "y" ],
help => "",
) );
$sample = $metric->make_sample(
"", $metric->labels( "a", "b" )->labelkey, 0
);
is( ( split m/\n/, $client->render )[2],
q(metric{x="a",y="b"} 0),
'_render_value for multi-dimensional label' );
$client->unregister( $metric );
}
done_testing;
|