Prometheus လက်တွေ့အခြေခံသုံးနည်း

Prometheus ကလက်ရှိ metrics monitoring tool တွေထဲမှာ နာမည်အကြီးဆုံးနဲ့ Microservice Architecture ကိုသုံးတဲ့ ကုမ္ပဏီတိုင်းနီးပါးရဲ့ tech stack မှာပါနေတဲ့ software တခုလည်းဖြစ်တယ်။ ရှေ့မှာတုန်းက Monitoring အခြေခံ နဲ့ Prometheus အကြောင်းတွေကို ပြောခဲ့ပြီးတာမို့လို့ အစိမ်းသက်သက် ဖြစ်နေတဲ့သူတွေအတွက် အဲဒါလေး အရင်သွားဖတ်ထားလိုက်ရင် ပိုအဆင်ပြေလိမ့်မယ်ထင်တယ်။

Prometheus က Go နဲ့ရေးထားတာဖြစ်တာမို့ binary အနေနဲ့ စက်ပေါ် download ဆွဲပြီး တန်းသုံးလို့ရပါတယ်။ ဥပမာ Java Application တွေဖြစ်တဲ့ Jenkins တို့၊ Apache Kafka, Cassandra, Hadoop စသဖြင့်တွေက JRE နဲ့ JDK တွေပါ download ဆွဲ configure လုပ်ပြီးမှ run လို့ရတာမျိုးပါ။ ဒါပေမယ့် Go ကတော့ native binary ထွက်တဲ့အတွက် Prometheus ကို install လုပ်ရတာ သိပ်လွယ်ပါတယ်။

ကျွန်တော်တို့ Prometheus ရဲ့ official website ကိုသွားလိုက်ရင် Operating System အလိုက် ရွေးချယ်စရာတွေ တွေ့ရလိမ့်မယ်။ ကျွန်တော့်စက်က Linux AMD (x86_64) ဖြစ်တဲ့အတွက် AMD အတွက် link ကို copy ယူပြီး download လိုက်ပါမယ်။

Download ဆွဲတဲ့အခါမှာ wget ဖြစ်ဖြစ် curl ဖြစ်ဖြစ် ကြိုက်တာသုံးလို့ရတယ်။ ဒါပေမယ့် wget က HTTP status 301 (Permenantly Moved) နဲ့ 302 (Temporarily Moved) တွေကို ဘာ flag မှထည့်ပေးဖို့မလိုပဲ auto follow ပြီးသားမို့ file download အတွက်ဆိုရင် wget ပိုကောင်းတယ်ထင်လို့ ကျွန်တော် wget သုံးပါတယ်။ curl ကတော့ redirect တွေ follow စေချင်ရင် -L လိုပါတယ်။ နောက်တခုက curl ရဲ့ default က download လိုက်တဲ့ content တွေကို စက်ပေါ်မှာ မသိမ်းပဲ terminal ပေါ်ကိုပဲ dump လိုက်တာဖြစ်လို့ ကိုယ်ပေးတဲ့ file နာမည်နဲ့ သိမ်းစေချင်ရင် -o (အိုအသေး)၊ link ထဲက နာမည်နဲ့ပဲ သိမ်းစေချင်ရင် -O (အိုအကြီး) ကိုသုံးဖို့လိုပါတယ်။

wget https://github.com/prometheus/prometheus/releases/download/v3.5.0/prometheus-3.5.0.linux-amd64.tar.gz

ရလာတဲ့ release package က tar.gz format နဲ့ဖြစ်နေတာ တွေ့ရမယ်။ ကြုံလို့တခု​ပြောရရင် Linux နဲ့တခြား Unix system တွေမှာ .txt တို့ .png တို့ .tar.gz တို့တွေက file အလုပ်လုပ်ဖို့အတွက် ဘာမှအရေးမပါပါဘူး။ အဲဒီ extension တွေက လူတွေဖတ်ပြီး နားလည်ဖို့ ထည့်ထားတဲ့ convention သက်သက်ပါ။ ဥပမာ ဒီနေရာမှာဆိုရင် .tar ဆိုတာက Prometheus release ထဲက file တွေကို tarball တလုံးအဖြစ် စုထားတယ်လို့ ပြောချင်တာဖြစ်ပြီး .gz ဆိုတာကတော့ storage သက်သာအောင်နဲ့ အင်တာနက်ပေါ် ရွှေ့ပြောင်းရ လွယ်ကူအောင် tarball ကို gzip algorithm သုံးပြီး compress လုပ်ထားတယ်လို့ ဆိုလိုချင်တာဖြစ်တယ်။

ဒီတော့ ကျွန်တော်တို့ နဂို file တွေ binary တွေပြန်ရလာဖို့ဆိုရင် tarball ကို decompress လုပ်ပြီး ဖြည်ဖို့လိုပါတယ်။ အဲဒီအတွက် tar ဆိုတဲ့ utility program ကိုသုံးပါမယ်။

tar xvzf prometheus-3.5.0.linux-amd64.tar.gz

prometheus-3.5.0.linux-amd64/
prometheus-3.5.0.linux-amd64/prometheus.yml
prometheus-3.5.0.linux-amd64/prometheus
prometheus-3.5.0.linux-amd64/NOTICE
prometheus-3.5.0.linux-amd64/LICENSE
prometheus-3.5.0.linux-amd64/promtool

tar နောက်က xvzf ဆိုတာ flag တွေဖြစ်ပြီး command line မှာဆိုရင် သူတို့ကို option တွေလို့လည်း ခေါ်ပါတယ်။ programmer စကားနဲ့ပြောရရင် tar က function တခုဖြစ်မယ်ဆို x, v, z, f တခုချင်းစီက function ရဲ့ parameter လေးတွေပေါ့။ x ဆိုတာ extract လုပ်ပေးပါ (tarball ကိုဖြည်ပေးပါ) လို့ဆိုလိုတယ်။ v ကတော့ ဖြည်လိုက်လို့ ရလာတဲ့ file တွေကို တခါတည်း list ပြပါပေါ့။ z ကတော့ tarball ဟာ gzip algorithm ကိုသုံးပြီး compress လုပ်ထားတာမို့ decompress ပြန်လုပ်ပေးပါပေါ့။ f ကတော့ tarball ရဲ့နာမည်ကို သတ်မှတ်တယ်။ x, v, z တွေက boolean type parameter တွေဖြစ်ပြီး f ကတော့ string argument လက်ခံတဲ့ parameter ပေါ့လေ။

အဲဒါပြီးရင် ရလာတဲ့ directory ထဲကို cd သုံးပြီး ဝင်ကြမယ်။

cd prometheus-3.5.0.linux-amd64/

cd ရဲ့အရှည်က change directory ဖြစ်ပြီး directory တွေကို click ပြီးဖွင့်စရာ GUI (ဥပမာ MacOS မှာဆိုရင် Finder နဲ့ Windows မှာဆိုရင် File Explorer စသဖြင့်) မရှိတဲ့ server တွေမှာ cd ကို directory တွေထဲဝင်ဖို့ သုံးတယ်ပေါ့။ ဒါပေမယ့် cd က tar လိုမျိုး 3rd party utility program တော့မဟုတ်ဘူး။ shell ထဲမှာ built-in ပါပြီးသား command တခုဖြစ်တယ်။ shell နဲ့ terminal ဘာကွာလဲနဲ့ အဲဒီ CLI command တွေရဲ့ အလုပ်လုပ်ပုံကိုတော့ နောက်တနေရာမှာ ကြုံကြိုက်မှ ရှင်းပြတော့မယ်။

ဒီတော့ အဲဒီ directory ထဲရောက်ပြီဆိုရင် ဘာ file တွေ extract လုပ်လိုက်လဲ list ကြည့်ရအောင်။ ဒီအတွက် ls ဆိုတဲ့ utility program တခုကို -lat ဆိုတဲ့ flag တွေနဲ့ သုံးပါမယ်။

ls -lat

total 302944
drwx------ 14 root root        4096 Aug 19 22:13 ..
drwxr-xr-x  2 1001 docker      4096 Jul 14 16:38 .
-rw-r--r--  1 1001 docker     11357 Jul 14 16:36 LICENSE
-rw-r--r--  1 1001 docker      3773 Jul 14 16:36 NOTICE
-rw-r--r--  1 1001 docker      1093 Jul 14 16:36 prometheus.yml
-rwxr-xr-x  1 1001 docker 150746286 Jul 14 16:17 promtool
-rwxr-xr-x  1 1001 docker 159425376 Jul 14 16:17 prometheus

ကျလာတဲ့ file တွေထဲမှာ ကျွန်တော်တို့ စိတ်ဝင်စားတာ ၂ ခုရှိပါတယ်။ prometheus ဆိုတဲ့ Go binary နဲ့ prometheus.yml ဆိုတဲ့ configuration file တို့ပါ။ Prometheus ကိုမ run ခင် prometheus.yml ကို text editor တခုခုနဲ့ဖွင့်ပြီး အောက်ကလို ကျွန်တော်တို့ ပြင်ကြပါမယ်။

global:
  scrape_interval: 15s
      
scrape_configs:
- job_name: "sample"
  static_configs:
  - targets:
    - "demo.promlabs.com:10000"
    - "demo.promlabs.com:10001"
    - "demo.promlabs.com:10002"

ရှေ့တပုဒ်မှာ ပြောခဲ့တဲ့အတိုင်း prometheus က push မဟုတ်ပဲ pull-based ဖြစ်တဲ့အတွက် metrics တွေကို frequency ဘယ်လောက်နဲ့ pull မယ်ဆိုတာ သတ်မှတ်လို့ရပါတယ်။ scrape_interval ဆိုတဲ့ key နဲ့ကျွန်တော်တို့ 15 စက္ကန့်တခါ pull မယ်လို့ သတ်မှတ်လိုက်ပါတယ်။

metrics တွေကို သွားယူရမယ့် scraping target တွေအတွက်ကတော့ service discovery backend တွေဖြစ်တဲ့ Kubernetes API server နဲ့ Consul စသဖြင့်တွေကို consult လုပ်ပြီး dynamic configuration လုပ်ထားတာ ဖြစ်သင့်ပါတယ်။ VM တွေ Pod တွေ Container တွေပေါ်လိုက်ပျောက်လိုက်ဖြစ်နေတဲ့ ephemeral environment မှာ target တွေကို hardcode လုပ်လို့ကို မရပါဘူး။ ဒါပေမယ့် အခုတော့ လက်တန်း စမ်းရလွယ်ကူအောင် ကျွန်တော်တို့ အင်တာနက်ပေါ် ရှိပြီးသား sample target 3 ခုကိုပဲ hardcode လုပ်လိုက်ပါ့မယ်။

ဒါဆိုရင် prometheus binary ကို run လို့ရပါပြီ။ default ဆိုရင် Prometheus က 0.0.0.0:9090 မှာ bind လိုက်မှာဖြစ်လို့ Browser ထဲ localhost:9090 နဲ့ဝင်လိုက်ရင် Prometheus dashboard တက်လာတာ မြင်ရပါမယ်။ တကယ်လို့ Status ထဲက Target health ဆိုတဲ့အထဲမှာ sample server 3 ခုလုံး UP ဖြစ်နေတာ မြင်ရရင် အောင်အောင်မြင်မြင်နဲ့ run လိုက်နိုင်ပါပြီခင်ဗျာ။ နောက်တပိုင်းမှာ ဆက်ကြပါမယ်။