Before you can use the Monitoring API, you must enable it and authorize users to access it.
This page describes how to enable and authorize use of the Monitoring API v3.
Enabling the API
The Monitoring API must be enabled before it can be used. You can check the status and enable the API if necessary by using either the the Google Cloud console or Google Cloud CLI.
Google Cloud console
In the Google Cloud console, select the Google Cloud project for which you want to enable the API, and then go to the APIs & Services page:
Click the Enable APIs and Service button.
Search for "Monitoring".
In the search results, click through to "Stackdriver Monitoring API".
If "API enabled" is displayed, then the API is already enabled. If not, then click Enable.
gcloud CLI
If you have not already installed the Google Cloud CLI on your workstation, see Installing the gcloud CLI.
To see if the Monitoring API is enabled, run the following command on your workstation, after replacing PROJECT_ID with the ID of the project for which you want to enable the API:
gcloud services list --project=PROJECT_ID
If
monitoring.googleapis.com
appears in the output, the API is enabled.If the API is not enabled, then run the following command to enable it:
gcloud services enable monitoring --project=PROJECT_ID
For more information, see
gcloud services
.
Authorizing use of the API
Access to the API is controlled by Identity and Access Management (IAM) roles and permissions. There is a permission associated with each method, and permissions for related methods are clustered into roles, which can then be granted to users. See Control access with Identity and Access Management for more information.
Once access to the API is authorized, the API can be used from anywhere.
For example, you can use the timeSeries.list
method from your local workstation. You can also use the API to send
metrics against any entity that can be represented as a monitored resource.
See Monitored-resource types for user-defined metrics
for the list of available
types. However, the Cloud Monitoring agent is designed to run on a cloud VM
and does not work on a local workstation.
If you are writing an application that uses the Monitoring API, the recommended client libraries authorize your application using Application Default Credentials. This hides authorization details from your application code.
You might also be able to use API keys in some situations, but service accounts are generally recommended. API keys are useful in clients like browsers and mobile applications that don't have a backend server. For more information, see Using API Keys.
You can find out more about authentication and authorization for Cloud APIs in our Authentication guide.
Using client libraries
After you have enabled and authorized the use of the API, you are ready to start writing code. See Client libraries to get started using the Monitoring client libraries.