Help Center/ Log Tank Service/ API Reference/ APIs/ Log Management/ Collecting Traffic Statistics on Top N Log Groups or Log Streams
Updated on 2024-06-13 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.

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

Project ID. For details about how to obtain a project ID, see Obtaining the Project ID, Account ID, Log Group ID, and Log Stream ID.

No default value.

Minimum: 32

Maximum: 32

Request Parameters

Table 2 Request header parameters

Parameter

Mandatory

Type

Description

X-Auth-Token

Yes

String

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

Minimum: 1000

Maximum: 2000

Content-Type

Yes

String

Set this parameter to application/json;charset=UTF-8.

Minimum: 30

Maximum: 30

Table 3 Request body parameters

Parameter

Mandatory

Type

Description

end_time

Yes

Long

End timestamp, in milliseconds.

is_desc

Yes

Boolean

Whether to sort data in descending order (true or false).

resource_type

Yes

String

Resource type: The value is log_group / log_stream / tenant.

log_group: log group

log_stream: log stream

tenant: tenant

sort_by

Yes

String

Sorting criteria: index, write, storage, basicTransfer, seniorTransfer, or coldStorage. The value must exist in search_list.

index: index

write: read and write

storage: storage

basicTransfer: basic transfer

seniorTransfer: advanced transfer

coldStorage: cold storage capacity

start_time

Yes

Long

Start timestamp of the query, in milliseconds. A maximum of 30 days are supported.

topn

Yes

Integer

Number of data records to be queried. The value ranges from 1 to 100.

filter

Yes

Map<String,String>

Filter criteria { "log_group_id": "xxxxxx" }Filter is in the map structure. The key is the filtering attribute, and the value is the attribute value. Fuzzy match is not supported.

search_list

Yes

Array of strings

Query data type. Multiple string arrays can be used together. This parameter can be set only to index, write, storage, basicTransfer, seniorTransfer, or coldStorage.

index: index

write: read and write

storage: storage

basicTransfer: basic transfer

seniorTransfer: advanced transfer

coldStorage: cold storage capacity

Response Parameters

Status code: 200

Table 4 Response body parameters

Parameter

Type

Description

results

Array of ResultsTopnBody objects

Response result.

Table 5 ResultsTopnBody

Parameter

Type

Description

index_traffic

Double

Index traffic, in bytes. This parameter is returned when the queried data type contains index.

storage

Double

Storage capacity, in bytes. This parameter is returned when the queried data type contains storage.

write_traffic

Double

Write traffic, in bytes. This parameter is returned when the queried data type contains write.

log_group_id

String

Log group ID. This parameter is returned when the resource type is log group.

log_group_name

String

Log group name. This parameter is returned when the resource type is log group.

log_stream_id

String

Log stream ID. This parameter is returned when the resource type is log stream.

log_stream_name

String

Log stream name. This parameter is returned when the resource type is log stream.

basic_transfer

Double

Basic transfer traffic, in bytes. This parameter is returned when the queried data type contains basicTransfer.

senior_transfer

Double

Advanced transfer traffic, in bytes. This parameter is returned when the queried data type contains seniorTransfer.

is_agency_transfer

Boolean

If the value is true, the stream displayed in the front-end resource statistics cannot be skipped.

Default: false

cold_storage

Double

Cold storage capacity.

Status code: 400

Table 6 Response body parameters

Parameter

Type

Description

errorCode

String

Error code.

Minimum: 8

Maximum: 36

errorMessage

String

Error description.

Minimum: 2

Maximum: 512

Status code: 500

Table 7 Response body parameters

Parameter

Type

Description

errorCode

String

Error code.

Minimum: 8

Maximum: 36

errorMessage

String

Error description.

Minimum: 2

Maximum: 512

Example Requests

Collecting Traffic Statistics on Top N Log Groups or Log Streams

/v2/2a473356cca5487f8373be891bffc1cf/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.

Collecting 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
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");

        ICredential auth = new BasicCredentials()
                .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());
        }
    }
}

Collecting 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
# 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"]

    credentials = BasicCredentials(ak, sk)

    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)

Collecting 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
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")

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        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.