use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.
the class VSphereAdapterStatsService method mockStats.
private List<ServiceStat> mockStats() {
List<ServiceStat> res = new ArrayList<>(2);
ServiceStat m1 = new ServiceStat();
m1.lastUpdateMicrosUtc = System.currentTimeMillis() * 1000;
m1.name = "cpu.mock";
m1.latestValue = 17;
res.add(m1);
ServiceStat m2 = new ServiceStat();
m2.lastUpdateMicrosUtc = System.currentTimeMillis() * 1000;
m2.name = "memory.mock";
m2.latestValue = 11;
res.add(m2);
return res;
}
use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.
the class StatsClientTest method test.
@Test
public void test() throws Exception {
String url = System.getProperty(TestProperties.VC_URL);
if (url == null) {
return;
}
String username = System.getProperty(TestProperties.VC_USERNAME);
String password = System.getProperty(TestProperties.VC_PASSWORD);
BasicConnection conn = new BasicConnection();
conn.setURI(URI.create(url));
conn.setUsername(username);
conn.setPassword(password);
conn.setIgnoreSslErrors(true);
conn.setRequestTimeout(30, TimeUnit.SECONDS);
conn.connect();
StatsClient client = new StatsClient(conn);
List<ServiceStat> metrics;
ManagedObjectReference vm = new ManagedObjectReference();
vm.setType(VimNames.TYPE_VM);
vm.setValue("vm-49");
metrics = client.retrieveMetricsForVm(vm);
this.logger.info("vm metrics " + metrics);
ManagedObjectReference host = new ManagedObjectReference();
host.setType(VimNames.TYPE_HOST);
host.setValue("host-504");
metrics = client.retrieveMetricsForHost(host);
this.logger.info("host metrics " + metrics);
}
use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.
the class StatsClient method querySingleEntity.
/**
* See <a href="https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.PerformanceManager.html#queryStats">queryStats method</a>
* @param ctx
* @return
* @throws RuntimeFaultFaultMsg
*/
private List<ServiceStat> querySingleEntity(StatCollectionContext ctx) throws RuntimeFaultFaultMsg {
List<PerfEntityMetricBase> metrics = getVimPort().queryPerf(PERF_MGR_MOREF, Collections.singletonList(ctx.getSpec()));
List<ServiceStat> res = new ArrayList<>();
if (metrics.isEmpty()) {
// nothing fetched
return Collections.emptyList();
}
// the metrics for the single entity
PerfEntityMetric m = (PerfEntityMetric) metrics.get(0);
for (PerfMetricSeries pms : m.getValue()) {
PerfMetricId metricId = pms.getId();
PerfMetricIntSeries series = (PerfMetricIntSeries) pms;
SamplesAggregator factory = ctx.getFactory(metricId.getCounterId());
PerfCounterInfo counter = this.perfCounterLookup.getCounterByKey(metricId.getCounterId());
ServiceStat stat = factory.createStat(counter, m.getSampleInfo(), series.getValue());
if (stat != null) {
res.add(stat);
}
}
return res;
}
use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.
the class SingleResourceStatsAggregationTaskServiceTest method testResourceStatsAggregation.
@Test
public void testResourceStatsAggregation() throws Throwable {
// create a resource pool
ResourcePoolState rpState = new ResourcePoolState();
rpState.name = "testName";
ResourcePoolState rpReturnState = postServiceSynchronously(ResourcePoolService.FACTORY_LINK, rpState, ResourcePoolState.class);
ComputeState[] computeStateArray = new ComputeState[NUM_COMPUTE_RESOURCES];
for (int i = 0; i < NUM_COMPUTE_RESOURCES; i++) {
ComputeDescription cDesc = new ComputeDescription();
cDesc.name = rpState.name;
cDesc.statsAdapterReference = UriUtils.buildUri(this.host, MockStatsAdapter.SELF_LINK);
ComputeDescription descReturnState = postServiceSynchronously(ComputeDescriptionService.FACTORY_LINK, cDesc, ComputeDescription.class);
ComputeState computeState = new ComputeState();
computeState.name = rpState.name;
computeState.descriptionLink = descReturnState.documentSelfLink;
computeState.resourcePoolLink = rpReturnState.documentSelfLink;
computeStateArray[i] = postServiceSynchronously(ComputeService.FACTORY_LINK, computeState, ComputeState.class);
}
StatsCollectionTaskState collectionTaskState = new StatsCollectionTaskState();
collectionTaskState.resourcePoolLink = rpReturnState.documentSelfLink;
collectionTaskState.taskInfo = TaskState.createDirect();
int counter = 0;
// executing stats collection multiple times
while (counter < NUM_COLLECTIONS) {
this.postServiceSynchronously(StatsCollectionTaskService.FACTORY_LINK, collectionTaskState, StatsCollectionTaskState.class);
counter++;
}
// wait for stats to be populated
this.host.waitFor("Error waiting for stats", () -> {
boolean returnStatus = false;
for (int i = 0; i < NUM_COMPUTE_RESOURCES; i++) {
String statsUriPath = UriUtils.buildUriPath(computeStateArray[i].documentSelfLink, ServiceHost.SERVICE_URI_SUFFIX_STATS);
ServiceStats resStats = getServiceSynchronously(statsUriPath, ServiceStats.class);
for (ServiceStat stat : resStats.entries.values()) {
if (stat.name.startsWith(UriUtils.getLastPathSegment(MockStatsAdapter.SELF_LINK))) {
returnStatus = true;
break;
}
}
}
return returnStatus;
});
// number of keys = 2, number of lastCollectionTimeMetrics = 2
int numberOfRawMetrics = NUM_COLLECTIONS * NUM_COMPUTE_RESOURCES * 2 * 2;
// kick off an aggregation task
SingleResourceStatsAggregationTaskState aggregationTaskState = new SingleResourceStatsAggregationTaskState();
aggregationTaskState.resourceLink = computeStateArray[0].documentSelfLink;
aggregationTaskState.metricNames = new HashSet<>(Arrays.asList(MockStatsAdapter.KEY_1, MockStatsAdapter.KEY_2, MockStatsAdapter.KEY_3));
postServiceSynchronously(SingleResourceStatsAggregationTaskService.FACTORY_LINK, aggregationTaskState, SingleResourceStatsAggregationTaskState.class);
long expectedExpirationTime = Utils.getNowMicrosUtc() + TimeUnit.DAYS.toMicros(DEFAULT_RETENTION_LIMIT_DAYS);
this.host.waitFor("Error waiting for rolled up stats", () -> {
ServiceDocumentQueryResult result = this.host.getExpandedFactoryState(UriUtils.buildUri(this.host, ResourceMetricsService.FACTORY_LINK));
String randomDocumentLink = result.documentLinks.get(0);
ResourceMetrics metric = Utils.fromJson(result.documents.get(randomDocumentLink), ResourceMetrics.class);
// Make sure all the documents have expiration time set.
Assert.assertTrue("Expiration time is not correctly set.", metric.documentExpirationTimeMicros < expectedExpirationTime);
return (result.documentCount == 2 + numberOfRawMetrics);
});
String statsUriPath = UriUtils.buildUriPath(computeStateArray[0].documentSelfLink, ServiceHost.SERVICE_URI_SUFFIX_STATS);
ServiceStats resStats = getServiceSynchronously(statsUriPath, ServiceStats.class);
int statCount = 0;
for (ServiceStat stat : resStats.entries.values()) {
if (stat.name.startsWith(MockStatsAdapter.KEY_1) || stat.name.startsWith(MockStatsAdapter.KEY_2)) {
statCount++;
}
}
Assert.assertEquals("Did not find in-memory stats", 2, statCount);
// verify that the aggregation tasks have been deleted
this.host.waitFor("Timeout waiting for task to expire", () -> {
ServiceDocumentQueryResult res = this.host.getFactoryState(UriUtils.buildUri(this.host, SingleResourceStatsAggregationTaskService.FACTORY_LINK));
return res.documentLinks.size() == 0;
});
}
use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.
the class StatsCollectionTaskServiceTest method testCustomStatsAdapter.
@Test
public void testCustomStatsAdapter() throws Throwable {
ResourcePoolState rpState = new ResourcePoolState();
rpState.name = UUID.randomUUID().toString();
ResourcePoolState rpReturnState = postServiceSynchronously(ResourcePoolService.FACTORY_LINK, rpState, ResourcePoolState.class);
ComputeDescription desc = new ComputeDescription();
desc.name = rpState.name;
desc.statsAdapterReferences = Collections.singleton(UriUtils.buildUri(this.host, CustomStatsAdapter.SELF_LINK));
ComputeDescription descReturnState = postServiceSynchronously(ComputeDescriptionService.FACTORY_LINK, desc, ComputeDescription.class);
ComputeState computeState = new ComputeState();
computeState.name = rpState.name;
computeState.descriptionLink = descReturnState.documentSelfLink;
computeState.resourcePoolLink = rpReturnState.documentSelfLink;
List<String> computeLinks = new ArrayList<>();
for (int i = 0; i < this.numResources; i++) {
ComputeState res = postServiceSynchronously(ComputeService.FACTORY_LINK, computeState, ComputeState.class);
computeLinks.add(res.documentSelfLink);
}
// create a stats collection scheduler task
StatsCollectionTaskState statCollectionState = new StatsCollectionTaskState();
statCollectionState.resourcePoolLink = rpReturnState.documentSelfLink;
statCollectionState.statsAdapterReference = UriUtils.buildUri(this.host, CustomStatsAdapter.SELF_LINK);
statCollectionState.options = EnumSet.of(TaskOption.SELF_DELETE_ON_COMPLETION);
ScheduledTaskState statsCollectionTaskState = new ScheduledTaskState();
statsCollectionTaskState.factoryLink = StatsCollectionTaskService.FACTORY_LINK;
statsCollectionTaskState.initialStateJson = Utils.toJson(statCollectionState);
statsCollectionTaskState.intervalMicros = TimeUnit.MILLISECONDS.toMicros(500);
statsCollectionTaskState = postServiceSynchronously(ScheduledTaskService.FACTORY_LINK, statsCollectionTaskState, ScheduledTaskState.class);
ServiceDocumentQueryResult res = this.host.getFactoryState(UriUtils.buildExpandLinksQueryUri(UriUtils.buildUri(this.host, ScheduledTaskService.FACTORY_LINK)));
assertTrue(res.documents.size() > 0);
// get stats from resources
for (int i = 0; i < computeLinks.size(); i++) {
String statsUriPath = UriUtils.buildUriPath(computeLinks.get(i), ServiceHost.SERVICE_URI_SUFFIX_STATS);
this.host.waitFor("Error waiting for stats", () -> {
ServiceStats resStats = getServiceSynchronously(statsUriPath, ServiceStats.class);
boolean returnStatus = false;
// was populated in the in memory stats
for (ServiceStat stat : resStats.entries.values()) {
if (stat.name.startsWith(UriUtils.getLastPathSegment(CustomStatsAdapter.SELF_LINK))) {
returnStatus = true;
break;
}
}
return returnStatus;
});
}
// clean up
deleteServiceSynchronously(statsCollectionTaskState.documentSelfLink);
}
Aggregations