gRPC Python में OpenTelemetry का बेसिक प्लगिन सेट अप करना

1. परिचय

इस कोडलैब में, gRPC का इस्तेमाल करके एक क्लाइंट और सर्वर बनाया जाएगा. ये दोनों, Python में लिखे गए रूट-मैपिंग ऐप्लिकेशन की बुनियादी ज़रूरतें पूरी करते हैं.

ट्यूटोरियल के आखिर तक, आपके पास gRPC OpenTelemetry प्लगिन के साथ इंस्ट्रुमेंट किया गया एक सामान्य gRPC HelloWorld ऐप्लिकेशन होगा. साथ ही, Prometheus में एक्सपोर्ट की गई ऑब्ज़र्वेबिलिटी मेट्रिक देखी जा सकेंगी.

आपको क्या सीखने को मिलेगा

  • मौजूदा gRPC Python ऐप्लिकेशन के लिए, OpenTelemetry प्लगिन को सेट अप करने का तरीका
  • स्थानीय Prometheus इंस्टेंस चलाना
  • Prometheus में मेट्रिक एक्सपोर्ट करना
  • Prometheus डैशबोर्ड से मेट्रिक देखना

2. शुरू करने से पहले

आपको किन चीज़ों की ज़रूरत होगी

  • git
  • curl
  • build-essential
  • Python 3.9 या इसके बाद का वर्शन. अलग-अलग प्लैटफ़ॉर्म पर Python को इंस्टॉल करने के निर्देशों के लिए, Python सेटअप और इस्तेमाल करना लेख पढ़ें. इसके अलावा, uv या pyenv जैसे टूल का इस्तेमाल करके, सिस्टम के बाहर का Python इंस्टॉल करें.
  • Python पैकेज इंस्टॉल करने के लिए, pip का 9.0.1 या इसके बाद का वर्शन.
  • Python वर्चुअल एनवायरमेंट बनाने के लिए, 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-codelabs

इस कोडलैब के लिए, स्कैफ़ोल्ड सोर्स कोड इस GitHub डायरेक्ट्री में उपलब्ध है. अगर आपको कोड खुद लागू नहीं करना है, तो पूरा सोर्स कोड completed डायरेक्ट्री में उपलब्ध है.

सबसे पहले, grpc कोडलैब रेपो को क्लोन करें और grpc-python-opentelemetry फ़ोल्डर में cd करें:

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

इसके अलावा, सिर्फ़ कोडलैब डायरेक्ट्री वाली .zip फ़ाइल डाउनलोड करके, उसे मैन्युअल तरीके से अनज़िप किया जा सकता है.

सबसे पहले, एक नया Python वर्चुअल एनवायरमेंट (venv) बनाएं, ताकि आपके प्रोजेक्ट की डिपेंडेंसी को सिस्टम पैकेज से अलग किया जा सके:

python3 -m venv --upgrade-deps .venv

bash/zsh शेल में वर्चुअल एनवायरमेंट चालू करने के लिए:

source .venv/bin/activate

Windows और नॉन-स्टैंडर्ड शेल के लिए, https://docs.python.org/3/library/venv.html#how-venvs-work पर मौजूद टेबल देखें.

इसके बाद, एनवायरमेंट में डिपेंडेंसी इंस्टॉल करने के लिए, यह कमांड इस्तेमाल करें:

python -m pip install -r requirements.txt

3. OpenTelemetry प्लगिन रजिस्टर करना

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()

4. उदाहरण को चलाना और मेट्रिक देखना

सर्वर चलाने के लिए, यह कमांड चलाएं -

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

हमने Prometheus का इस्तेमाल करके मेट्रिक एक्सपोर्ट करने के लिए, 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

5. Prometheus पर मेट्रिक देखना

यहां हम एक ऐसा Prometheus इंस्टेंस सेट अप करेंगे जो हमारे gRPC उदाहरण क्लाइंट और सर्वर को स्क्रैप करेगा. ये दोनों, Prometheus का इस्तेमाल करके मेट्रिक एक्सपोर्ट कर रहे हैं.

दिए गए लिंक का इस्तेमाल करके, अपने प्लैटफ़ॉर्म के लिए Prometheus की नई रिलीज़ डाउनलोड करें या इस कमांड का इस्तेमाल करें:

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-*

नीचे दी गई जानकारी के साथ, 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 शुरू करें -

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

इससे क्लाइंट और सर्वर कोडलैब प्रोसेस से मेट्रिक को कॉन्फ़िगर किया जाएगा, ताकि हर पांच सेकंड में उन्हें स्क्रैप किया जा सके.

मेट्रिक देखने के लिए, http://localhost:9090/graph पर जाएं. उदाहरण के लिए, क्वेरी -

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

क्वांटाइल कैलकुलेशन के लिए एक मिनट की समयावधि का इस्तेमाल करके, कोशिश की गई औसत लेटेन्सी का ग्राफ़ दिखाएगा.

क्वेरी की दर -

increase(grpc_client_attempt_duration_seconds_bucket[1m])

6. (ज़रूरी नहीं) उपयोगकर्ता के लिए कसरत

आपको Prometheus डैशबोर्ड में दिखेगा कि QPS कम है. देखें कि क्या आपको उदाहरण में कोई ऐसा संदिग्ध कोड मिल रहा है जिसकी वजह से क्यूपीएस सीमित हो रहा है.

क्लाइंट कोड में, एक समय में सिर्फ़ एक आरपीसी को प्रोसेस किया जा सकता है. इसे बदला जा सकता है, ताकि क्लाइंट पिछले आरपीसी के पूरा होने का इंतज़ार किए बिना ज़्यादा आरपीसी भेज सके. (इस समस्या को हल करने का तरीका नहीं बताया गया है.)