use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.
the class SingleResourceStatsCollectionTaskService method populateLastCollectionTimeForMetricsInStatsRequest.
/**
* Gets the last collection for a compute for a given adapter URI.
* As a first step, the in memory stats for the compute are queried and if the metric
* for the last collection time is found, then the timestamp for that is returned.
*
* Else, the ResoureMetric table is queried and the latest version of the metric is used
* to determine the last collection time for the stats.
*/
private void populateLastCollectionTimeForMetricsInStatsRequest(SingleResourceStatsCollectionTaskState currentState, ComputeStatsRequest computeStatsRequest, URI patchUri, List<String> tenantLinks) {
URI computeStatsUri = UriUtils.buildStatsUri(UriUtils.extendUri(ClusterUtil.getClusterUri(getHost(), ServiceTypeCluster.INVENTORY_SERVICE), currentState.computeLink));
Operation.createGet(computeStatsUri).setCompletion((o, e) -> {
if (e != null) {
logSevere(() -> String.format("Could not get the last collection time from" + " in memory stats: %s", Utils.toString(e)));
// get the value from the persisted store.
populateLastCollectionTimeFromPersistenceStore(currentState, computeStatsRequest, patchUri, tenantLinks);
return;
}
ServiceStats serviceStats = o.getBody(ServiceStats.class);
String statsAdapterLink = getAdapterLinkFromURI(patchUri);
String lastSuccessfulRunMetricKey = getLastCollectionMetricKeyForAdapterLink(statsAdapterLink, true);
if (serviceStats.entries.containsKey(lastSuccessfulRunMetricKey)) {
ServiceStat lastRunStat = serviceStats.entries.get(lastSuccessfulRunMetricKey);
computeStatsRequest.lastCollectionTimeMicrosUtc = lastRunStat.sourceTimeMicrosUtc;
sendStatsRequestToAdapter(currentState, patchUri, computeStatsRequest);
} else {
populateLastCollectionTimeFromPersistenceStore(currentState, computeStatsRequest, patchUri, tenantLinks);
}
}).sendWith(this);
}
use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.
the class MockStatsAdapter method handleRequest.
@Override
public void handleRequest(Operation op) {
if (!op.hasBody()) {
op.fail(new IllegalArgumentException("body is required"));
return;
}
switch(op.getAction()) {
case PATCH:
op.complete();
ComputeStatsRequest statsRequest = op.getBody(ComputeStatsRequest.class);
SingleResourceStatsCollectionTaskState statsResponse = new SingleResourceStatsCollectionTaskState();
Map<String, List<ServiceStat>> statValues = new HashMap<>();
double currentCounter = this.counter.incrementAndGet();
ServiceStat key1 = new ServiceStat();
key1.latestValue = currentCounter;
long timestampMicros = Utils.getNowMicrosUtc();
key1.sourceTimeMicrosUtc = timestampMicros;
key1.unit = UNIT_1;
statValues.put(KEY_1, Collections.singletonList(key1));
sendBatchResponse(statsRequest, statsResponse, statValues, false);
statValues.clear();
ServiceStat key2 = new ServiceStat();
key2.latestValue = currentCounter;
key2.sourceTimeMicrosUtc = timestampMicros;
key2.unit = UNIT_2;
statValues.put(KEY_2, Collections.singletonList(key2));
sendBatchResponse(statsRequest, statsResponse, statValues, true);
break;
default:
super.handleRequest(op);
}
}
use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.
the class StatsCollectionTaskServiceTest method testCustomStatsAdapterPrecedence.
@Test
public void testCustomStatsAdapterPrecedence() 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.statsAdapterReference = UriUtils.buildUri(this.host, MockStatsAdapter.SELF_LINK);
desc.statsAdapterReferences = new HashSet<>(Arrays.asList(UriUtils.buildUri(this.host, "/foo"), 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;
// populated correctly.
for (ServiceStat stat : resStats.entries.values()) {
// host.log(Level.INFO, "*****%s", stat.name);
if (stat.name.startsWith(UriUtils.getLastPathSegment(CustomStatsAdapter.SELF_LINK))) {
returnStatus = true;
break;
}
}
return returnStatus;
});
}
// clean up
deleteServiceSynchronously(statsCollectionTaskState.documentSelfLink);
}
use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.
the class AWSCostStatsService method createStat.
private ServiceStat createStat(String unit, String name, Long timestamp, Number value) {
ServiceStat stat = new ServiceStat();
stat.latestValue = value.doubleValue();
stat.sourceTimeMicrosUtc = TimeUnit.MILLISECONDS.toMicros(timestamp);
stat.unit = unit;
stat.name = name;
return stat;
}
use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.
the class AWSCostStatsService method createServiceStatsForAccount.
protected void createServiceStatsForAccount(AWSCostStatsCreationContext statsData, LocalDate billMonth, AwsAccountDetailDto awsAccountDetailDto) {
Consumer<ComputeState> serviceStatsProcessor = (accountComputeState) -> {
ComputeStats awsServiceStats = new ComputeStats();
awsServiceStats.statValues = new ConcurrentHashMap<>();
awsServiceStats.computeLink = accountComputeState.documentSelfLink;
awsServiceStats.addCustomProperty(PhotonModelConstants.DOES_CONTAIN_SERVICE_STATS, Boolean.TRUE.toString());
for (AwsServiceDetailDto serviceDetailDto : awsAccountDetailDto.serviceDetailsMap.values()) {
Map<String, List<ServiceStat>> statsForAwsService = createStatsForAwsService(serviceDetailDto);
awsServiceStats.statValues.putAll(statsForAwsService);
}
if (!awsServiceStats.statValues.isEmpty()) {
statsData.statsResponse.statsList.add(awsServiceStats);
}
};
insertEC2ServiceDetail(awsAccountDetailDto);
processAccountStats(statsData, billMonth, awsAccountDetailDto, serviceStatsProcessor);
}
Aggregations