-
Notifications
You must be signed in to change notification settings - Fork 66
/
MpMetricTest.java
1399 lines (1190 loc) · 61.4 KB
/
MpMetricTest.java
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
**********************************************************************
* Copyright (c) 2017, 2019 Contributors to the Eclipse Foundation
*
* See the NOTICES file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
**********************************************************************/
package org.eclipse.microprofile.metrics.test;
import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.when;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.allOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.stream.Collectors;
import javax.inject.Inject;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.eclipse.microprofile.metrics.MetricRegistry;
import org.hamcrest.Matcher;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.junit.InSequence;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import io.restassured.RestAssured;
import io.restassured.builder.ResponseBuilder;
import io.restassured.http.Header;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
/**
* Rest Test Kit
*
* @author Heiko W. Rupp <hrupp@redhat.com>
* @author Don Bourne <dbourne@ca.ibm.com>
*/
@SuppressWarnings("CdiInjectionPointsInspection")
@RunWith(Arquillian.class)
public class MpMetricTest {
private static final String APPLICATION_JSON = "application/json";
private static final String TEXT_PLAIN = "text/plain";
private static final String JSON_APP_LABEL_REGEX = ";_app=[-/A-Za-z0-9]+([;\\\"]?)";
private static final String JSON_APP_LABEL_REGEXS_SUB = "$1";
private static final String OPENMETRICS_APP_LABEL_REGEX = "_app=\"[-/A-Za-z0-9]+\"";
private static final String DEFAULT_PROTOCOL = "http";
private static final String DEFAULT_HOST = "localhost";
private static final int DEFAULT_PORT = 8080;
public static final double TOLERANCE = 0.025;
private static String filterOutAppLabelJSON(String responseBody){
return responseBody.replaceAll(JSON_APP_LABEL_REGEX, JSON_APP_LABEL_REGEXS_SUB);
}
private static String filterOutAppLabelOpenMetrics(String responseBody) {
return responseBody.replaceAll(OPENMETRICS_APP_LABEL_REGEX, "").replaceAll("\\{,", "{").replaceAll(",\\}", "}");
}
@Inject
private MetricAppBean metricAppBean;
@BeforeClass
static public void setup() throws MalformedURLException {
// set base URI and port number to use for all requests
String serverUrl = System.getProperty("test.url");
String protocol = DEFAULT_PROTOCOL;
String host = DEFAULT_HOST;
int port = DEFAULT_PORT;
if (serverUrl != null) {
URL url = new URL(serverUrl);
protocol = url.getProtocol();
host = url.getHost();
port = (url.getPort() == -1) ? DEFAULT_PORT : url.getPort();
}
RestAssured.baseURI = protocol + "://" + host;
RestAssured.port = port;
// set user name and password to use for basic authentication for all requests
String userName = System.getProperty("test.user");
String password = System.getProperty("test.pwd");
if (userName != null && password != null) {
RestAssured.authentication = RestAssured.basic(userName, password);
RestAssured.useRelaxedHTTPSValidation();
}
}
@Deployment
public static WebArchive createDeployment() {
WebArchive jar = ShrinkWrap.create(WebArchive.class).addClass(MetricAppBean.class)
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
System.out.println(jar.toString(true));
return jar;
}
@Test
@RunAsClient
@InSequence(1)
public void testApplicationJsonResponseContentType() {
Header acceptHeader = new Header("Accept", APPLICATION_JSON);
given().header(acceptHeader).when().get("/metrics").then().statusCode(200).and().contentType(APPLICATION_JSON);
}
@Test
@RunAsClient
@InSequence(2)
public void testTextPlainResponseContentType() {
Header acceptHeader = new Header("Accept", TEXT_PLAIN);
given().header(acceptHeader).when().get("/metrics").then().statusCode(200).and().contentType(TEXT_PLAIN);
}
@Test
@RunAsClient
@InSequence(3)
public void testBadSubTreeWillReturn404() {
when().get("/metrics/bad-tree").then().statusCode(404);
}
@Test
@RunAsClient
@InSequence(4)
public void testListsAllJson() {
Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
Header acceptHeader = new Header("Accept", APPLICATION_JSON);
Map response = given().header(acceptHeader).when().get("/metrics").as(Map.class);
// all servers should have some base metrics
assertTrue(response.containsKey("base"));
// There may be application metrics, so check if the key exists and bail if it has no data
if (response.containsKey("application")) {
Map applicationData = (Map) response.get("application");
assertThat(applicationData.size(), greaterThan(0));
}
}
@Test
@RunAsClient
@InSequence(5)
public void testBase() {
Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
Response resp = given().header("Accept", APPLICATION_JSON).get("/metrics/base");
JsonPath filteredJSONPath = new JsonPath(filterOutAppLabelJSON(resp.jsonPath().prettify()));
ResponseBuilder responseBuilder = new ResponseBuilder();
responseBuilder.clone(resp);
responseBuilder.setBody(filteredJSONPath.prettify());
resp = responseBuilder.build();
resp.then().statusCode(200).and()
.contentType(MpMetricTest.APPLICATION_JSON).and().body(containsString("thread.max.count;tier=integration"));
}
@Test
@RunAsClient
@InSequence(6)
public void testBaseOpenMetrics() {
Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
Response resp = given().header("Accept", TEXT_PLAIN).get("/metrics/base");
ResponseBuilder responseBuilder = new ResponseBuilder();
responseBuilder.clone(resp);
responseBuilder.setBody(filterOutAppLabelOpenMetrics(resp.getBody().asString()));
resp = responseBuilder.build();
resp.then().statusCode(200).and().contentType(TEXT_PLAIN).and()
.body(containsString("# TYPE base_thread_max_count"), containsString("base_thread_max_count{tier=\"integration\"}"));
}
@Test
@RunAsClient
@InSequence(7)
public void testBaseAttributeJson() {
Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
Header wantJson = new Header("Accept", APPLICATION_JSON);
Response resp = given().header(wantJson).get("/metrics/base/thread.max.count");
JsonPath filteredJSONPath = new JsonPath(filterOutAppLabelJSON(resp.jsonPath().prettify()));
ResponseBuilder responseBuilder = new ResponseBuilder();
responseBuilder.clone(resp);
responseBuilder.setBody(filteredJSONPath.prettify());
resp = responseBuilder.build();
resp.then().statusCode(200).and()
.contentType(MpMetricTest.APPLICATION_JSON).and().body(containsString("thread.max.count;tier=integration"));
}
@Test
@RunAsClient
@InSequence(8)
public void testBaseSingularMetricsPresent() {
Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
Header wantJson = new Header("Accept", APPLICATION_JSON);
JsonPath jsonPath = given().header(wantJson).get("/metrics/base").jsonPath();
JsonPath filteredJSONPath = new JsonPath(jsonPath.prettify().replaceAll(JSON_APP_LABEL_REGEX, JSON_APP_LABEL_REGEXS_SUB));
Map<String, Object> elements = filteredJSONPath.getMap(".");
List<String> missing = new ArrayList<>();
Map<String, MiniMeta> baseNames = getExpectedMetadataFromXmlFile(MetricRegistry.Type.BASE);
for (MiniMeta item : baseNames.values()) {
if (item.name.startsWith("gc.")) {
continue;
}
if (!elements.containsKey(item.toJSONName()) && !baseNames.get(item.name).optional) {
missing.add(item.toJSONName());
}
}
assertTrue("Following base items are missing: " + Arrays.toString(missing.toArray()), missing.isEmpty());
}
@Test
@RunAsClient
@InSequence(9)
public void testBaseAttributeOpenMetrics() {
Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
Response resp = given().header("Accept", TEXT_PLAIN).get("/metrics/base/thread.max.count");
ResponseBuilder responseBuilder = new ResponseBuilder();
responseBuilder.clone(resp);
responseBuilder.setBody(filterOutAppLabelOpenMetrics(resp.getBody().asString()));
resp = responseBuilder.build();
resp.then().statusCode(200).and()
.contentType(TEXT_PLAIN).and().body(containsString("# TYPE base_thread_max_count"),
containsString("base_thread_max_count{tier=\"integration\"}"));
}
@Test
@RunAsClient
@InSequence(10)
public void testBaseMetadata() {
Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
Header wantJson = new Header("Accept", APPLICATION_JSON);
given().header(wantJson).options("/metrics/base").then().statusCode(200).and()
.contentType(MpMetricTest.APPLICATION_JSON);
}
@Test
@RunAsClient
@InSequence(11)
public void testBaseMetadataSingluarItems() {
Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
Header wantJson = new Header("Accept", APPLICATION_JSON);
JsonPath jsonPath = given().header(wantJson).options("/metrics/base").jsonPath();
Map<String, Object> elements = jsonPath.getMap(".");
List<String> missing = new ArrayList<>();
Map<String, MiniMeta> baseNames = getExpectedMetadataFromXmlFile(MetricRegistry.Type.BASE);
for (String item : baseNames.keySet()) {
if (item.startsWith("gc.") || baseNames.get(item).optional) {
continue;
}
if (!elements.containsKey(item)) {
missing.add(item);
}
}
assertTrue("Following base items are missing: " + Arrays.toString(missing.toArray()), missing.isEmpty());
}
@Test
@RunAsClient
@InSequence(12)
public void testBaseMetadataTypeAndUnit() {
Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
Header wantJson = new Header("Accept", APPLICATION_JSON);
JsonPath jsonPath = given().header(wantJson).options("/metrics/base").jsonPath();
Map<String, Map<String, Object>> elements = jsonPath.getMap(".");
Map<String, MiniMeta> expectedMetadata = getExpectedMetadataFromXmlFile(MetricRegistry.Type.BASE);
checkMetadataPresent(elements, expectedMetadata);
}
private void checkMetadataPresent(Map<String, Map<String, Object>> elements, Map<String, MiniMeta> expectedMetadata) {
for (Map.Entry<String, MiniMeta> entry : expectedMetadata.entrySet()) {
MiniMeta item = entry.getValue();
if (item.name.startsWith("gc.") || expectedMetadata.get(item.name).optional) {
continue; // We don't deal with them here
}
Map<String, Object> fromServer = elements.get(item.name);
assertNotNull("Got no data for metric " + item.name + " from the server", fromServer);
assertEquals("expected " + item.type + " but got "
+ fromServer.get("type") + " for type of metric " + item.name, item.type, fromServer.get("type"));
assertEquals("expected " + item.unit + " but got "
+ fromServer.get("unit") + " for unit of metric " + item.name, item.unit, fromServer.get("unit"));
if(item.description != null && !item.description.isEmpty()) {
assertEquals("expected " + item.description + " but got "
+ fromServer.get("description") + " for description of metric " + item.name,
item.description, fromServer.get("description"));
}
if(item.displayName != null && !item.displayName.isEmpty()) {
assertEquals("expected " + item.displayName + " but got "
+ fromServer.get("displayName") + " for displayName of " + item.name,
item.displayName, fromServer.get("displayName"));
}
}
}
@Test
@RunAsClient
@InSequence(13)
public void testOpenMetricsFormatNoBadChars() {
Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
Header wantOpenMetricsFormat = new Header("Accept", TEXT_PLAIN);
String data = given().header(wantOpenMetricsFormat).get("/metrics/base").asString();
String[] lines = data.split("\n");
for (String line : lines) {
if (line.startsWith("#")) {
continue;
}
String nameAndTagsPart = line.substring(0, line.lastIndexOf(" "));
String namePart = nameAndTagsPart.contains("{") ?
nameAndTagsPart.substring(0, nameAndTagsPart.lastIndexOf("{")) : nameAndTagsPart;
assertFalse("Name has illegal chars " + line, namePart.matches(".*[-.].*"));
assertFalse("Found __ in " + line, line.matches(".*__.*"));
}
}
/*
* Technically OpenMetrics has no metadata call and this is included inline
* in the response.
*/
@Test
@RunAsClient
@InSequence(14)
public void testBaseMetadataSingluarItemsOpenMetrics() {
Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
Header wantOpenMetricsFormat = new Header("Accept", TEXT_PLAIN);
String data = given().header(wantOpenMetricsFormat).get("/metrics/base").asString();
String[] lines = data.split("\n");
Map<String, MiniMeta> expectedMetadata = getExpectedMetadataFromXmlFile(MetricRegistry.Type.BASE);
for (MiniMeta mm : expectedMetadata.values()) {
boolean found = false;
// Skip GC
if (mm.name.startsWith("gc.") || expectedMetadata.get(mm.name).optional) {
continue;
}
for (String line : lines) {
if (!line.startsWith("# TYPE base_")) {
continue;
}
String fullLine = line;
int c = line.indexOf("_");
line = line.substring(c + 1);
String promName = mm.toPromString();
String[] tmp = line.split(" ");
assertEquals("Bad entry: " + line, tmp.length, 2);
if (tmp[0].startsWith(promName)) {
found = true;
assertEquals("Expected [" + mm.toString() + "] got [" + fullLine + "]", tmp[1], mm.type);
}
}
assertTrue("Not found [" + mm.toString() + "]", found);
}
}
@Test
@RunAsClient
@InSequence(15)
public void testBaseMetadataGarbageCollection() {
Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
Header wantJson = new Header("Accept", APPLICATION_JSON);
JsonPath jsonPath = given().header(wantJson).options("/metrics/base").jsonPath();
int count = 0;
Map<String, Object> elements = jsonPath.getMap(".");
for (String name : elements.keySet()) {
if (name.startsWith("gc.")) {
assertTrue(name.endsWith(".total") || name.endsWith(".time"));
count++;
}
}
assertThat(count, greaterThan(0));
}
@Test
@RunAsClient
@InSequence(16)
public void testApplicationMetadataOkJson() {
Header wantJson = new Header("Accept", APPLICATION_JSON);
Response response = given().header(wantJson).options("/metrics/application");
int code = response.getStatusCode();
assertTrue(code == 200 || code == 204);
}
@Test
@InSequence(17)
public void testSetupApplicationMetrics() {
metricAppBean.countMe();
metricAppBean.countMeA();
metricAppBean.countMeB();
metricAppBean.gaugeMe();
metricAppBean.gaugeMeA();
metricAppBean.gaugeMeB();
metricAppBean.gaugeMeTagged();
metricAppBean.gaugeMeTaggedOne();
metricAppBean.gaugeMeTaggedTwo();
metricAppBean.histogramMe();
metricAppBean.meterMe();
metricAppBean.meterMeA();
metricAppBean.timeMe();
metricAppBean.timeMeA();
metricAppBean.simpleTimeMe();
metricAppBean.simpleTimeMeA();
metricAppBean.concGaugeMeA();
}
@Test
@RunAsClient
@InSequence(18)
public void testApplicationMetricsJSON() {
Header wantJson = new Header("Accept", APPLICATION_JSON);
Response resp = given().header(wantJson).get("/metrics/application");
JsonPath filteredJSONPath = new JsonPath(filterOutAppLabelJSON(resp.jsonPath().prettify()));
ResponseBuilder responseBuilder = new ResponseBuilder();
responseBuilder.clone(resp);
responseBuilder.setBody(filteredJSONPath.prettify());
resp = responseBuilder.build();
resp.then().statusCode(200)
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.redCount;tier=integration'", equalTo(0))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.blue;tier=integration'", equalTo(0))
.body("'greenCount;tier=integration'", equalTo(0))
.body("'purple;app=myShop;tier=integration'", equalTo(0))
.body("'metricTest.test1.count;tier=integration'", equalTo(1))
.body("'metricTest.test1.countMeA;tier=integration'", equalTo(1))
.body("'metricTest.test1.countMeB;tier=integration'", equalTo(1))
.body("'metricTest.test1.gauge;tier=integration'", equalTo(19))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.gaugeMeA;tier=integration'", equalTo(1000))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.gaugeMeB;tier=integration'", equalTo(7777777))
.body("'metricTest.test1.histogram'.'count;tier=integration'", equalTo(1000))
.body("'metricTest.test1.histogram'.'max;tier=integration'", equalTo(999))
.body("'metricTest.test1.histogram'.'mean;tier=integration'", closeTo(499.5))
.body("'metricTest.test1.histogram'.'min;tier=integration'", equalTo(0))
.body("'metricTest.test1.histogram'.'p50;tier=integration'", closeTo(499.0))
.body("'metricTest.test1.histogram'.'p75;tier=integration'", closeTo(749))
.body("'metricTest.test1.histogram'.'p95;tier=integration'", closeTo(949))
.body("'metricTest.test1.histogram'.'p98;tier=integration'", closeTo(979))
.body("'metricTest.test1.histogram'.'p99;tier=integration'", closeTo(989))
.body("'metricTest.test1.histogram'.'p999;tier=integration'", closeTo(998))
.body("'metricTest.test1.histogram'", hasKey("stddev;tier=integration"))
.body("'metricTest.test1.meter'.'count;tier=integration'", equalTo(1))
.body("'metricTest.test1.meter'", hasKey("fifteenMinRate;tier=integration"))
.body("'metricTest.test1.meter'", hasKey("fiveMinRate;tier=integration"))
.body("'metricTest.test1.meter'", hasKey("meanRate;tier=integration"))
.body("'metricTest.test1.meter'", hasKey("oneMinRate;tier=integration"))
.body("'meterMeA'.'count;tier=integration'", equalTo(1))
.body("'meterMeA'", hasKey("fifteenMinRate;tier=integration"))
.body("'meterMeA'", hasKey("fiveMinRate;tier=integration"))
.body("'meterMeA'", hasKey("meanRate;tier=integration"))
.body("'meterMeA'", hasKey("oneMinRate;tier=integration"))
.body("'metricTest.test1.timer'.'count;tier=integration'", equalTo(1))
.body("'metricTest.test1.timer'", hasKey("fifteenMinRate;tier=integration"))
.body("'metricTest.test1.timer'", hasKey("fiveMinRate;tier=integration"))
.body("'metricTest.test1.timer'", hasKey("meanRate;tier=integration"))
.body("'metricTest.test1.timer'", hasKey("oneMinRate;tier=integration"))
.body("'metricTest.test1.timer'", hasKey("max;tier=integration"))
.body("'metricTest.test1.timer'", hasKey("mean;tier=integration"))
.body("'metricTest.test1.timer'", hasKey("min;tier=integration"))
.body("'metricTest.test1.timer'", hasKey("p50;tier=integration"))
.body("'metricTest.test1.timer'", hasKey("p75;tier=integration"))
.body("'metricTest.test1.timer'", hasKey("p95;tier=integration"))
.body("'metricTest.test1.timer'", hasKey("p98;tier=integration"))
.body("'metricTest.test1.timer'", hasKey("p99;tier=integration"))
.body("'metricTest.test1.timer'", hasKey("p999;tier=integration"))
.body("'metricTest.test1.timer'", hasKey("stddev;tier=integration"))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.timeMeA'.'count;tier=integration'", equalTo(1))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.timeMeA'", hasKey("fifteenMinRate;tier=integration"))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.timeMeA'", hasKey("fiveMinRate;tier=integration"))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.timeMeA'", hasKey("meanRate;tier=integration"))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.timeMeA'", hasKey("oneMinRate;tier=integration"))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.timeMeA'", hasKey("max;tier=integration"))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.timeMeA'", hasKey("mean;tier=integration"))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.timeMeA'", hasKey("min;tier=integration"))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.timeMeA'", hasKey("p50;tier=integration"))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.timeMeA'", hasKey("p75;tier=integration"))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.timeMeA'", hasKey("p95;tier=integration"))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.timeMeA'", hasKey("p98;tier=integration"))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.timeMeA'", hasKey("p99;tier=integration"))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.timeMeA'", hasKey("p999;tier=integration"))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.timeMeA'", hasKey("stddev;tier=integration"))
.body("'metricTest.test1.simpleTimer'.'count;tier=integration'", equalTo(1))
.body("'metricTest.test1.simpleTimer'", hasKey("elapsedTime;tier=integration"))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.simpleTimeMeA'.'count;tier=integration'", equalTo(1))
.body("'org.eclipse.microprofile.metrics.test.MetricAppBean.simpleTimeMeA'", hasKey("elapsedTime;tier=integration"));
}
@Test
@RunAsClient
@InSequence(19)
public void testApplicationMetadataItems() {
Header wantJson = new Header("Accept", APPLICATION_JSON);
JsonPath jsonPath = given().header(wantJson).options("/metrics/application").jsonPath();
Map<String, Object> elements = jsonPath.getMap(".");
List<String> missing = new ArrayList<>();
Map<String, MiniMeta> names = getExpectedMetadataFromXmlFile(MetricRegistry.Type.APPLICATION);
for (String item : names.keySet()) {
if (!elements.containsKey(item)) {
missing.add(item);
}
}
assertTrue("Following application items are missing: " + Arrays.toString(missing.toArray()), missing.isEmpty());
}
@Test
@RunAsClient
@InSequence(20)
public void testApplicationMetadataTypeAndUnit() {
Header wantJson = new Header("Accept", APPLICATION_JSON);
JsonPath jsonPath = given().header(wantJson).options("/metrics/application").jsonPath();
Map<String, Map<String, Object>> elements = jsonPath.getMap(".");
Map<String, MiniMeta> expectedMetadata = getExpectedMetadataFromXmlFile(MetricRegistry.Type.APPLICATION);
checkMetadataPresent(elements, expectedMetadata);
}
@Test
@RunAsClient
@InSequence(21)
public void testApplicationTagJson() {
JsonPath jsonPath = given().header("Accept", APPLICATION_JSON)
.when()
.options("/metrics/application/purple").jsonPath();
String tags = jsonPath.getString("purple.tags");
assertNotNull(tags);
assertTrue(tags.contains("app=myShop"));
assertTrue(tags.contains("tier=integration"));
}
@Test
@RunAsClient
@InSequence(22)
public void testApplicationTagOpenMetrics() {
given().header("Accept", TEXT_PLAIN).when().get("/metrics/application/purple")
.then().statusCode(200)
.and()
.body(containsString("tier=\"integration\""))
.body(containsString("app=\"myShop\""));
}
@Test
@RunAsClient
@InSequence(23)
public void testApplicationMeterUnitOpenMetrics() {
String prefix = "meterMeA_";
given().header("Accept", TEXT_PLAIN).when().get("/metrics/application/meterMeA")
.then().statusCode(200)
.and()
.body(containsString(prefix + "total"))
.body(containsString(prefix + "rate_per_second"))
.body(containsString(prefix + "one_min_rate_per_second"))
.body(containsString(prefix + "five_min_rate_per_second"))
.body(containsString(prefix + "fifteen_min_rate_per_second"));
}
@Test
@RunAsClient
@InSequence(24)
public void testApplicationTimerUnitOpenMetrics() {
String prefix = "org_eclipse_microprofile_metrics_test_MetricAppBean_timeMeA_";
Response resp = given().header("Accept", TEXT_PLAIN).get("/metrics/application/org.eclipse.microprofile.metrics.test.MetricAppBean.timeMeA");
ResponseBuilder responseBuilder = new ResponseBuilder();
responseBuilder.clone(resp);
responseBuilder.setBody(filterOutAppLabelOpenMetrics(resp.getBody().asString()));
resp = responseBuilder.build();
resp.then().statusCode(200)
.and()
.body(containsString("# TYPE application_" + prefix + "seconds summary"))
.body(containsString(prefix + "seconds_count"))
.body(containsString(prefix + "rate_per_second"))
.body(containsString(prefix + "one_min_rate_per_second"))
.body(containsString(prefix + "five_min_rate_per_second"))
.body(containsString(prefix + "fifteen_min_rate_per_second"))
.body(containsString(prefix + "mean_seconds"))
.body(containsString(prefix + "min_seconds"))
.body(containsString(prefix + "max_seconds"))
.body(containsString(prefix + "stddev_second"))
.body(containsString(prefix + "seconds{tier=\"integration\",quantile=\"0.5\"}"))
.body(containsString(prefix + "seconds{tier=\"integration\",quantile=\"0.75\"}"))
.body(containsString(prefix + "seconds{tier=\"integration\",quantile=\"0.95\"}"))
.body(containsString(prefix + "seconds{tier=\"integration\",quantile=\"0.98\"}"))
.body(containsString(prefix + "seconds{tier=\"integration\",quantile=\"0.99\"}"))
.body(containsString(prefix + "seconds{tier=\"integration\",quantile=\"0.999\"}"))
;
}
@Test
@RunAsClient
@InSequence(25)
public void testApplicationHistogramUnitBytesOpenMetrics() {
String prefix = "metricTest_test1_histogram_";
Response resp = given().header("Accept", TEXT_PLAIN).get("/metrics/application/metricTest.test1.histogram");
ResponseBuilder responseBuilder = new ResponseBuilder();
responseBuilder.clone(resp);
responseBuilder.setBody(filterOutAppLabelOpenMetrics(resp.getBody().asString()));
resp = responseBuilder.build();
resp.then().statusCode(200)
.and()
.body(containsString(prefix + "bytes_count"))
.body(containsString("# TYPE application_" + prefix + "bytes summary"))
.body(containsString(prefix + "mean_bytes"))
.body(containsString(prefix + "min_bytes"))
.body(containsString(prefix + "max_bytes"))
.body(containsString(prefix + "stddev_bytes"))
.body(containsString(prefix + "bytes{tier=\"integration\",quantile=\"0.5\"}"))
.body(containsString(prefix + "bytes{tier=\"integration\",quantile=\"0.75\"}"))
.body(containsString(prefix + "bytes{tier=\"integration\",quantile=\"0.95\"}"))
.body(containsString(prefix + "bytes{tier=\"integration\",quantile=\"0.98\"}"))
.body(containsString(prefix + "bytes{tier=\"integration\",quantile=\"0.99\"}"))
.body(containsString(prefix + "bytes{tier=\"integration\",quantile=\"0.999\"}"))
;
}
@Test
@RunAsClient
@InSequence(26)
public void testApplicationHistogramUnitNoneOpenMetrics() {
String prefix = "metricTest_test1_histogram2";
Response resp = given().header("Accept", TEXT_PLAIN).get("/metrics/application/metricTest.test1.histogram2");
ResponseBuilder responseBuilder = new ResponseBuilder();
responseBuilder.clone(resp);
responseBuilder.setBody(filterOutAppLabelOpenMetrics(resp.getBody().asString()));
resp = responseBuilder.build();
resp.then().statusCode(200)
.and()
.body(containsString(prefix + "_count"))
.body(containsString("# TYPE application_" + prefix + " summary"))
.body(containsString(prefix + "_mean"))
.body(containsString(prefix + "_min"))
.body(containsString(prefix + "_max"))
.body(containsString(prefix + "_stddev"))
.body(containsString(prefix + "{tier=\"integration\",quantile=\"0.5\"}"))
.body(containsString(prefix + "{tier=\"integration\",quantile=\"0.75\"}"))
.body(containsString(prefix + "{tier=\"integration\",quantile=\"0.95\"}"))
.body(containsString(prefix + "{tier=\"integration\",quantile=\"0.98\"}"))
.body(containsString(prefix + "{tier=\"integration\",quantile=\"0.99\"}"))
.body(containsString(prefix + "{tier=\"integration\",quantile=\"0.999\"}"))
;
}
@Test
@RunAsClient
@InSequence(27)
public void testOpenMetrics406ForOptions() {
given()
.header("Accept", TEXT_PLAIN)
.when()
.options("/metrics/application/metricTest.test1.histogram2")
.then()
.statusCode(406);
}
@Test
@RunAsClient
@InSequence(28)
public void testConvertingToBaseUnit() {
Header wantOpenMetricsFormat = new Header("Accept", TEXT_PLAIN);
given().header(wantOpenMetricsFormat).get("/metrics/application").then().statusCode(200)
.and().body(containsString(
"TYPE application_org_eclipse_microprofile_metrics_test_MetricAppBean_gaugeMeA_bytes gauge"))
.and().body(containsString("TYPE application_metricTest_test1_gauge_bytes gauge"));
}
@Test
@RunAsClient
@InSequence(29)
public void testNonStandardUnitsJSON() {
Header wantJSONFormat = new Header("Accept", APPLICATION_JSON);
given().header(wantJSONFormat).options("/metrics/application/jellybeanHistogram").then().statusCode(200)
.body("jellybeanHistogram.unit", equalTo("jellybeans"));
}
@Test
@RunAsClient
@InSequence(30)
public void testNonStandardUnitsOpenMetrics() {
String prefix = "jellybeanHistogram_";
Header wantOpenMetricsFormat = new Header("Accept", TEXT_PLAIN);
Response resp = given().header(wantOpenMetricsFormat).get("/metrics/application/jellybeanHistogram");
ResponseBuilder responseBuilder = new ResponseBuilder();
responseBuilder.clone(resp);
responseBuilder.setBody(filterOutAppLabelOpenMetrics(resp.getBody().asString()));
resp = responseBuilder.build();
resp.then().statusCode(200)
.and()
.body(containsString(prefix + "jellybeans_count"))
.body(containsString("# TYPE application_" + prefix + "jellybeans summary"))
.body(containsString(prefix + "mean_jellybeans"))
.body(containsString(prefix + "min_jellybeans"))
.body(containsString(prefix + "max_jellybeans"))
.body(containsString(prefix + "stddev_jellybeans"))
.body(containsString(prefix + "jellybeans{tier=\"integration\",quantile=\"0.5\"}"))
.body(containsString(prefix + "jellybeans{tier=\"integration\",quantile=\"0.75\"}"))
.body(containsString(prefix + "jellybeans{tier=\"integration\",quantile=\"0.95\"}"))
.body(containsString(prefix + "jellybeans{tier=\"integration\",quantile=\"0.98\"}"))
.body(containsString(prefix + "jellybeans{tier=\"integration\",quantile=\"0.99\"}"))
.body(containsString(prefix + "jellybeans{tier=\"integration\",quantile=\"0.999\"}"));
}
@Test
@RunAsClient
@InSequence(31)
public void testOptionalBaseMetrics() {
Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
Header wantJson = new Header("Accept", APPLICATION_JSON);
JsonPath jsonPath = given().header(wantJson).options("/metrics/base").jsonPath();
Map<String, Object> elements = jsonPath.getMap(".");
Map<String, MiniMeta> names = getExpectedMetadataFromXmlFile(MetricRegistry.Type.BASE);
for (MiniMeta item : names.values()) {
if (elements.containsKey(item.toJSONName()) && names.get(item.name).optional) {
String prefix = names.get(item.name).name;
String type = "'"+item.toJSONName()+"'"+".type";
String unit= "'"+item.toJSONName()+"'"+".unit";
given().header(wantJson).options("/metrics/base/"+prefix).then().statusCode(200)
.body(type, equalTo(names.get(item.name).type))
.body(unit, equalTo(names.get(item.name).unit));
}
}
}
@Test
@InSequence(32)
public void testGlobalTagsViaConfig() {
/*
* Test modified to check that it contains tier=integration
* due to the possibility of vendor's implementation either
* providing an _app tag. Or not.
*/
boolean expectedTag = metricAppBean.getGlobalTags().contains("tier=integration");
assertEquals(expectedTag, true);
}
@Test
@InSequence(33)
public void testSetupPromNoBadCharsInNames() {
metricAppBean.createPromMetrics();
}
@Test
@RunAsClient
@InSequence(34)
public void testPromNoBadCharsInNames() {
given().header("Accept", TEXT_PLAIN).when().get("/metrics/application")
.then().statusCode(200)
.and()
// metrics.counter("pm_counter-with-dashes");
.body(containsString("pm_counter_with_dashes"))
// metrics.counter("pm_counter#hash_x'y_");
.body(containsString("pm_counter_hash_x_y_"))
// metrics.counter("pm_counter-umlaut-äöü");
.body(containsString("pm_counter_umlaut_"))
// metrics.counter("pm_counter+accent_ê_");
.body(containsString("pm_counter_accent_"));
}
@Test
@RunAsClient
@InSequence(35)
public void testAccept1() {
given().header("Accept","application/json;q=0.5,text/plain;q=0.5")
.when().get("/metrics/application")
.then().statusCode(200)
.and()
.contentType(TEXT_PLAIN);
}
@Test
@RunAsClient
@InSequence(36)
public void testAccept2() {
given().header("Accept","application/json;q=0.1,text/plain;q=0.9")
.when().get("/metrics/application")
.then().statusCode(200)
.and()
.contentType(TEXT_PLAIN);
}
@Test
@RunAsClient
@InSequence(37)
public void testAccept3() {
given().header("Accept","image/png,image/jpeg")
.when().get("/metrics/application")
.then().statusCode(406);
}
@Test
@RunAsClient
@InSequence(38)
public void testAccept4() {
given().header("Accept","*/*")
.when().get("/metrics/application")
.then().statusCode(200)
.and()
.contentType(TEXT_PLAIN);
}
@Test
@RunAsClient
@InSequence(39)
public void testAccept5() {
given().header("Accept","image/png;q=1,*/*;q=0.1")
.when().get("/metrics/application")
.then().statusCode(200)
.and()
.contentType(TEXT_PLAIN);
}
@Test
@RunAsClient
@InSequence(40)
public void testNoAcceptHeader() {
when().get("/metrics/application")
.then().statusCode(200)
.and()
.contentType(TEXT_PLAIN);
}
@Test
@RunAsClient
@InSequence(41)
public void testCustomUnitAppendToGaugeName() {
Header wantOpenMetricsFormat = new Header("Accept", TEXT_PLAIN);
given().header(wantOpenMetricsFormat).get("/metrics/application").then().statusCode(200)
.and().body(containsString("TYPE application_org_eclipse_microprofile_metrics_test_MetricAppBean_gaugeMeB_hands gauge"));
}
@Test
@RunAsClient
@InSequence(42)
public void testNoCustomUnitForCounter() {
Header wantOpenMetricsFormat = new Header("Accept", TEXT_PLAIN);
given().header(wantOpenMetricsFormat).get("/metrics/application").then().statusCode(200)
.and().body(containsString("TYPE application_metricTest_test1_countMeB_total counter"));
}
/**
* Check that there is at least one metric named gc.total and that they all contain
* expected tags (actually this is just 'name' for now).
*/
@Test
@RunAsClient
@InSequence(43)
public void testGcCountMetrics() {
Assume.assumeFalse(Boolean.getBoolean("skip.base.metric.tests"));
Header wantJson = new Header("Accept", APPLICATION_JSON);
JsonPath jsonPath = given().header(wantJson).get("/metrics/base").jsonPath();
Map<String, MiniMeta> baseNames = getExpectedMetadataFromXmlFile(MetricRegistry.Type.BASE);
MiniMeta gcCountMetricMeta = baseNames.get("gc.total");
Set<String> expectedTags = gcCountMetricMeta.tags.keySet();
// obtain list of actual base metrics from the runtime and find all named gc.total
Map<String, Object> elements = jsonPath.getMap(".");
boolean found = false;
for (Map.Entry<String, Object> metricEntry : elements.entrySet()) {
if(metricEntry.getKey().startsWith("gc.total")) {
// We found a metric named gc.total. Now check that it contains all expected tags
for(String expectedTag : expectedTags) {
assertThat("The metric should contain a " + expectedTag + " tag",