Error Reporting 用の Python アプリのインストルメント化

Python アプリケーションから Error Reporting にエラーレポートを送信するには、Python 用 Error Reporting ライブラリを使用します。 Python 用 Error Reporting ライブラリを使用して、次の場合にエラーグループを作成します。

  • ログバケットには顧客管理の暗号鍵(CMEK)が含まれています。
  • ログバケットが global リージョンにない
  • カスタム エラーイベントを報告する。

Error Reporting は、App EngineCompute EngineGoogle Kubernetes Engine などの一部の Google Cloud サービスに統合されています。Error Reporting は、こうしたサービスで実行されるアプリケーションによって Cloud Logging に記録されたエラーを表示します。詳細については、このページの Google Cloud での実行をご覧ください。

Logging を使用して、エラーデータを Error Reporting に送信することもできます。データ形式の要件については、Logging のエラー メッセージの形式設定をご覧ください。

始める前に

  1. Google Cloud アカウントにログインします。Google Cloud を初めて使用する場合は、アカウントを作成して、実際のシナリオでの Google プロダクトのパフォーマンスを評価してください。新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $300 分を差し上げます。
  2. Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。

    プロジェクト セレクタに移動

  3. Google Cloud プロジェクトで課金が有効になっていることを確認します

  4. Error Reporting API を有効にします。

    API を有効にする

  5. Google Cloud Console の [プロジェクト セレクタ] ページで、Google Cloud プロジェクトを選択または作成します。

    プロジェクト セレクタに移動

  6. Google Cloud プロジェクトで課金が有効になっていることを確認します

  7. Error Reporting API を有効にします。

    API を有効にする

  8. Python 開発用に環境を準備します。

    Python 設定ガイドに移動

クライアント ライブラリをインストールする

Python 用 Error Reporting ライブラリを使用すると、ほぼすべての場所で動作している Python アプリケーションから報告されるエラーをモニタリングして表示できます。

  1. 仮想環境をインスタンス化し、pip を使用してパッケージをインストールします。

    pip install google-cloud-error-reporting --upgrade
    
  2. エラーの報告を開始するために、ライブラリをインポートしてクライアントをインスタンス化します。

    Python

    Error Reporting に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、ローカル開発環境の認証の設定をご覧ください。

    #!/usr/bin/env python
    #
    # Copyright 2022 Google, Inc.
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #    http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    
    def report_exception():
        from google.cloud import error_reporting
    
        client = error_reporting.Client()
        try:
            raise Exception("Something went wrong")
        except Exception:
            client.report_exception()
    
    def report_manual_error():
        from google.cloud import error_reporting
    
        client = error_reporting.Client()
        client.report("An error has occurred.")
    
    if __name__ == "__main__":
        report_exception()
        report_manual_error()
    

インストールの詳細については、Python 用 Error Reporting ライブラリのドキュメントをご覧ください。公開バグトラッカーを使用して問題を報告することもできます。

クライアント ライブラリを設定する

Python 用 Error Reporting ライブラリの動作をカスタマイズできます。構成例については、クライアント ライブラリのドキュメントをご覧ください。

Google Cloud でアプリを実行する

projects.events.report を使用してエラーグループを作成するには、サービス アカウントに Error Reporting 書き込みロールroles/errorreporting.writer)が必要です。

一部の Google Cloud サービスでは、Error Reporting 書き込みロール(roles/errorreporting.writer)が適切なサービス アカウントに自動的に付与されます。ただし、一部のサービスに適したサービス アカウントにこのロールを付与する必要があります。

App Engine フレキシブル環境

App Engine では、デフォルトのサービス アカウントに Error Reporting 書き込みロールroles/errorreporting.writer)が自動的に付与されます。

Python 用 Error Reporting ライブラリは、明示的に認証情報を提示しなくても使用できます。

App Engine フレキシブル環境のアプリケーションでは、Error Reporting が自動的に有効になります。特別な設定は必要ありません。

Google Kubernetes Engine

Google Kubernetes Engine で Error Reporting を使用するには、次の操作を行います。

  1. コンテナが使用するサービス アカウントに、Error Reporting 書き込みロールroles/errorreporting.writer)が付与されていることを確認します。

    Compute Engine のデフォルトのサービス アカウントまたはカスタム サービス アカウントを使用できます。

    ロールの付与については、プロジェクト、フォルダ、組織へのアクセスを管理するをご覧ください。

  2. クラスタを作成し、そのクラスタに cloud-platform アクセス スコープを付与します。

    たとえば、次の create コマンドでは、cloud-platform アクセス スコープとサービス アカウントを指定します。

    gcloud container clusters create CLUSTER_NAME --service-account  SERVICE_ACCT_NAME --scopes=cloud-platform
    

Compute Engine

Compute Engine の VM インスタンスで Error Reporting を使用するには、次のようにします。

  1. VM インスタンスが使用するサービス アカウントに、Error Reporting 書き込みロールroles/errorreporting.writer)が付与されていることを確認します。

    Compute Engine のデフォルトのサービス アカウントまたはカスタム サービス アカウントを使用できます。

    ロールの付与については、プロジェクト、フォルダ、組織へのアクセスを管理するをご覧ください。

  2. Google Cloud コンソールのナビゲーション パネルで、[Compute Engine] を選択してから、[VM インスタンス] を選択します。

    [VM インスタンス] に移動

  3. cloud-platform アクセス スコープを受け取る VM インスタンスを選択します。

  4. [停止]、[編集] の順にクリックします。

  5. [ID と API へのアクセス] セクションで、Error Reporting 書き込みロール(roles/errorreporting.writer)を持つサービス アカウントを選択します。

  6. [アクセス スコープ] セクションで、[すべての Cloud API に完全アクセス権を許可] を選択して、変更内容を保存します。

  7. [開始 / 再開] をクリックします。

手動でエラーを報告するには、次の例で示すように、report メソッドを呼び出します。

Python

Error Reporting に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。 詳細については、認証を設定するをご覧ください。

#!/usr/bin/env python
#
# Copyright 2022 Google, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

def report_exception():
    from google.cloud import error_reporting

    client = error_reporting.Client()
    try:
        raise Exception("Something went wrong")
    except Exception:
        client.report_exception()

def report_manual_error():
    from google.cloud import error_reporting

    client = error_reporting.Client()
    client.report("An error has occurred.")

if __name__ == "__main__":
    report_exception()
    report_manual_error()

ローカル開発環境でアプリを実行する

自分のワークステーションでライブラリを実行するなど、ローカル開発環境で Python 用 Error Reporting ライブラリを使用するには、ローカル アプリケーションのデフォルト認証情報を使用して Python 用 Error Reporting ライブラリを指定する必要があります。詳細については、Error Reporting に対する認証をご覧ください。

このページの Python サンプルをローカル開発環境から使用するには、gcloud CLI をインストールして初期化し、自身のユーザー認証情報を使用してアプリケーションのデフォルト認証情報を設定してください。

  1. Google Cloud CLI をインストールします。
  2. gcloud CLI を初期化するには:

    gcloud init
  3. Google アカウントのローカル認証情報を作成します。

    gcloud auth application-default login

詳細については、 ローカル開発環境の認証の設定 をご覧ください。

projects.events.report メソッドは API キーもサポートしています。 認証に API キーを使用する場合、ローカルのアプリケーションのデフォルト認証情報ファイルを設定する必要はありません。詳細については、Google Cloud の認証に関するドキュメントの API キーを作成するをご覧ください。

エラーレポートの表示

Google Cloud コンソールのナビゲーション パネルで [エラーレポート] を選択し、Google Cloud プロジェクトを選択します。

Error Reporting に移動

詳細については、エラーの表示をご覧ください。