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?

More than 1 year has passed since last update.

Google Cloud, Cloud Run, Python, Flaskアプリのモニタリング概観

Posted at

以下のような構成のWebアプリで、ビルトイン+簡単なコーディングだけで、どのくらいのモニタリングを行えるのかを確認します。

  • Google Cloud
  • Cloud Run
  • Python
  • Flaskアプリ

Logging

標準出力に出力するだけで自動的にCloud Loggingにログが集約されます。
単純なテキストも、JSONなどの構造化ログもOKです。

  • コーディングの例
logger.info("query string: " + request.query_string)
logger.info(json.dumps({"query_string": request.query_string}))
  • ログの例
INFO:app:query string: hoge=fuga&foo=bar
INFO:app:{"query_string": "hoge=fuga&foo=bar"}

Monitoring

  • 特にコーディングしなくても、リクエスト数、起動しているコンテナの数などの情報を取れます。
  • 例 CPU使用率
    monitoring.png

Profiler

Cloud Runサポートと記述されていませんが、使えます。
プログラム先頭でProfilerを有効にします。

  • コーディングの例
try:
    googlecloudprofiler.start(service='example')
except Exception as e:
    app.logger.error("failed to start Google Cloud Profiler: " + str(e))
  • Profilerの例

profiler.png

注意

本記事執筆時点では、Python 3.11でGoogle Cloud Profilerを使用できませんでした。
Python 3.10では問題ありませんでした。

Trace

  • 特にコーディングしなくても、リクエスト単位の処理時間を見られます。
  • OpenTelemetry対応すると、さらに細かいトレースを取得できます。

trace.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?