BetaAnalyticsDataClient (AnalyticsDataClient) に RunReport メソッド実行時のタイムアウトを設定する - C#

BetaAnalyticsDataClient (AnalyticsDataClient) に RunReport メソッド実行時のタイムアウトを設定するコードを紹介します。

概要

こちらの記事では、 Google Analytics Data API を利用して、Google Analytics 4からデータを取得するコードを紹介しました。 通常の利用では、紹介したコードで問題ないですが、通信環境が良くない状況や、Google Analyticsまでの経路にプロキシ/透過型プロキシがある場合は、 タイムアウトしてしまう場合があります。
この記事では、BetaAnalyticsDataClient (AnalyticsDataClient)でのRunReport メソッド実行時のタイムアウト時間を設定して、 タイムアウト時間を延長するコードを紹介します。

タイムアウト

タイムアウトした場合にはアプリケーションで以下の例外 / エラーが発生します。
エラーメッセージ
Grpc.Core.RpcException: Status(StatusCode="DeadlineExceeded", Detail="")
詳細メッセージの例
************** 例外テキスト **************
Grpc.Core.RpcException: Status(StatusCode="DeadlineExceeded", Detail="")
   場所 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   場所 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   場所 Grpc.Net.Client.Internal.HttpClientCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request) 場所 /_/src/Grpc.Net.Client/Internal/HttpClientCallInvoker.cs:行 153
   場所 Grpc.Core.Interceptors.InterceptingCallInvoker.<BlockingUnaryCall>b__3_0[TRequest,TResponse](TRequest req, ClientInterceptorContext`2 ctx) 場所 /_/src/Grpc.Core.Api/Interceptors/InterceptingCallInvoker.cs:行 53
   場所 Grpc.Core.ClientBase.ClientBaseConfiguration.ClientBaseConfigurationInterceptor.BlockingUnaryCall[TRequest,TResponse](TRequest request, ClientInterceptorContext`2 context, BlockingUnaryCallContinuation`2 continuation) 場所 /_/src/Grpc.Core.Api/ClientBase.cs:行 205
   場所 Grpc.Core.Interceptors.InterceptingCallInvoker.BlockingUnaryCall[TRequest,TResponse](Method`2 method, String host, CallOptions options, TRequest request) 場所 /_/src/Grpc.Core.Api/Interceptors/InterceptingCallInvoker.cs:行 50
   場所 Google.Analytics.Data.V1Beta.BetaAnalyticsData.BetaAnalyticsDataClient.RunReport(RunReportRequest request, CallOptions options) 場所 /_/apis/Google.Analytics.Data.V1Beta/Google.Analytics.Data.V1Beta/AnalyticsDataApiGrpc.g.cs:行 521
   場所 Google.Api.Gax.Grpc.ApiCall.GrpcCallAdapter`2.CallSync(TRequest request, CallSettings callSettings) 場所 /_/Google.Api.Gax.Grpc/ApiCall.cs:行 92
   場所 Google.Api.Gax.Grpc.ApiCallRetryExtensions.<>c__DisplayClass1_0`2.<WithRetry>b__0(TRequest request, CallSettings callSettings) 場所 /_/Google.Api.Gax.Grpc/ApiCallRetryExtensions.cs:行 59
   場所 Google.Api.Gax.Grpc.ApiCall`2.<>c__DisplayClass12_0.<WithCallSettingsOverlay>b__1(TRequest req, CallSettings cs) 場所 /_/Google.Api.Gax.Grpc/ApiCall.cs:行 187
   場所 Google.Api.Gax.Grpc.ApiCall`2.Sync(TRequest request, CallSettings perCallCallSettings) 場所 /_/Google.Api.Gax.Grpc/ApiCall.cs:行 150
   場所 Google.Analytics.Data.V1Beta.BetaAnalyticsDataClientImpl.RunReport(RunReportRequest request, CallSettings callSettings) 場所 /_/apis/Google.Analytics.Data.V1Beta/Google.Analytics.Data.V1Beta/BetaAnalyticsDataClient.g.cs:行 2003

   (以下、作成アプリケーションの呼出し履歴が続く....)

対処法

RunReportメソッドの第二引数に、CallSettingsオブジェクトを与えられる書式があります。 タイムアウトを設定したCallSettingsオブジェクトを作成して、RunReportメソッドの第二引数に与えて呼び出します。
メモ
未指定の場合は、60秒(1分)でタイムアウトになります。

コード例

CallSettings オブジェクトを作成し、FromExpiration() メソッドでタイムアウト時間を設定するコード例です。
  //補足 aadc は BetaAnalyticsDataClient です。

  RunReportRequest request = new RunReportRequest()
  {
    Property = "(プロパティ)",
    Dimensions = { new Dimension() { Name = "Date" } },
    Metrics = { new Metric() { Name = "screenPageViews" } },
    DateRanges = { new DateRange() { StartDate = "2024-01-01", EndDate = "2024-06-30" } },
  };

  //タイムアウトを5分に設定
  CallSettings settings = CallSettings.FromExpiration(
    Expiration.FromTimeout(TimeSpan.FromMinutes(5))
  );

  //RunReportの実行
  RunReportResponse response = aadc.RunReport(request, settings);

動作確認

タイムアウト環境で修正コードでプログラムを実行し、問題が改善されるかを確認します。

著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
掲載日: 2024-05-08
iPentec all rights reserverd.