use of com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics in project photon-model by vmware.
the class SingleResourceStatsAggregationTaskService method setDefaultMetricValue.
/**
* Sets metric constants related to a resource to '0' value.
* Metric constants like 'EstimatedCharges', 'Cost', 'CurrentBurnRatePerHour',
* 'AverageBurnRatePerHour' stored in 'lastRollupTimeForMetric' map attribute will be
* set to 0 when project is newly created or has all endpoints removed.
*/
private List<Operation> setDefaultMetricValue(SingleResourceStatsAggregationTaskState currentState, List<Operation> operations, Set<String> publishedKeys) {
Map<String, Long> lastRollTimeMetric = currentState.lastRollupTimeForMetric;
for (Entry<String, Long> aggregateEntries : lastRollTimeMetric.entrySet()) {
// value is null when project is newly created and raw metrics are not yet generated.
// upon stats aggregation value is set to 0. As project resources don't exist,
// only newly created projects will have aggregate-metrics services generated .
Long aggregateMetricLastRollUpTime = aggregateEntries.getValue() == null ? 0L : aggregateEntries.getValue();
ResourceMetrics resourceMetrics = new ResourceMetrics();
resourceMetrics.entries = new HashMap<>();
resourceMetrics.entries.put(aggregateEntries.getKey(), 0.0);
resourceMetrics.timestampMicrosUtc = aggregateMetricLastRollUpTime;
resourceMetrics.documentSelfLink = StatsUtil.getMetricKey(currentState.resourceLink, Utils.getNowMicrosUtc());
operations.add(Operation.createPost(UriUtils.buildUri(ClusterUtil.getClusterUri(getHost(), ServiceTypeCluster.METRIC_SERVICE), ResourceMetricsService.FACTORY_LINK)).setBody(resourceMetrics));
publishedKeys.add(aggregateEntries.getKey());
ServiceStats.ServiceStat lastUpdateStat = new ServiceStats.ServiceStat();
lastUpdateStat.name = aggregateEntries.getKey();
lastUpdateStat.latestValue = aggregateMetricLastRollUpTime;
URI inMemoryStatsUri = UriUtils.buildStatsUri(UriUtils.extendUri(ClusterUtil.getClusterUri(getHost(), ServiceTypeCluster.INVENTORY_SERVICE), currentState.resourceLink));
operations.add(Operation.createPost(inMemoryStatsUri).setBody(lastUpdateStat));
}
return operations;
}
use of com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics in project photon-model by vmware.
the class AWSCostStatsService method createAccountStats.
protected void createAccountStats(AWSCostStatsCreationContext statsData, LocalDate billMonth, AwsAccountDetailDto awsAccountDetailDto) {
Consumer<ComputeState> accountStatsProcessor = (accountComputeState) -> {
ComputeStats accountStats = new ComputeStats();
accountStats.statValues = new ConcurrentHashMap<>();
accountStats.computeLink = accountComputeState.documentSelfLink;
if (isBillUpdated(statsData, awsAccountDetailDto)) {
logWithContext(statsData, Level.INFO, () -> String.format("Persisting cost of Account: %s (%s) for month: %s", awsAccountDetailDto.id, accountComputeState.documentSelfLink, billMonth));
ServiceStat costStat = createStat(AWSStatsNormalizer.getNormalizedUnitValue(DIMENSION_CURRENCY_VALUE), AWSStatsNormalizer.getNormalizedStatKeyValue(AWSConstants.COST), awsAccountDetailDto.billProcessedTimeMillis, awsAccountDetailDto.cost);
accountStats.statValues.put(costStat.name, Collections.singletonList(costStat));
ServiceStat otherCostsStat = createStat(AWSStatsNormalizer.getNormalizedUnitValue(DIMENSION_CURRENCY_VALUE), AWSConstants.OTHER_CHARGES, awsAccountDetailDto.billProcessedTimeMillis, awsAccountDetailDto.otherCharges);
accountStats.statValues.put(otherCostsStat.name, Collections.singletonList(otherCostsStat));
ServiceStat oneTimeChargesStat = createStat(AWSStatsNormalizer.getNormalizedUnitValue(DIMENSION_CURRENCY_VALUE), PhotonModelConstants.ACCOUNT_ONE_TIME_CHARGES, awsAccountDetailDto.billProcessedTimeMillis, awsAccountDetailDto.accountOneTimeCharges);
accountStats.statValues.put(oneTimeChargesStat.name, Collections.singletonList(oneTimeChargesStat));
}
if (!accountStats.statValues.isEmpty()) {
statsData.statsResponse.statsList.add(accountStats);
}
};
processAccountStats(statsData, billMonth, awsAccountDetailDto, accountStatsProcessor);
ResourceMetrics prevMarkerMetrics = statsData.accountsMarkersMap.get(awsAccountDetailDto.id);
if (prevMarkerMetrics != null) {
prevMarkerMetrics.entries.putAll(transformMapDataTypes(awsAccountDetailDto.lineCountPerInterval));
}
}
use of com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics in project photon-model by vmware.
the class AWSCostStatsService method createMarkerMetrics.
private void createMarkerMetrics(AWSCostStatsCreationContext context, AwsAccountDetailDto accountDto) {
List<ComputeState> accountComputeStates = context.awsAccountIdToComputeStates.get(accountDto.id);
if ((accountComputeStates == null) || accountComputeStates.isEmpty()) {
logFine(() -> "AWS account with ID '%s' is not configured yet. Not creating marker metrics for the same.");
return;
}
// We use root compute state representing this account to save the account level stats
Map<String, ComputeState> rootComputesByEndpoint = findRootAccountComputeStateByEndpoint(accountComputeStates);
URI uri = UriUtils.buildUri(ClusterUtil.getClusterUri(getHost(), ServiceTypeCluster.METRIC_SERVICE), ResourceMetricsService.FACTORY_LINK);
for (ComputeState compute : rootComputesByEndpoint.values()) {
ResourceMetrics markerMetrics = new ResourceMetrics();
markerMetrics.documentSelfLink = StatsUtil.getMetricKey(compute.documentSelfLink, Utils.getNowMicrosUtc());
markerMetrics.entries = new HashMap<>();
markerMetrics.entries.putAll(transformMapDataTypes(accountDto.lineCountPerInterval));
markerMetrics.entries.put(AWS_ACCOUNT_BILL_PROCESSED_TIME_MILLIS, accountDto.billProcessedTimeMillis.doubleValue());
markerMetrics.timestampMicrosUtc = getCurrentMonthStartTimeMicros();
markerMetrics.customProperties = new HashMap<>();
markerMetrics.customProperties.put(ResourceMetrics.PROPERTY_RESOURCE_LINK, compute.documentSelfLink);
markerMetrics.customProperties.put(PhotonModelConstants.CONTAINS_BILL_PROCESSED_TIME_STAT, Boolean.TRUE.toString());
markerMetrics.documentExpirationTimeMicros = Utils.getNowMicrosUtc() + TimeUnit.DAYS.toMicros(7);
sendRequest(Operation.createPost(uri).setBodyNoCloning(markerMetrics));
}
}
use of com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics in project photon-model by vmware.
the class LongRunEndToEndAzureStatsAggregation method checkExpirationTime.
/**
* Performs check to verify that expiration time for aggregate metric documents for every
* compute resource has been set to be expired after 56 days.
*/
private void checkExpirationTime(ServiceDocumentQueryResult aggrResult) {
long expectedExpirationTime = Utils.getNowMicrosUtc() + TimeUnit.DAYS.toMicros(DEFAULT_RETENTION_LIMIT_DAYS);
for (Object aggrDocument : aggrResult.documents.values()) {
ResourceMetrics aggrMetric = Utils.fromJson(aggrDocument, ResourceMetrics.class);
// Make sure all the documents have expiration time set.
assertTrue("Expiration time is not correctly set.", aggrMetric.documentExpirationTimeMicros < expectedExpirationTime);
}
}
use of com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics in project photon-model by vmware.
the class TestAzureStatsCollection method getResourceMetrics.
/**
* Query to get ResourceMetrics document for a specific resource containing a specific metric.
* @param resourceLink Link to the resource on which stats are being collected.
* @param metricKey Metric name.
* @return ResourceMetrics document.
*/
private ResourceMetrics getResourceMetrics(String resourceLink, String metricKey) {
QueryTask qt = QueryTask.Builder.createDirectTask().addOption(QueryOption.EXPAND_CONTENT).addOption(QueryOption.SORT).orderDescending(ServiceDocument.FIELD_NAME_SELF_LINK, ServiceDocumentDescription.TypeName.STRING).setQuery(QueryTask.Query.Builder.create().addKindFieldClause(ResourceMetrics.class).addFieldClause(ServiceDocument.FIELD_NAME_SELF_LINK, UriUtils.buildUriPath(ResourceMetricsService.FACTORY_LINK, UriUtils.getLastPathSegment(resourceLink)), QueryTask.QueryTerm.MatchType.PREFIX).addRangeClause(QueryTask.QuerySpecification.buildCompositeFieldName(ResourceMetrics.FIELD_NAME_ENTRIES, metricKey), QueryTask.NumericRange.createDoubleRange(0.0, Double.MAX_VALUE, true, true)).build()).build();
Operation op = QueryUtils.createQueryTaskOperation(this.host, qt, ServiceTypeCluster.METRIC_SERVICE).setReferer(this.host.getUri()).setBody(qt).setCompletion((o, e) -> {
if (e != null) {
this.host.log(Level.WARNING, e.toString());
}
});
Operation result = this.host.waitForResponse(op);
QueryTask qtResult = result.getBody(QueryTask.class);
ResourceMetrics resourceMetric = null;
if (qtResult.results.documentLinks.size() > 0) {
String documentLink = qtResult.results.documentLinks.get(0);
resourceMetric = Utils.fromJson(qtResult.results.documents.get(documentLink), ResourceMetrics.class);
}
return resourceMetric;
}
Aggregations