PrometheusMetric implements Metric { @Override public void increment(String metricName) { increment(metricName, null, 1); } 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 testIncrement() { CollectorRegistry registry = new CollectorRegistry(); ConcurrentHashMap<String, Collector> namesToCollectors = new ConcurrentHashMap<>(); String namespace = "metric_test"; int count = 24; PrometheusMetric metric_1 = new PrometheusMetric(registry, namesToCollectors, null, namespace); String metricName_1 = "test_counter_1"; String fullMetricName_1 = namespace + "_" + metricName_1 + "_" + PrometheusMetric.COUNTER_SUFFIX; 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.increment(metricName_1); metric_1.increment(metricName_1, requestDomainName_1); metric_1.increment(metricName_1, null, principalDomainName_1); metric_1.increment(metricName_1, requestDomainName_1, principalDomainName_1); metric_1.increment(metricName_1, null, count); metric_1.increment(metricName_1, requestDomainName_1, count); metric_1.increment(metricName_1, null, principalDomainName_1, count); metric_1.increment(metricName_1, requestDomainName_1, principalDomainName_1, count); Assert.assertEquals(registry.getSampleValue(fullMetricName_1, this.labelNames, new String[]{"", "", "", "", ""}), 4d + 24d * 4d, 0.1d); 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_counter_2"; String fullMetricName_2 = namespace + "_" + metricName_2 + "_" + PrometheusMetric.COUNTER_SUFFIX; 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.increment(metricName_2); metric_2.increment(metricName_2, requestDomainName_2); metric_2.increment(metricName_2, null, principalDomainName_2); metric_2.increment(metricName_2, requestDomainName_2, principalDomainName_2); metric_2.increment(metricName_2, null, count); metric_2.increment(metricName_2, requestDomainName_2, count); metric_2.increment(metricName_2, null, principalDomainName_2, count); metric_2.increment(metricName_2, requestDomainName_2, principalDomainName_2, count); Assert.assertEquals(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{"", "", "", "", ""}), 2d + 24d * 2d, 0.1d); Assert.assertEquals(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{requestDomainName_2, "", "", "", ""}), 2d + 24d * 2d, 0.1d); 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_counter_3"; String fullMetricName_3 = namespace + "_" + metricName_3 + "_" + PrometheusMetric.COUNTER_SUFFIX; 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.increment(metricName_3); metric_3.increment(metricName_3, requestDomainName_3); metric_3.increment(metricName_3, null, principalDomainName_3); metric_3.increment(metricName_3, requestDomainName_3, principalDomainName_3); metric_3.increment(metricName_3, null, count); metric_3.increment(metricName_3, requestDomainName_3, count); metric_3.increment(metricName_3, null, principalDomainName_3, count); metric_3.increment(metricName_3, requestDomainName_3, principalDomainName_3, count); Assert.assertEquals(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{"", "", "", "", ""}), 2d + 24d * 2d, 0.1d); Assert.assertNull(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{requestDomainName_3, "", "", "", ""})); Assert.assertEquals(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{"", principalDomainName_3, "", "", ""}), 2d + 24d * 2d, 0.1d); 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_counter_4"; String fullMetricName_4 = namespace + "_" + metricName_4 + "_" + PrometheusMetric.COUNTER_SUFFIX; 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.increment(metricName_4); metric_4.increment(metricName_4, requestDomainName_4); metric_4.increment(metricName_4, null, principalDomainName_4); metric_4.increment(metricName_4, requestDomainName_4, principalDomainName_4); metric_4.increment(metricName_4, null, count); metric_4.increment(metricName_4, requestDomainName_4, count); metric_4.increment(metricName_4, null, principalDomainName_4, count); metric_4.increment(metricName_4, requestDomainName_4, principalDomainName_4, count); Assert.assertEquals(registry.getSampleValue(fullMetricName_4, this.labelNames, new String[]{"", "", "", "", ""}), 1d + 24d, 0.1d); Assert.assertEquals(registry.getSampleValue(fullMetricName_4, this.labelNames, new String[]{requestDomainName_4, "", "", "", ""}), 1d + 24d, 0.1d); Assert.assertEquals(registry.getSampleValue(fullMetricName_4, this.labelNames, new String[]{"", principalDomainName_4, "", "", ""}), 1d + 24d, 0.1d); Assert.assertEquals(registry.getSampleValue(fullMetricName_4, this.labelNames, new String[]{requestDomainName_4, principalDomainName_4, "", "", ""}), 1d + 24d, 0.1d); }
@Test public void testIncrementHttpRequest() { 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_counter_1"; String fullMetricName_1 = namespace + "_" + metricName_1 + "_" + PrometheusMetric.COUNTER_SUFFIX; String requestDomainName_1 = "request_domain_1"; String principalDomainName_1 = "principal_domain_1"; String httpMethodName_1 = "http_method_1"; String httpStatusName_1 = "200"; int httpStatus_1 = 200; String apiName_1 = "http_name_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, "", httpStatusName_1, ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_1, this.labelNames, new String[]{requestDomainName_1, principalDomainName_1, httpMethodName_1, httpStatusName_1, ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_1, this.labelNames, new String[]{null, null, httpMethodName_1, httpStatusName_1, apiName_1})); Assert.assertNull(registry.getSampleValue(fullMetricName_1, this.labelNames, new String[]{requestDomainName_1, principalDomainName_1, httpMethodName_1, httpStatusName_1, apiName_1})); metric_1.increment(metricName_1, null, null, null, 0, null); metric_1.increment(metricName_1, requestDomainName_1, null, null, 0, null); metric_1.increment(metricName_1, null, principalDomainName_1, null, 200, null); metric_1.increment(metricName_1, requestDomainName_1, principalDomainName_1, httpMethodName_1, httpStatus_1, null); metric_1.increment(metricName_1, null, null, httpMethodName_1, httpStatus_1, apiName_1); metric_1.increment(metricName_1, requestDomainName_1, principalDomainName_1, httpMethodName_1, httpStatus_1, apiName_1); Assert.assertEquals(registry.getSampleValue(fullMetricName_1, this.labelNames, new String[]{"", "", "", "", ""}), 6d, 0.1d); Assert.assertNull(registry.getSampleValue(fullMetricName_1, this.labelNames, new String[]{requestDomainName_1, "", "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_1, this.labelNames, new String[]{"", principalDomainName_1, "", httpStatusName_1, ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_1, this.labelNames, new String[]{requestDomainName_1, principalDomainName_1, httpMethodName_1, httpStatusName_1, ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_1, this.labelNames, new String[]{"", "", httpMethodName_1, httpStatusName_1, apiName_1})); Assert.assertNull(registry.getSampleValue(fullMetricName_1, this.labelNames, new String[]{requestDomainName_1, principalDomainName_1, httpMethodName_1, httpStatusName_1, apiName_1})); PrometheusMetric metric_2 = new PrometheusMetric(registry, namesToCollectors, null, namespace, false, true, false, true, false); String metricName_2 = "test_counter_2"; String fullMetricName_2 = namespace + "_" + metricName_2 + "_" + PrometheusMetric.COUNTER_SUFFIX; String requestDomainName_2 = "request_domain_2"; String principalDomainName_2 = "principal_domain_2"; String httpMethodName_2 = "http_method_2"; String httpStatusName_2 = "200"; int httpStatus_2 = 200; String apiName_2 = "http_name_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, "", httpStatusName_2, ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{requestDomainName_2, principalDomainName_2, httpMethodName_2, httpStatusName_2, ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{null, null, httpMethodName_2, httpStatusName_2, apiName_2})); Assert.assertNull(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{requestDomainName_2, principalDomainName_2, httpMethodName_2, httpStatusName_2, apiName_2})); metric_2.increment(metricName_2, null, null, null, 0, null); metric_2.increment(metricName_2, requestDomainName_2, null, null, 0, null); metric_2.increment(metricName_2, null, principalDomainName_2, null, 200, null); metric_2.increment(metricName_2, requestDomainName_2, principalDomainName_2, httpMethodName_2, httpStatus_2, null); metric_2.increment(metricName_2, null, null, httpMethodName_2, httpStatus_2, apiName_2); metric_2.increment(metricName_2, requestDomainName_2, principalDomainName_2, httpMethodName_2, httpStatus_2, apiName_2); Assert.assertNull(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{"", "", "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{requestDomainName_2, "", "", "", ""})); Assert.assertEquals(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{"", principalDomainName_2, "", httpStatusName_2, ""}), 3d, 0.1d); Assert.assertNull(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{requestDomainName_2, principalDomainName_2, httpMethodName_2, httpStatusName_2, ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{"", "", httpMethodName_2, httpStatusName_2, apiName_2})); Assert.assertNull(registry.getSampleValue(fullMetricName_2, this.labelNames, new String[]{requestDomainName_2, principalDomainName_2, httpMethodName_2, httpStatusName_2, apiName_2})); PrometheusMetric metric_3 = new PrometheusMetric(registry, namesToCollectors, null, namespace, true, true, true, true, true); String metricName_3 = "test_counter_3"; String fullMetricName_3 = namespace + "_" + metricName_3 + "_" + PrometheusMetric.COUNTER_SUFFIX; String requestDomainName_3 = "request_domain_3"; String principalDomainName_3 = "principal_domain_3"; String httpMethodName_3 = "http_method_3"; String httpStatusName_3 = "200"; int httpStatus_3 = 200; String apiName_3 = "http_name_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, "", httpStatusName_3, ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{requestDomainName_3, principalDomainName_3, httpMethodName_3, httpStatusName_3, ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{null, null, httpMethodName_3, httpStatusName_3, apiName_3})); Assert.assertNull(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{requestDomainName_3, principalDomainName_3, httpMethodName_3, httpStatusName_3, apiName_3})); metric_3.increment(metricName_3, null, null, null, 0, null); metric_3.increment(metricName_3, requestDomainName_3, null, null, 0, null); metric_3.increment(metricName_3, null, principalDomainName_3, null, 200, null); metric_3.increment(metricName_3, requestDomainName_3, principalDomainName_3, httpMethodName_3, httpStatus_3, null); metric_3.increment(metricName_3, null, null, httpMethodName_3, httpStatus_3, apiName_3); metric_3.increment(metricName_3, requestDomainName_3, principalDomainName_3, httpMethodName_3, httpStatus_3, apiName_3); Assert.assertNull(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{"", "", "", "", ""})); Assert.assertNull(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{requestDomainName_3, "", "", "", ""})); Assert.assertEquals(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{"", principalDomainName_3, "", httpStatusName_3, ""}), 1d, 0.1d); Assert.assertEquals(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{requestDomainName_3, principalDomainName_3, httpMethodName_3, httpStatusName_3, ""}), 1d, 0.1d); Assert.assertEquals(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{"", "", httpMethodName_3, httpStatusName_3, apiName_3}), 1d, 0.1d); Assert.assertEquals(registry.getSampleValue(fullMetricName_3, this.labelNames, new String[]{requestDomainName_3, principalDomainName_3, httpMethodName_3, httpStatusName_3, apiName_3}), 1d, 0.1d); }