Query log distribution using GetHistograms
This topic shows how to use the GetHistograms operation to query log distribution within a specified time range.
Prerequisites
Simple Log Service is activated.
Simple Log Service SDK for Python is initialized.
Simple Log Service SDK for Python is installed. For more information, see Install Simple Log Service SDK for Python.
A project and a logstore are created in Simple Log Service, and logs are collected. For more information, see Manage projects, Create a basic logstore, and Data collection.
Precautions
In this example, the public Simple Log Service endpoint for the China (Hangzhou) region is used. Endpoint: https://cn-hangzhou.log.aliyuncs.com.
If you want to access Simple Log Service from 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.66Sample code
The following code queries the distribution of PUT requests within a one-minute interval.
import time
import os
from aliyun.log import LogClient
from aliyun.log import GetHistogramsRequest
# Obtain the AccessKey ID and AccessKey Secret from environment variables.
accessKeyId = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_ID', '')
accessKey = os.environ.get('ALIBABA_CLOUD_ACCESS_KEY_SECRET', '')
# The service endpoint for Log Service. This example uses the endpoint for the China (Hangzhou) region. Replace the value with the endpoint for your region.
endpoint = "cn-hangzhou.log.aliyuncs.com"
# Create a Log Service client.
client = LogClient(endpoint, accessKeyId, accessKey)
# The project name.
project_name = "ali-test-project"
# The Logstore name.
logstore_name = "ali-test-logstore"
if __name__ == '__main__':
# Query the distribution of PUT requests within a one-minute interval.
query = "request_method:PUT"
# The query time range, specified as 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)
# To simplify the output, this example prints only the intervals that contain log data.
for Histogram in response.get_histograms():
if Histogram.get_count() > 0:
Histogram.log_print()The sample output shows three time intervals with request counts of 83, 32, and 43. The progress field is Complete, indicating that the query has finished.
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: CompleteRelated documents
In addition to its native SDK, SLS also supports the common Alibaba Cloud SDKs. For more information, see Log Service_SDKcenter-Alibaba CloudOpenAPIDeveloper Portal.
For more information about the API operation, see Query the distribution of logs.
For more code examples, see Aliyun Log Python SDK on GitHub.