Python@OpenTelemetry クライアントパッケージ¶
はじめに¶
本サイトにつきまして、以下をご認識のほど宜しくお願いいたします。
01. 概要¶
otel クライアントパッケージ¶
記入中...
拡張 otel クライアントパッケージ¶
記入中...
分散トレース SDK¶
記入中...
02. アプリで gRPC を使用しない場合¶
宛先が Google Cloud Trace の場合¶
▼ パッケージの初期化¶
ここでは、Flask というフレームワークで Python のアプリケーションを作成したとする。
otel クライアントパッケージを初期化する。
初期化の段階で、トレースコンテキストを抽出する。
import time
from opentelemetry import trace
from opentelemetry.propagate import set_global_textmap
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
from opentelemetry.propagators.cloud_trace_propagator import (CloudTraceFormatPropagator,)
# 送信元マイクロサービスのリクエストからトレースコンテキストを抽出する。
set_global_textmap(CloudTraceFormatPropagator())
# 任意のトレースコンテキストを設定する
resource = Resource.create({
"service.name": "flask_e2e_client",
"service.namespace": "examples",
"service.instance.id": "instance554",
})
tracer_provider = TracerProvider()
# Exporter (スパンの宛先) として、Google Cloud Traceを設定する。
cloud_trace_exporter = CloudTraceSpanExporter()
tracer_provider.add_span_processor(BatchSpanProcessor(cloud_trace_exporter))
trace.set_tracer_provider(tracer_provider)
tracer = trace.get_tracer(__name__)
- https://opentelemetry.io/docs/instrumentation/python/manual/
- opentelemetry-operations-python/docs/examples/flask_e2e/client.py at 7d7a5990a7125d3174af4c3a3443be3f03e7667c · GoogleCloudPlatform/opentelemetry-operations-python · GitHub
- opentelemetry-operations-python/docs/examples/flask_e2e/server.py at 7d7a5990a7125d3174af4c3a3443be3f03e7667c · GoogleCloudPlatform/opentelemetry-operations-python · GitHub
- 分散トレーシングとOpenTelemetryのススメ / Getting started distributed tracing and OpenTelemetry - Speaker Deck
ここでは、requests パッケージでリクエストを送信するため、RequestsInstrumentor() 関数による初期化も必要である。
import requests
from opentelemetry.instrumentation.requests import RequestsInstrumentor
RequestsInstrumentor().instrument()
response = requests.get("http://flask-app:6000")
print(response.text, flush=True)
- https://opentelemetry.io/docs/instrumentation/python/manual/
- OpenTelemetry requests Instrumentation — OpenTelemetry Python documentation
- Python インストルメンテーション サンプル | Cloud Trace | Google Cloud Documentation
- opentelemetry-operations-python/docs/examples/flask_e2e/client.py at 7d7a5990a7125d3174af4c3a3443be3f03e7667c · GoogleCloudPlatform/opentelemetry-operations-python · GitHub
▼ 親スパン作成¶
親スパンを作成する。
なお、親スパンと子スパンでスパン作成の実装方法は変わらない。
ここでは、Flask でリクエストを受信するため、FlaskInstrumentor() 関数でスパンを処理している。
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from flask import Flask
tracer = trace.get_tracer(__name__)
app = Flask(__name__)
FlaskInstrumentor().instrument_app(app)
@app.route("/")
def hello_world():
...
# Carrierにトレースコンテキストを注入する。
with tracer.start_as_current_span("do_work"):
time.sleep(0.1)
...
- https://opentelemetry.io/docs/instrumentation/python/manual/
- OpenTelemetry Flask Instrumentation — OpenTelemetry Python Contrib documentation
- Python インストルメンテーション サンプル | Cloud Trace | Google Cloud Documentation
- opentelemetry-operations-python/docs/examples/flask_e2e/server.py at 7d7a5990a7125d3174af4c3a3443be3f03e7667c · GoogleCloudPlatform/opentelemetry-operations-python · GitHub
▼ トレースコンテキスト注入と子スパン作成¶
Carrier にトレースコンテキストを注入し、また子スパンを作成する。
なお、親スパンと子スパンでスパン作成の実装方法は変わらない。
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from flask import Flask
tracer = trace.get_tracer(__name__)
app = Flask(__name__)
FlaskInstrumentor().instrument_app(app)
@app.route("/")
def hello_world():
...
# Carrierにトレースコンテキストを注入する。
with tracer.start_as_current_span("do_work"):
time.sleep(0.1)
...