Query the number of distributed logs

Updated at:

Calls the GetHistograms operation to query the distribution of logs that match specified conditions in a Logstore.

Prerequisites

Parameters

def get_histograms(self, request):

Request parameters

Name

Type

Required

Description

request

GetHistogramsRequest

Yes

The request parameters for the GetHistograms operation. Specifies conditions for querying log distribution in a Logstore.

GetHistogramsRequest parameters:

Name

Type

Required

Description

project

String

Yes

Project name: The name of the project. A project is the basic resource management unit in Simple Log Service. It is used to isolate resources and control access for different users. For more information, see Manage projects.

logstore

String

Yes

Logstore name: The name of the Logstore. A Logstore is a unit for collecting, storing, and querying log data in Simple Log Service. For more information, see Manage Logstores.

fromTime

int

Yes

The start time of the subinterval. The value is a UNIX timestamp.

toTime

int

Yes

The end time of the subinterval. The value is a UNIX timestamp.

topic

String

No

The topic of logs. The default value is an empty string. For more information, see Log topics.

query

String

No

The query statement. For more information, see Query and analytic statements.

Note

Only query statements are supported. Analytic statements are not supported.

Response parameters

For details about response parameters, see GetHistograms.

Sample code

from aliyun.log import LogClient, GetHistogramsRequest
import os

# This example shows how to obtain an AccessKey ID and an AccessKey secret from environment variables.
access_key_id = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
access_key_secret = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')

# The endpoint of Simple Log Service.
endpoint = "cn-hangzhou.log.aliyuncs.com"

client = LogClient(endpoint, access_key_id, access_key_secret)


def main():
    project = "proj-test-1"
    logstore = "logstore-1"
    fromTime = 1728973609
    toTime = 1728973612
    # Instantiate the GetHistogramsRequest class.
    request = GetHistogramsRequest(project, logstore, fromTime, toTime)
    # Query the number of distributed logs.
    response = client.get_histograms(request)
    response.log_print()


if __name__ == '__main__':
    main()

Sample response

GetHistogramsResponse:
headers: {'Server': 'AliyunSLS', 'Content-Type': 'application/json', 'Content-Length': '205', 'Connection': 'keep-alive', 'Access-Control-Allow-Origin': '*', 'Date': 'Tue, 15 Oct 2024 06:48:10 GMT', 'x-log-time': '1728974890', 'x-log-count': '1', 'x-log-progress': 'Complete', 'x-log-requestid': '670E102A147071CE569B****'}
progress: Complete
count: 1

histograms class:

Histogram:
from: 1728973609
to: 1728973610
count: 0
progress: Complete


Histogram:
from: 1728973610
to: 1728973611
count: 0
progress: Complete


Histogram:
from: 1728973611
to: 1728973612
count: 1
progress: Complete



Process finished with exit code 0

References