PrometheusMetric implements Metric { private Collector createOrGetCollector(String metricName, SimpleCollector.Builder<?, ?> builder) { String key = metricName; ConcurrentMap<String, Collector> map = this.namesToCollectors; Collector collector = map.get(key); if (collector == null) { synchronized (map) { if (!map.containsKey(key)) { builder = builder .namespace(this.namespace) .name(metricName) .help(metricName) .labelNames(REQUEST_DOMAIN_LABEL_NAME, PRINCIPAL_DOMAIN_LABEL_NAME, REQUEST_HTTP_METHOD_LABEL_NAME, REQUEST_HTTP_STATUS_LABEL_NAME, REQUEST_API_LABEL_NAME); collector = builder.register(this.registry); map.put(key, collector); } else { collector = map.get(key); } } }; return collector; } 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 testCreateOrGetCollector() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { CollectorRegistry registry = new CollectorRegistry(); ConcurrentHashMap<String, Collector> namesToCollectors = new ConcurrentHashMap<>(); PrometheusMetric metric = new PrometheusMetric(registry, namesToCollectors, null, ""); Method createOrGetCollector = metric.getClass().getDeclaredMethod("createOrGetCollector", String.class, SimpleCollector.Builder.class); createOrGetCollector.setAccessible(true); String metricName = "metric_test"; Counter.Builder builder = Counter.build(); double countValue = 110.110d; Counter counter = (Counter) createOrGetCollector.invoke(metric, metricName, builder); counter.labels("", "", "", "", "").inc(countValue); Assert.assertSame(counter, namesToCollectors.get(metricName)); Assert.assertEquals(registry.getSampleValue(metricName, this.labelNames, new String[]{"", "", "", "", ""}), countValue); Counter counter_2 = (Counter) createOrGetCollector.invoke(metric, metricName, builder); Assert.assertSame(counter_2, namesToCollectors.get(metricName)); Assert.assertSame(counter_2, counter); }