Este tema muestra cómo usar la operación GetHistograms para consultar la distribución de registros dentro de un rango de tiempo específico.
Requisitos previos
Simple Log Service está activado.
El SDK de Simple Log Service para Python está inicializado.
El SDK de Simple Log Service para Python está instalado. Para obtener más información, consulte Install Simple Log Service SDK for Python.
Se crearon un proyecto y un logstore en Simple Log Service, y se recopilaron los registros. Para obtener más información, consulte Manage projects, Create a basic LogStore y Data collection.
Precauciones
En este ejemplo se utiliza el endpoint público de Simple Log Service correspondiente a la región China (Hangzhou). Endpoint: https://cn-hangzhou.log.aliyuncs.com.
Si desea acceder a Simple Log Service desde otros servicios de Alibaba Cloud ubicados en la misma región que su proyecto, utilice el endpoint interno de Simple Log Service: https://cn-hangzhou-intranet.log.aliyuncs.com.
Para obtener más información sobre las regiones y los endpoints compatibles de Simple Log Service, consulte Endpoint.
Registro sin procesar
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
Código de ejemplo
A continuación se muestra el código que consulta la distribución de solicitudes PUT en un intervalo de un minuto.
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()
La salida de ejemplo muestra tres intervalos de tiempo con recuentos de solicitudes de 83, 32 y 43. El campo progress es Complete, lo cual indica que la consulta finalizó.
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
Documentos relacionados
Además de su SDK nativo, SLS también es compatible con los SDK comunes de Alibaba Cloud. Para obtener más información, consulte Simple Log Service_SDK Center_Alibaba Cloud OpenAPI Explorer.
Para obtener más información sobre la operación de API, consulte Query the distribution of logs.