Observability/Athena
[Athena] CloudFront 로그 분석하기 - Daily Partitioning
[앙금빵]
2025. 4. 20. 21:58
개요
ELB 로그와 같이 CloudFront도 Athena 를 활용하여 로그 분석을 진행할 수 있다. 어떤 항목을 로깅할지 여러 필드를 확인할 수 있는데, 꽤나 많은 데이터를 로깅할 수 있다. 각 필드에 대한 정의는 AWS 공식문서(여기클릭)에서 확인할 수 있다.
트래픽이 많은 서비스를 담당하는 경우 하루에도 많은 양이 쌓이게 된다. 이를 일별 파티셔닝 전략을 통해 효율적으로 데이터를 관리할 수 있으며, 이를 통해 쿼리 성능을 개선하고 비용을 절감하는 기대효과를 가진다.
본문
(1) 첫째, 먼저 CloudFront 로그가 어떤 형태로 기록되는지 확인한다. CloudFront는 설정한 Field에 따라 생성되는 Column의 구성이 달라지므로, 실제 로깅된 원본 데이터를 기반으로 컬럼을 명확히 식별해야 한다.
(2) 일별 파티셔닝 테이블 생성 관리 효율성과 성능 향상을 위해 일별 파티셔닝된 테이블을 생성하였다. 이를 통해 특정 날짜의 데이터를 빠르게 탐색하고 쿼리 실행 속도를 최적화할 수 있다. 테이블 생성 쿼리는 다음과 같이 구성하였다.
유의할 점은 실제 로그를 확인하여 쌓이는 순서를 확인하여야 한다. 구성시 어떤 필드값을 먼저 선택하느냐에 따라 필드값 구성이 다른 것을 확인하여 AWS에 확인중에 있다.
CREATE EXTERNAL TABLE `{table_name}_250420`(
`date` string,
`time` string,
`x_edge_location` string,
`sc_bytes` bigint,
`c_ip` string,
`cs_method` string,
`cs_host` string,
`cs_uri_stem` string,
`sc_status` int,
`cs_referer` string,
`cs_user_agent` string,
`cs_uri_query` string,
`cs_cookie` string,
`x_edge_result_type` string,
`x_edge_request_id` string,
`x_host_header` string,
`cs_protocol` string,
`cs_bytes` bigint,
`time_taken` double,
`x_forwarded_for` string,
`ssl_protocol` string,
`ssl_cipher` string,
`x_edge_response_result_type` string,
`cs_protocol_version` string,
`fle_status` string,
`fle_encrypted_fields` string,
`c_port` int,
`time_to_first_byte` double,
`x_edge_detailed_result_type` string,
`sc_content_type` string,
`sc_content_len` bigint,
`sc_range_start` bigint,
`sc_range_end` bigint,
`origin_fbl` double,
`origin_lbl` double,
`c_country` string,
`cache_behavior_path_pattern` string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '\t'
STORED AS INPUTFORMAT
'org.apache.hadoop.mapred.TextInputFormat'
OUTPUTFORMAT
'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
's3://bubble-cloudfront-log/{account}/{cloudfront_name}/{yy}/{mm}/{dd}'
TBLPROPERTIES (
'skip.header.line.count'='2',
)
(3) Athena에서 정상적으로 분석가능한지 확인해본다.