Help Center/ Log Tank Service/ API Reference/ APIs/ Log Management/ Collecting Traffic Statistics on Top N Log Groups or Log Streams
Updated on 2025-08-21 GMT+08:00

Collecting Traffic Statistics on Top N Log Groups or Log Streams

Function

This API is used to collect traffic statistics on top n log groups or log streams, ranking them by traffic volume. You can specify a time range and traffic type for the statistics. The results are useful for resource monitoring, abnormal traffic locating and analysis, real-time traffic statistics collection, and data backtracking.

Calling Method

For details, see Calling APIs.

URI

POST /v2/{project_id}/lts/topn-traffic-statistics

Table 1 Path Parameters

Parameter

Mandatory

Type

Description

project_id

Yes

String

Definition

Project ID. It can be obtained by calling an API or from the console. For details, see Obtaining a Project ID.

Constraints

N/A

Range

N/A

Default Value

N/A

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

Yes

String

Definition

User token obtained from IAM. For details about how to obtain it, see Obtaining a User Token.

Constraints

N/A

Range

N/A

Default Value

N/A

Content-Type

Yes

String

Definition

Message body format. Set this parameter to application/json;charset=utf8.

Constraints

N/A

Range

N/A

Default Value

N/A

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

end_time

Yes

Long

Definition

End time for querying logs by time range. The value is a timestamp in seconds.

The time range between start_time and end_time includes both the start time and end time.

Constraints

  • The maximum time range for a query is 30 days.

  • The values of start_time and end_time must be different.

Range

The value must be a timestamp, measured in milliseconds from January 1, 1970, 00:00:00 UTC.

Default Value

N/A

is_desc

Yes

Boolean

Definition

Whether to sort the traffic.

Constraints

N/A

Range

  • true: descending order

  • false: ascending order

Default Value

N/A

resource_type

Yes

String

Definition

Resource ID. It varies depending on the resource type.

  • When the resource type is set to log_group, resource_id indicates the log group ID.

  • When the resource type is set to log_stream, resource_id indicates the log stream ID.

  • When the resource type is set to tenant, resource_id should be left empty.

Constraints

N/A

Range

N/A

Default Value

N/A

sort_by

Yes

String

Definition

Type of traffic used for sorting. The value can be index, write, storage, basicTransfer, seniorTransfer, or coldStorage.

Constraints

N/A

Range

  • index

  • write: read and write

  • storage

  • basicTransfer: basic transfer

  • seniorTransfer: advanced transfer

  • coldStorage: cold storage capacity

Default Value

N/A

start_time

Yes

Long

Definition

End time for querying logs by time range. The value is a timestamp in seconds.

The time range between start_time and end_time includes both the start time and end time.

Constraints

  • The maximum time range for a query is 30 days.

  • The values of start_time and end_time must be different.

Range

The value must be a timestamp, measured in milliseconds from January 1, 1970, 00:00:00 UTC.

Default Value

N/A

topn

Yes

Integer

Definition

Number of traffic records to be queried. n indicates the number of traffic records.

Constraints

N/A

Range

1~100

Default Value

N/A

filter

Yes

Map<String,String>

Definition

Filter criteria for collecting traffic statistics. The value is a key-value pair. You can filter logs by log group ID or log stream ID.

  • key: log_group_id or log_stream_id

  • value: log_group_id or log_stream_id

Example:

{ "log_group_id": "xxxxxx" }

Constraints

N/A

search_list

Yes

Array of strings

Definition

Traffic list to be queried. Multiple string arrays can be used together. This parameter can be set only to index, write, storage, basicTransfer, seniorTransfer, or coldStorage.

Example:

"search_list": [ "index", "write", "storage" ]

Constraints

N/A

Response Parameters

Status code: 200

Table 4 Response body parameters

Parameter

Type

Description

results

Array of ResultsTopnBody objects

Definition

Traffic statistics results of top n log groups or log streams.

Table 5 ResultsTopnBody

Parameter

Type

Description

index_traffic

Double

Definition

Index traffic (byte), which is returned when the traffic type is set to index.

Range

N/A

storage

Double

Definition

Storage traffic (byte), which is returned when the traffic type is set to storage.

Range

N/A

write_traffic

Double

Definition

Read and write traffic (byte), which is returned when the traffic type is set to write.

Range

N/A

log_group_id

String

Definition

Log group ID.

Range

N/A

log_group_name

String

Definition

Log group name.

Range

N/A

log_group_name_alias

String

Definition

Log group alias.

Range

N/A

log_stream_id

String

Definition

Log stream ID.

Range

N/A

log_stream_name

String

Definition

Name of the log stream.

Range

N/A

log_stream_name_alias

String

Definition

Log stream alias.

Range

N/A

basic_transfer

Double

Definition

Basic transfer traffic (byte), which is returned when the traffic type is set to basicTransfer.

Range

N/A

senior_transfer

Double

Definition

Advanced transfer traffic (byte), which is returned when the traffic type is set to seniorTransfer.

Range

N/A

is_agency_transfer

Boolean

Definition

Whether to delegate transfer. This parameter is returned when the traffic type is set to basicTransfer or seniorTransfer.

Range

N/A

cold_storage

Double

Definition

Cold storage traffic (byte), which is returned when the traffic type is set to coldStorage.

Range

N/A

Status code: 400

Table 6 Response body parameters

Parameter

Type

Description

errorCode

String

Definition

Error code. For details, see Error Codes.

Range

N/A

errorMessage

String

Definition

Error message.

Range

N/A

Status code: 500

Table 7 Response body parameters

Parameter

Type

Description

errorCode

String

Definition

Error code. For details, see Error Codes.

Range

N/A

errorMessage

String

Definition

Error message.

Range

N/A

Example Requests

Collect traffic statistics on top n log groups or log streams.

/v2/{project_id}/lts/topn-traffic-statistics

{
  "sort_by" : "storage",
  "is_desc" : true,
  "resource_type" : "log_stream",
  "filter" : { },
  "start_time" : 1668668183969,
  "end_time" : 1669272983969,
  "search_list" : [ "index", "write", "storage" ],
  "topn" : 100
}

Example Responses

Status code: 200

Query succeeded.

{
  "results" : [ {
    "index_traffic" : 0,
    "log_stream_id" : "6fd93d47-7630-4284-a622-311d0082f6bb",
    "log_stream_name" : "cmdb-cce-cluster",
    "storage" : 59810657587,
    "write_traffic" : 0
  }, {
    "index_traffic" : 0,
    "log_stream_id" : "504ec3dd-ac28-4783-babb-22a49f36afe3",
    "log_stream_name" : "CMSkaifatest",
    "storage" : 20033606015,
    "write_traffic" : 0
  }, {
    "index_traffic" : 6825703991,
    "log_stream_id" : "a14dacb0-5a13-43a8-89a3-ea5424d95133",
    "log_stream_name" : "ELB",
    "storage" : 15659303771,
    "write_traffic" : 1.3651407982E9
  }, {
    "index_traffic" : 302172889,
    "log_stream_id" : "25fe7494-7395-438e-8340-647613673ffa",
    "log_stream_name" : "LTStest-916-statefulset",
    "storage" : 316552589,
    "write_traffic" : 6.04345778E7
  }, {
    "index_traffic" : 0,
    "log_stream_id" : "956586fc-b828-44be-8672-0a323962a8fa",
    "log_stream_name" : "mongodb_slow",
    "storage" : 0,
    "write_traffic" : 0
  } ]
}

Status code: 400

Invalid request. Modify the request based on the description in error_msg before a retry.

{
  "errorCode" : "LTS.0208",
  "errorMessage" : "The log stream does not existed"
}

Status code: 500

The server has received the request but encountered an internal error.

{
  "errorCode" : "LTS.0203",
  "errorMessage" : "Internal Server Error"
}

SDK Sample Code

The SDK sample code is as follows.

Collect traffic statistics on top n log groups or log streams.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.lts.v2.region.LtsRegion;
import com.huaweicloud.sdk.lts.v2.*;
import com.huaweicloud.sdk.lts.v2.model.*;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;

public class ListTopnTrafficStatisticsSolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");
        String projectId = "{project_id}";

        ICredential auth = new BasicCredentials()
                .withProjectId(projectId)
                .withAk(ak)
                .withSk(sk);

        LtsClient client = LtsClient.newBuilder()
                .withCredential(auth)
                .withRegion(LtsRegion.valueOf("<YOUR REGION>"))
                .build();
        ListTopnTrafficStatisticsRequest request = new ListTopnTrafficStatisticsRequest();
        TopnRequstBody body = new TopnRequstBody();
        List<String> listbodySearchList = new ArrayList<>();
        listbodySearchList.add("index");
        listbodySearchList.add("write");
        listbodySearchList.add("storage");
        body.withSearchList(listbodySearchList);
        body.withTopn(100);
        body.withStartTime(1668668183969L);
        body.withSortBy("storage");
        body.withResourceType("log_stream");
        body.withIsDesc(true);
        body.withEndTime(1669272983969L);
        request.withBody(body);
        try {
            ListTopnTrafficStatisticsResponse response = client.listTopnTrafficStatistics(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

Collect traffic statistics on top n log groups or log streams.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdklts.v2.region.lts_region import LtsRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdklts.v2 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = os.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]
    projectId = "{project_id}"

    credentials = BasicCredentials(ak, sk, projectId)

    client = LtsClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(LtsRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = ListTopnTrafficStatisticsRequest()
        listSearchListbody = [
            "index",
            "write",
            "storage"
        ]
        request.body = TopnRequstBody(
            search_list=listSearchListbody,
            topn=100,
            start_time=1668668183969,
            sort_by="storage",
            resource_type="log_stream",
            is_desc=True,
            end_time=1669272983969
        )
        response = client.list_topn_traffic_statistics(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

Collect traffic statistics on top n log groups or log streams.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    lts "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/lts/v2/region"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")
    projectId := "{project_id}"

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        WithProjectId(projectId).
        Build()

    client := lts.NewLtsClient(
        lts.LtsClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.ListTopnTrafficStatisticsRequest{}
	var listSearchListbody = []string{
        "index",
	    "write",
	    "storage",
    }
	request.Body = &model.TopnRequstBody{
		SearchList: listSearchListbody,
		Topn: int32(100),
		StartTime: int64(1668668183969),
		SortBy: "storage",
		ResourceType: "log_stream",
		IsDesc: true,
		EndTime: int64(1669272983969),
	}
	response, err := client.ListTopnTrafficStatistics(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

For SDK sample code of more programming languages, see the Sample Code tab in API Explorer. SDK sample code can be automatically generated.

Status Codes

Status Code

Description

200

Query succeeded.

400

Invalid request. Modify the request based on the description in error_msg before a retry.

500

The server has received the request but encountered an internal error.

Error Codes

See Error Codes.