This page shows how to get started with the Cloud Client Libraries for the Stackdriver Monitoring API. Read more about the client libraries for Cloud APIs, including the older Google APIs Client Libraries, in Client Libraries Explained.
The samples on this page use custom, or user-defined, metrics to illustrate the use of the client libraries. The system-defined metrics described in the Metrics list are collected for you. You don't need to write any code to collect them, although the agent metrics do require the installation of the Stackdriver agents. For more information on agent metrics, see the Agent metrics list.For information about the previous Monitoring API client libraries, see Monitoring API Client Libraries.
Installing the client library
C#
For more information, see Setting Up a C# Development Environment.In Visual Studio 2013/2015, open the Package Manager Console and run this command:
Install-Package Google.Cloud.Monitoring.V3 -Pre
Go
go get -u cloud.google.com/go/monitoring/apiv3
Java
For more information, see Setting Up a Java Development Environment. Maven を使用している場合は、次のものをpom.xml
ファイルに追加します。
Gradle を使用している場合は、次のものを依存関係に追加します。
SBT を使用している場合は、次のものを依存関係に追加します。
IntelliJ または Eclipse を使用している場合は、次の IDE プラグインを使用してプロジェクトにクライアント ライブラリを追加できます。
プラグインでは、サービス アカウントのキー管理などの追加機能も提供されます。詳細は各プラグインのドキュメントをご覧ください。
Node.js
For more information, see Setting Up a Node.js Development Environment.npm install --save @google-cloud/monitoring
PHP
composer require google/cloud-monitoring
Python
For more information, see Setting Up a Python Development Environment.pip install --upgrade google-cloud-monitoring
Ruby
For more information, see Setting Up a Ruby Development Environment.gem install google-cloud-monitoring
Setting up authentication
To run the client library, you must first set up authentication by creating a service account and setting an environment variable. Complete the following steps to set up authentication. For other ways to authenticate, see the GCP authentication documentation.
GCP Console
-
GCP Console で [サービス アカウントキーの作成] ページに移動します。
[サービス アカウントキーの作成] ページに移動 - [サービス アカウント] リストから [新しいサービス アカウント] を選択します。
- [サービス アカウント名] フィールドに名前を入力します。
[役割] リストで、[プロジェクト] > [オーナー] を選択します。
注: [役割] フィールドの設定により、リソースにアクセスするサービス アカウントが承認されます。このフィールドは、後から GCP Console で表示または変更できます。本番環境アプリケーションを開発している場合は、[プロジェクト] > [オーナー] よりも詳細な権限を指定します。詳しくはサービス アカウントへの役割の付与をご覧ください。- [作成] をクリックします。キーが含まれている JSON ファイルがパソコンにダウンロードされます。
コマンドライン
ローカルマシン上の Cloud SDK を使用するか、または Cloud Shell 内で以下のコマンドを実行できます。
-
サービス アカウントを作成します。[NAME] をサービス アカウントの名前に置き換えます。
gcloud iam service-accounts create [NAME]
-
サービス アカウントに権限を付与します。[PROJECT_ID] は実際のプロジェクト ID に置き換えます。
gcloud projects add-iam-policy-binding [PROJECT_ID] --member "serviceAccount:[NAME]@[PROJECT_ID].iam.gserviceaccount.com" --role "roles/owner"
注: [役割] フィールドの設定により、リソースにアクセスするサービス アカウントが承認されます。後で GCP Console を使用して、このフィールドを表示したり変更したりできます。本番環境アプリケーションを開発している場合は、[プロジェクト] > [オーナー] よりも詳細な権限を指定します。詳しくはサービス アカウントへの役割の付与をご覧ください。 -
キーファイルを生成します。[FILE_NAME] はキーファイルの名前に置き換えます。
gcloud iam service-accounts keys create [FILE_NAME].json --iam-account [NAME]@[PROJECT_ID].iam.gserviceaccount.com
環境変数 GOOGLE_APPLICATION_CREDENTIALS を設定して、アプリケーション コードに認証情報を指定します。[PATH] は、サービス アカウント キーが含まれる JSON ファイルのファイルパスに置き換え、[FILE_NAME] はファイル名に置き換えます。この変数は現在のシェル セッションにのみ適用されるため、新しいセッションを開く場合は、変数を再度設定してください。
Linux または macOS
export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
例:
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/[FILE_NAME].json"
Windows
PowerShell を使用する場合:
$env:GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
例:
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\[FILE_NAME].json"
コマンド プロンプトを使用する場合:
set GOOGLE_APPLICATION_CREDENTIALS=[PATH]
Using the client library
The following example shows how to use the client library.
C#
See README.md for instructions on using Visual Studio to build and run this sample C# code.
Go
Java
Node.js
PHP
Python
Ruby