All Products
Search
Document Center

Simple Log Service:Use GetHistograms to query the distribution of logs

最終更新日:Oct 26, 2023

This topic describes how to call the GetHistograms operation of Simple Log Service SDK for Python to query the distribution of logs within a specific time range and provides sample code.

Prerequisites

  • A Resource Access Management (RAM) user is created, and the required permissions are granted to the RAM user. For more information, see Create a RAM user and grant permissions to the RAM user.

  • The ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured. For more information, see Configure environment variables.

    Important
    • The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. We recommend that you use the AccessKey pair of a RAM user to call API operations or perform routine O&M.

    • We recommend that you do not save the AccessKey ID or AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked, and the security of all resources within your account may be compromised.

  • Simple Log Service SDK for Python is installed. For more information, see Install Simple Log Service SDK for Python.

  • Logs are written to a Logstore. For more information, see Data collection overview.

Usage notes

In this example, the public Simple Log Service endpoint for the China (Hangzhou) region is used, which is https://cn-hangzhou.log.aliyuncs.com. If you want to access Simple Log Service by using other Alibaba Cloud services that reside in the same region as your project, you can use the internal Simple Log Service endpoint, which is https://cn-hangzhou-intranet.log.aliyuncs.com. For more information about the supported regions and endpoints of Simple Log Service, see Endpoints.

Raw log

body_bytes_sent:1750
host:www.example.com
http_referer:www.example.com
http_user_agent:Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; it-it) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27
http_x_forwarded_for:203.0.XX.XX
remote_addr:203.0.XX.XX
remote_user:p288
request_length:13741
request_method:GET
request_time:71
request_uri:/request/path-1/file-1
http_code:200
time_local:11/Aug/2021:06:52:27
upstream_response_time:0.66

Sample code

The following sample code provides an example on how to query the distribution of PUT requests within 1 minute:

import time
import os

from aliyun.log import LogClient
from aliyun.log import GetHistogramsRequest

# Configure environment variables. In this example, the AccessKey ID and AccessKey secret are obtained from environment variables. 
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The Simple Log Service endpoint. In this example, the Simple Log Service endpoint for the China (Hangzhou) region is used. Replace the parameter value with the actual endpoint. 
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Simple Log Service client. 
client = LogClient(endpoint, accessKeyId, accessKey)

# The name of the project. 
project_name = "ali-test-project"
# The name of the Logstore. 
logstore_name = "ali-test-logstore"

if __name__ == '__main__':
    # Query the distribution of PUT requests within 1 minute. 
    query = "request_method:PUT"

    # Configure the from_time and to_time parameters to specify the start time and end time of the time range within which you want to query logs. Set the values to UNIX timestamps. 
    from_time = int(time.time()) - 60
    to_time = time.time()

    # The query request. 
    request = GetHistogramsRequest(project_name, logstore_name, from_time, to_time, "", query)
    response = client.get_histograms(request)

    # Only the subintervals within which logs are distributed are returned. 
    for Histogram in response.get_histograms():
        if Histogram.get_count() > 0:
            Histogram.log_print()

The following sample code shows the expected results. The number of requests for the three subintervals within 1 minute is 83, 32, and 43, and the results are complete.

Histogram:
from: 1671002771
to: 1671002780
count: 83
progress: Complete
Histogram:
from: 1671002790
to: 1671002800
count: 32
progress: Complete
Histogram:
from: 1671002800
to: 1671002810
count: 43
progress: Complete

References

  • Alibaba Cloud OpenAPI Explorer provides debugging capabilities, SDKs, examples, and related documents. You can use OpenAPI Explorer to debug Log Service API operations without the need to manually encapsulate or sign requests. For more information, visit OpenAPI Portal.
  • Log Service provides the command-line interface (CLI) to meet the requirements for automated configurations in Log Service. For more information, see Log Service CLI.
  • For more information about the GetHistograms operation, see GetHistograms.

  • For more information about sample code, see Alibaba Cloud Simple Log Service SDK for Python on GitHub.