Search in sources :

Example 1 with ServiceStat

use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.

the class LongRunEndToEndStatsAggregationTest method checkInMemoryStatsPresent.

/**
 * Perform check to verify that every compute resource has stats generated for all the metric
 * keys using which stats aggregation was performed on resources.
 */
private void checkInMemoryStatsPresent(ServiceDocumentQueryResult res) {
    for (Map.Entry<String, Object> resourceMap : res.documents.entrySet()) {
        ServiceStats resStats = this.host.getServiceState(null, ServiceStats.class, UriUtils.buildStatsUri(createServiceURI(host, null, resourceMap.getKey())));
        int statCount = 0;
        for (ServiceStat stat : resStats.entries.values()) {
            for (String key : TestAWSSetupUtils.getMetricNames()) {
                if (stat.name.startsWith(key)) {
                    statCount++;
                    break;
                }
            }
        }
        // after stats aggregation all resources should have all metrics.
        assertEquals("Did not find in-memory stats", TestAWSSetupUtils.getMetricNames().size(), statCount);
    }
}
Also used : ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) ServiceStats(com.vmware.xenon.common.ServiceStats) Map(java.util.Map)

Example 2 with ServiceStat

use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.

the class TestAWSProvisionTask method verifyCollectedStats.

private void verifyCollectedStats(ComputeStatsResponse response, Long lastCollectionTime) {
    ComputeStats computeStats = response.statsList.get(0);
    assertTrue("Compute Link is empty", !computeStats.computeLink.isEmpty());
    // Check that stat values are accompanied with Units.
    for (String key : computeStats.statValues.keySet()) {
        List<ServiceStat> stats = computeStats.statValues.get(key);
        for (ServiceStat stat : stats) {
            assertTrue("Unit is empty", !stat.unit.isEmpty());
            // Check if burn rate values are positive.
            if (key.equalsIgnoreCase(PhotonModelConstants.AVERAGE_BURN_RATE_PER_HOUR)) {
                assertTrue("Average burn rate is negative", stat.latestValue >= 0);
            }
            if (key.equalsIgnoreCase(PhotonModelConstants.CURRENT_BURN_RATE_PER_HOUR)) {
                assertTrue("Current burn rate is negative", stat.latestValue >= 0);
            }
        }
        // from the provider.
        if (lastCollectionTime != null && key.equalsIgnoreCase(PhotonModelConstants.CPU_UTILIZATION_PERCENT)) {
            assertTrue("incorrect number of data points received when collection window is specified for metric ." + key, stats.size() > 1);
        }
        // Check if the datapoints collected are after the lastCollectionTime.
        if (lastCollectionTime != null && key.equalsIgnoreCase(PhotonModelConstants.ESTIMATED_CHARGES)) {
            for (ServiceStat stat : stats) {
                assertTrue("The datapoint collected is older than last collection time.", stat.sourceTimeMicrosUtc >= lastCollectionTime);
            }
        }
    }
}
Also used : ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats)

Example 3 with ServiceStat

use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.

the class TestAWSCostAdapterService method verifyCollectedStats.

private void verifyCollectedStats(List<ComputeStats> statsList) {
    Map<String, ComputeStats> computeStatsByLink = statsList.stream().collect(Collectors.toMap(e -> e.computeLink, Function.identity(), (allStats, stats) -> {
        allStats.statValues.putAll(stats.statValues);
        return allStats;
    }));
    ComputeStats account1Stats = computeStatsByLink.get(account1SelfLink);
    ComputeStats account2Stats = computeStatsByLink.get(account2SelfLink);
    String normalizedStatKeyValue = AWSStatsNormalizer.getNormalizedStatKeyValue(AWSConstants.COST);
    // verify account costs
    assertTrue(account1Stats.statValues.get(normalizedStatKeyValue).get(0).latestValue == account1TotalCost);
    assertTrue(account2Stats.statValues.get(normalizedStatKeyValue).get(0).latestValue == account2TotalCost);
    // check that service level stats exist
    String serviceCode = AwsServices.EC2_Instance_Usage.getName().replaceAll(" ", "");
    String serviceResourceCostMetric = String.format(AWSConstants.SERVICE_RESOURCE_COST, serviceCode);
    assertTrue(!account1Stats.statValues.get(serviceResourceCostMetric).isEmpty());
    String serviceCode2 = AwsServices.EC2_Others.getName().replaceAll(" ", "");
    String serviceOtherCostMetric = String.format(AWSConstants.SERVICE_OTHER_COST, serviceCode2);
    assertTrue(!account1Stats.statValues.get(serviceOtherCostMetric).isEmpty());
    String serviceMonthlyOtherCostMetric = String.format(AWSConstants.SERVICE_MONTHLY_OTHER_COST, serviceCode2);
    assertTrue(!account1Stats.statValues.get(serviceMonthlyOtherCostMetric).isEmpty());
    String serviceReservedRecurringCostMetric = String.format(AWSConstants.SERVICE_RESERVED_RECURRING_COST, serviceCode2);
    assertTrue(!account1Stats.statValues.get(serviceReservedRecurringCostMetric).isEmpty());
    ComputeStats instance1Stats = computeStatsByLink.get(INSTANCE_1_SELF_LINK);
    ComputeStats instance2Stats = computeStatsByLink.get(INSTANCE_2_SELF_LINK);
    assertEquals(instance1TotalCost, instance1Stats.statValues.get(normalizedStatKeyValue).get(0).latestValue, 0);
    assertEquals(instance2TotalCost, instance2Stats.statValues.get(normalizedStatKeyValue).get(0).latestValue, 0);
    String normalizedReservedInstanceStatKeyValue = AWSStatsNormalizer.getNormalizedStatKeyValue(AWSConstants.RESERVED_INSTANCE_DURATION);
    assertEquals(1.0, instance1Stats.statValues.get(normalizedReservedInstanceStatKeyValue).get(0).latestValue, 0);
    assertEquals(1.0, instance1Stats.statValues.get(normalizedReservedInstanceStatKeyValue).get(0).latestValue, 0);
    // Check that stat values are accompanied with Units.
    for (String key : account1Stats.statValues.keySet()) {
        List<ServiceStat> stats = account1Stats.statValues.get(key);
        for (ServiceStat stat : stats) {
            assertTrue("Unit is empty", !stat.unit.isEmpty());
        }
    }
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) BaseModelTest(com.vmware.photon.controller.model.helpers.BaseModelTest) QueryTask(com.vmware.xenon.services.common.QueryTask) StatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.StatsCollectionTaskService.StatsCollectionTaskState) ServiceDocument(com.vmware.xenon.common.ServiceDocument) EndpointAllocationTaskService(com.vmware.photon.controller.model.tasks.EndpointAllocationTaskService) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) CommandLineArgumentParser(com.vmware.xenon.common.CommandLineArgumentParser) Utils(com.vmware.xenon.common.Utils) QuerySpecification.buildCompositeFieldName(com.vmware.xenon.services.common.QueryTask.QuerySpecification.buildCompositeFieldName) Map(java.util.Map) After(org.junit.After) ServiceDocumentQueryResult(com.vmware.xenon.common.ServiceDocumentQueryResult) StatsCollectionTaskService(com.vmware.photon.controller.model.tasks.monitoring.StatsCollectionTaskService) EnumSet(java.util.EnumSet) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) EndpointAllocationTaskState(com.vmware.photon.controller.model.tasks.EndpointAllocationTaskService.EndpointAllocationTaskState) ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) StatelessService(com.vmware.xenon.common.StatelessService) INSTANCE_1_SELF_LINK(com.vmware.photon.controller.model.adapters.awsadapter.MockCostStatsAdapterService.INSTANCE_1_SELF_LINK) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) List(java.util.List) UriUtils(com.vmware.xenon.common.UriUtils) TaskState(com.vmware.xenon.common.TaskState) AwsServices(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSCsvBillParser.AwsServices) TaskOption(com.vmware.photon.controller.model.tasks.TaskOption) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) HashMap(java.util.HashMap) ResourceMetricsService(com.vmware.photon.controller.model.monitoring.ResourceMetricsService) Function(java.util.function.Function) ArrayList(java.util.ArrayList) EndpointConfigRequest(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest) NumericRange.createDoubleRange(com.vmware.xenon.services.common.QueryTask.NumericRange.createDoubleRange) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) PhotonModelTaskServices(com.vmware.photon.controller.model.tasks.PhotonModelTaskServices) Operation(com.vmware.xenon.common.Operation) Assert.assertTrue(org.junit.Assert.assertTrue) SingleResourceStatsCollectionTaskService(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService) Test(org.junit.Test) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) LocalDate(org.joda.time.LocalDate) PhotonModelAdaptersRegistryAdapters(com.vmware.photon.controller.model.adapters.registry.PhotonModelAdaptersRegistryAdapters) AWSStatsNormalizer(com.vmware.photon.controller.model.adapters.awsadapter.util.AWSStatsNormalizer) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) INSTANCE_2_SELF_LINK(com.vmware.photon.controller.model.adapters.awsadapter.MockCostStatsAdapterService.INSTANCE_2_SELF_LINK) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats)

Example 4 with ServiceStat

use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.

the class AzureComputeHostStatsGatherer method aggregateComputeStatsResponses.

/**
 * Aggregates stats from all the compute VMs to make up compute Host stats.
 */
private ComputeStats aggregateComputeStatsResponses(AzureStatsDataHolder statsData, List<QueryTask> items) {
    int numberOfComputeResponse = items.size();
    ComputeStats computeStats = new ComputeStats();
    computeStats.computeLink = statsData.computeHost.documentSelfLink;
    Map<String, ServiceStat> statMap = new HashMap<>();
    // Gather all the stats in a single response.
    for (QueryTask queryResult : items) {
        if (queryResult.results.documents != null) {
            for (String key : queryResult.results.documents.keySet()) {
                ResourceMetrics metric = Utils.fromJson(queryResult.results.documents.get(key), ResourceMetrics.class);
                for (Map.Entry<String, Double> entry : metric.entries.entrySet()) {
                    String metricName = entry.getKey();
                    if (statMap.containsKey(metricName)) {
                        statMap.get(metricName).latestValue += entry.getValue();
                    } else {
                        ServiceStat stat = new ServiceStat();
                        stat.latestValue = entry.getValue();
                        statMap.put(metricName, stat);
                    }
                }
            }
        }
    }
    computeStats.statValues = new ConcurrentSkipListMap<>();
    // Divide each metric value by the number of computes to get an average value.
    for (String key : statMap.keySet()) {
        ServiceStat serviceStatValue = statMap.get(key);
        serviceStatValue.unit = PhotonModelConstants.getUnitForMetric(key);
        serviceStatValue.sourceTimeMicrosUtc = Utils.getNowMicrosUtc();
        serviceStatValue.latestValue = serviceStatValue.latestValue / numberOfComputeResponse;
        computeStats.statValues.put(key, Collections.singletonList(serviceStatValue));
    }
    return computeStats;
}
Also used : ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) QueryTask(com.vmware.xenon.services.common.QueryTask) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap)

Example 5 with ServiceStat

use of com.vmware.xenon.common.ServiceStats.ServiceStat in project photon-model by vmware.

the class AzureCostStatsService method createAzureSubscriptionStats.

// Create Azure account stats
private void createAzureSubscriptionStats(Context context, AzureSubscription subscription) {
    // convert the subscription daily costs to cumulative
    AtomicDouble cumulativeValue = new AtomicDouble(0.0);
    subscription.cost = subscription.cost.entrySet().stream().sorted(Comparator.comparing(Entry::getKey)).collect(Collectors.toMap(Entry::getKey, e -> cumulativeValue.addAndGet(e.getValue())));
    Consumer<List<ComputeState>> subscriptionStatsProcessor = (subscriptionComputeStates) -> subscriptionComputeStates.forEach(subscriptionComputeState -> {
        String statName = AzureStatsNormalizer.getNormalizedStatKeyValue(AzureCostConstants.COST);
        String costUnit = AzureStatsNormalizer.getNormalizedUnitValue(AzureCostConstants.DEFAULT_CURRENCY_VALUE);
        ComputeStats subscriptionStats = new ComputeStats();
        subscriptionStats.computeLink = subscriptionComputeState.documentSelfLink;
        subscriptionStats.statValues = new ConcurrentSkipListMap<>();
        List<ServiceStat> costStats = new ArrayList<>();
        for (Entry<Long, Double> cost : subscription.cost.entrySet()) {
            ServiceStat azureAccountStat = AzureCostHelper.createServiceStat(statName, cost.getValue(), costUnit, cost.getKey());
            costStats.add(azureAccountStat);
        }
        subscriptionStats.statValues.put(statName, costStats);
        context.statsResponse.statsList.add(subscriptionStats);
    });
    processSubscriptionStats(context, subscription, subscriptionStatsProcessor);
}
Also used : AzureCostConstants(com.vmware.photon.controller.model.adapters.azure.constants.AzureCostConstants) AzureStatsNormalizer(com.vmware.photon.controller.model.adapters.azure.utils.AzureStatsNormalizer) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) Arrays(java.util.Arrays) DateTimeZone(org.joda.time.DateTimeZone) ServiceTypeCluster(com.vmware.photon.controller.model.util.ClusterUtil.ServiceTypeCluster) QueryTask(com.vmware.xenon.services.common.QueryTask) ServiceDocument(com.vmware.xenon.common.ServiceDocument) AzureUriPaths(com.vmware.photon.controller.model.adapters.azure.AzureUriPaths) StringUtils(org.apache.commons.lang3.StringUtils) ComputeType(com.vmware.photon.controller.model.resources.ComputeDescriptionService.ComputeDescription.ComputeType) EndpointAllocationTaskService(com.vmware.photon.controller.model.tasks.EndpointAllocationTaskService) SingleResourceStatsCollectionTaskState(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState) Utils(com.vmware.xenon.common.Utils) BufferedSink(okio.BufferedSink) Map(java.util.Map) AzureService(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureService) URI(java.net.URI) AzureSubscriptionsEnumerationRequest(com.vmware.photon.controller.model.adapters.azure.ea.enumeration.AzureSubscriptionsEnumerationService.AzureSubscriptionsEnumerationRequest) Builder(com.vmware.xenon.services.common.QueryTask.Builder) Path(java.nio.file.Path) EndpointState(com.vmware.photon.controller.model.resources.EndpointService.EndpointState) Interceptor(okhttp3.Interceptor) ComputeStatsResponse(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse) AzureDetailedBillHandler(com.vmware.photon.controller.model.adapters.azure.ea.utils.AzureDetailedBillHandler) Request(okhttp3.Request) ComputeStatsRequest(com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest) StatelessService(com.vmware.xenon.common.StatelessService) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) OldEaSummarizedBillElement(com.vmware.photon.controller.model.adapters.azure.model.cost.OldEaSummarizedBillElement) TaskManager(com.vmware.photon.controller.model.adapters.util.TaskManager) Set(java.util.Set) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) AzureSubscriptionsEnumerationService(com.vmware.photon.controller.model.adapters.azure.ea.enumeration.AzureSubscriptionsEnumerationService) AzureCostHelper(com.vmware.photon.controller.model.adapters.azure.ea.utils.AzureCostHelper) List(java.util.List) Stream(java.util.stream.Stream) UriUtils(com.vmware.xenon.common.UriUtils) Entry(java.util.Map.Entry) QueryOption(com.vmware.xenon.services.common.QueryTask.QuerySpecification.QueryOption) INTERNAL_REQUEST_TIMEOUT_SECONDS(com.vmware.photon.controller.model.adapters.azure.constants.AzureCostConstants.INTERNAL_REQUEST_TIMEOUT_SECONDS) ResourceMetrics(com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics) SingleResourceTaskCollectionStage(com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceTaskCollectionStage) Okio(okio.Okio) QueryByPages(com.vmware.photon.controller.model.query.QueryUtils.QueryByPages) OperationContext(com.vmware.xenon.common.OperationContext) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) HashMap(java.util.HashMap) Function(java.util.function.Function) AzureResource(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureResource) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) EndpointConfigRequest(com.vmware.photon.controller.model.adapterapi.EndpointConfigRequest) Query(com.vmware.xenon.services.common.QueryTask.Query) CollectionUtils(org.apache.commons.collections.CollectionUtils) EaBillLinkElement(com.vmware.photon.controller.model.adapters.azure.model.cost.EaBillLinkElement) ComputeState(com.vmware.photon.controller.model.resources.ComputeService.ComputeState) AUTO_DISCOVERED_ENTITY(com.vmware.photon.controller.model.constants.PhotonModelConstants.AUTO_DISCOVERED_ENTITY) EaBillLinks(com.vmware.photon.controller.model.adapters.azure.model.cost.EaBillLinks) BiConsumer(java.util.function.BiConsumer) Response(okhttp3.Response) EndpointType(com.vmware.photon.controller.model.constants.PhotonModelConstants.EndpointType) Call(okhttp3.Call) Callback(okhttp3.Callback) AzureSubscription(com.vmware.photon.controller.model.adapters.azure.model.cost.AzureSubscription) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) ExecutorService(java.util.concurrent.ExecutorService) OldApi(com.vmware.photon.controller.model.adapters.azure.model.cost.OldApi) AdapterUtils(com.vmware.photon.controller.model.adapters.util.AdapterUtils) ServiceErrorResponse(com.vmware.xenon.common.ServiceErrorResponse) Files(java.nio.file.Files) Operation(com.vmware.xenon.common.Operation) QueryUtils(com.vmware.photon.controller.model.query.QueryUtils) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) TypeName(com.vmware.xenon.common.ServiceDocumentDescription.TypeName) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) File(java.io.File) QueryTop(com.vmware.photon.controller.model.query.QueryUtils.QueryTop) BillParsingStatus(com.vmware.photon.controller.model.adapters.azure.model.cost.BillParsingStatus) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) LocalDate(org.joda.time.LocalDate) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) OkHttpClient(okhttp3.OkHttpClient) Paths(java.nio.file.Paths) ClusterUtil(com.vmware.photon.controller.model.util.ClusterUtil) ComputeStateWithDescription(com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription) PhotonModelConstants(com.vmware.photon.controller.model.constants.PhotonModelConstants) ServiceDocumentDescription(com.vmware.xenon.common.ServiceDocumentDescription) Comparator(java.util.Comparator) QuerySpecification(com.vmware.xenon.services.common.QueryTask.QuerySpecification) OperationJoin(com.vmware.xenon.common.OperationJoin) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) ArrayList(java.util.ArrayList) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) ServiceStat(com.vmware.xenon.common.ServiceStats.ServiceStat) Entry(java.util.Map.Entry) ComputeStats(com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

ServiceStat (com.vmware.xenon.common.ServiceStats.ServiceStat)37 ArrayList (java.util.ArrayList)21 ComputeStats (com.vmware.photon.controller.model.adapterapi.ComputeStatsResponse.ComputeStats)16 SingleResourceStatsCollectionTaskState (com.vmware.photon.controller.model.tasks.monitoring.SingleResourceStatsCollectionTaskService.SingleResourceStatsCollectionTaskState)15 List (java.util.List)11 Map (java.util.Map)11 ComputeStatsRequest (com.vmware.photon.controller.model.adapterapi.ComputeStatsRequest)10 ResourceMetrics (com.vmware.photon.controller.model.monitoring.ResourceMetricsService.ResourceMetrics)10 ComputeState (com.vmware.photon.controller.model.resources.ComputeService.ComputeState)9 Operation (com.vmware.xenon.common.Operation)9 QueryTask (com.vmware.xenon.services.common.QueryTask)9 HashMap (java.util.HashMap)9 ServiceStats (com.vmware.xenon.common.ServiceStats)8 UriUtils (com.vmware.xenon.common.UriUtils)8 URI (java.net.URI)8 PhotonModelConstants (com.vmware.photon.controller.model.constants.PhotonModelConstants)7 ComputeStateWithDescription (com.vmware.photon.controller.model.resources.ComputeService.ComputeStateWithDescription)7 ServiceDocument (com.vmware.xenon.common.ServiceDocument)7 StatelessService (com.vmware.xenon.common.StatelessService)7 Utils (com.vmware.xenon.common.Utils)7