全部產品
Search
文件中心

Simple Log Service:使用e_table_map函數對HTTP請求返回碼進行富化

更新時間:Aug 30, 2024

Nginx日誌是營運網站的重要訊息,Log Service通過e_table_map函數快速對HTTP請求的返回碼進行富化,便於您分析日誌資料。本文介紹通過e_table_map函數富化HTTP返回碼的操作方法。

前提條件

已採集到Nginx日誌資料。更多資訊,請參見資料擷取

情境描述

某企業在應用A開發過程中,統一定義了HTTP請求的返回碼資訊(資料量不定期更新),便於統一維護。在應用程式維護過程中,原始Nginx日誌中僅http_code用於標記HTTP請求的狀態,該欄位無法滿足人員定位問題。

針對以上需求,需要基於HTTP返回碼映射表,使用e_table_map函數對日誌欄位進行富化,便於直觀判斷HTTP請求狀態。

  • 原始日誌範例

    body_bytes_sent:1750
    host:www.example.com
    http_referer:www.example.aliyundoc.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.XXX.XX
    remote_addr:203.0.XXX.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

    該原始日誌儲存在名稱為nginx-demo的Logstore中,http_code欄位即為HTTP返回碼。

  • HTTP返回碼映射表

    例如,典型的HTTP返回碼映射表如下所示。

    code

    alias

    category

    desc

    100

    1xx

    Informational

    Continue

    200

    2xx

    Success

    OK

    300

    3xx

    Redirection

    Multiple Choices

    400

    4xx

    Client Error

    Bad Request

  • 富化後日誌範例

    body_bytes_sent:1750
    host:www.example.com
    http_code:200
    http_code_alias:2xx
    http_code_category:Success
    http_code_desc:OK
    http_referer:www.example.aliyundoc.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.XXX.XX
    remote_addr:203.0.XXX.XX
    remote_user:p288
    request_length:13741
    request_method:GET
    request_time:71
    request_uri:/request/path-1/file-1
    time_local:11/Aug/2021:06:52:27
    upstream_response_time:0.66

可選方案

加工流程

  1. 將HTTP返回碼轉換為Table對象。

  2. 使用e_table_map函數進行資料加工富化。

推薦方案

為實現以上需求,您可以選擇如下合適方案進行資料富化。

方案

資料量支援能力

累加式更新

批次更新

適用情境

使用Logstore實現富化(推薦)

大量

支援

支援

巨量資料量且頻繁更新的映射表。

通過MySQL表實現富化

較大

不支援

支援

頻繁更新的映射表。

通過使用OSS檔案實現富化

較大

不支援

支援

相對靜態,更新不頻繁的映射表。

代碼內嵌

不支援

不支援

典型HTTP返回碼映射表。

方案一:使用Logstore實現富化(推薦)

  1. 通過SDK方式將HTTP返回碼寫入名為http_code的Logstore。

    Logstore中HTTP返回碼日誌範例如下:

    __source__:203.0.XXX.XX
    __tag__:__receive_time__:1595424194
    __topic__:
    code:200
    alias:2xx
    description:OK
    category:Success

    更多資訊,請參見SDK參考

  2. 擷取HTTP返回碼Logstore的名稱、服務入口和AccessKey,用於後續編輯資料加工語句。

    Log Service的服務入口和存取金鑰AccessKey。更多資訊,請參見服務入口存取金鑰

  3. 登入原始日誌nginx-demo的Logstore,進入資料加工頁面。

    具體操作,請參見建立資料加工任務

  4. 在編輯框中,輸入資料加工語句。

    從HTTP返回碼Logstore(名稱為http_code)中讀取資料,並通過e_table_map函數將對應欄位的值返回。

    e_table_map( res_log_logstore_pull("cn-hangzhou-intranet.log.aliyuncs.com",
            res_local("AK_ID"),res_local("AK_KEY"),"live-demo","http_code",
            ["code","alias","description","category"]),
                  [("http_code","code")],
                  [("alias","http_code_alias"), ("description","http_code_desc"), 
                  ("category","http_code_category")])
    重要

    為了資料安全,建議在進階參數配置中配置AccessKey。關於如何配置進階參數,請參見建立資料加工任務

    • 使用res_log_logstore_pull函數從另一個Logstore中拉取資料。更多資訊,請參見res_log_logstore_pull

    • 根據輸入欄位的值,在表格中尋找對應的行,返回對應欄位的值。更多資訊,請參見e_table_map

  5. 單擊預覽資料

    Nginx日誌富化後,已新增HTTP返回碼相關欄位。

    body_bytes_sent:1750
    host:www.example.com
    http_code:200
    http_code_alias:2xx
    http_code_category:Success
    http_code_desc:OK
    http_referer:www.example.aliyundoc.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.XXX.XX
    remote_addr:203.0.XXX.XX
    remote_user:p288
    request_length:13741
    request_method:GET
    request_time:71
    request_uri:/request/path-1/file-1
    time_local:11/Aug/2021:06:52:27
    upstream_response_time:0.66
  6. 建立資料加工任務。

    更多資訊,請參見建立資料加工任務

方案二:通過MySQL表實現富化

  1. 將HTTP返回碼存入RDS MySQL資料庫。

    RDS MySQL中HTTP返回碼映射表如下所示。mysql

  2. 擷取RDS MySQL資料庫的主機地址、使用者名稱、密碼和資料庫表等,用於後續編輯資料加工語句。

  3. 登入原始日誌nginx-demo的Logstore,進入資料加工頁面。

    具體操作,請參見建立資料加工任務

  4. 在編輯框中,輸入資料加工語句。

    從MySQL資料庫中讀取資料,並通過e_table_map函數將對應欄位的值返回。

    e_table_map(res_rds_mysql(address="MySQL主機地址", 
                      username="使用者名稱", password="密碼",
                      database="資料庫",table="表名", refresh_interval=300),
                  [("http_code","code")],
                  [("alias","http_code_alias"), ("description","http_code_desc"), 
                  ("category","http_code_category")])
    重要

    為了資料安全,建議在進階參數配置中配置AccessKey。關於如何配置進階參數,請參見建立資料加工任務

    • 使用res_rds_mysql函數從RDS MySQL資料庫中拉取資料庫表內容。更多資訊,請參見res_rds_mysql

    • 根據輸入欄位的值,在表格中尋找對應的行,返回對應欄位的值。更多資訊,請參見e_table_map

  5. 單擊預覽資料

    Nginx日誌富化後,已新增HTTP返回碼相關欄位。

    body_bytes_sent:1750
    host:www.example.com
    http_code:200
    http_code_alias:2xx
    http_code_category:Success
    http_code_desc:OK
    http_referer:www.example.aliyundoc.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.XXX.XX
    remote_addr:203.0.XXX.XX
    remote_user:p288
    request_length:13741
    request_method:GET
    request_time:71
    request_uri:/request/path-1/file-1
    time_local:11/Aug/2021:06:52:27
    upstream_response_time:0.66
  6. 建立資料加工任務。

    更多資訊,請參見建立資料加工任務

方案三:通過使用OSS檔案實現富化

  1. 將HTTP返回碼儲存至名為http_code.csv的檔案中,上傳至OSS Bucket。

    更多資訊,請參見OSS上傳檔案

  2. 擷取http_code.csv檔案所在OSS Bucket名稱、服務入口和AccessKey,用於後續編輯資料加工語句。

    Object Storage Service的服務入口。更多資訊,請參見訪問網域名稱和資料中心

  3. 登入原始日誌nginx-demo的Logstore,進入資料加工頁面。

    具體操作,請參見建立資料加工任務

  4. 在編輯框中,輸入資料加工語句。

    從OSS Bucket中讀取資料,並通過e_table_map函數將對應欄位的值返回。

    e_table_map(
          tab_parse_csv(
               res_oss_file(endpoint="oss-cn-shanghai-internal.aliyuncs.com",
                  ak_id=res_local("AK_ID"), ak_key=res_local("AK_KEY"), 
                  bucket="ali-sls-etl-test", 
                  file="http_code.csv", format='text')),
                  [("http_code","code")],
                  [("alias","http_code_alias"),
                   ("description","http_code_desc"),
                   ("category","http_code_category")])
    重要

    為了資料安全,建議在進階參數配置中配置AccessKey。關於如何配置進階參數,請參見建立資料加工任務

    • 使用res_oss_file函數從OSS Bucket中擷取檔案內容,並支援定期重新整理。更多資訊,請參見res_oss_file

    • 使用tab_parse_csv函數從CSV格式的文本構建表格。更多資訊,請參見tab_parse_csv

    • 根據輸入欄位的值,在表格中尋找對應的行,返回對應欄位的值。更多資訊,請參見e_table_map

  5. 單擊預覽資料

    Nginx日誌富化後,已新增HTTP返回碼相關欄位。

    body_bytes_sent:1750
    host:www.example.com
    http_code:200
    http_code_alias:2xx
    http_code_category:Success
    http_code_desc:OK
    http_referer:www.example.aliyundoc.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.XXX.XX
    remote_addr:203.0.XXX.XX
    remote_user:p288
    request_length:13741
    request_method:GET
    request_time:71
    request_uri:/request/path-1/file-1
    time_local:11/Aug/2021:06:52:27
    upstream_response_time:0.66
  6. 建立資料加工任務。

    更多資訊,請參見建立資料加工任務

方案四:代碼內嵌

  1. 準備CSV格式的HTTP返回碼映射表。

  2. 登入原始日誌nginx-demo的Logstore,進入資料加工頁面。

    具體操作,請參見建立資料加工任務

  3. 在編輯框中,輸入資料加工語句。

    通過tab_parse_csv函數對CSV格式的HTTP返回碼進行轉換,並通過e_table_map函數將對應欄位的值返回。

    e_table_map(tab_parse_csv("code,alias,category,description\n100,1xx,Informational,Continue\n101,1xx,Informational,Switching Protocols\n102,1xx,Informational,Processing (WebDAV)\n200,2xx,Success,OK\n201,2xx,Success,Created\n202,2xx,Success,Accepted\n203,2xx,Success,Non-Authoritative Information\n204,2xx,Success,No Content\n205,2xx,Success,Reset Content\n206,2xx,Success,Partial Content\n207,2xx,Success,Multi-Status (WebDAV)\n208,2xx,Success,Already Reported (WebDAV)\n226,2xx,Success,IM Used\n300,3xx,Redirection,Multiple Choices\n301,3xx,Redirection,Moved Permanently\n302,3xx,Redirection,Found\n303,3xx,Redirection,See Other\n304,3xx,Redirection,Not Modified\n305,3xx,Redirection,Use Proxy\n306,3xx,Redirection,(Unused)\n307,3xx,Redirection,Temporary Redirect\n308,3xx,Redirection,Permanent Redirect (experimental)\n400,4xx,Client Error,Bad Request\n401,4xx,Client Error,Unauthorized\n402,4xx,Client Error,Payment Required\n403,4xx,Client Error,Forbidden\n404,4xx,Client Error,Not Found\n405,4xx,Client Error,Method Not Allowed\n406,4xx,Client Error,Not Acceptable\n407,4xx,Client Error,Proxy Authentication Required\n408,4xx,Client Error,Request Timeout\n409,4xx,Client Error,Conflict\n410,4xx,Client Error,Gone\n411,4xx,Client Error,Length Required\n412,4xx,Client Error,Precondition Failed\n413,4xx,Client Error,Request Entity Too Large\n414,4xx,Client Error,Request-URI Too Long\n415,4xx,Client Error,Unsupported Media Type\n416,4xx,Client Error,Requested Range Not Satisfiable\n417,4xx,Client Error,Expectation Failed\n418,4xx,Client Error,I'm a teapot (RFC 2324)\n420,4xx,Client Error,Enhance Your Calm (Twitter)\n422,4xx,Client Error,Unprocessable Entity (WebDAV)\n423,4xx,Client Error,Locked (WebDAV)\n424,4xx,Client Error,Failed Dependency (WebDAV)\n425,4xx,Client Error,Reserved for WebDAV\n426,4xx,Client Error,Upgrade Required\n428,4xx,Client Error,Precondition Required\n429,4xx,Client Error,Too Many Requests\n431,4xx,Client Error,Request Header Fields Too Large\n444,4xx,Client Error,No Response (Nginx)\n449,4xx,Client Error,Retry With (Microsoft)\n450,4xx,Client Error,Blocked by Windows Parental Controls (Microsoft)\n451,4xx,Client Error,Unavailable For Legal Reasons\n499,4xx,Client Error,Client Closed Request (Nginx)\n500,5xx,Server Error,Internal Server Error\n501,5xx,Server Error,Not Implemented\n502,5xx,Server Error,Bad Gateway\n503,5xx,Server Error,Service Unavailable\n504,5xx,Server Error,Gateway Timeout\n505,5xx,Server Error,HTTP Version Not Supported\n506,5xx,Server Error,Variant Also Negotiates (Experimental)\n507,5xx,Server Error,Insufficient Storage (WebDAV)\n508,5xx,Server Error,Loop Detected (WebDAV)\n509,5xx,Server Error,Bandwidth Limit Exceeded (Apache)\n510,5xx,Server Error,Not Extended\n511,5xx,Server Error,Network Authentication Required\n598,5xx,Server Error,Network read timeout error\n599,5xx,Server Error,Network connect timeout error\n"),
                  [("http_code","code")],
                  [("alias","http_code_alias"), ("description","http_code_desc"), 
                  ("category","http_code_category")])
    重要

    為了資料安全,建議在進階參數配置中配置AccessKey。關於如何配置進階參數,請參見建立資料加工任務

    • 使用tab_parse_csv函數從CSV格式的文本構建表格。更多資訊,請參見tab_parse_csv

    • 根據輸入欄位的值,在表格中尋找對應的行,返回對應欄位的值。更多資訊,請參見e_table_map

  4. 單擊預覽資料

    Nginx日誌富化後,已新增HTTP返回碼相關欄位。

    body_bytes_sent:1750
    host:www.example.com
    http_code:200
    http_code_alias:2xx
    http_code_category:Success
    http_code_desc:OK
    http_referer:www.example.aliyundoc.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.XXX.XX
    remote_addr:203.0.XXX.XX
    remote_user:p288
    request_length:13741
    request_method:GET
    request_time:71
    request_uri:/request/path-1/file-1
    time_local:11/Aug/2021:06:52:27
    upstream_response_time:0.66
  5. 建立資料加工任務。

    更多資訊,請參見建立資料加工任務