Search in sources :

Example 1 with DomainMetric

use of com.yahoo.athenz.zts.DomainMetric in project athenz by yahoo.

the class TestZpeMetric method testZpeMetric.

@Test
public void testZpeMetric() throws IOException {
    // setting the system property to write in file every 5 secs
    System.setProperty(ZpeConsts.ZPE_PROP_METRIC_WRITE_INTERVAL, "5000");
    final String metricDirPath = "/tmp/zpe-metrics";
    File metricsDir = new File(metricDirPath);
    metricsDir.mkdirs();
    System.setProperty(ZpeConsts.ZPE_PROP_METRIC_FILE_PATH, metricDirPath);
    final String TEST_DOMAIN = "test";
    ZpeMetric.statsEnabled = true;
    ZpeMetric test = new ZpeMetric();
    // cleaning the directory
    File dir = new File(test.getFilePath());
    if (dir.exists()) {
        for (File file : dir.listFiles()) {
            if (!file.isDirectory()) {
                file.delete();
            }
        }
    } else {
        dir.mkdirs();
    }
    // incrementing metrics for testing
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    try {
        Thread.sleep(4000);
    } catch (InterruptedException e) {
    }
    test.increment(ZpeConsts.ZPE_METRIC_NAME, TEST_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, TEST_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    test.increment(ZpeConsts.ZPE_METRIC_NAME, AuthZpeClient.DEFAULT_DOMAIN);
    try {
        Thread.sleep(2000);
    } catch (InterruptedException e) {
    }
    // Reading from the json file generated
    boolean sysDomainMetricVerified = false;
    boolean testDomainMetricVerified = false;
    for (File file : dir.listFiles()) {
        String filepath = test.getFilePath() + file.getName();
        Path path = Paths.get(filepath);
        DomainMetrics domainMetrics = JSON.fromBytes(Files.readAllBytes(path), DomainMetrics.class);
        // verifying the value of the metric
        List<DomainMetric> metricList = domainMetrics.getMetricList();
        for (DomainMetric metric : metricList) {
            if (metric.getMetricType().toString().equals(ZpeConsts.ZPE_METRIC_NAME)) {
                if (domainMetrics.getDomainName().equals("sys.auth")) {
                    assertEquals(10, metric.getMetricVal());
                    sysDomainMetricVerified = true;
                } else if (domainMetrics.getDomainName().equals("test")) {
                    assertEquals(2, metric.getMetricVal());
                    testDomainMetricVerified = true;
                }
            }
        }
    }
    assertTrue(sysDomainMetricVerified);
    assertTrue(testDomainMetricVerified);
    // unsetting the system property
    System.clearProperty(ZpeConsts.ZPE_PROP_METRIC_WRITE_INTERVAL);
}
Also used : Path(java.nio.file.Path) DomainMetrics(com.yahoo.athenz.zts.DomainMetrics) DomainMetric(com.yahoo.athenz.zts.DomainMetric) File(java.io.File) ZpeMetric(com.yahoo.athenz.zpe.ZpeMetric) Test(org.testng.annotations.Test)

Example 2 with DomainMetric

use of com.yahoo.athenz.zts.DomainMetric in project athenz by yahoo.

the class ZpeMetric method getMetrics.

// to convert the atomicIntegerArray to JSON object
public DomainMetrics getMetrics(String domainName) {
    ArrayList<DomainMetric> metricList = new ArrayList<>();
    for (DomainMetricType label : DomainMetricType.values()) {
        DomainMetric domainMetric = new DomainMetric();
        domainMetric.setMetricType(label);
        domainMetric.setMetricVal(counter.get(domainName).getAndSet(label.ordinal(), 0));
        metricList.add(domainMetric);
    }
    DomainMetrics domainMetrics = new DomainMetrics().setDomainName(domainName).setMetricList(metricList);
    return domainMetrics;
}
Also used : DomainMetrics(com.yahoo.athenz.zts.DomainMetrics) DomainMetric(com.yahoo.athenz.zts.DomainMetric) ArrayList(java.util.ArrayList) DomainMetricType(com.yahoo.athenz.zts.DomainMetricType)

Aggregations

DomainMetric (com.yahoo.athenz.zts.DomainMetric)2 DomainMetrics (com.yahoo.athenz.zts.DomainMetrics)2 ZpeMetric (com.yahoo.athenz.zpe.ZpeMetric)1 DomainMetricType (com.yahoo.athenz.zts.DomainMetricType)1 File (java.io.File)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 Test (org.testng.annotations.Test)1