1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PythonのLLMをopenllmetryで計測する (GCP Cloud Trace)

Posted at

はじめに

LLMの呼び出しがあるAPIの場合、Latencyが大幅に増えるケースが多いので、Traceabilityがとても重要になります。
Performanceだけでなく、実際にLLMがどこでどのように呼ばれてどんな結果が帰っているのかも含めて一連の呼び出しを紐づけるTracingは調査に役立ちます。

openllmetry

Open-source observability for your LLM application, based on OpenTelemetry

Open-sourceのOpenTelemetryベースのLLM app用のObservabilityツールです。

DatadogやGoogle Cloudをはじめとする多数のExporterがサポートされており、Tracing、Metrics、Loggingを紐づける事ができます。

Example

今回は、前回紹介した Flask appをOpentelemetryでGCP Cloud Traceに連携するを拡張して、Flaskで作ったLLM appにopenllmetryを入れる例を紹介します。

コード

main.py
from traceloop.sdk import Traceloop
from opentelemetry.exporter.cloud_trace import CloudTraceSpanExporter
from opentelemetry.instrumentation.flask import FlaskInstrumentor

Traceloop.init(
    app_name="<app name>",
    exporter=CloudTraceSpanExporter(resource_regex="env|version|service.name"),
    resource_attributes={
        "env": os.getenv("ENV", "dev"),
        "version": os.getenv("VERSION", "unknown"),
    },
)


app = Flask(__name__)

FlaskInstrumentor().instrument_app(app)

@app.route("/")
def hello():
    return "Hello!"

if __name__ == "__main__":
    app.run(debug=True)

前回設定していたCloudTraceSpanExporterを同様にTraceloop.initのexporterに渡してあげるだけでOkです。

これで、flask appの中で使われる openai, anthropicなどのLLMの呼び出し、LangChain、LangGraphの呼び出し部分の計装が自動的に行われます。

Cloud Trace

この設定をするとCloud Traceで以下のようなTraceが取れるようになります :tada:

Screenshot 2025-06-02 at 7.58.03.png

このトレースは上のサンプルコードから出たものではありません。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?