4343 */
4444public class TestPrometheusFormatter {
4545
46- @ Test
46+ @ Test ( timeout = 30000 )
4747 public void testStatsOutput () throws Exception {
4848 PrometheusMetricsProvider provider = new PrometheusMetricsProvider ();
4949 StatsLogger statsLogger = provider .getStatsLogger ("test" );
@@ -56,6 +56,12 @@ public void testStatsOutput() throws Exception {
5656 opStats .registerSuccessfulEvent (10 , TimeUnit .MILLISECONDS );
5757 opStats .registerSuccessfulEvent (5 , TimeUnit .MILLISECONDS );
5858
59+ OpStatsLogger opStats1 = statsLogger .scopeLabel ("test_label" , "test_value" )
60+ .getOpStatsLogger ("op_label" );
61+ opStats1 .registerSuccessfulEvent (10 , TimeUnit .MILLISECONDS );
62+ opStats1 .registerSuccessfulEvent (5 , TimeUnit .MILLISECONDS );
63+ opStats1 .registerFailedEvent (1 , TimeUnit .MILLISECONDS );
64+
5965 provider .rotateLatencyCollection ();
6066
6167 StringWriter writer = new StringWriter ();
@@ -112,6 +118,52 @@ public void testStatsOutput() throws Exception {
112118 }
113119
114120 assertTrue (found );
121+
122+ // test_op_label_sum
123+ cm = (List <Metric >) metrics .get ("test_op_label_sum" );
124+ assertEquals (2 , cm .size ());
125+ m = cm .get (0 );
126+ assertEquals (2 , m .tags .size ());
127+ assertEquals (1.0 , m .value , 0.0 );
128+ assertEquals ("false" , m .tags .get ("success" ));
129+ assertEquals ("test_value" , m .tags .get ("test_label" ));
130+
131+ m = cm .get (1 );
132+ assertEquals (15.0 , m .value , 0.0 );
133+ assertEquals (2 , m .tags .size ());
134+ assertEquals ("true" , m .tags .get ("success" ));
135+ assertEquals ("test_value" , m .tags .get ("test_label" ));
136+
137+ // test_op_label_count
138+ cm = (List <Metric >) metrics .get ("test_op_label_count" );
139+ assertEquals (2 , cm .size ());
140+ m = cm .get (0 );
141+ assertEquals (1 , m .value , 0.0 );
142+ assertEquals (2 , m .tags .size ());
143+ assertEquals ("false" , m .tags .get ("success" ));
144+ assertEquals ("test_value" , m .tags .get ("test_label" ));
145+
146+ m = cm .get (1 );
147+ assertEquals (2.0 , m .value , 0.0 );
148+ assertEquals (2 , m .tags .size ());
149+ assertEquals ("true" , m .tags .get ("success" ));
150+ assertEquals ("test_value" , m .tags .get ("test_label" ));
151+
152+ // Latency
153+ cm = (List <Metric >) metrics .get ("test_op_label" );
154+ assertEquals (14 , cm .size ());
155+
156+ found = false ;
157+ for (Metric mt : cm ) {
158+ if ("true" .equals (mt .tags .get ("success" ))
159+ && "test_value" .equals (mt .tags .get ("test_label" ))
160+ && "1.0" .equals (mt .tags .get ("quantile" ))) {
161+ assertEquals (10.0 , mt .value , 0.0 );
162+ found = true ;
163+ }
164+ }
165+
166+ assertTrue (found );
115167 }
116168
117169 /**
@@ -126,6 +178,7 @@ private static Multimap<String, Metric> parseMetrics(String metrics) {
126178 // pulsar_subscriptions_count{cluster="standalone", namespace="sample/standalone/ns1",
127179 // topic="persistent://sample/standalone/ns1/test-2"} 0.0 1517945780897
128180 Pattern pattern = Pattern .compile ("^(\\ w+)(\\ {([^\\ }]+)\\ })?\\ s(-?[\\ d\\ w\\ .]+)(\\ s(\\ d+))?$" );
181+ Pattern formatPattern = Pattern .compile ("^(\\ w+)(\\ {(\\ w+=[\\ \" \\ .\\ w]+(,\\ s?\\ w+=[\\ \" \\ .\\ w]+)*)\\ })?\\ s(-?[\\ d\\ w\\ .]+)(\\ s(\\ d+))?$" );
129182 Pattern tagsPattern = Pattern .compile ("(\\ w+)=\" ([^\" ]+)\" (,\\ s?)?" );
130183
131184 Splitter .on ("\n " ).split (metrics ).forEach (line -> {
@@ -135,6 +188,7 @@ private static Multimap<String, Metric> parseMetrics(String metrics) {
135188
136189 System .err .println ("LINE: '" + line + "'" );
137190 Matcher matcher = pattern .matcher (line );
191+ Matcher formatMatcher = pattern .matcher (line );
138192 System .err .println ("Matches: " + matcher .matches ());
139193 System .err .println (matcher );
140194
@@ -144,6 +198,7 @@ private static Multimap<String, Metric> parseMetrics(String metrics) {
144198 }
145199
146200 checkArgument (matcher .matches ());
201+ checkArgument (formatMatcher .matches ());
147202 String name = matcher .group (1 );
148203
149204 Metric m = new Metric ();
0 commit comments