PrometheusMetric implements Metric { @Override public Object startTiming(String metricName, String requestDomainName) { return startTiming(metricName, requestDomainName, null); } PrometheusMetric(CollectorRegistry registry, ConcurrentMap<String, Collector> namesToCollectors, PrometheusExporter exporter, String namespace);  PrometheusMetric(CollectorRegistry registry,
                            ConcurrentMap<String, Collector> namesToCollectors,
                            PrometheusExporter exporter,
                            String namespace,
                            boolean isLabelRequestDomainNameEnable,
                            boolean isLabelPrincipalDomainNameEnable,
                            boolean isLabelHttpMethodNameEnable,
                            boolean isLabelHttpStatusNameEnable,
                            boolean isLabelApiNameEnable); @Override void increment(String metricName); @Override void increment(String metricName, String requestDomainName); @Override void increment(String metricName, String requestDomainName, String principalDomainName); @Override void increment(String metricName, String requestDomainName, int count); @Override void increment(String metricName, String requestDomainName, String principalDomainName, int count); @Override void increment(String metricName, String requestDomainName, String principalDomainName, String httpMethod, int httpStatus, String apiName); @Override Object startTiming(String metricName, String requestDomainName); @Override Object startTiming(String metricName, String requestDomainName, String principalDomainName); @Override Object startTiming(String metricName, String requestDomainName, String principalDomainName, String httpMethod, String apiName); @Override void stopTiming(Object timerObj); @Override void stopTiming(Object timerObj, String requestDomainName, String principalDomainName); @Override void flush(); @Override void quit(); static final String REQUEST_DOMAIN_LABEL_NAME; static final String PRINCIPAL_DOMAIN_LABEL_NAME; static final String REQUEST_HTTP_METHOD_LABEL_NAME; static final String REQUEST_HTTP_STATUS_LABEL_NAME; static final String REQUEST_API_LABEL_NAME; static final String METRIC_NAME_DELIMITER; static final String COUNTER_SUFFIX; static final String TIMER_UNIT; }
@Test public void testStartTiming() { CollectorRegistry registry = new CollectorRegistry(); ConcurrentHashMap<String, Collector> namesToCollectors = new ConcurrentHashMap<>(); String namespace = "metric_test"; PrometheusMetric metric_1 = new PrometheusMetric(registry, namesToCollectors, null, namespace); String metricName_1 = "test_timer_1"; String fullMetricName_1 = namespace + "_" + metricName_1 + "_" + PrometheusMetric.TIMER_UNIT + "_sum"; String requestDomainName_1 = "request_domain_1"; String principalDomainName_1 = "principal_domain_1"; Assert.assertNull(registry.getSampleValue(fullMetricName_1, this.labelNames, new String[]{"", "", "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_1, this.labelNames, new String[]{requestDomainName_1, "", "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_1, this.labelNames, new String[]{"", principalDomainName_1, "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_1, this.labelNames, new String[]{requestDomainName_1, principalDomainName_1, "", "", ""})); metric_1.stopTiming(metric_1.startTiming(metricName_1, null)); metric_1.stopTiming(metric_1.startTiming(metricName_1, requestDomainName_1)); metric_1.stopTiming(metric_1.startTiming(metricName_1, null, principalDomainName_1)); metric_1.stopTiming(metric_1.startTiming(metricName_1, requestDomainName_1, principalDomainName_1)); Assert.assertNotNull(registry.getSampleValue(fullMetricName_1, this.labelNames, new String[]{"", "", "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_1, this.labelNames, new String[]{requestDomainName_1, "", "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_1, this.labelNames, new String[]{"", principalDomainName_1, "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_1, this.labelNames, new String[]{requestDomainName_1, principalDomainName_1, "", "", ""})); PrometheusMetric metric_2 = new PrometheusMetric(registry, namesToCollectors, null, namespace, true, false, false, false, false); String metricName_2 = "test_timer_2"; String fullMetricName_2 = namespace + "_" + metricName_2 + "_" + PrometheusMetric.TIMER_UNIT + "_sum"; String requestDomainName_2 = "request_domain_2"; String principalDomainName_2 = "principal_domain_2"; Assert.assertNull(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{"", "", "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{requestDomainName_2, "", "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{"", principalDomainName_2, "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{requestDomainName_2, principalDomainName_2, "", "", ""})); metric_2.stopTiming(metric_2.startTiming(metricName_2, null)); metric_2.stopTiming(metric_2.startTiming(metricName_2, requestDomainName_2)); metric_2.stopTiming(metric_2.startTiming(metricName_2, null, principalDomainName_2)); metric_2.stopTiming(metric_2.startTiming(metricName_2, requestDomainName_2, principalDomainName_2)); Assert.assertNotNull(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{"", "", "", "", ""})); Assert.assertNotNull(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{requestDomainName_2, "", "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{"", principalDomainName_2, "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{requestDomainName_2, principalDomainName_2, "", "", ""})); PrometheusMetric metric_3 = new PrometheusMetric(registry, namesToCollectors, null, namespace, false, true, false, false, false); String metricName_3 = "test_timer_3"; String fullMetricName_3 = namespace + "_" + metricName_3 + "_" + PrometheusMetric.TIMER_UNIT + "_sum"; String requestDomainName_3 = "request_domain_3"; String principalDomainName_3 = "principal_domain_3"; Assert.assertNull(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{"", "", "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{requestDomainName_3, "", "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{"", principalDomainName_3, "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{requestDomainName_3, principalDomainName_3, "", "", ""})); metric_3.stopTiming(metric_3.startTiming(metricName_3, null)); metric_3.stopTiming(metric_3.startTiming(metricName_3, requestDomainName_3)); metric_3.stopTiming(metric_3.startTiming(metricName_3, null, principalDomainName_3)); metric_3.stopTiming(metric_3.startTiming(metricName_3, requestDomainName_3, principalDomainName_3)); Assert.assertNotNull(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{"", "", "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{requestDomainName_3, "", "", "", ""})); Assert.assertNotNull(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{"", principalDomainName_3, "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{requestDomainName_3, principalDomainName_3, "", "", ""})); PrometheusMetric metric_4 = new PrometheusMetric(registry, namesToCollectors, null, namespace, true, true, false, false, false); String metricName_4 = "test_timer_4"; String fullMetricName_4 = namespace + "_" + metricName_4 + "_" + PrometheusMetric.TIMER_UNIT + "_sum"; String requestDomainName_4 = "request_domain_4"; String principalDomainName_4 = "principal_domain_4"; Assert.assertNull(registry.getSampleValue(fullMetricName_4, this.labelNames, new String[]{"", "", "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_4, this.labelNames, new String[]{requestDomainName_4, "", "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_4, this.labelNames, new String[]{"", principalDomainName_4, "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_4, this.labelNames, new String[]{requestDomainName_4, principalDomainName_4, "", "", ""})); metric_4.stopTiming(metric_4.startTiming(metricName_4, null)); metric_4.stopTiming(metric_4.startTiming(metricName_4, requestDomainName_4)); metric_4.stopTiming(metric_4.startTiming(metricName_4, null, principalDomainName_4)); metric_4.stopTiming(metric_4.startTiming(metricName_4, requestDomainName_4, principalDomainName_4)); Assert.assertNotNull(registry.getSampleValue(fullMetricName_4, this.labelNames, new String[]{"", "", "", "", ""})); Assert.assertNotNull(registry.getSampleValue(fullMetricName_4, this.labelNames, new String[]{requestDomainName_4, "", "", "", ""})); Assert.assertNotNull(registry.getSampleValue(fullMetricName_4, this.labelNames, new String[]{"", principalDomainName_4, "", "", ""})); Assert.assertNotNull(registry.getSampleValue(fullMetricName_4, this.labelNames, new String[]{requestDomainName_4, principalDomainName_4, "", "", ""})); }