Cette rubrique explique comment appeler l'opération GetHistograms du SDK Simple Log Service pour Java afin d'interroger la distribution des journaux sur une plage horaire donnée. Elle fournit également un exemple de code.
Prérequis
Simple Log Service est activé.
Le SDK Simple Log Service pour Python est initialisé.
Le SDK Simple Log Service pour Java est installé. Pour plus d'informations, consultez la rubrique Installer le SDK Java.
Des journaux ont été écrits dans un Logstore et l'indexation est activée pour ce Logstore. Pour plus d'informations, consultez les rubriques Exemple de code utilisé pour créer un Logstore et Créer un index.
Précautions
Cet exemple utilise l'endpoint public de Simple Log Service pour la région Chine (Hangzhou) : https://cn-hangzhou.log.aliyuncs.com.
Pour accéder à Simple Log Service depuis d'autres services Alibaba Cloud situés dans la même région que votre projet, utilisez l'endpoint interne de Simple Log Service : https://cn-hangzhou-intranet.log.aliyuncs.com.
Pour plus d'informations sur les régions et les endpoints pris en charge par Simple Log Service, consultez la rubrique Endpoint.
Journal brut
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
Exemple de code
L'exemple de code suivant montre comment interroger la distribution des requêtes d'accès provenant de l'adresse IP 47.100.XX.XX sur une période d'une heure :
import com.aliyun.openservices.log.Client;
import com.aliyun.openservices.log.common.Histogram;
import com.aliyun.openservices.log.exception.LogException;
import com.aliyun.openservices.log.response.GetHistogramsResponse;
public class GetHistograms {
public static void main(String[] args) throws LogException {
// In this example, the AccessKey ID and AccessKey secret are obtained from environment variables.
String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
// The name of the project.
String projectName = "ali-test-project";
// The endpoint of Simple Log Service. This example uses the endpoint of the China (Hangzhou) region. Replace the endpoint with the one for your region.
String host = "https://cn-hangzhou.log.aliyuncs.com";
// Create a Simple Log Service client.
Client client = new Client(host, accessId, accessKey);
try {
// The name of the Logstore.
String logstoreName = "ali-test-logstore";
// Query the number of access requests from 47.100.XX.XX within one hour.
String query = "remote_addr:47.100.XX.XX";
System.out.println(String.format("ready to get histograms from %s",logstoreName));
// fromTime and toTime specify the time range to query logs. The values are UNIX timestamps. The time range is one hour.
int fromTime = (int) (System.currentTimeMillis()/1000 - 3600);
int toTime = fromTime + 3600;
GetHistogramsResponse response = client.GetHistograms(projectName,logstoreName,fromTime,toTime,"",query);
for (Histogram histogram : response.GetHistograms()) {
// Because there can be many intervals, only intervals that contain logs are printed.
if (0 < histogram.GetCount()){
// The number of logs.
System.out.println("log number is :" + histogram.GetCount());
// The start time of the log distribution.
System.out.println("from time is :" + histogram.GetFrom());
// The end time of the log distribution.
System.out.println("to time is :" + histogram.GetTo());
// Indicates whether the result in this time interval is complete.
System.out.println("is completed :" + histogram.IsCompleted());
}
}
System.out.println(String.format("get histograms from %s success",logstoreName));
} catch (LogException e) {
System.out.println("LogException e :" + e.toString());
System.out.println("error code :" + e.GetErrorCode());
System.out.println("error message :" + e.GetErrorMessage());
throw e;
}
}
}
Le résultat attendu est un résultat de requête complet indiquant 10 tentatives d'accès provenant de l'adresse IP 47.100.XX.XX sur une période d'une heure.
ready to get histograms from nginx-moni
log number is :10
from time is :1667875920
to time is :1667879520
is completed :true
get histograms from nginx-moni success
Références
Outre son SDK natif, SLS prend également en charge les SDK Alibaba Cloud courants. Pour plus d'informations, consultez Simple Log Service_SDK Center_Alibaba Cloud OpenAPI Explorer.
Pour plus d'informations sur l'opération API, consultez la rubrique Interroger la distribution des journaux.
Consultez le dépôt Alibaba Cloud Simple Log Service SDK for Java sur GitHub.