gRPC Python-এ বেসিক OpenTelemetry প্লাগইন সেটআপ করুন

1. ভূমিকা

এই কোডল্যাবে, আপনি gRPC ব্যবহার করে একটি ক্লায়েন্ট এবং সার্ভার তৈরি করবেন যা পাইথনে লেখা একটি রুট-ম্যাপিং অ্যাপ্লিকেশনের ভিত্তি তৈরি করবে।

টিউটোরিয়ালের শেষে, আপনার কাছে gRPC OpenTelemetry প্লাগইন দিয়ে তৈরি একটি সহজ gRPC HelloWorld অ্যাপ্লিকেশন থাকবে এবং আপনি Prometheus-এ রপ্তানি করা পর্যবেক্ষণযোগ্যতা মেট্রিক্স দেখতে সক্ষম হবেন।

তুমি কি শিখবে

  • বিদ্যমান gRPC পাইথন অ্যাপ্লিকেশনের জন্য OpenTelemetry প্লাগইন কীভাবে সেটআপ করবেন
  • একটি স্থানীয় প্রমিথিউস ইনস্ট্যান্স চালানো হচ্ছে
  • প্রমিথিউসে মেট্রিক্স রপ্তানি করা হচ্ছে
  • প্রমিথিউস ড্যাশবোর্ড থেকে মেট্রিক্স দেখুন

2. শুরু করার আগে

তোমার যা লাগবে

  • গিট
  • কার্ল করা
  • বিল্ড-প্রয়োজনীয়
  • পাইথন ৩.৯ বা তার বেশি। প্ল্যাটফর্ম-নির্দিষ্ট পাইথন ইনস্টলেশন নির্দেশাবলীর জন্য, পাইথন সেটআপ এবং ব্যবহার দেখুন। বিকল্পভাবে, uv বা pyenv এর মতো সরঞ্জাম ব্যবহার করে একটি নন-সিস্টেম পাইথন ইনস্টল করুন।
  • পাইথন প্যাকেজ ইনস্টল করার জন্য পাইপ সংস্করণ 9.0.1 বা তার বেশি।
  • venv ব্যবহার করে পাইথন ভার্চুয়াল পরিবেশ তৈরি করা যাবে।

পূর্বশর্তগুলি ইনস্টল করুন:

sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get install -y git curl build-essential clang
sudo apt install python3
sudo apt install python3-pip python3-venv

কোডটি পান

আপনার শেখার গতি আরও সহজ করার জন্য, এই কোডল্যাবটি আপনাকে শুরু করতে সাহায্য করার জন্য একটি পূর্ব-নির্মিত সোর্স কোড স্ক্যাফোল্ড অফার করে। নিম্নলিখিত পদক্ষেপগুলি আপনাকে একটি অ্যাপ্লিকেশনে gRPC OpenTelemetry প্লাগইন ব্যবহার করার ক্ষেত্রে সহায়তা করবে।

জিআরপিসি-কোডল্যাবস

এই কোডল্যাবের স্ক্যাফোল্ড সোর্স কোডটি এই গিটহাব ডিরেক্টরিতে পাওয়া যাবে। যদি আপনি নিজে কোডটি বাস্তবায়ন করতে না চান, তাহলে সম্পূর্ণ সোর্স কোডটি সম্পূর্ণ ডিরেক্টরিতে পাওয়া যাবে।

প্রথমে, grpc codelab repo এবং cd কে grpc-python-opentelemetry ফোল্ডারে ক্লোন করুন:

git clone https://github.com/grpc-ecosystem/grpc-codelabs.git
cd grpc-codelabs/codelabs/grpc-python-opentelemetry/

অন্যথায়, আপনি শুধুমাত্র কোডল্যাব ডিরেক্টরি সম্বলিত .zip ফাইলটি ডাউনলোড করতে পারেন এবং ম্যানুয়ালি এটি আনজিপ করতে পারেন।

প্রথমে সিস্টেম প্যাকেজ থেকে আপনার প্রকল্পের নির্ভরতা আলাদা করার জন্য একটি নতুন পাইথন ভার্চুয়াল পরিবেশ (venv) তৈরি করা যাক:

python3 -m venv --upgrade-deps .venv

bash/zsh শেলে ভার্চুয়াল পরিবেশ সক্রিয় করতে:

source .venv/bin/activate

উইন্ডোজ এবং নন-স্ট্যান্ডার্ড শেলের জন্য, https://docs.python.org/3/library/venv.html#how-venvs-work এ টেবিলটি দেখুন।

এরপর, পরিবেশে নির্ভরতাগুলি ইনস্টল করুন:

python -m pip install -r requirements.txt

৩. ওপেনটেলিমেট্রি প্লাগইন নিবন্ধন করুন

gRPC OpenTelemetry প্লাগইন যোগ করার জন্য আমাদের একটি gRPC অ্যাপ্লিকেশন প্রয়োজন। এই কোডল্যাবে, আমরা একটি সাধারণ gRPC HelloWorld ক্লায়েন্ট এবং সার্ভার ব্যবহার করব যা আমরা gRPC OpenTelemetry প্লাগইন দিয়ে ব্যবহার করব।

আপনার প্রথম পদক্ষেপ হল ক্লায়েন্টে Prometheus এক্সপোর্টার দিয়ে কনফিগার করা OpenTelemetry প্লাগইনটি নিবন্ধন করা। আপনার প্রিয় এডিটর দিয়ে start_here/observability_greeter_client.py খুলুন। প্রথমে সম্পর্কিত নির্ভরতা এবং ম্যাক্রো যোগ করুন যাতে এটি দেখতে এরকম হয় -

import logging
import time

import grpc
import grpc_observability
import helloworld_pb2
import helloworld_pb2_grpc
from opentelemetry.exporter.prometheus import PrometheusMetricReader
from opentelemetry.sdk.metrics import MeterProvider
from prometheus_client import start_http_server

_SERVER_PORT = "50051"
_PROMETHEUS_PORT = 9465

তারপর run() কে এমনভাবে রূপান্তর করুন -

def run():
    # Start Prometheus client
    start_http_server(port=_PROMETHEUS_PORT, addr="0.0.0.0")
    meter_provider = MeterProvider(metric_readers=[PrometheusMetricReader()])

    otel_plugin = grpc_observability.OpenTelemetryPlugin(
        meter_provider=meter_provider
    )
    otel_plugin.register_global()

    with grpc.insecure_channel(target=f"localhost:{_SERVER_PORT}") as channel:
        stub = helloworld_pb2_grpc.GreeterStub(channel)
        # Continuously send RPCs every second.
        while True:
            try:
                response = stub.SayHello(helloworld_pb2.HelloRequest(name="You"))
                print(f"Greeter client received: {response.message}")
                time.sleep(1)
            except grpc.RpcError as rpc_error:
                print("Call failed with code: ", rpc_error.code())

    # Deregister is not called in this example, but this is required to clean up.
    otel_plugin.deregister_global()

পরবর্তী ধাপ হল সার্ভারে OpenTelemetry প্লাগইন যোগ করা। start_here/observability_greeter_server.py খুলুন এবং সম্পর্কিত নির্ভরতা এবং ম্যাক্রো যোগ করুন যাতে এটি দেখতে এরকম হয় -

from concurrent import futures
import logging
import time

import grpc
import grpc_observability
import helloworld_pb2
import helloworld_pb2_grpc
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.exporter.prometheus import PrometheusMetricReader
from prometheus_client import start_http_server

_SERVER_PORT = "50051"
_PROMETHEUS_PORT = 9464

তারপর run() কে এমনভাবে রূপান্তর করুন -

def serve():
    # Start Prometheus client
    start_http_server(port=_PROMETHEUS_PORT, addr="0.0.0.0")

    meter_provider = MeterProvider(metric_readers=[PrometheusMetricReader()])

    otel_plugin = grpc_observability.OpenTelemetryPlugin(
        meter_provider=meter_provider
    )
    otel_plugin.register_global()

    server = grpc.server(
        thread_pool=futures.ThreadPoolExecutor(max_workers=10),
    )
    helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
    server.add_insecure_port("[::]:" + _SERVER_PORT)
    server.start()
    print("Server started, listening on " + _SERVER_PORT)

    server.wait_for_termination()

    # Deregister is not called in this example, but this is required to clean up.
    otel_plugin.deregister_global()

৪. উদাহরণটি চালানো এবং মেট্রিক্স দেখা

সার্ভার চালানোর জন্য, চালান -

cd start_here
python -m observability_greeter_server

একটি সফল সেটআপের মাধ্যমে, আপনি সার্ভারের জন্য নিম্নলিখিত আউটপুট দেখতে পাবেন -

Server started, listening on 50051

সার্ভারটি চলমান থাকাকালীন, অন্য টার্মিনালে, ক্লায়েন্টটি চালান -

# Run the below commands to cd to the working directory and activate virtual environment in the new terminal
cd grpc-codelabs/codelabs/grpc-python-opentelemetry/
source .venv/bin/activate

cd start_here
python -m observability_greeter_client

একটি সফল দৌড় দেখতে এরকম হবে -

Greeter client received: Hello You
Greeter client received: Hello You
Greeter client received: Hello You

যেহেতু আমরা প্রোমিথিউস ব্যবহার করে মেট্রিক্স এক্সপোর্ট করার জন্য gRPC OpenTelemetry প্লাগইন সেট-আপ করেছি। সেই মেট্রিক্সগুলি সার্ভারের জন্য localhost:9464 এবং ক্লায়েন্টের জন্য localhost:9465 এ উপলব্ধ থাকবে।

ক্লায়েন্ট মেট্রিক্স দেখতে -

curl localhost:9465/metrics

ফলাফলটি হবে এই ধরণের -

# HELP python_gc_objects_collected_total Objects collected during gc
# TYPE python_gc_objects_collected_total counter
python_gc_objects_collected_total{generation="0"} 241.0
python_gc_objects_collected_total{generation="1"} 163.0
python_gc_objects_collected_total{generation="2"} 0.0
# HELP python_gc_objects_uncollectable_total Uncollectable objects found during GC
# TYPE python_gc_objects_uncollectable_total counter
python_gc_objects_uncollectable_total{generation="0"} 0.0
python_gc_objects_uncollectable_total{generation="1"} 0.0
python_gc_objects_uncollectable_total{generation="2"} 0.0
# HELP python_gc_collections_total Number of times this generation was collected
# TYPE python_gc_collections_total counter
python_gc_collections_total{generation="0"} 78.0
python_gc_collections_total{generation="1"} 7.0
python_gc_collections_total{generation="2"} 0.0
# HELP python_info Python platform information
# TYPE python_info gauge
python_info{implementation="CPython",major="3",minor="10",patchlevel="9",version="3.10.9"} 1.0
# HELP process_virtual_memory_bytes Virtual memory size in bytes.
# TYPE process_virtual_memory_bytes gauge
process_virtual_memory_bytes 1.868988416e+09
# HELP process_resident_memory_bytes Resident memory size in bytes.
# TYPE process_resident_memory_bytes gauge
process_resident_memory_bytes 4.1680896e+07
# TYPE process_resident_memory_bytes gauge                                                                                                                                                                                                                                                                21:20:16 [154/966]
process_resident_memory_bytes 4.1680896e+07
# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
# TYPE process_start_time_seconds gauge
process_start_time_seconds 1.72375679833e+09
# HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.
# TYPE process_cpu_seconds_total counter
process_cpu_seconds_total 0.38
# HELP process_open_fds Number of open file descriptors.
# TYPE process_open_fds gauge
process_open_fds 9.0
# HELP process_max_fds Maximum number of open file descriptors.
# TYPE process_max_fds gauge
process_max_fds 4096.0
# HELP target_info Target metadata
# TYPE target_info gauge
target_info{service_name="unknown_service",telemetry_sdk_language="python",telemetry_sdk_name="opentelemetry",telemetry_sdk_version="1.26.0"} 1.0
# HELP grpc_client_attempt_started_total Number of client call attempts started
# TYPE grpc_client_attempt_started_total counter
grpc_client_attempt_started_total{grpc_method="other",grpc_target="localhost:50051"} 18.0
# HELP grpc_client_attempt_sent_total_compressed_message_size_bytes Compressed message bytes sent per client call attempt
# TYPE grpc_client_attempt_sent_total_compressed_message_size_bytes histogram
grpc_client_attempt_sent_total_compressed_message_size_bytes_bucket{grpc_method="other",grpc_status="OK",grpc_target="localhost:50051",le="0.0"} 0.0
grpc_client_attempt_sent_total_compressed_message_size_bytes_bucket{grpc_method="other",grpc_status="OK",grpc_target="localhost:50051",le="5.0"} 18.0
grpc_client_attempt_sent_total_compressed_message_size_bytes_bucket{grpc_method="other",grpc_status="OK",grpc_target="localhost:50051",le="10.0"} 18.0
grpc_client_attempt_sent_total_compressed_message_size_bytes_bucket{grpc_method="other",grpc_status="OK",grpc_target="localhost:50051",le="25.0"} 18.0
grpc_client_attempt_sent_total_compressed_message_size_bytes_bucket{grpc_method="other",grpc_status="OK",grpc_target="localhost:50051",le="50.0"} 18.0
grpc_client_attempt_sent_total_compressed_message_size_bytes_bucket{grpc_method="other",grpc_status="OK",grpc_target="localhost:50051",le="75.0"} 18.0
grpc_client_attempt_sent_total_compressed_message_size_bytes_bucket{grpc_method="other",grpc_status="OK",grpc_target="localhost:50051",le="100.0"} 18.0
grpc_client_attempt_sent_total_compressed_message_size_bytes_bucket{grpc_method="other",grpc_status="OK",grpc_target="localhost:50051",le="250.0"} 18.0

একইভাবে, সার্ভার সাইড মেট্রিক্সের জন্য -

curl localhost:9464/metrics

৫. প্রমিথিউসে মেট্রিক্স দেখা

এখানে, আমরা একটি প্রমিথিউস ইনস্ট্যান্স সেটআপ করব যা আমাদের gRPC উদাহরণ ক্লায়েন্ট এবং সার্ভারকে স্ক্র্যাপ করবে যারা প্রমিথিউস ব্যবহার করে মেট্রিক্স রপ্তানি করছে।

প্রদত্ত লিঙ্কটি ব্যবহার করে আপনার প্ল্যাটফর্মের জন্য প্রমিথিউসের সর্বশেষ সংস্করণটি ডাউনলোড করুন , অথবা নিম্নলিখিত কমান্ডটি ব্যবহার করুন:

curl -sLO https://github.com/prometheus/prometheus/releases/download/v3.7.3/prometheus-3.7.3.linux-amd64.tar.gz

তারপর নিচের কমান্ডটি ব্যবহার করে এটি এক্সট্র্যাক্ট করুন এবং রান করুন:

tar xvfz prometheus-*.tar.gz
cd prometheus-*

নিম্নলিখিতগুলি দিয়ে একটি প্রোমিথিউস কনফিগারেশন ফাইল তৈরি করুন -

cat > grpc_otel_python_prometheus.yml <<EOF
scrape_configs:
  - job_name: "prometheus"
    scrape_interval: 5s
    static_configs:
      - targets: ["localhost:9090"]
  - job_name: "grpc-otel-python"
    scrape_interval: 5s
    static_configs:
      - targets: ["localhost:9464", "localhost:9465"]
EOF

নতুন কনফিগারেশন দিয়ে প্রমিথিউস শুরু করুন -

./prometheus --config.file=grpc_otel_python_prometheus.yml

এটি ক্লায়েন্ট এবং সার্ভার কোডল্যাব প্রক্রিয়াগুলির মেট্রিক্সগুলিকে প্রতি 5 সেকেন্ডে স্ক্র্যাপ করার জন্য কনফিগার করবে।

মেট্রিক্স দেখতে http://localhost:9090/graph এ যান। উদাহরণস্বরূপ, কোয়েরি -

histogram_quantile(0.5, rate(grpc_client_attempt_duration_seconds_bucket[1m]))

কোয়ান্টাইল গণনার জন্য সময় উইন্ডো হিসেবে ১ মিনিট ব্যবহার করে মধ্যম প্রচেষ্টার বিলম্ব সহ একটি গ্রাফ দেখাবে।

প্রশ্নের হার -

increase(grpc_client_attempt_duration_seconds_bucket[1m])

৬. (ঐচ্ছিক) ব্যবহারকারীর জন্য অনুশীলনী

প্রমিথিউস ড্যাশবোর্ডে, আপনি লক্ষ্য করবেন যে QPS কম। দেখুন আপনি উদাহরণে এমন কোনও সন্দেহজনক কোড সনাক্ত করতে পারেন কিনা যা QPS সীমাবদ্ধ করছে।

উৎসাহীদের জন্য, ক্লায়েন্ট কোডটি একটি নির্দিষ্ট মুহূর্তে শুধুমাত্র একটি মুলতুবি RPC-এর মধ্যেই সীমাবদ্ধ। এটি এমনভাবে পরিবর্তন করা যেতে পারে যাতে ক্লায়েন্ট পূর্ববর্তীগুলি সম্পূর্ণ হওয়ার জন্য অপেক্ষা না করে আরও RPC পাঠাতে পারে। (এর সমাধান এখনও দেওয়া হয়নি।)