Este tópico mostra como usar a operação GetHistograms para consultar a distribuição de logs em um intervalo de tempo específico.
Pré-requisitos
O Simple Log Service está ativado.
O SDK do Simple Log Service para Python está inicializado.
O SDK do Simple Log Service para Python está instalado. Para obter mais informações, consulte Instale Simple Log Service SDK for Python.
Um projeto e um Logstore foram criados no Simple Log Service e os logs estão sendo coletados. Para obter mais informações, consulte Gerencie projects, Crie a basic LogStore e Data collection.
Precauções
Este exemplo usa o endpoint público do Simple Log Service para a região China (Hangzhou). Endpoint: https://cn-hangzhou.log.aliyuncs.com.
Para acessar o Simple Log Service a partir de outros serviços da Alibaba Cloud localizados na mesma região do seu projeto, use o endpoint interno do Simple Log Service: https://cn-hangzhou-intranet.log.aliyuncs.com.
Para obter mais informações sobre as regiões e os endpoints com suporte no Simple Log Service, consulte Endpoint.
Log bruto
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 exemplo
O código a seguir consulta a distribuição de requisições PUT em um intervalo de um 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()
A saída de exemplo mostra três intervalos de tempo com contagens de requisições de 83, 32 e 43. O campo progress é Complete, indicando que a consulta foi concluída.
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
Além do SDK nativo, o SLS também suporta os SDKs padrão da Alibaba Cloud. Para obter mais informações, consulte Simple Log Service_SDK Center_Alibaba Cloud OpenAPI Explorer.
Para obter mais informações sobre a operação da API, consulte Query the distribution of logs.