Skip to main content

زبان کوئری Loki

به طور کلی دو دسته کوئری داریم .

  1. Log Queries: کوئری هایی که محتوای لاگ برمیگردانند. این دسته برای مشاهده محتوای لاگ کاربرد دارد .

    برای ساخت این نوع کوئری از Log stream selector و یا از ترکیب Log stream selector و Filter expression استفاده نمایید.

  2. Metric Queries: کوئری هایی که دیتای متریک برمیگردانند. این دسته برای مصور سازی (Visualization) لاگ ها کاربرد دارد.

    برای ساخت این نوع کوئری از اپراتور های aggregation روی کوئری های دسته اول استفاده نمایید.

  توجه

در نظر داشته باشید برای پاسخ سریع و بدون مشکل کوئری روی لاگ‌های قدیمی از Log stream selector بیشتر به منظور محدود کردن هرچه بیشتر لاگ ها استفاده نمایید.

اپراتور های log stream selector

 = : equals
!= : not equals
=~ : regex matches
!~ : regex does not match

تمام خطوط لاگ‌های پاد hamlog را برمیگرداند.

{pod="hamlog"}

تمام خطوط لاگ اپلیکیشن هایی که با hamlog شروع میشوند را برمیگرداند. (regex)

{pod=~"hamlog-.*"}

اپراتور های filter expressions

|= : equals
!= : not equals
|~ : regex matches
!~ : regex does not match

تمام خطوطی که حاوی متن error هستند را برمی‌گرداند.

{pod="hamlog"} |= "error"

تمام خطوطی که حاوی متن error نیستند را برمی‌گرداند.

{pod="hamlog"} != "error"

تمام خطوطی که حاوی متن error یا info هستند را برمی‌گرداند. (regex)

{pod="hamlog"} |~ "error|info"

تمام خطوطی که حاوی status 403 و یا status 503 را برمی‌گرداند. (regex)

{pod="hamlog"} |~ "status [45]03"

اپراتور های aggregation

به طور کلی دو دسته اپراتور داریم .

دسته اول روی خطوط لاگ اجرا میشوند و قابلیت تبدیل خطوط لاگ به مقادیر شمارشی را دارند .

count_over_time : Shows the total count of log lines for time range
rate : Similar as count_over_time but converted to number of entries per second
bytes_over_time : Number of bytes in each log stream in the range
bytes_rate : Similar to bytes_over_time but converted to number of bytes per second

بدست آوردن نرخ ورود لاگ پاد hamlog در هر ۱ دقیقه

rate({pod="hamlog"}[1m])

بدست آوردن تعداد ارور های پاد hamlog در هر ۱ ساعت

count_over_time({pod="hamlog"} |= "error" [1h])

دسته دوم اپراتور های unwrapped ranges هستند که از برچسب های استخراج شده به جای خطوط لاگ استفاده میکنند.

rate(unwrapped-range): calculates per second rate of all values in the specified interval
sum_over_time(unwrapped-range): the sum of all values in the specified interval
avg_over_time(unwrapped-range): the average value of all points in the specified interval
max_over_time(unwrapped-range): the maximum value of all points in the specified interval
min_over_time(unwrapped-range): the minimum value of all points in the specified interval
stdvar_over_time(unwrapped-range): the population standard variance of the values in the specified interval
stddev_over_time(unwrapped-range): the population standard deviation of the values in the specified interval
quantile_over_time(scalar,unwrapped-range): the φ-quantile (0 ≤ φ ≤ 1) of the values in the specified interval

بدست آوردن نرخ ریکوئست ها بر اساس status code در هر ۱ دقیقه

sum by (response_status) (
rate({pod="hamlog",cluster="hamravesh-c11”,namespace=”hamravesh”} | json | __error__="" [1m])
)