天天看點

OSS通路日志分析(1):概念+宏觀名額

OSS(Object Storage Service)

是阿裡雲提供的海量、安全、低成本、高可靠的對象存儲服務,提供非常高的可用性、可持久性。由于使用RESTful API 可以在網際網路任何位置存儲和通路,容量和處理能力彈性擴充等特點,在雲場景上OSS被大量使用。常見的場景有:

  • 網站的靜态資料:存放今天資料
  • 多媒體資料處理:例如圖檔、視訊等
  • 雲端資料處理:例如處理日志、多媒體檔案等
  • 支援多種存儲類型:标準、低頻、備份等降低存儲成本
OSS通路日志分析(1):概念+宏觀名額

由于使用範圍廣,而存儲又是各業務開展的基礎,OSS在各場景中扮演了重要的角色。為能夠用好OSS,如何了解線上使用者的行為和體驗變得非常重要。我們需要用到OSS通路日志功能,以下是OSS一條通路日志:

bucket:xxxxoper-img-16  bucket_location:  oss-cn-beijing-h  bucket_storage_type:  standard  client_ip:  10.111.23.179  content_length_in:  -  content_length_out:  21835  delta_data_size:  -  error_code:  -  host:  xxxoper-img-16.oss-cn-beijing-internal.aliyuncs.com  http_method:  GET  http_status:  200  http_type:  http  logging_flag:  true  object:  1526920607501476%xxxx.jpg  object_size:  21835  operation:  GetObject  owner_id:  1020833216692xxxx  referer:  -  request_id:  5B02FCB08293F4BCF409C5A8  request_length:  117  request_uri:  /1526920607501xxx/697.jpg HTTP/1.0  requester_id:  -  response_body_length:  21835  response_time:  6  server_cost_time:  4  sign_type:  NotSign  sync_request:  -  time:  22/May/2018:01:06:56  user_agent:  -  vpc_addr:  121271xxx  vpc_id:  7923xxx            

熟悉的人應該知道,通路日志和Nginx類似,記錄了時間、地點,人物、通路對象、延時以及一些重要附帶資訊(可參見:

OSS通路日志字段描述

)。OSS通路日志有兩種手段可以獲得,在這裡,我們主要講述通過第二種方法:日志服務進行日志查詢與分析的用法。

​時效性 費用(每GB) 功能
存儲OSS <2 Hour 存儲0.148元/M OSS處理
日志服務(SLS) <10 S 存儲0.35元/M 實時查詢分析+可視化

OSS下的通路日志已和日志服務打通,可以

在控制台上開通使用

,開通後可以提供:

  1. 通過關鍵詞、區間、模糊查詢等對日志進行篩選排查
  2. 通過SQL語句進行實時日志分析、可視化、配置告警等
OSS通路日志分析(1):概念+宏觀名額

該方案具有如下特點:

  • 實時:通路在秒級即可分析
  • 所見即所得: 自定義儀表盤 +控制台查詢
  • 靈活:基于SQL文法 查詢 分析
  • 豐富:與10+主流計算引擎,存儲格式互通,免去繁瑣對接過程

除此之外,可以通過日志服務:

整體統計

在開通OSS日志分析後,使用者對應Logstore下會有如下兩類資料:

1. 整體流量(PV、UV等)

PV:一天請求次數是多少?

__topic__: oss_access_log and http_status < 400 | SELECT count(1) AS PV           

UV:有多少獨立訪客來通路資源?

__topic__: oss_access_log and http_status < 400 | SELECT approx_distinct(client_ip) AS UV           

吞吐量:

__topic__: oss_access_log and http_status < 400 | SELECT sum(if(content_length_in IS NULL, 0, content_length_in) + if(content_length_out IS NULL, 0, content_length_out))/1024/1024 AS "Total throughput (MB)"           

進入流量:根據同樣方式我們可以計算出流量。

__topic__: oss_access_log and http_status < 400 | SELECT sum(if(content_length_in IS NULL, 0, content_length_in))/1024/1024 AS "Inbound throughput"           

最後關于整體流量的監控如下:

OSS通路日志分析(1):概念+宏觀名額

2. 整體業務趨勢(PV、UV等跟随時間變化)

對象存儲追求大業務吞吐量,如果希望統計吞吐量和時間分布,我們需要用到基于時間視窗函數對每個視窗内的流量進行統計。視窗函數原理如下:

1. 通過時間字段聚合到一個視窗,例如我們以300秒進行聚合:"__time__ - __time__% 300" 作為一個統計時段
2. 根據該時間段對視窗流量進行聚合
3. 根據時間視窗進行排序輸出           

例如建構以下SQL獲得24小時内,每5分鐘的流量圖

__topic__: oss_access_log and http_status < 400 | select sum(if(content_length_in is null, 0, content_length_in))/1024/1024 + sum(if(content_length_out is null, 0, content_length_out))/1024/1024 as "Total throughut (MB)", sum(content_length_in)/1024/1024 as "Inbound throughut (MB)", sum(content_length_out)/1024/1024 as "Outbound throughut (MB)", date_format(from_unixtime(__time__ - __time__% 300), '%m/%d %H:%i') as "Time per 5 min" group by "Time per 5 min" order by "Time per 5 min" limit 1000           
OSS通路日志分析(1):概念+宏觀名額

掌握該技能後,統計PV、UV随時間的分布就不在話下了:

__topic__: oss_access_log and http_status < 400 | select count(*) as PV,  approx_distinct(client_ip) as UV, date_format(from_unixtime(__time__ - __time__% 300), '%m/%d %H:%i') as "Time per 5 min" group by "Time per 5 min" order by "Time per 5 min" limit 1000           
OSS通路日志分析(1):概念+宏觀名額

3. 使用者來源

每條通路日志中會有IP字段,可以使用

IP解析函數

來判斷國家、地域、來源等。

我們可以用ip_to_country函數結合世界地圖拿到分布:

__topic__: oss_access_log and http_status < 400 | select count(*) as PV,  ip_to_country(client_ip) as "國家" group by "國家" limit 100           

要更細節的分布時,可以使用ip_to_province, ip_to_geo獲得更精确的位置,例如省份,地理位置坐标等:

__topic__: oss_access_log and http_status < 400 | select ip_to_geo(client_ip) as geo, count(1) as PV  group by geo limit 1000           
OSS通路日志分析(1):概念+宏觀名額

也可以通過ip_to_provider獲得營運商分布:

__topic__: oss_access_log and http_status < 400 |   (select round(sum(if(content_length_in is null, 0, content_length_in) + if(content_length_out is null, 0, content_length_out))/1024.0/1024.0, 3) as throughput, ip_to_provider(client_ip) as provider group by provider having ip_to_provider(client_ip) <> '' limit 1000)            

也可以根據城市、位置來統計整體的流量分布。

__topic__: oss_access_log and http_status < 400 |  select country as "國家", province as "省份", throughput as "總流量 (MB)", round(throughput*100.0/sum(throughput) over(), 2) as "百分比 (%)"  from (select round(sum(if(content_length_in is null, 0, content_length_in) + if(content_length_out is null, 0, content_length_out))/1024.0/1024.0, 1) as throughput, sum(if(content_length_in is null, 0, content_length_in))/1024/1024 as throughput_in, sum(if(content_length_out is null, 0, content_length_out))/1024/1024 as "Throughput Out (MB)",  ip_to_country(client_ip) as country, ip_to_province(client_ip) as province from log  group by country, province having ip_to_country(client_ip) <> '' order by throughput desc limit 1000)  order by throughput desc           
OSS通路日志分析(1):概念+宏觀名額

日志服務提供的完整IP識别函數如下,能夠滿足絕大部分需求:

函數名 含義 樣例

ip_to_domain(ip)

判斷IP所在的域,是内網還是外網。傳回intranet或internet。

SELECT ip_do_domain(ip)

ip_to_country(ip)

判斷IP所在的國家。

SELECT ip_to_country(ip)

ip_to_province(ip)

判斷IP所在的省份,如果是外國,則傳回國家名稱。

SELECT ip_to_province(ip)

ip_to_city(ip)

判斷IP所在的城市,如果是外國,則傳回國家名稱。

SELECT ip_to_city(ip)

ip_to_geo(ip)

判斷IP所在的經緯度,傳回的是高精度經緯度資料,範圍結果格式為

緯度,經度

SELECT ip_to_geo(ip)

ip_to_city_geo(ip)

判斷IP所在的城市的經緯度,傳回的是城市經緯度,每個城市隻有一個經緯度,範圍結果格式為

緯度,經度

SELECT ip_to_city_geo(ip)

ip_to_provider(ip)

擷取IP對應的網絡營運商。

SELECT ip_to_provider(ip)

ip_to_country(ip,'en')

判斷IP所在的國家,傳回國際碼。

SELECT ip_to_country(ip,'en')

ip_to_country_code(ip)

SELECT ip_to_country_code(ip)

ip_to_province(ip,'en')

判斷IP所在的省份,傳回英文省名或者中文拼音。

SELECT ip_to_province(ip,'en')

ip_to_city(ip,'en')

判斷IP所在的城市,傳回英文城市名或者中文拼音。

SELECT ip_to_city(ip,'en')

最後

最後我們可以建構成一張酷炫的實時日志分析儀表盤,是不是很酷炫?對了,如果

開通OSS通路日志分析

,這張儀表盤已經預設幫你建立好了,可以在此基礎上擴充哦。

OSS通路日志分析(1):概念+宏觀名額