1+ using System ;
2+ using System . Linq ;
3+ using Prometheus . Advanced . DataContracts ;
4+ using Prometheus . Client . Advanced ;
5+ using Xunit ;
6+
7+ namespace Prometheus . Client . Tests
8+ {
9+ public class MetricsTests
10+ {
11+ public MetricsTests ( )
12+ {
13+ PrometheusCollectorRegistry . Instance . Clear ( ) ;
14+ }
15+
16+ [ Fact ]
17+ public void api_usage ( )
18+ {
19+ var gauge = Metrics . CreateGauge ( "name1" , "help1" ) ;
20+ gauge . Inc ( ) ;
21+ Assert . Equal ( 1 , gauge . Value ) ;
22+ gauge . Inc ( 3.2 ) ;
23+ Assert . Equal ( 4.2 , gauge . Value ) ;
24+ gauge . Set ( 4 ) ;
25+ Assert . Equal ( 4 , gauge . Value ) ;
26+ gauge . Dec ( 0.2 ) ;
27+ Assert . Equal ( 3.8 , gauge . Value ) ;
28+
29+ Assert . Throws < InvalidOperationException > ( ( ) => gauge . Labels ( "1" ) ) ;
30+
31+ var counter = Metrics . CreateCounter ( "name2" , "help2" , "label1" ) ;
32+ counter . Inc ( ) ;
33+ counter . Inc ( 3.2 ) ;
34+ Assert . Equal ( 4.2 , counter . Value ) ;
35+
36+ Assert . Equal ( 0 , counter . Labels ( "a" ) . Value ) ;
37+ counter . Labels ( "a" ) . Inc ( 3.3 ) ;
38+ Assert . Equal ( 3.3 , counter . Labels ( "a" ) . Value ) ;
39+ counter . Labels ( "a" ) . Inc ( 1.1 ) ;
40+ Assert . Equal ( 4.4 , counter . Labels ( "a" ) . Value ) ;
41+ }
42+
43+ [ Fact ]
44+ public void counter_collection ( )
45+ {
46+ var counter = Metrics . CreateCounter ( "name1" , "help1" , "label1" ) ;
47+
48+ counter . Inc ( ) ;
49+ counter . Inc ( 3.2 ) ;
50+ counter . Labels ( "abc" ) . Inc ( 3.2 ) ;
51+
52+ MetricFamily [ ] exported = PrometheusCollectorRegistry . Instance . CollectAll ( ) . ToArray ( ) ;
53+
54+ Assert . Equal ( 1 , exported . Length ) ;
55+ var familiy1 = exported [ 0 ] ;
56+ Assert . Equal ( "name1" , familiy1 . name ) ;
57+ Assert . Equal ( "help1" , familiy1 . help ) ;
58+ var metrics = familiy1 . metric ;
59+ Assert . Equal ( 2 , metrics . Count ) ;
60+
61+ foreach ( var metric in metrics )
62+ {
63+ Assert . Null ( metric . gauge ) ;
64+ Assert . Null ( metric . histogram ) ;
65+ Assert . Null ( metric . summary ) ;
66+ Assert . Null ( metric . untyped ) ;
67+ Assert . NotNull ( metric . counter ) ;
68+ }
69+
70+ Assert . Equal ( 4.2 , metrics [ 0 ] . counter . value ) ;
71+ Assert . Equal ( 0 , metrics [ 0 ] . label . Count ) ;
72+
73+ Assert . Equal ( 3.2 , metrics [ 1 ] . counter . value ) ;
74+ var labelPairs = metrics [ 1 ] . label ;
75+ Assert . Equal ( 1 , labelPairs . Count ) ;
76+ Assert . Equal ( "label1" , labelPairs [ 0 ] . name ) ;
77+ Assert . Equal ( "abc" , labelPairs [ 0 ] . value ) ;
78+ }
79+
80+ [ Fact ]
81+ public void custom_registry ( )
82+ {
83+ var myRegistry = new PrometheusCollectorRegistry ( ) ;
84+ var counter1 = Metrics . WithCustomRegistry ( myRegistry ) . CreateCounter ( "counter1" , "help1" ) ; //registered on a custom registry
85+
86+ var counter2 = Metrics . CreateCounter ( "counter1" , "help1" ) ; //created on different registry - same name is hence permitted
87+
88+ counter1 . Inc ( 3 ) ;
89+ counter2 . Inc ( 4 ) ;
90+
91+ Assert . Equal ( 3 , myRegistry . CollectAll ( ) . ToArray ( ) [ 0 ] . metric [ 0 ] . counter . value ) ; //counter1 == 3
92+ Assert . Equal ( 4 , PrometheusCollectorRegistry . Instance . CollectAll ( ) . ToArray ( ) [ 0 ] . metric [ 0 ] . counter . value ) ; //counter2 == 4
93+ }
94+
95+ [ Fact ]
96+ public void gauge_collection ( )
97+ {
98+ var gauge = Metrics . CreateGauge ( "name1" , "help1" ) ;
99+
100+ gauge . Inc ( ) ;
101+ gauge . Inc ( 3.2 ) ;
102+ gauge . Set ( 4 ) ;
103+ gauge . Dec ( 0.2 ) ;
104+
105+ var exported = PrometheusCollectorRegistry . Instance . CollectAll ( ) . ToArray ( ) ;
106+
107+ Assert . Equal ( 1 , exported . Length ) ;
108+ var familiy1 = exported [ 0 ] ;
109+ Assert . Equal ( "name1" , familiy1 . name ) ;
110+ var metrics = familiy1 . metric ;
111+ Assert . Equal ( 1 , metrics . Count ) ;
112+ foreach ( var metric in metrics )
113+ {
114+ Assert . Null ( metric . counter ) ;
115+ Assert . Null ( metric . histogram ) ;
116+ Assert . Null ( metric . summary ) ;
117+ Assert . Null ( metric . untyped ) ;
118+ Assert . NotNull ( metric . gauge ) ;
119+ }
120+
121+ Assert . Equal ( 3.8 , metrics [ 0 ] . gauge . value ) ;
122+ }
123+
124+ [ Fact ]
125+ public void histogram_tests ( )
126+ {
127+ Histogram histogram = Metrics . CreateHistogram ( "hist1" , "help" , new [ ] { 1.0 , 2.0 , 3.0 } ) ;
128+ histogram . Observe ( 1.5 ) ;
129+ histogram . Observe ( 2.5 ) ;
130+ histogram . Observe ( 1 ) ;
131+ histogram . Observe ( 2.4 ) ;
132+ histogram . Observe ( 2.1 ) ;
133+ histogram . Observe ( 0.4 ) ;
134+ histogram . Observe ( 1.4 ) ;
135+ histogram . Observe ( 1.5 ) ;
136+ histogram . Observe ( 3.9 ) ;
137+
138+ var metric = histogram . Collect ( ) . metric [ 0 ] ;
139+ Assert . NotNull ( metric . histogram ) ;
140+ Assert . Equal ( 9ul , metric . histogram . sample_count ) ;
141+ Assert . Equal ( 16.7 , metric . histogram . sample_sum ) ;
142+ Assert . Equal ( 4 , metric . histogram . bucket . Count ) ;
143+ Assert . Equal ( 2ul , metric . histogram . bucket [ 0 ] . cumulative_count ) ;
144+ Assert . Equal ( 5ul , metric . histogram . bucket [ 1 ] . cumulative_count ) ;
145+ Assert . Equal ( 8ul , metric . histogram . bucket [ 2 ] . cumulative_count ) ;
146+ Assert . Equal ( 9ul , metric . histogram . bucket [ 3 ] . cumulative_count ) ;
147+ }
148+
149+ [ Fact ]
150+ public void summary_tests ( )
151+ {
152+ var summary = Metrics . CreateSummary ( "summ1" , "help" ) ;
153+
154+ summary . Observe ( 1 ) ;
155+ summary . Observe ( 2 ) ;
156+ summary . Observe ( 3 ) ;
157+
158+ var metric = summary . Collect ( ) . metric [ 0 ] ;
159+ Assert . NotNull ( metric . summary ) ;
160+ Assert . Equal ( 3ul , metric . summary . sample_count ) ;
161+ Assert . Equal ( 6 , metric . summary . sample_sum ) ;
162+ }
163+
164+ [ Fact ]
165+ public void same_labels_return_same_instance ( )
166+ {
167+ var gauge = Metrics . CreateGauge ( "name1" , "help1" , "label1" ) ;
168+
169+ var labelled1 = gauge . Labels ( "1" ) ;
170+
171+ var labelled2 = gauge . Labels ( "1" ) ;
172+
173+ Assert . Same ( labelled1 , labelled2 ) ;
174+ }
175+
176+ [ Fact ]
177+ public void cannot_create_metrics_with_the_same_name_but_different_labels ( )
178+ {
179+ Metrics . CreateGauge ( "name1" , "h" ) ;
180+ Assert . Throws < InvalidOperationException > ( ( ) => Metrics . CreateCounter ( "name1" , "h" , "label1" ) ) ;
181+ }
182+
183+ [ Fact ]
184+ public void metric_names ( )
185+ {
186+ Assert . Throws < ArgumentException > ( ( ) => Metrics . CreateGauge ( "my-metric" , "help" ) ) ;
187+ Assert . Throws < ArgumentException > ( ( ) => Metrics . CreateGauge ( "my!metric" , "help" ) ) ;
188+ Assert . Throws < ArgumentException > ( ( ) => Metrics . CreateGauge ( "%" , "help" ) ) ;
189+ Assert . Throws < ArgumentException > ( ( ) => Metrics . CreateGauge ( "5a" , "help" ) ) ;
190+
191+ Metrics . CreateGauge ( "abc" , "help" ) ;
192+ Metrics . CreateGauge ( "myMetric2" , "help" ) ;
193+ Metrics . CreateGauge ( "a:3" , "help" ) ;
194+ }
195+
196+ [ Fact ]
197+ public void label_names ( )
198+ {
199+ Assert . Throws < ArgumentException > ( ( ) => Metrics . CreateGauge ( "a" , "help1" , "my-metric" ) ) ;
200+ Assert . Throws < ArgumentException > ( ( ) => Metrics . CreateGauge ( "a" , "help1" , "my!metric" ) ) ;
201+ Assert . Throws < ArgumentException > ( ( ) => Metrics . CreateGauge ( "a" , "help1" , "my%metric" ) ) ;
202+ Assert . Throws < ArgumentException > ( ( ) => Metrics . CreateHistogram ( "a" , "help1" , null , "le" ) ) ;
203+ Metrics . CreateGauge ( "a" , "help1" , "my:metric" ) ;
204+ Metrics . CreateGauge ( "b" , "help1" , "good_name" ) ;
205+
206+ Assert . Throws < ArgumentException > ( ( ) => Metrics . CreateGauge ( "c" , "help1" , "__reserved" ) ) ;
207+ }
208+ }
209+ }
0 commit comments