設定用戶端指標

本頁說明如何安裝、設定及使用 Bigtable 用戶端指標。

下列用戶端程式庫的使用者可以查看用戶端指標:

如需總覽,請參閱「用戶端指標總覽」。 如需指標的詳細清單,請參閱「用戶端指標說明」。

新增或確認服務帳戶權限

如要為服務帳戶新增 IAM 權限,請完成下列步驟:

  1. 前往 Google Cloud 控制台的「IAM」頁面。

    前往 IAM

  2. 選取您建立 Bigtable 叢集的專案。

  3. 搜尋要新增權限的服務帳戶。

  4. 按一下「編輯主體」

  5. 按一下「新增其他角色」。

  6. 搜尋「monitoring metric writer」,然後選取結果。

  7. 按一下 [儲存]

啟用 Cloud Monitoring API

  1. 在 Google Cloud 控制台中,前往「APIs & Services」(API 和服務) 頁面。

    前往「API 和服務」頁面

  2. 選取您建立 Bigtable 叢集的專案。

  3. 按一下「啟用 API 和服務」

  4. 搜尋「監控」。

  5. 在搜尋結果中,按一下「Stackdriver Monitoring API」。

  6. 如果畫面顯示「API enabled」(API 已啟用),代表 API 已啟用。 如果沒有,請按一下「啟用」

設定 Bigtable 用戶端程式庫

啟用 Cloud Monitoring API 後,請設定 Bigtable 用戶端程式庫。

Go

如果您使用 Bigtable Go 用戶端程式庫 (會呼叫 Bigtable API),請按照下列步驟操作。

安裝及設定 Go 專用的用戶端程式庫

將 Go 專用的 Bigtable 用戶端程式庫更新至 1.27.0 以上版本。您的 go.mod 應如下所示:

require (
  cloud.google.com/go/bigtable v1.27.2-0.20240725222120-ce31365acc54

選用:停用用戶端指標

Go 用戶端程式庫預設會啟用用戶端指標。如要停用,請在 ClientConfig 中傳遞 NoopMetricsProvider

client, err := bigtable.NewClientWithConfig(ctx, project, instance,
bigtable.ClientConfig{MetricsProvider: bigtable.NoopMetricsProvider{},
})

HBase

如果您使用 Java 適用的 Bigtable HBase 用戶端 (會呼叫 HBase API) 將應用程式連線至 Bigtable,請按照下列步驟操作。

安裝及設定 Java 適用的 HBase 用戶端程式庫

  1. 下載並安裝最新版本的 Bigtable HBase Java 用戶端。如要啟用用戶端指標,必須使用 2.6.4 以上版本。

  2. 建立空白的 Maven 專案。

  3. 判斷適合您用途的構件依附元件。2.x 版本包含 HBase 非同步用戶端,1.x 版本則沒有。

    • bigtable-hbase-1.xbigtable-hbase-2.x:在您控制依附元件的獨立應用程式中運用將這項成果。
    • bigtable-hbase-1.x-hadoopbigtable-hbase-2.x-hadoop:在 Hadoop 環境中使用。
    • bigtable-hbase-1.x-shadedbigtable-hbase-2.x-shaded:適用於 Hadoop 以外的環境,這些環境需要較舊版本的元件,例如 protobuf 或 Guava。
  4. 更新 pom.xml 檔案:

    <dependencyManagement>
        <dependencies>
            <dependency>
                  <groupId>com.google.cloud.bigtable</groupId>
                  <artifactId>BIGTABLE_HBASE_ARTIFACT</artifactId>
                  <version>VERSION_NUMBER</version>
              </dependency>
        </dependencies>
    </dependencyManagement>
    

    請提供下列資訊:

    • BIGTABLE_HBASE_ARTIFACT:專案的構件依附元件,格式為 bigtable-hbase-1.xbigtable-hbase-2.x-hadoop
    • VERSION_NUMBER:您使用的用戶端程式庫版本,格式為 2.6.4

在應用程式中啟用用戶端指標

使用 bigtable-hbase-1.xbigtable-hbase-2.x

使用 HBase 用戶端程式庫和 bigtable-hbase-1.xbigtable-hbase-2.x 構件建立連線時,啟用內建用戶端指標的程式碼如下所示:

  import com.google.cloud.bigtable.data.v2.BigtableDataSettings;

  Configuration configuration = BigtableConfiguration.configure("my-project-id", "my-instance-id");
  Connection connection = new BigtableConnection(configuration);

  BigtableDataSettings.enableBuiltinMetrics();

使用 -hadoop-shaded 構件

使用 HBase 用戶端程式庫和 -hadoop-shaded 構件建立連線時,啟用內建用戶端指標的程式碼如下所示:

  import com.google.bigtable.repackaged.com.google.cloud.bigtable.data.v2.BigtableDataSettings;

  Configuration configuration = BigtableConfiguration.configure("my-project-id", "my-instance-id");
  Connection connection = new BigtableConnection(configuration);

  BigtableDataSettings.enableBuiltinMetrics();

選用:停用用戶端指標

如果您使用的是 2.14.1 以上版本,系統會預設啟用用戶端指標。你可以選擇停用。

Configuration configuration = BigtableConfiguration.configure("my-project-id", "my-instance-id");
configuration.setBoolean(BigtableOptionsFactory.BIGTABLE_ENABLE_CLIENT_SIDE_METRICS, false);
Connection connection = new BigtableConnection(configuration);

Java

如果您使用 Java 適用的 Bigtable 用戶端程式庫 (會呼叫 Bigtable API),請按照下列步驟操作。

事前準備

選用:如果您使用 OpenCensus Stats 整合功能取得 Bigtable 指標,請還原應用程式中的依附元件和 StackdriverStatsExporter 變更。如要瞭解原因,請參閱「升級時機」。

安裝及設定 Java 專用的用戶端程式庫

  1. 下載並安裝最新版本的 Bigtable Java 用戶端程式庫。如要使用用戶端指標,必須使用 2.16.0 以上版本。
  2. 更新 Java 用戶端程式庫的 pom.xml 檔案,使用最新版本。 您的 pom 應類似以下內容:

    <dependencyManagement>
        <dependencies>
            <dependency>
                  <groupId>com.google.cloud</groupId>
                  <artifactId>google-cloud-bigtable-bom</artifactId>
                  <version>2.38.0</version>
                  <scope>import</scope>
                  <type>pom</type>
              </dependency>
        </dependencies>
    </dependencyManagement>
    
    <dependencies>
        <dependency>
              <groupId>com.google.cloud</groupId>
              <artifactId>google-cloud-bigtable</artifactId>
        </dependency>
    </dependencies>
    

在應用程式中啟用用戶端指標

使用用戶端程式庫建立用戶端時,請啟用內建指標。您的程式碼應如下所示:

  BigtableDataSettings settings = BigtableDataSettings.newBuilder()
              .setProjectId("our-project-2-12")
              .setInstanceId("our-instance-85")
              .setAppProfileId("our-appProfile-06")
              .build();

  BigtableDataSettings.enableBuiltinMetrics();

  try (BigtableDataClient client = BigtableDataClient.create(settings)) {
          // Application logic
  }

設定自訂 OpenTelemetry 執行個體

如果您使用 2.38.0 以上版本,且想將用戶端指標發布至 Cloud Monitoring 和自訂接收器,可以設定自訂 OpenTelemetry 執行個體。

  SdkMeterProviderBuilder meterProviderBuilder = SdkMeterProvider.builder();

  // register client side metrics on your meter provider
  BuiltinMetricsView.registerBuiltinMetrics("my-project-id", meterProviderBuilder);

  // register other metric reader and views
  meterProviderBuilder.registerMetricReader().registerView();

  // create the OTEL instance
  OpenTelemetry openTelemetry = OpenTelemetrySdk.builder()
          .setMeterProvider(meterProviderBuilder.build())
          .build();

  // Override MetricsProvider in BigtableDataSettings
  BigtableDataSettings settings = BigtableDataSettings.newBuilder()
          .setProjectId("my-project-id")
          .setInstanceId("my-instance-id")
          .setMetricsProvider(CustomOpenTelemetryMetricsProvider.create(openTelemetry))
          .build();

  try (BigtableDataClient client = BigtableDataClient.create(settings)) {
        // Application logic
  }

選用:停用用戶端指標

如果您使用的是 2.38.0 以上版本,可以選擇不使用用戶端指標。

BigtableDataSettings settings = BigtableDataSettings.newBuilder()
        .setProjectId("my-project")
        .setInstanceId("my-instance")
        .setMetricsProvider(NoopMetricsProvider.INSTANCE)
        .build();

在 Metrics Explorer 中查看指標

  1. 前往 Google Cloud 控制台的「Metrics Explorer」頁面。

    前往 Metrics Explorer

  2. 選取專案。

  3. 按一下「Select a metric」(選取指標)

  4. 搜尋 bigtable.googleapis.com/client

  5. 選取指標、group by方法和狀態,然後挑選匯總工具。如要瞭解更多選項,請參閱「在使用 Metrics Explorer 時選取指標」。

啟用用戶端指標後,請讓應用程式執行至少一分鐘,再檢查是否有已發布的指標。

在 Bigtable 監控中查看指標

Bigtable 監控頁面會顯示下列用戶端指標的圖表:

  • 用戶端讀取作業延遲時間
  • 用戶端寫入作業延遲時間
  • 用戶端讀取嘗試延遲時間
  • 用戶端寫入嘗試延遲時間

如要在「監控」頁面查看用戶端指標,請按照下列步驟操作:

  1. 在 Google Cloud 控制台中開啟 Bigtable 執行個體清單。

    開啟執行個體清單

  2. 按一下要查看指標的執行個體。

  3. 按一下左側窗格中的 [監控]。 Google Cloud 控制台會顯示執行個體的一系列圖表。

  4. 捲動窗格,查看用戶端指標。

  5. 選用:如要查看表格層級的指標,請按一下「表格」

  6. 選用:如要查看個別應用程式設定檔的指標,請按一下「應用程式設定檔」

詳情請參閱 Bigtable 監控

後續步驟