Google Kubernetes Engine での設定

次のどちらかの方法で、Google Kubernetes Engine アプリケーションから Error Reporting にエラーを送信できます。

ロギングを使用してエラーを報告する

GKE のデフォルトの Logging エージェントによって、クラスタのログを Cloud Logging に送信するエージェントをデプロイして管理するためのマネージド ソリューションが提供されます。エージェントの構造は、クラスタのバージョンによって異なります。このエージェントについては、GKE ログの管理をご覧ください。

Error Reporting では、例外またはスタック トレースが 1 つのログエントリに含まれる必要があります。ほとんどのロギング エージェントは、複数のログ行(新しい行にそれぞれ出力されるスタックフレーム)が 1 つのスタック トレースを表していることを認識でき、それを単一のログエントリとして Cloud Logging に送信します。エージェントが複数の行を 1 つのエラーに再構築できない場合は、projects.events.report API エンドポイントを使用します。これにより、エラーの内容を制御できます。

Error Reporting API を使用してエラーを書き込む

Error Reporting API は、エラー情報をサービスに書き込むための report エンドポイントを提供します。

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

    API を有効にするために必要なロール

    API を有効にするには、serviceusage.services.enable 権限を含む Service Usage 管理者 IAM ロール(roles/serviceusage.serviceUsageAdmin)が必要です。ロールを付与する方法を確認する

    API の有効化

  2. REST API またはクライアント ライブラリのいずれかを使用して、API にエラーを報告します。

サンプル

ASP.NET

ASP.NET NuGet パッケージにより、捕捉されなかった例外が ASP.NET ウェブ アプリケーションから Error Reporting に報告されます。

NuGet パッケージをインストールする

Stackdriver ASP.NET NuGet パッケージを Visual Studio にインストールするには:

  1. ソリューションを右クリックして、[ソリューションの NuGet パッケージの管理] を選択します。
  2. [プレリリースを含む] チェックボックスを選択します。
  3. Google.Cloud.Diagnostics.AspNet という名前のパッケージを検索してインストールします。

使用法

Stackdriver ASP.NET NuGet パッケージをインストールしたら、次のステートメントをアプリケーション コードに追加して Stackdriver へのエラーの送信を開始します。

using Google.Cloud.Diagnostics.AspNet;

例外の報告を有効にするには、次の HttpConfiguration コードを .NET ウェブアプリの Register メソッドに追加します(your-project-id は実際のプロジェクト ID に置き換えます)。

public static void Register(HttpConfiguration config)
{
    string projectId = "YOUR-PROJECT-ID";
    string serviceName = "NAME-OF-YOUR-SERVICE";
    string version = "VERSION-OF-YOUR-SERVCICE";
    // ...
    // Add a catch all for the uncaught exceptions.
    config.Services.Add(typeof(IExceptionLogger),
        ErrorReportingExceptionLogger.Create(projectId, serviceName, version));
    // ...
}

このメソッドを ASP.NET アプリケーションに追加すると、発生した例外で捕捉されなかった例外が Google Cloud コンソールの Error Reporting セクションで Google Cloudに報告されるため、これらの例外を表示することができます。

C#

次の例は、GoogleCloudPlatform/dotnet-docs-samples リポジトリにあります。これを使用するには、プロジェクトのビルド後に次のようにプロジェクト ID を指定します。

C:\...\bin\Debug> set GOOGLE_PROJECT_ID=[YOUR_PROJECT_ID]

[YOUR_PROJECT_ID] はGoogle Cloud コンソールの正しい値に置き換えます。

その後、次のようなコードで例外データを送信します。

public class ErrorReportingSample
{
    public static void Main(string[] args)
    {
        try
        {
            throw new Exception("Generic exception for testing Stackdriver Error Reporting");
        }
        catch (Exception e)
        {
            report(e);
            Console.WriteLine("Stackdriver Error Report Sent");
        }
    }

    /// <summary>
    /// Create the Error Reporting service (<seealso cref="ClouderrorreportingService"/>)
    /// with the Application Default Credentials and the proper scopes.
    /// See: https://developers.google.com/identity/protocols/application-default-credentials.
    /// </summary>
    private static ClouderrorreportingService CreateErrorReportingClient()
    {
        // Get the Application Default Credentials.
        GoogleCredential credential = GoogleCredential.GetApplicationDefaultAsync().Result;

        // Add the needed scope to the credentials.
        credential.CreateScoped(ClouderrorreportingService.Scope.CloudPlatform);

        // Create the Error Reporting Service.
        ClouderrorreportingService service = new ClouderrorreportingService(new BaseClientService.Initializer
        {
            HttpClientInitializer = credential,
        });
        return service;
    }

    /// <summary>
    /// Creates a <seealso cref="ReportRequest"/> from a given exception.
    /// </summary>
    private static ReportRequest CreateReportRequest(Exception e)
    {
        // Create the service.
        ClouderrorreportingService service = CreateErrorReportingClient();

        // Get the project ID from the environement variables.
        string projectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");

        // Format the project id to the format Error Reporting expects. See:
        // https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events/report
        string formattedProjectId = string.Format("projects/{0}", projectId);

        // Add a service context to the report.  For more details see:
        // https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events#ServiceContext
        ServiceContext serviceContext = new ServiceContext()
        {
            Service = "myapp",
            Version = "8c1917a9eca3475b5a3686d1d44b52908463b989",
        };
        ReportedErrorEvent errorEvent = new ReportedErrorEvent()
        {
            Message = e.ToString(),
            ServiceContext = serviceContext,
        };
        return new ReportRequest(service, errorEvent, formattedProjectId);
    }

    /// <summary>
    /// Report an exception to the Error Reporting service.
    /// </summary>
    private static void report(Exception e)
    {
        // Create the report and execute the request.
        ReportRequest request = CreateReportRequest(e);
        request.Execute();
    }
}

Go

Go 用 Error Reporting の設定をご覧ください。

Java

Java 用 Error Reporting の設定をご覧ください。

Node.js

Node.js 用 Error Reporting の設定をご覧ください。

Ruby

Ruby 用 Error Reporting の設定をご覧ください。

Python

Python 用 Error Reporting の設定をご覧ください。

PHP

PHP 用 Error Reporting の設定をご覧ください。

エラーグループを表示する

Google Cloud コンソールで [Error Reporting] ページに移動します。

Error Reporting に移動

このページは、検索バーを使用して見つけることもできます。