Prometheus වලින් App එක Monitor කරමු - පියවරෙන් පියවර Prometheus Monitoring Guide in Sinhala

Prometheus වලින් App එක Monitor කරමු - පියවරෙන් පියවර Prometheus Monitoring Guide in Sinhala

ඉතින් කොහොමද යාලුවනේ?

කට්ටියම සනීපෙන් ඇති කියලා හිතනවා. අද අපි කතා කරන්න යන්නේ අපි Software Engineers ලට නැතුවම බැරි, ඒ වගේම මාරම වැදගත් මාතෘකාවක් ගැන. ඒ තමයි Prometheus වලින් අපේ Application Monitor කරන එක. Application එකක් හදනවා වගේම, ඒක හරිහැටි වැඩ කරනවද, කොතනද අවුල තියෙන්නේ, මොනවද Improve කරන්න පුළුවන් කියන එක දැනගන්න එකත් හරිම වැදගත් නේද? අන්න ඒකට තමයි Monitoring කියන concept එක අපිට උදව් වෙන්නේ.

හිතන්නකෝ, අපේ App එකේ users ලා එකපාරටම වැඩිවෙලා server crash වෙනවා. නැත්නම් database එක slow වෙලා queries run වෙන්නේ නැහැ. ඔය වගේ වෙලාවට අපි කොහොමද හරියටම දැනගන්නේ මොකක්ද වුණේ කියලා? එක එක log file වල පොරක පොරක ඉන්න පුළුවන්ද? බැහැ නේද? අන්න එතකොට තමයි Prometheus වගේ monitoring system එකක් වටිනාකම තේරෙන්නේ. මේක අපේ App එකේ හද ගැස්ම වගේ. හැම වෙලාවෙම ඒක දිහා බලාගෙන ඉඳලා, අවුලක් ආපු ගමන් අපිට signal එකක් දෙනවා.

අද මේ post එකෙන් අපි Prometheus කියන්නේ මොකක්ද, ඒකෙන් metrics export කරන්නේ කොහොමද, ඒ වගේම පොඩි application එකක් monitor කරන්න ඒක setup කරගන්නේ කොහොමද කියලත් සරලව කතා කරමු.

Prometheus කියන්නේ මොකක්ද? (Prometheus Explained Simply)

Prometheus කියන්නේ open-source monitoring system එකක්. මේක Google එකේ Borgmon කියන system එකෙන් inspired වෙලා හදපු එකක්. මේකේ ප්‍රධානම දේ තමයි ඒක time-series data එකතු කරන එක. ඒ කියන්නේ, යම්කිසි අවස්ථාවක අපේ system එකේ තියෙන metric එකක (උදා: CPU usage, RAM usage, request count) අගය record කරලා තියාගන්නවා. මේකෙදි Prometheus server එක විසින් metrics export කරන targets වලින් data “pull” කරගන්නවා. (pull-based model එකක්).

Prometheus වල ප්‍රධාන components කිහිපයක් තියෙනවා:

  • Prometheus Server: මේක තමයි core එක. metrics එකතු කරන, storage කරන, සහ query කරන එක මේකෙන් සිද්ධ වෙනවා.
  • Client Libraries: මේවා අපි ලියන applications වලින් metrics expose කරන්න උදව් වෙන libraries. Python, Java, Go, Node.js වගේ ගොඩක් programming languages වලට මේවා තියෙනවා.
  • Exporters: මේවා තමයි metrics generate කරන applications. අපි හදන App එකෙන් metrics expose කරන්නේ මේවා හරහා. ඒ වගේම, Node Exporter, Blackbox Exporter වගේ, OS level metrics (CPU, RAM, Disk) හෝ වෙනත් services (Databases, Message Queues) වලින් metrics ගන්න පුදගලිකව හදපු exporters ත් තියෙනවා.
  • Pushgateway: සමහර වෙලාවට, අපේ Application එක කෙටි කාලයක් run වෙලා ඉවර වෙනවා වෙන්න පුළුවන් (Batch jobs වගේ). එතකොට Prometheus server එකට data pull කරන්න අවස්ථාවක් නැතිවෙන්න පුළුවන්. අන්න ඒ වෙලාවට තමයි Pushgateway එකට metrics “push” කරලා, පස්සේ Prometheus server එකට ඒකෙන් pull කරගන්න පුළුවන්.
  • Alertmanager: Metrics යම්කිසි සීමාවක් පැන්නොත් (උදා: CPU usage 80% ට වඩා වැඩි නම්) alert එකක් notify කරන්න මේක පාවිච්චි කරනවා. Email, Slack, PagerDuty වගේ විවිධ මාධ්‍ය හරහා alerts යවන්න පුළුවන්.
  • Grafana: මේක visualization tool එකක්. Prometheus වලින් ගන්න data ලස්සන dashboard විදිහට බලන්න මේක පාවිච්චි කරනවා. මේ දෙක එකට පාවිච්චි කරන එක තමයි සාමාන්‍යයෙන් කරන්නේ.

Metrics Export කරන්නේ කොහොමද? (How to Export Metrics?)

Prometheus වලට metrics දෙන්න පුළුවන් ප්‍රධාන ක්‍රම දෙකක් තියෙනවා. එකක් තමයි ඒකටම හදපු Exporters පාවිච්චි කරන එක. අනිත් එක තමයි අපිම අපේ Application එකේ metrics generate කරලා expose කරන එක. අපි දෙවෙනි ක්‍රමය ගැන වැඩි අවධානයක් දෙමු.

අපි හිතමු Python වලින් ලියපු Web application එකක් තියෙනවා කියලා. ඒකෙදි අපිට prometheus_client library එක පාවිච්චි කරන්න පුළුවන්. මේකෙන් metrics generate කරලා, සාමාන්‍යයෙන් `/metrics` කියන endpoint එකක් හරහා HTTP request එකකට පිළිතුරක් විදිහට expose කරනවා. Prometheus server එකට මේ endpoint එකට request කරලා data pull කරගන්න පුළුවන්.

Metric Types:

Prometheus වල ප්‍රධාන metric types හතරක් තියෙනවා:

  • Counter: මේක වැඩිවෙන අගයක් විතරයි (never decreases). උදා: total number of requests, errors.
  • Gauge: මේක වැඩිවෙන්නත් පුළුවන්, අඩු වෙන්නත් පුළුවන් අගයක්. උදා: current number of active users, CPU utilization, temperature.
  • Histogram: මේකෙන් observations වල sample values (e.g., request durations) සහ configured buckets වල distribute වෙන හැටි report කරනවා.
  • Summary: Histogram එකට සමානයි, නමුත් client-side quantile calculation කරනවා.

පොඩි Python Flask Application එකක් Metrics Export කරන්න හදමු:

මුලින්ම අවශ්‍ය library එක install කරගමු:

pip install Flask prometheus_client

දැන් මෙන්න මේ වගේ python file එකක් හදමු (app.py):

from flask import Flask
from prometheus_client import generate_latest, Counter, Gauge, Histogram
import time
import random

app = Flask(__name__)

# Metrics define කරමු
REQUEST_COUNT = Counter(
    'app_request_count',
    'Application Request Count',
    ['method', 'endpoint']
)

IN_PROGRESS_REQUESTS = Gauge(
    'app_in_progress_requests',
    'Number of in-progress requests'
)

REQUEST_LATENCY = Histogram(
    'app_request_latency_seconds',
    'Request latency in seconds',
    buckets=[.1, .2, .5, 1, 2, 5, 10]
)

# Endpoint එකක් හදමු
@app.route('/')
def hello_world():
    # Request එකක් එනකොට counter එක වැඩි කරමු
    REQUEST_COUNT.labels(method='GET', endpoint='/').inc()
    
    # In-progress requests ගණන වැඩි කරමු
    IN_PROGRESS_REQUESTS.inc()
    
    # Latency මනින්න පටන් ගමු
    with REQUEST_LATENCY.time():
        # Process කරන්න යන වෙලාව simulation එකක් විදිහට
        time.sleep(random.uniform(0.1, 0.5))
    
    # In-progress requests ගණන අඩු කරමු
    IN_PROGRESS_REQUESTS.dec()
    
    return 'Hello, Prometheus! Your App is being monitored!'

# Metrics endpoint එක
@app.route('/metrics')
def metrics():
    return generate_latest(), 200, {'Content-Type': 'text/plain; version=0.0.4; charset=utf-8'}

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000)

මේ app.py එක run කරලා (python app.py), http://localhost:5000/metrics වලට ගිහින් බලන්න. ඔයාලට මෙන්න මේ වගේ output එකක් බලාගන්න පුළුවන් වෙයි (පළවෙනි වතාවට App එකට access කරාට පස්සේ):

# HELP app_request_count Application Request Count
# TYPE app_request_count counter
app_request_count{endpoint="/",method="GET"} 1.0
# HELP app_in_progress_requests Number of in-progress requests
# TYPE app_in_progress_requests gauge
app_in_progress_requests 0.0
# HELP app_request_latency_seconds Request latency in seconds
# TYPE app_request_latency_seconds histogram
app_request_latency_seconds_bucket{le="0.1"} 0.0
app_request_latency_seconds_bucket{le="0.2"} 0.0
app_request_latency_seconds_bucket{le="0.5"} 1.0
app_request_latency_seconds_bucket{le="1.0"} 1.0
app_request_latency_seconds_bucket{le="2.0"} 1.0
app_request_latency_seconds_bucket{le="5.0"} 1.0
app_request_latency_seconds_bucket{le="10.0"} 1.0
app_request_latency_seconds_bucket{le="+Inf"} 1.0
app_request_latency_seconds_sum 0.321456789
app_request_latency_seconds_count 1.0
# HELP app_request_latency_seconds_bucket Request latency in seconds
# TYPE app_request_latency_seconds_bucket histogram

මේක තමයි Prometheus එකට තේරෙන format එක. මේකේ # HELP එකෙන් metric එක ගැන පොඩි description එකක් දෙනවා. # TYPE එකෙන් metric type එක කියනවා. ඊට පස්සේ metric name එක, ඒකේ labels (වර්ගීකරණය කරන්න) සහ අගය පෙන්නනවා.

Prometheus Setup කරමුද? (Shall we Set up Prometheus?)

දැන් අපේ App එකෙන් metrics expose කරන නිසා, ඒ metrics ටික collect කරන්න Prometheus server එකක් setup කරමු. Docker use කරනවා නම් වැඩේ තවත් ලේසියි.

1. Prometheus Configuration File (prometheus.yml)

මුලින්ම Prometheus server එකට අපි හදපු App එකෙන් metrics pull කරන්න කියලා කියන්න ඕනේ. ඒකට මෙන්න මේ වගේ prometheus.yml file එකක් හදමු:

global:
  scrape_interval: 15s # metrics pull කරන interval එක
  evaluation_interval: 15s # rules evaluate කරන interval එක

scrape_configs:
  - job_name: 'flask_app'
    # metrics ගන්න ඕන endpoint එක
    static_configs:
      - targets: ['localhost:5000'] # අපේ flask app එක run වෙන port එක

මේ scrape_configs කියන section එකේ තමයි Prometheus server එකට metrics collect කරන්න ඕන targets ටික define කරන්නේ. job_name එකෙන් identification එකක් දෙනවා, targets එකෙන් metrics තියෙන IP address:port එක දෙනවා.

2. Prometheus Run කරමු

දැන් prometheus.yml file එකත් එක්ක Prometheus server එක run කරන්න පුළුවන්. Docker පාවිච්චි කරනවා නම් මෙන්න මේ command එකෙන් run කරන්න පුළුවන් (prometheus.yml file එකත් එක්කම එකම folder එකක ඉඳන්):

docker run \
  -p 9090:9090 \
  -v $(pwd)/prometheus.yml:/etc/prometheus/prometheus.yml \
  prom/prometheus

මේකෙන් Prometheus server එක 9090 port එකේ run වෙනවා. දැන් http://localhost:9090 වලට ගිහින් බලන්න. ඔයාලට Prometheus UI එක බලාගන්න පුළුවන් වෙයි.

3. Metrics Verify කරමු

Prometheus UI එකට ගිහින්, Status > Targets කියන එකට යන්න. එතන ඔයාලට flask_app කියන job එක UP කියලා පෙන්නන්න ඕනේ. ඒ කියන්නේ Prometheus server එකට අපේ App එකෙන් metrics සාර්ථකව pull කරන්න පුළුවන් කියන එකයි.

දැන් UI එකේ තියෙන search bar එකේ (Graph tab එකේ) අපි හදපු metric names ටික type කරලා බලන්න. උදා: app_request_count, app_in_progress_requests, app_request_latency_seconds_count වගේ. ඒවායේ අගයන් graphs විදිහට පෙන්නනවා ඇති. App එකට ගිහින් refresh කරලා, ආයෙත් metrics බලන්න. අගයන් වෙනස් වෙනවා බලාගන්න පුළුවන් වෙයි.

Grafana එක්ක Visualise කරමු

සාමාන්‍යයෙන් Prometheus වල තියෙන Graph UI එක එච්චර ලස්සන නැහැ. Monitoring කරනකොට ලස්සන, තේරුම් ගන්න ලේසි Dashboards හදාගන්න Grafana තමයි use කරන්නේ. ඒක setup කරන එකත් හරිම ලේසියි. Docker වලින් Grafana run කරලා, Prometheus එක datasource එකක් විදිහට add කරලා, අපේ metrics වලට අදාළ dashboards හදාගන්න පුළුවන්. මේ Post එක වැඩිය දික් වෙයි නිසා ඒ ගැන මම වෙනමම Post එකක් ලියන්නම්.

Monitoring වලින් ලැබෙන වාසි (Benefits of Monitoring)

Prometheus වගේ monitoring system එකක් අපි පාවිච්චි කරන එකෙන් අපිට ගොඩක් වාසි තියෙනවා:

  • Proactive Issue Detection: ප්‍රශ්නයක් ලොකුවට එන්න කලින් ඒක හඳුනාගන්න පුළුවන්. උදා: CPU usage වැඩිවෙනවා නම් ඒක peak වෙනකම් ඉන්නේ නැතුව alert එකක් අරගෙන ඒකට විසඳුම් දෙන්න පුළුවන්.
  • Performance Optimization: අපේ App එකේ slow වෙන්නේ මොන parts ද, memory leak වෙනවද වගේ දේවල් metrics වලින් බලලා optimize කරන්න පුළුවන්.
  • Capacity Planning: Users ලා වැඩි වෙනකොට අපිට තව servers ඕනෙද, database එක upgrade කරන්න ඕනෙද වගේ දේවල් ගැන තීරණ ගන්න පුලුවන්, historical data බලාගෙන.
  • Faster Debugging: ප්‍රශ්නයක් ආවොත්, ඉක්මනටම root cause එක හොයාගන්න metrics උදව් වෙනවා. Log files වල රිංගනවාට වඩා මේක ගොඩක් ලේසියි.
  • SLA Management: අපේ Application එකේ Service Level Agreements (SLA) maintain වෙනවද කියලා බලන්න පුළුවන්. (උදා: Uptime 99.99% කියන එක monitor කරන්න).

මේ වගේ දේවල් නිසා තමයි ලොකු පොඩි ඕනෑම software project එකකට monitoring කියන එක නැතුවම බැරි අංගයක් වෙන්නේ. විශේෂයෙන් ලංකාවේ වගේ busy applications තියෙන තැන් වල, users ලා ගොඩක් ඉන්න වෙලාවට system down වෙන එක වලක්වගන්න monitoring අත්‍යාවශ්‍යයි.

ඉතින් එහෙනම්...

මේ Post එකෙන් ඔයාලට Prometheus ගැන, metrics export කරන විදිහ ගැන, ඒ වගේම පොඩි setup එකක් කරගන්න විදිහ ගැන හොඳ අදහසක් එන්න ඇති කියලා හිතනවා. මතක තියාගන්න, App එකක් හදනවා වගේම, ඒක දිගටම හරියට වැඩ කරනවද කියලා බලන එකත් අපේ වගකීමක්. ඒකට තමයි monitoring කියන්නේ.

මේක අනිවාර්යයෙන්ම ඔයාලගේ projects වලට integrate කරලා බලන්න. පොඩි App එකකින් පටන් අරන්, ඒක monitor කරලා බලන්න. ප්‍රශ්න එනකොට, ඒවට විසඳුම් හොයන්න මේක කොයිතරම් වැදගත්ද කියලා ඔයාලටම තේරෙයි.

ඔයාලට මේ Post එක ගැන මොනවා හරි ප්‍රශ්න තියෙනවා නම්, තව මොනවා හරි දැනගන්න ඕන නම්, නැත්නම් ඔයාලගේ අදහස් මොනවා හරි තියෙනවා නම්, පහලින් comment එකක් දාලා යන්න අමතක කරන්න එපා. අපි ඊළඟ Post එකෙන් හම්බවෙමු! තෙරුවන් සරණයි!