File: 15-labels.t

package info (click to toggle)
libprometheus-tiny-perl 0.011-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 180 kB
  • sloc: perl: 746; makefile: 2
file content (154 lines) | stat: -rw-r--r-- 4,479 bytes parent folder | download | duplicates (2)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!perl

use warnings;
use strict;

use Test::More;

use Prometheus::Tiny;

{
  my $p = Prometheus::Tiny->new;
  $p->set('some_metric', 5, { some_label => 'aaa' });
  is $p->format, <<EOF, 'single metric with label formatted correctly';
some_metric{some_label="aaa"} 5
EOF
}

{
  my $p = Prometheus::Tiny->new;
  $p->set('some_metric', 5, { some_label => "aaa" });
  $p->set('other_metric', 10, { other_label => "bbb" });
  is $p->format, <<EOF, 'multiple metrics with labels formatted correctly';
other_metric{other_label="bbb"} 10
some_metric{some_label="aaa"} 5
EOF
}

{
  my $p = Prometheus::Tiny->new;
  $p->set('some_metric', 5, { some_label => "aaa" });
  $p->set('other_metric', 10);
  is $p->format, <<EOF, 'multiple metrics with mixed labels formatted correctly';
other_metric 10
some_metric{some_label="aaa"} 5
EOF
}

{
  my $p = Prometheus::Tiny->new;
  $p->set('some_metric', 3, { some_label => "aaa" });
  $p->set('some_metric', 8, { some_label => "aaa" });
  is $p->format, <<EOF, 'single metric with same label is overwritten correctly';
some_metric{some_label="aaa"} 8
EOF
}

{
  my $p = Prometheus::Tiny->new;
  $p->set('some_metric', 2, { some_label => "aaa" });
  $p->set('some_metric', 7, { other_label => "bbb" });
  is $p->format, <<EOF, 'single metric with different label keys are both formatted';
some_metric{other_label="bbb"} 7
some_metric{some_label="aaa"} 2
EOF
}

{
  my $p = Prometheus::Tiny->new;
  $p->set('some_metric', 3, { some_label => "aaa" });
  $p->set('some_metric', 8, { some_label => "bbb" });
  is $p->format, <<EOF, 'single metric with different label values are both formatted';
some_metric{some_label="aaa"} 3
some_metric{some_label="bbb"} 8
EOF
}


{
  my $p = Prometheus::Tiny->new;
  $p->set('some_metric', 5, { some_label => 'aaa' }, 1234);
  is $p->format, <<EOF, 'single metric with label and timestamp formatted correctly';
some_metric{some_label="aaa"} 5 1234
EOF
}

{
  my $p = Prometheus::Tiny->new;
  $p->set('some_metric', 5, { some_label => "aaa" }, 1234);
  $p->set('other_metric', 10, { other_label => "bbb" }, 2345);
  is $p->format, <<EOF, 'multiple metrics with labels and timestamps formatted correctly';
other_metric{other_label="bbb"} 10 2345
some_metric{some_label="aaa"} 5 1234
EOF
}

{
  my $p = Prometheus::Tiny->new;
  $p->set('some_metric', 5, { some_label => "aaa" }, 1234);
  $p->set('some_metric', 5, { some_label => "bbb" }, 2345);
  $p->set('some_metric', 5, { some_label => "ccc" }, 3456);
  $p->set('other_metric', 10, { other_label => "bbb" }, 4567);
  is $p->format, <<EOF, 'multiple metrics with labels and timestamps formatted correctly';
other_metric{other_label="bbb"} 10 4567
some_metric{some_label="aaa"} 5 1234
some_metric{some_label="bbb"} 5 2345
some_metric{some_label="ccc"} 5 3456
EOF
}

{
  my $p = Prometheus::Tiny->new;
  $p->set('some_metric', 5, { some_label => "a\\\n\n\\aa\\" }, 1234);
  $p->set('some_metric', 5, { some_label => "b\nbb\nx" }, 2345);
  $p->set('some_metric', 5, { some_label => 'ccc""' }, 3456);
  $p->set('other_metric', 10, { other_label => "bbb" }, 4567);
  is $p->format, <<'EOF', 'multiple metrics with escaped char labels and timestamps formatted correctly';
other_metric{other_label="bbb"} 10 4567
some_metric{some_label="a\\\n\n\\aa\\"} 5 1234
some_metric{some_label="b\nbb\nx"} 5 2345
some_metric{some_label="ccc\"\""} 5 3456
EOF
}

{
  my $p = Prometheus::Tiny->new;
  $p->set('some_metric', 5, { some_label => "aaa" }, 1234);
  $p->set('other_metric', 10);
  is $p->format, <<EOF, 'multiple metrics with mixed labels formatted correctly';
other_metric 10
some_metric{some_label="aaa"} 5 1234
EOF
}

{
  my $p = Prometheus::Tiny->new(default_labels => { default_label => 'frob' });
  $p->set('some_metric', 10);
  is $p->format, <<EOF, 'metric with no label gets single default label';
some_metric{default_label="frob"} 10
EOF
}

{
  my $p = Prometheus::Tiny->new(default_labels => {
    default_one => 'whiz',
    default_two => 'bang',
  });
  $p->set('some_metric', 10);
  is $p->format, <<EOF, 'metric with no label gets all default labels';
some_metric{default_one="whiz",default_two="bang"} 10
EOF
}

{
  my $p = Prometheus::Tiny->new(default_labels => {
    default_one => 'whiz',
    default_two => 'bang',
  });
  $p->set('some_metric', 10, { other => 'pow', default_two => 'blam' });
  is $p->format, <<EOF, 'we can overwrite default labels if we want';
some_metric{default_one="whiz",default_two="blam",other="pow"} 10
EOF
}

done_testing;