Analyze resource usage with CloudLens for SLS

Updated at:

Use CloudLens for SLS billing data to analyze your Simple Log Service resource usage, monitor trends, and configure alerts.

Background

CloudLens for SLS provides free billing data sourced from Alibaba Cloud Billing Management. Simple Log Service pulls this data into Tablestore (free of charge) and exposes it for query and analysis through external data association.

image

With this billing data, you can analyze Simple Log Service billable items in real time, run custom SQL queries, and configure usage alerts for your Simple Log Service resources.

Procedure

  1. Log on to the Log Service console.
  2. In the Log Application section, click the Cloud Service Lens tab, then click CloudLens for SLS.

    For more information, see CloudLens for SLS.

Billing data

SLS billing data records resource usage per Simple Log Service instance and billable item. The following table describes the key concepts.

Concept

Field

Description

instance

InstanceID

The most granular resource in Simple Log Service. An Simple Log Service corresponds to a Simple Log Service.

billable item

BillingItem

Billable items in Simple Log Service. Examples include hot-tier log storage and log index traffic.

Instances

A Simple Log Service Simple Log Service is the most granular resource (a Simple Log Service) in the service. The instance ID format is ${aliUid};${Project};${Logstore};${Region}.

Variable

Description

${aliUid}

Your Alibaba Cloud account ID.

${Project}

A resource management unit in Simple Log Service. It serves as the primary boundary for resource isolation and access control. For more information, see Project.

${Logstore}

The log collection, storage, and query unit in Simple Log Service. For more information, see Logstore.

${Region}

The physical data center location for Simple Log Service. You can specify a region when you create a Project. Once specified, the region cannot be changed. For more information, see Supported regions.

Billable items

Billable items are the specific, chargeable capabilities of Simple Log Service. Each Simple Log Service in Simple Log Service is billed separately. For example, storage fees for log retention and write traffic fees for log collection. For more information, see Billable items of pay-by-feature.

The BillingItem field represents the Simple Log Service. For a breakdown of Simple Log Service billable items, see Billing structure.

Scenarios for SLS usage analysis

Use billing data for custom analysis and real-time usage monitoring.

Monitor usage trends and instance details

Monitor usage trends and per-instance details to identify sudden billing changes.

  • Monitor usage trends

    The date_trunc() function lets you analyze usage trends at different time granularities.

    • Query and analysis statement

      In this query, BillingItem is the billable item and Usage is the resource usage.

      * |
      select
       date_trunc('day', __time__) as "Date",
       BillingItem,
       round(sum(Usage), 3) as "Usage"
      FROM instance_bill
      where
       productcode = 'sls'
      group by
       "Date",
       BillingItem
      order by
       "Date"
    • Query and analysis results: You can use a Flow chart (Pro) to visualize the results. Configure the chart by setting Axis X Field to Date, Axis Y Field to Usage, and Aggregate Column to BillingItem.image.png

  • Monitor instance usage details

    When you analyze usage details, you often need to drill down to a specific instance. For an SLS instance, combine queries for cost, usage, and storage:

    • t1: Gathers usage statistics for each billable item per instance.

    • t2: Gathers cost statistics for each instance, including the payable amount (PretaxAmount) and the month-over-month growth rate.

    • t3: Gathers total storage usage. Storage is calculated daily, and this query retrieves the total usage up to the last day of the selected time range.

      • Query and analysis statement

        * | with t1 as (
        	select
        	InstanceID,
        	BillingItem,
        	sum(Usage) as Usage
        	FROM instance_bill
        	where
        	ProductCode = 'sls'
        	and BillingItem not like '%Storage%'
        	group by
        	InstanceID,
        	BillingItem
        ),
        t2 as (
        	select
        	InstanceID,
        	round(di [1], 3) as "Fee",
        	concat(
        		cast(round(di [3] * 100 -100, 3) as varchar),
        		'%'
        	) "Growth rate compared with the previous month"
        	from(
        		select
        		InstanceID,
        		compare(cost, 2592000) as di
        		FROM (
        			select
        			InstanceID,
        			round(sum(PretaxAmount), 2) as cost
        			FROM instance_bill
        			where
        			ProductCode = 'sls'
        			group by
        			InstanceID
        		)
        		group by
        		InstanceID
        	)
        ),
        t3 as (
        	select
        	InstanceID,
        	max_by("Storage", t) as "Storage"
        	FROM (
        		select
        		t,
        		InstanceID,
        		round(sum("Storage"), 2) as "Storage"
        		from(
        			select
        			date_trunc('day', __time__) as t,
        			InstanceID,
        			case
        			when BillingItem like '%Storage%' then Usage
        			else 0
        			end as "Storage"
        			FROM instance_bill
        			WHERE
        			ProductCode = 'sls'
        			group by
        			t,
        			InstanceID,
        			"Storage"
        		)
        		group by
        		t,
        		InstanceID
        		order by
        		t asc
        	)
        	group by
        	InstanceID
        )
        select
         t1.InstanceID as InstanceID,
         t1.BillingItem as BillingItem,
         t1.Usage as Usage,
         t2."Fee" as "Fee (CNY)",
         t2."Growth rate compared with the previous month" as "Growth rate compared with the previous month (%)",
         t3."Storage" as "Storage"
        FROM t1
         left join t2 on t1.InstanceID = t2.InstanceID
         left join t3 on t1.InstanceID = t3.InstanceID
        order by
         "Fee (CNY)" desc
        limit
         150000
      • Query and analysis results

        You can use a cross table to visualize the billable item details for each instance. Configure the table with the following settings: Category Column: BillingItem, Metric Column: Usage, and Aggregate Column: InstanceID, Fee (CNY), Growth rate compared with the previous month (%), and Storage. After the configuration, sort by a specific billable item to identify the highest-usage instances. The resource cost statistics table includes columns such as InstanceID, Fee (CNY), Growth rate compared with the previous month (%), Storage, Active Shard Lease, index traffic, read and write traffic, Read/Write Operations, Raw Data Traffic, Data Shipped to MaxCompute, and Outbound Internet Traffic. For example, instance1 has a fee of 544.71 CNY, a month-over-month growth of 1.428%, and high consumption in areas like storage (32,696.46) and index traffic (4,472.05). In contrast, instance2 has a fee of 458.65 CNY, with significantly lower resource usage across all dimensions.

With usage monitoring in place, analyze changes in typical scenarios such as read/write traffic, storage, and index traffic.

Scenario: Increased read and write traffic

The following figure shows a detailed chart of read and write traffic usage for Simple Log Service. You can see from the chart that read and write traffic usage increased significantly from March 31 to April 1. You need to perform further analysis to identify the cause of this increase.

image.png

Analyze per-instance read and write traffic with the following query.

  • Query and analysis statement

    * |
    select
     "Date",
     InstanceID,
    "Usage"
    from(
    	select
    	date_trunc('day', __time__) as "Date",
    	InstanceID,
    	round(sum(Usage), 3) as "Usage"
    	FROM instance_bill
    	where
    	productcode = 'sls'
    	and BillingItem = 'Read/Write Traffic'
    	group by
    	"Date",
    	InstanceID
    )
    group by
     "Date",
     "Usage",
     InstanceID
    order by
     "Date"
    limit
     10000
  • Query and analysis results

    You can use a cross table to display the read and write traffic for each instance over the week. For example, by sorting the data for April 1, 2023, in descending order, you can identify that instance1 and instance2 experienced a sudden usage spike, which caused the overall increase. Using the instance ID, you can pinpoint the corresponding Project and Logstore to further optimize the read and write traffic.

Scenario: Increased storage usage

The following figure shows that the Simple Log Service storage usage increased significantly from 12-26 to 01-09. You must analyze the cause and optimize your storage.

image.png

You can use the following query to check the storage usage of each instance on a specific day, such as January 3, and compare it with the data from the previous one, two, and three days. You can then visualize the results in a chart.

  • Query and analysis statement

    * |
    select
     InstanceID,
     round(diffreal [1], 3) as "Usage",
     concat(
    		cast(
    			round(diffreal [1] / sum(diffreal [1]) over() * 100, 2) as varchar
    		),
    		'%'
    	) as "Usage Share",
     concat(
    		cast(
    			coalesce(round(diffreal [5] * 100 -100), 2) as varchar
    		),
    		'%'
    	) as "Growth vs. 1 Day Ago (%)",
     concat(
    		cast(
    			round(coalesce(diffreal [6] * 100 -100, 2)) as varchar
    		),
    		'%'
    	) as "Growth vs. 2 Days Ago (%)",
     concat(
    		cast(
    			round(coalesce(diffreal [7] * 100 -100, 2) as varchar
    		),
    		'%'
    	) as "Growth vs. 3 Days Ago (%)"
    from(
    	select
    	InstanceID,
    	compare(Usage, 86400, 172800, 259200) as diffreal
    	from(
    		select
    		InstanceID,
    		sum(Usage) as Usage
    		FROM instance_bill
    		where ProductCode='sls' and BillingItem = 'Storage'
    		group by
    		InstanceID
    	)
    	group by
    	InstanceID
    )
    order by
     diffreal [1] desc
    limit
     1000
  • Query and analysis results

    You can use a cross table to visualize the results. By sorting the instances by their share of storage usage, you can see that the top four instances maintain a stable usage trend. However, the storage usage for instance5 shows a significant upward trend over the last three days. Therefore, you need to investigate the cause of the storage increase for instance5.

You can run the following query to further analyze the cause of the storage increase for instance5.

  • Query and analysis statement

    Replace ${instance5} with the actual instance ID.

    * |
    select
     date_trunc('day', __time__) as "Date",
     BillingItem,
     round(sum(Usage), 3) as "Usage"
    FROM instance_bill
    where
     productcode = 'sls'
    and InstanceID = ${instance5}
    group by
     "Date",
     BillingItem
    order by
     "Date"
  • Query and analysis results

    The usage trend chart for this instance shows that in addition to storage usage, its read and write traffic and index traffic have also increased. Although the read and write traffic and index traffic later stabilized, the storage usage continued to grow, especially from 01-01 to 01-09. This happens because storage usage includes both data from log writes and index data. If the data retention period remains unchanged, a daily increase in write traffic and index traffic will lead to an increase in total storage.

    For example, assume the daily new write and index traffic is 1 GB and the data retention period is 30 days. The total storage will stabilize at 30 GB. If the new write and index traffic increases to 2 GB per day, the total storage on the first day after the increase will be 31 GB, 32 GB on the second day, and so on. After 30 days, the total storage will reach 60 GB (2 GB × 30 days). This explains why storage usage continues to rise even when index traffic and read and write traffic are no longer changing significantly.

    After you analyze the reason for the increase in storage usage, you need to optimize it. Since write traffic is tied to your business workload, you can reduce the number of indexes based on actual needs and shorten the data retention period to decrease storage.

    image.png

    image.png

Scenario: Increased index traffic

The following figure shows the usage trend of an instance. You can see a significant increase in index traffic from 12-26 to 12-28. image.png

Log index traffic is related to write traffic and the number of indexes (full-text and field indexes). The billing data indicates that an increase in read and write traffic led to a corresponding increase in index traffic. Since read and write traffic is tied to your business, you can optimize index traffic by reducing the number of indexes. For example, you can disable full-text indexing and reduce the number of field indexes, creating indexes only for key fields.

Scenario: Billed usage is zero

When you analyze resource usage with billing data, you may find that billed usage is zero even though resources are being consumed. As shown in the following figure, resource usage drops to zero on 02-28. You need to investigate the cause.

image.png

  1. Use the billing data to check if fees are being offset by a resource plan.

    • Query and analysis statement

      Query both the usage data and the amount of usage offset by a resource plan.

      (*) |
      select
      	InstanceID,
       BillingItem,
       	Usage as "Usage",
      	DeductedByResourcePackage as "Resource usage for which fees are offset by using resource plans"
      FROM instance_bill
      where
       productcode = 'sls'
    • Query and analysis results

      Although the billed usage is zero, the 'Resource usage for which fees are offset by using resource plans' is not zero. This indicates that a resource plan is covering the costs.

  2. Determine if the resource plan covers the total usage of the billable item.

    • Query and analysis statement

      Query the original resource usage before it is offset by a resource plan. The offset amount is represented by DeductedByResourcePackage, and the total original usage is Usage + DeductedByResourcePackage.

      * |
      select
       date_trunc('day', __time__) as "Date",
       BillingItem,
       round(sum(Usage + DeductedByResourcePackage), 3) as "Resource usage before the fees are offset by using resource plans"
      FROM instance_bill
      where
       productcode = 'sls'
      group by
       "Date",
       BillingItem
      order by
       "Date"
    • Query and analysis results

      image.png

Configure usage alerts

Simple Log Service alerting provides alert monitoring, noise reduction, incident management, and notification dispatching. CloudLens for SLS includes anomaly detection that connects directly to the alerting center for real-time Simple Log Service usage monitoring. For more information, see Set up alerts.

Alerts for total daily usage

Monitor total usage to control costs. Use the following query to check storage usage.

  • Query and analysis statement

     * | 
    select
    	round(sum(Usage), 2) as "Storage Usage"
    from instance_bill
    where ProductCode='sls' 
    and BillingItem='Storage'
  • Alert configuration

    In the alert configuration, set the Trigger Condition to Data Matches and define the condition to fire when storage usage exceeds your threshold, for example, "Storage Usage" > 400.

Alerts for a single instance

Monitor individual instance usage. The following query triggers an alert when any instance's storage usage exceeds 10.

  • Query and analysis statement

    * |
    select
     InstanceID,
     "Storage Usage"
    from(
     select
     InstanceID,
     round(sum(Usage), 2) as "Storage Usage"
     FROM instance_bill
     where
     ProductCode = 'sls'
     and BillingItem = 'Storage'
     group by
     InstanceID
     )
    where
     "Storage Usage" > 10
  • Alert configuration

    In the alert configuration, set the Trigger Condition to Data Exists. This means an alert is triggered if any instance has storage usage exceeding the threshold. The alert notification will display the instance ID and its corresponding storage usage.

Create custom alerts for your business requirements. For more information, see Quick start for alert configuration.