Nathan.Lu
  • NEW!
    NEW!  Connect Ideas Across Notes
    Save time and share insights. With Paragraph Citation, you can quote others’ work with source info built in. If someone cites your note, you’ll see a card showing where it’s used—bringing notes closer together.
    Got it
      • Create new note
      • Create a note from template
        • Sharing URL Link copied
        • /edit
        • View mode
          • Edit mode
          • View mode
          • Book mode
          • Slide mode
          Edit mode View mode Book mode Slide mode
        • Customize slides
        • Note Permission
        • Read
          • Only me
          • Signed-in users
          • Everyone
          Only me Signed-in users Everyone
        • Write
          • Only me
          • Signed-in users
          • Everyone
          Only me Signed-in users Everyone
        • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invite by email
        Invitee

        This note has no invitees

      • Publish Note

        Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

        Your note will be visible on your profile and discoverable by anyone.
        Your note is now live.
        This note is visible on your profile and discoverable online.
        Everyone on the web can find and read all notes of this public team.

        Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

        Explore these features while you wait
        Complete general settings
        Bookmark and like published notes
        Write a few more notes
        Complete general settings
        Write a few more notes
        See published notes
        Unpublish note
        Please check the box to agree to the Community Guidelines.
        View profile
      • Commenting
        Permission
        Disabled Forbidden Owners Signed-in users Everyone
      • Enable
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
        • Everyone
      • Suggest edit
        Permission
        Disabled Forbidden Owners Signed-in users Everyone
      • Enable
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
      • Emoji Reply
      • Enable
      • Versions and GitHub Sync
      • Note settings
      • Note Insights New
      • Engagement control
      • Make a copy
      • Transfer ownership
      • Delete this note
      • Save as template
      • Insert from template
      • Import from
        • Dropbox
        • Google Drive
        • Gist
        • Clipboard
      • Export to
        • Dropbox
        • Google Drive
        • Gist
      • Download
        • Markdown
        • HTML
        • Raw HTML
    Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Engagement control Make a copy Transfer ownership Delete this note
    Import from
    Dropbox Google Drive Gist Clipboard
    Export to
    Dropbox Google Drive Gist
    Download
    Markdown HTML Raw HTML
    Back
    Sharing URL Link copied
    /edit
    View mode
    • Edit mode
    • View mode
    • Book mode
    • Slide mode
    Edit mode View mode Book mode Slide mode
    Customize slides
    Note Permission
    Read
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Write
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- title: LogQL tags: Loki description: LogQL --- # LogQL LogQL(Language to Query Logs from Loki) 是Loki用的查詢語法, 語法格式與操作收到PromQL啟蒙很深. 使用上很像是個分散式的[grep](https://ithelp.ithome.com.tw/articles/10265120) log聚合檢視器. LogQL用Label與Operator來進行過濾. 語法查詢上分為兩個部分 - Log queries 對本文內容進行過濾查找 - Metric queries 對log queries進行擴展並允許基於查找結果來進行值的計算或聚合 利用這兩個部份就能在LogQL中組合出我們想要的功能 ## Log Stream Selector ![](https://i.imgur.com/0D4WVlD.png) 一個基本的Log查詢通常會由 Log stream selector 和 Log pipeline所組成 每一個查詢會包含一個stream selector, 然後有需要會面會跟著pipeline, 將值給傳遞下去. 一個查詢動作就是把這兩個類型, 依據前後執行順序給組合起來就是 舉例 : ```bash= {container="query-frontend",namespace="loki-dev"} |= "metrics.go" | logfmt | duration > 10s and throughput_mb < 500 ``` ![](https://i.imgur.com/i2Ckp4p.png) ### Log stream selector 由**{ }**(curly braces) 做分隔符號 ```bash= {app="mysql",name="mysql-backup"} ``` 所有log steam滿足label是app, 且value是mysql 以及 label是name, value是mysql-backup的log都會被選取. 所以一定要有至少一個label-value的pair, 但也能多組. 這裡不只能用 = 還有其他的match operators - = exactly equal - != not equal - =~ regex matches - !~ regex does not match regex是比對整個字串做anchor定位, 也包含newlines換行符號 ```bash= {name =~ "mysql.+"} {name !~ "mysql.+"} {name !~ `mysql-\d+`} ``` ```bash= {job="ta3", level!~"FAT.+"} {job="ta3", level=~`FATA\w+`} ``` ### Log pipeline A Log pipeline會被append to 一個log stream selector後面, 後面就組合一組expression 每一個express會從左至右依序執行 如果都執行完了,該log就會被停止處理, 並且開始處理下一個log ```bash= | line_format "{{.status_code}}" ``` #### Log pipeline expressions log pipeline expression有三種類型 - Filter expressions - Line filter expressions - Label filter expressions - Parsing expressions - Formatting expressions - Line format expressions - Label format expressions ![](https://i.imgur.com/n3BGtkk.png) ##### Line filter expression - |= Log line containers string - != Log line does not contain string - |~ Log line container a match to the regex - !~ Log line does not contain a match to the regex ```bash= {job="mysql"} |= "error" # substring error {instance=~"kafka-[23]",name="kafka"} != "kafka.server:type=ReplicaManager" # 丟棄有substring kafka.server:type=ReplicaManager {name="cassandra"} |~ `error=\w+` # 查詢substring 開頭是error=的 {job="mysql"} |= "error" != "timeout" # 查詢 substring有error 但沒有timeout的 ``` ##### Label filter expression 用來過濾或者是提取每一筆log 上的label label的值目前允許是 - string : **\__error\_\_** 這label的value是string - duration : Valid time units are “ns”, “us” (or “µs”), “ms”, “s”, “m”, “h” - number : floating-point number (64bits) - bytes : Valid bytes units are “b”, “kib”, “kb”, “mib”, “mb”, “gib”, “gb”, “tib”, “tb”, “pib”, “pb”, “eib”, “eb” ```bash= {job="iis",site="default_site"} | json | cs_method = "GET" and (time_taken > 500 and sc_status > 200) ``` ##### Parser expression - JSON 能把所有的properties都當成label ```bash= | json ``` - logfmt ```bash= at=info method=GET path=/ host=grafana.net fwd="124.133.124.161" service=8ms status=200 | logfmt "at" => "info" "method" => "GET" "path" => "/" "host" => "grafana.net" "fwd" => "124.133.124.161" "service" => "8ms" "status" => "200" ``` - pattern 透過pattern expression 描述log的結構, 以<label_name>做描述 **<_>** 表是會被捕捉但會被skip ```bash= 0.191.12.2 - - [10/Jun/2021:09:14:29 +0000] "GET /api/plugins/versioncheck HTTP/1.1" 200 2 "-" "Go-http-client/2.0" "13.76.247.102, 34.120.177.193" "TLSv1.2" "US" "" 3 <ip> - - <_> "<method> <uri> <_>" <status> <size> <_> "<agent>" <_> "ip" => "0.191.12.2" "method" => "GET" "uri" => "/api/plugins/versioncheck" "status" => "200" "size" => "2" "agent" => "Go-http-client/2.0" ``` - regexp 使用[Golang RE2 syntax](https://github.com/google/re2/wiki/Syntax) (?P<name>regex) 跟pattern一樣, 只是用regex對match到的做label描述 ```bash= POST /api/prom/api/v1/query_range (200) 1.5s | regexp "(?P<method>\\w+) (?P<path>[\\w|/]+) \\((?P<status>\\d+?)\\) (?P<duration>.*)" "method" => "POST" "path" => "/api/prom/api/v1/query_range" "status" => "200" "duration" => "1.5s" ``` ##### Line format expression ```bash= | line_format "{{.label_name}}" ``` 這expression用來rewrite log line content. 透過single string parameter 來宣告rewrite的樣貌 ```bash= {job="iis"} | json | line_format "{{.cs_method}},{{.s_ip}} " GET,10.10.240.103 ``` ##### Label format expression 用來rename, 修改或新增label ```bash= | label_format ``` ## Binary operators ### Arithmetic operators - + - - - * - / - % - ^ 能在LogQL中執行1+1看看結果! ```bash= sum(rate({job="ta3"}[10m])) * 100 ``` 把一段時間內每秒發生的Log數量 給*2 ```bash= sum(rate({job="ta3"}[1m]))*2 ``` ### Logical operators - and (intersection) : v1 and v2 - or (union) : v1 or v2 - unless (complement) : v1 - v2 ### Comparison operators - == (equality) - != (inequality) - > (greater than) - >= (greater than or equal to) - < (less than) - <= (less than or equal to) 一樣的能嘗試 ```bash= 1 >= 1 1 > 2 ``` ### Order of operations 跟常規數學一樣, 同優先權的operator則先進行左關聯(left-associative) ```bash= 1 + 2 / 3 is equal to 1 + (2/3) 2 * 3 % 2 is equal to (2 * 3 ) % 2 ``` ### Comments 使用 **#** ```bash= {app="foo"} | json # this line will be ignored | bar="baz" # this checks if bar = "baz" ``` ### Pipeline Errors ```bash= __error__ ``` 當pipeline過程中出問題, 其實Loki不會把那些log給濾掉, 一樣會pass到下一個pipeline執行, 但會新增一個label __error__ 在身上 當然就能去過濾這些error 來試試看, 並觀察log entity身上是不是多個label ```bash= {job="ta3"} | logfmt | __error__!="" ``` ## Metric Queries ### [Log range aggregations](https://grafana.com/docs/loki/latest/logql/metric_queries/#log-range-aggregations) aggregation函數搭配一個查詢,以及一個時間做計算 - rate(long-range) 計算每秒的Log數量 - count_over_time(long-range) 計算一個時間範圍內, 每個log stream的Log數量 ```bash= count_over_time({job="ta3"}[1h]) # 計算最後一小時內的log量 rate({job="ta3"}[1h]) ## 計算一小時內, 每秒的log量 ``` ### [Unwrapped range aggregations](https://grafana.com/docs/loki/latest/logql/metric_queries/#unwrapped-range-aggregations) 相較於上者, 這邊針對的是Label 選擇在aggregation內使用哪個label, 但是這裡的query expression, 必須以unwrap expression 做結尾 ```bash= <aggr-op>([parameter,] <unwrapped-range>) [without|by (<label list>)] ``` - rate(unwarpped-range) - sum_over_time(unwarpped-range) - avg_over_time(unwarpped-range) - max_over_time(unwarpped-range) - min_over_time(unwarpped-range) - quantile_over_time(unwarpped-range) 算分位數用the φ-quantile (0 ≤ φ ≤ 1) of the values in the specified interval. ```bash= # 對每個path, 計算p99的延遲 quantile_over_time(0.99, {cluster="ops-tools1",container="ingress-nginx"} | json | __error__ = "" | unwrap request_time [1m]) by (path) ``` ```bash= # 針對每個org_id, 計算bytes的總和 sum by (org_id) ( sum_over_time( {cluster="ops-tools1",container="loki-dev"} |= "metrics.go" | logfmt | unwrap bytes_processed [1m]) ) ``` ### [Built-in aggregation operators](https://grafana.com/docs/loki/latest/logql/metric_queries/#built-in-aggregation-operators) 主要透過buit-in aggregation operator 能對一個vector計算計算, 並以新的vector為其結果, 且這新的vector它內含的元素個數還更少 ```bash= <aggr-op>([parameter,] <vector expression>) [without|by (<label list>)] ``` aggr-operators : - sum - avg - min - max - stddev - stdvar - count - topk Select largest k elements by sample value - bottomk Select smallest k elements by sample value ```bash= # 計算iis的qps, 並按照site來分組 sum(rate({job="iis"}[5m])) by (site) #取應用程式名稱, 是前10高吞吐量的. 這裡的10就是topk的parameter topk(10,sum(rate({region="us-east1"}[5m])) by (name)) ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully