查询 Application Load Balancer 日志 - Amazon Athena

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

查询 Application Load Balancer 日志

Application Load Balancer 是 Elastic Load Balancing 的负载均衡选项,它允许使用容器在微服务部署中实现流量分配。通过查询 Application Load Balancer 日志,您可以查看进出 Elastic Load Balancing 实例和后端应用程序的流量来源、延迟和传输字节。有关更多信息,请参阅《适用于 Application Load Balancer 的用户指南》中的 Application Load Balancer 的访问日志

先决条件

为 ALB 日志创建表

  1. 在 Athena 控制台中将以下 CREATE TABLE 语句复制并粘贴到查询编辑器中。有关 Amazon 控制台入门的更多信息,请参阅 开始使用。将 LOCATION 's3://DOC-EXAMPLE-BUCKET/AWSLogs/<ACCOUNT-ID>/elasticloadbalancing/<REGION>/' 中的值替换为与您的 Amazon S3 存储桶位置相对应的值。有关每个字段的信息,请参阅《Application Load Balancer 用户指南》中的 访问日志条目

    注意

    以下 CREATE TABLE 语句包含最近添加的 classificationclassification_reason 列。要为不包含这些条目的 Application Load Balancer 访问日志创建表,请从 CREATE TABLE 语句中删除这两列并相应地修改正则表达式。

    CREATE EXTERNAL TABLE IF NOT EXISTS alb_logs ( type string, time string, elb string, client_ip string, client_port int, target_ip string, target_port int, request_processing_time double, target_processing_time double, response_processing_time double, elb_status_code int, target_status_code string, received_bytes bigint, sent_bytes bigint, request_verb string, request_url string, request_proto string, user_agent string, ssl_cipher string, ssl_protocol string, target_group_arn string, trace_id string, domain_name string, chosen_cert_arn string, matched_rule_priority string, request_creation_time string, actions_executed string, redirect_url string, lambda_error_reason string, target_port_list string, target_status_code_list string, classification string, classification_reason string ) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.RegexSerDe' WITH SERDEPROPERTIES ( 'serialization.format' = '1', 'input.regex' = '([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*):([0-9]*) ([^ ]*)[:-]([0-9]*) ([-.0-9]*) ([-.0-9]*) ([-.0-9]*) (|[-0-9]*) (-|[-0-9]*) ([-0-9]*) ([-0-9]*) \"([^ ]*) (.*) (- |[^ ]*)\" \"([^\"]*)\" ([A-Z0-9-_]+) ([A-Za-z0-9.-]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^\"]*)\" ([-.0-9]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^ ]*)\" \"([^\s]+?)\" \"([^\s]+)\" \"([^ ]*)\" \"([^ ]*)\"') LOCATION 's3://DOC-EXAMPLE-BUCKET/AWSLogs/<ACCOUNT-ID>/elasticloadbalancing/<REGION>/'
  2. 在 Athena 控制台中运行查询。查询完成后,Athena 将注册 alb_logs 表,使其中的数据可以供您发出查询。

使用分区投影为 Athena 中的 ALB 日志创建表

由于 ALB 日志具有一个您可以预先指定其分区方案的已知结构,因此您可以使用 Athena 分区投影功能减少查询运行时间并自动管理分区。当添加新数据时,分区投影会自动添加新分区。这样就不必使用 ALTER TABLE ADD PARTITION 手动添加分区了。

以下示例 CREATE TABLE 语句会自动在 ALB 日志上从指定日期开始到当前为单个 AWS 区域使用分区投影。该语句以上一部分中的示例为基础,但添加了 PARTITIONED BYTBLPROPERTIES 子句以启用分区投影。在 LOCATIONstorage.location.template 子句中,将占位符替换为值,该值标识 ALB 日志在 Simple Storage Service (Amazon S3) 存储桶中的位置。对于 projection.day.range,将 2022/01/01 替换为要使用的开始日期。成功运行查询后,您可以查询表。您无需运行 ALTER TABLE ADD PARTITION 来加载分区。

CREATE EXTERNAL TABLE IF NOT EXISTS alb_logs ( type string, time string, elb string, client_ip string, client_port int, target_ip string, target_port int, request_processing_time double, target_processing_time double, response_processing_time double, elb_status_code int, target_status_code string, received_bytes bigint, sent_bytes bigint, request_verb string, request_url string, request_proto string, user_agent string, ssl_cipher string, ssl_protocol string, target_group_arn string, trace_id string, domain_name string, chosen_cert_arn string, matched_rule_priority string, request_creation_time string, actions_executed string, redirect_url string, lambda_error_reason string, target_port_list string, target_status_code_list string, classification string, classification_reason string ) PARTITIONED BY ( day STRING ) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.RegexSerDe' WITH SERDEPROPERTIES ( 'serialization.format' = '1', 'input.regex' = '([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*):([0-9]*) ([^ ]*)[:-]([0-9]*) ([-.0-9]*) ([-.0-9]*) ([-.0-9]*) (|[-0-9]*) (-|[-0-9]*) ([-0-9]*) ([-0-9]*) \"([^ ]*) (.*) (- |[^ ]*)\" \"([^\"]*)\" ([A-Z0-9-_]+) ([A-Za-z0-9.-]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^\"]*)\" ([-.0-9]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^ ]*)\" \"([^\s]+?)\" \"([^\s]+)\" \"([^ ]*)\" \"([^ ]*)\"') LOCATION 's3://DOC-EXAMPLE-BUCKET/AWSLogs/<ACCOUNT-ID>/elasticloadbalancing/<REGION>/' TBLPROPERTIES ( "projection.enabled" = "true", "projection.day.type" = "date", "projection.day.range" = "2022/01/01,NOW", "projection.day.format" = "yyyy/MM/dd", "projection.day.interval" = "1", "projection.day.interval.unit" = "DAYS", "storage.location.template" = "s3://DOC-EXAMPLE-BUCKET/AWSLogs/<ACCOUNT-ID>/elasticloadbalancing/<REGION>/${day}" )

更多有关分区投影的信息,请参阅 使用 Amazon Athena 分区投影

ALB 日志的示例查询

以下查询计算按客户端 IP 地址分组的负载均衡器接收到的 HTTP GET 请求的数目:

SELECT COUNT(request_verb) AS count, request_verb, client_ip FROM alb_logs GROUP BY request_verb, client_ip LIMIT 100;

另一个查询显示 Safari 浏览器用户访问的 URL:

SELECT request_url FROM alb_logs WHERE user_agent LIKE '%Safari%' LIMIT 10;

以下查询显示了 ELB 状态代码值大于或等于 500 的记录。

SELECT * FROM alb_logs WHERE elb_status_code >= 500

以下示例演示了如何根据 datetime 解析日志:

SELECT client_ip, sum(received_bytes) FROM alb_logs WHERE parse_datetime(time,'yyyy-MM-dd''T''HH:mm:ss.SSSSSS''Z') BETWEEN parse_datetime('2018-05-30-12:00:00','yyyy-MM-dd-HH:mm:ss') AND parse_datetime('2018-05-31-00:00:00','yyyy-MM-dd-HH:mm:ss') GROUP BY client_ip;

以下查询会查询在指定日期内对所有 ALB 日志使用分区投影的表。

SELECT * FROM alb_logs WHERE day = '2022/02/12'

另请参阅