PrometheusMetricFactory implements MetricFactory { @Override public Metric create() { boolean isEnable = Boolean.valueOf(getProperty(ENABLE_PROP, "true")); if (!isEnable) { return new NoOpMetric(); } CollectorRegistry registry = new CollectorRegistry(); ConcurrentHashMap<String, Collector> namesToCollectors = new ConcurrentHashMap<>(); if (Boolean.valueOf(getProperty(JVM_ENABLE_PROP, "false"))) { new StandardExports().register(registry); new MemoryPoolsExports().register(registry); new MemoryAllocationExports().register(registry); new BufferPoolsExports().register(registry); new GarbageCollectorExports().register(registry); new ThreadExports().register(registry); new ClassLoadingExports().register(registry); new VersionInfoExports().register(registry); } PrometheusExporter exporter = null; if (Boolean.valueOf(getProperty(HTTP_SERVER_ENABLE_PROP, "true"))) { int pullingPort = Integer.valueOf(getProperty(HTTP_SERVER_PORT_PROP, "8181")); try { exporter = new PrometheusPullServer(pullingPort, registry); } catch (IOException e) { throw new RuntimeException(e); } } String namespace = getProperty(NAMESPACE_PROP, "athenz_server"); boolean isLabelRequestDomainNameEnable = Boolean.valueOf(getProperty(LABEL_REQUEST_DOMAIN_NAME_ENABLE_PROP, "false")); boolean isLabelPrincipalDomainNameEnable = Boolean.valueOf(getProperty(LABEL_PRINCIPAL_DOMAIN_NAME_ENABLE_PROP, "false")); boolean isLabelHttpMethodNameEnable = Boolean.valueOf(getProperty(LABEL_HTTP_METHOD_NAME_ENABLE_PROP, "false")); boolean isLabelHttpStatusNameEnable = Boolean.valueOf(getProperty(LABEL_HTTP_STATUS_NAME_ENABLE_PROP, "false")); boolean isLabelApiNameEnable = Boolean.valueOf(getProperty(LABEL_API_NAME_ENABLE_PROP, "false")); return new PrometheusMetric( registry, namesToCollectors, exporter, namespace, isLabelRequestDomainNameEnable, isLabelPrincipalDomainNameEnable, isLabelHttpMethodNameEnable, isLabelHttpStatusNameEnable, isLabelApiNameEnable); }  @Override Metric create(); static String getProperty(String key, String def); static final String SYSTEM_PROP_PREFIX; static final String ENABLE_PROP; static final String JVM_ENABLE_PROP; static final String HTTP_SERVER_ENABLE_PROP; static final String HTTP_SERVER_PORT_PROP; static final String NAMESPACE_PROP; static final String LABEL_REQUEST_DOMAIN_NAME_ENABLE_PROP; static final String LABEL_PRINCIPAL_DOMAIN_NAME_ENABLE_PROP; static final String LABEL_HTTP_METHOD_NAME_ENABLE_PROP; static final String LABEL_HTTP_STATUS_NAME_ENABLE_PROP; static final String LABEL_API_NAME_ENABLE_PROP; }
@Test public void testCreateMetricDisable() { Class<?> expected = NoOpMetric.class; setProperty(PrometheusMetricFactory.ENABLE_PROP, "false"); Metric metric = new PrometheusMetricFactory().create(); clearProperty(PrometheusMetricFactory.ENABLE_PROP); Assert.assertEquals(metric.getClass(), expected); }
@Test public void testCreateJvmMetricEnable() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { setProperty(PrometheusMetricFactory.JVM_ENABLE_PROP, "true"); setProperty(PrometheusMetricFactory.HTTP_SERVER_ENABLE_PROP, "false"); PrometheusMetric metric = (PrometheusMetric) new PrometheusMetricFactory().create(); clearProperty(PrometheusMetricFactory.JVM_ENABLE_PROP); clearProperty(PrometheusMetricFactory.HTTP_SERVER_ENABLE_PROP); Field registryField = metric.getClass().getDeclaredField("registry"); registryField.setAccessible(true); CollectorRegistry registry = (CollectorRegistry) registryField.get(metric); Assert.assertNotNull(registry.getSampleValue("process_cpu_seconds_total")); }
@Test(expectedExceptions = { RuntimeException.class, BindException.class }, expectedExceptionsMessageRegExp = ".* Address already in use.*") public void testCreateErrorUsedPort() throws IOException { int port = 18181; try (Socket socket = new Socket()) { socket.bind(new InetSocketAddress(port)); setProperty(PrometheusMetricFactory.HTTP_SERVER_ENABLE_PROP, "true"); setProperty(PrometheusMetricFactory.HTTP_SERVER_PORT_PROP, String.valueOf(port)); new PrometheusMetricFactory().create(); clearProperty(PrometheusMetricFactory.HTTP_SERVER_ENABLE_PROP); clearProperty(PrometheusMetricFactory.HTTP_SERVER_PORT_PROP); } }
@Test public void testCreate() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { Class<PrometheusPullServer> expectedExporterClass = PrometheusPullServer.class; String expectedNamespace = "expected_athenz_server"; boolean expectedIsLabelRequestDomainNameEnable = true; boolean expectedIsLabelPrincipalDomainNameEnable = true; boolean expectedIsLabelHttpMethodNameEnable = true; boolean expectedIsLabelHttpStatusNameEnable = true; boolean expectedIsLabelApiNameEnable = true; PrometheusMetric metric = null; try { setProperty(PrometheusMetricFactory.HTTP_SERVER_ENABLE_PROP, "true"); setProperty(PrometheusMetricFactory.NAMESPACE_PROP, expectedNamespace); setProperty(PrometheusMetricFactory.LABEL_REQUEST_DOMAIN_NAME_ENABLE_PROP, String.valueOf(expectedIsLabelRequestDomainNameEnable)); setProperty(PrometheusMetricFactory.LABEL_PRINCIPAL_DOMAIN_NAME_ENABLE_PROP, String.valueOf(expectedIsLabelPrincipalDomainNameEnable)); setProperty(PrometheusMetricFactory.LABEL_HTTP_METHOD_NAME_ENABLE_PROP, String.valueOf(expectedIsLabelHttpMethodNameEnable)); setProperty(PrometheusMetricFactory.LABEL_HTTP_STATUS_NAME_ENABLE_PROP, String.valueOf(expectedIsLabelHttpStatusNameEnable)); setProperty(PrometheusMetricFactory.LABEL_API_NAME_ENABLE_PROP, String.valueOf(expectedIsLabelApiNameEnable)); metric = (PrometheusMetric) new PrometheusMetricFactory().create(); clearProperty(PrometheusMetricFactory.HTTP_SERVER_ENABLE_PROP); clearProperty(PrometheusMetricFactory.NAMESPACE_PROP); clearProperty(PrometheusMetricFactory.LABEL_REQUEST_DOMAIN_NAME_ENABLE_PROP); clearProperty(PrometheusMetricFactory.LABEL_PRINCIPAL_DOMAIN_NAME_ENABLE_PROP); clearProperty(PrometheusMetricFactory.LABEL_HTTP_METHOD_NAME_ENABLE_PROP); clearProperty(PrometheusMetricFactory.LABEL_HTTP_STATUS_NAME_ENABLE_PROP); clearProperty(PrometheusMetricFactory.LABEL_API_NAME_ENABLE_PROP); Field exporterField = metric.getClass().getDeclaredField("exporter"); exporterField.setAccessible(true); PrometheusExporter exporter = (PrometheusExporter) exporterField.get(metric); Assert.assertEquals(exporter.getClass(), expectedExporterClass); Field namespaceField = metric.getClass().getDeclaredField("namespace"); namespaceField.setAccessible(true); String namespace = (String) namespaceField.get(metric); Assert.assertEquals(namespace, expectedNamespace); Field isLabelRequestDomainNameEnableField = metric.getClass().getDeclaredField("isLabelRequestDomainNameEnable"); isLabelRequestDomainNameEnableField.setAccessible(true); boolean isLabelRequestDomainNameEnable = (Boolean) isLabelRequestDomainNameEnableField.get(metric); Assert.assertEquals(isLabelRequestDomainNameEnable, expectedIsLabelRequestDomainNameEnable); Field isLabelPrincipalDomainNameEnableField = metric.getClass().getDeclaredField("isLabelPrincipalDomainNameEnable"); isLabelPrincipalDomainNameEnableField.setAccessible(true); boolean isLabelPrincipalDomainNameEnable = (Boolean) isLabelPrincipalDomainNameEnableField.get(metric); Assert.assertEquals(isLabelPrincipalDomainNameEnable, expectedIsLabelPrincipalDomainNameEnable); Field isLabelHttpMethodNameEnableField = metric.getClass().getDeclaredField("isLabelHttpMethodNameEnable"); isLabelHttpMethodNameEnableField.setAccessible(true); boolean isLabelHttpMethodNameEnable = (Boolean) isLabelHttpMethodNameEnableField.get(metric); Assert.assertEquals(isLabelHttpMethodNameEnable, expectedIsLabelHttpMethodNameEnable); Field isLabelHttpStatusNameEnableField = metric.getClass().getDeclaredField("isLabelHttpStatusNameEnable"); isLabelHttpStatusNameEnableField.setAccessible(true); boolean isLabelHttpStatusNameEnable = (Boolean) isLabelHttpStatusNameEnableField.get(metric); Assert.assertEquals(isLabelHttpStatusNameEnable, expectedIsLabelHttpStatusNameEnable); Field isLabelApiNameEnableField = metric.getClass().getDeclaredField("isLabelApiNameEnable"); isLabelApiNameEnableField.setAccessible(true); boolean isLabelApiNameEnable = (Boolean) isLabelApiNameEnableField.get(metric); Assert.assertEquals(isLabelApiNameEnable, expectedIsLabelApiNameEnable); } finally { if (metric != null) { metric.quit(); } } }