use of com.yahoo.athenz.zts.DomainMetrics 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);
}
use of com.yahoo.athenz.zts.DomainMetrics 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;
}
use of com.yahoo.athenz.zts.DomainMetrics in project athenz by yahoo.
the class ZpeMetric method writeToFile.
// to write the JSON to file
public void writeToFile() {
final String dirPath = getFilePath();
for (String domainName : counter.keySet()) {
DomainMetrics domainMetrics = getMetrics(domainName);
Long epoch = System.currentTimeMillis();
String filepath = dirPath + domainName + "_" + Long.toString(epoch) + ".json";
try {
Path path = Paths.get(filepath);
Files.write(path, JSON.bytes(domainMetrics));
} catch (IOException e) {
counter.remove(domainName);
}
}
}
use of com.yahoo.athenz.zts.DomainMetrics in project athenz by yahoo.
the class PolicyUpdater method postDomainMetrics.
public static void postDomainMetrics(ZTSClient zts) {
final String filepath = getFilePath();
File dir = new File(filepath);
File[] filenames = dir.listFiles();
if (filenames == null) {
return;
}
for (int i = 0; i < filenames.length; i++) {
String domainName = filenames[i].getName().split("_")[0];
DomainMetrics domainMetrics = null;
final String metricFile = filepath + filenames[i].getName();
try {
Path path = Paths.get(metricFile);
domainMetrics = JSON.fromBytes(Files.readAllBytes(path), DomainMetrics.class);
zts.postDomainMetrics(domainName, domainMetrics);
Files.deleteIfExists(path);
} catch (ZTSClientException | IOException ex) {
LOG.error("Unable to push domain metrics from {} - error: {}", metricFile, ex.getMessage());
}
}
}
Aggregations