Stackdriver Monitoring Client Libraries

This page shows how to get started with the new 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.

For information about the previous Stackdriver Monitoring API client libraries, see Stackdriver Monitoring API Client Libraries .

Installing the client library


In Visual Studio 2013/2015, open the Package Manager Console and run this command:
Install-Package Google.Cloud.Monitoring.V3 -Pre


go get -u


If you are using Maven, add this to your pom.xml file:


If you are using Gradle, add this to your dependencies:

compile group: '', name: 'google-cloud-monitoring', version: '0.17.2-alpha'


npm install --save @google-cloud/monitoring


pip install --upgrade google-cloud-monitoring


gem install google-cloud-monitoring

Using the client library

Here's an example of how to use the client library. To run it on your local workstation you must first install the Google Cloud SDK and authenticate by running the following command:

gcloud auth application-default login

For information about authenticating in other environments, see the Google Cloud Platform Auth Guide.


See for instructions on using Visual Studio to build and run this sample C# code.
using System;
using System.Collections.Generic;
using Google.Cloud.Monitoring.V3;
using Google.Protobuf.WellKnownTypes;
using Google.Api;

namespace GoogleCloudSamples
    public class QuickStart
        public static void Main(string[] args)
            // Your Google Cloud Platform project ID.
            string projectId = "YOUR-PROJECT-ID";

            // Create client.
            MetricServiceClient metricServiceClient = MetricServiceClient.Create();

            // Initialize request argument(s).
            ProjectName name = new ProjectName(projectId);

            // Prepare a data point. 
            Point dataPoint = new Point();
            TypedValue salesTotal = new TypedValue();
            salesTotal.DoubleValue = 123.45;
            dataPoint.Value = salesTotal;
            // Sets data point's interval end time to current time.
            Timestamp timeStamp = new Timestamp();
            DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            timeStamp.Seconds = (long)(DateTime.UtcNow - UnixEpoch).TotalSeconds;
            TimeInterval interval = new TimeInterval();
            interval.EndTime = timeStamp;
            dataPoint.Interval = interval;

            // Prepare custom metric.
            Metric metric = new Metric();
            metric.Type = "";
            metric.Labels.Add("store_id", "Pittsburgh");

            // Prepare monitored resource.
            MonitoredResource resource = new MonitoredResource();
            resource.Type = "global";
            resource.Labels.Add("project_id", projectId);

            // Create a new time series using inputs.
            TimeSeries timeSeriesData = new TimeSeries();
            timeSeriesData.Metric = metric;
            timeSeriesData.Resource = resource;

            // Add newly created time series to list of time series to be written.
            IEnumerable<TimeSeries> timeSeries = new List<TimeSeries> { timeSeriesData };
            // Write time series data.
            metricServiceClient.CreateTimeSeries(name, timeSeries);
            Console.WriteLine("Done writing time series data.");
Read more in the C# API Reference Documentation for the Stackdriver Monitoring API Client Library.


// Sample monitoring-quickstart writes a data point to Stackdriver Monitoring.
package main

import (

	// Imports the Stackdriver Monitoring client package.
	monitoring ""
	googlepb ""
	metricpb ""
	monitoredrespb ""
	monitoringpb ""

func main() {
	ctx := context.Background()

	// Creates a client.
	client, err := monitoring.NewMetricClient(ctx)
	if err != nil {
		log.Fatalf("Failed to create client: %v", err)

	// Sets your Google Cloud Platform project ID.
	projectID := "YOUR_PROJECT_ID"

	// Prepares an individual data point
	dataPoint := &monitoringpb.Point{
		Interval: &monitoringpb.TimeInterval{
			EndTime: &googlepb.Timestamp{
				Seconds: time.Now().Unix(),
		Value: &monitoringpb.TypedValue{
			Value: &monitoringpb.TypedValue_DoubleValue{
				DoubleValue: 123.45,

	// Writes time series data.
	if err := client.CreateTimeSeries(ctx, &monitoringpb.CreateTimeSeriesRequest{
		Name: monitoring.MetricProjectPath(projectID),
		TimeSeries: []*monitoringpb.TimeSeries{
				Metric: &metricpb.Metric{
					Type: "",
					Labels: map[string]string{
						"store_id": "Pittsburg",
				Resource: &monitoredrespb.MonitoredResource{
					Type: "global",
					Labels: map[string]string{
						"project_id": projectID,
				Points: []*monitoringpb.Point{
	}); err != nil {
		log.Fatalf("Failed to write time series data: %v", err)

	fmt.Printf("Done writing time series data.\n")
Read more in the Go API Reference Documentation for the Stackdriver Monitoring API Client Library.



// Imports the Google Cloud client library


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class QuickstartSample {
  public static void main(String... args) throws Exception {
    // Your Google Cloud Platform project ID
    String projectId = System.getProperty("projectId");

    if (projectId == null) {
      System.err.println("Usage: QuickstartSample -DprojectId=YOUR_PROJECT_ID");

    // Instantiates a client
    MetricServiceClient metricServiceClient = MetricServiceClient.create();

    // Prepares an individual data point
    TimeInterval interval = TimeInterval.newBuilder()
    TypedValue value = TypedValue.newBuilder()
    Point point = Point.newBuilder()

    List<Point> pointList = new ArrayList<>();

    ProjectName name = ProjectName.create(projectId);

    // Prepares the metric descriptor
    Map<String, String> metricLabels = new HashMap<String, String>();
    metricLabels.put("store_id", "Pittsburg");
    Metric metric = Metric.newBuilder()

    // Prepares the monitored resource descriptor
    Map<String, String> resourceLabels = new HashMap<String, String>();
    resourceLabels.put("project_id", projectId);
    MonitoredResource resource = MonitoredResource.newBuilder()

    // Prepares the time series request
    TimeSeries timeSeries = TimeSeries.newBuilder()
    List<TimeSeries> timeSeriesList = new ArrayList<>();

    CreateTimeSeriesRequest request = CreateTimeSeriesRequest.newBuilder()

    // Writes time series data

    System.out.printf("Done writing time series data.%n");

Read more in the Java API Reference Documentation for the Stackdriver Monitoring API Client Library.


// Imports the Google Cloud client library
const Monitoring = require('@google-cloud/monitoring');

// Your Google Cloud Platform project ID
const projectId = 'YOUR_PROJECT_ID';

// Instantiates a client
const client = Monitoring.v3().metricServiceClient();

// Prepares an individual data point
const dataPoint = {
  interval: {
    endTime: {
      seconds: / 1000
  value: {
    // The amount of sales
    doubleValue: 123.45

// Prepares the time series request
const request = {
  name: client.projectPath(projectId),
  timeSeries: [
      // Ties the data point to a custom metric
      metric: {
        type: '',
        labels: {
          store_id: 'Pittsburgh'
      resource: {
        type: 'global',
        labels: {
          project_id: projectId
      points: [

// Writes time series data
  .then((results) => {
    console.log(`Done writing time series data.`);
Read more in the Node.js API Reference Documentation for the Stackdriver Monitoring API Client Library.


from import monitoring

client = monitoring.Client()

resource = client.resource(
        'instance_id': '1234567890123456789',
        'zone': 'us-central1-f',

metric = client.metric(

# Default arguments use endtime datetime.utcnow()
client.write_point(metric, resource, 3.14)
print('Successfully wrote time series.')
Read more in the Python API Reference Documentation for the Stackdriver Monitoring API Client Library.

Additional Resources

Send feedback about...

Stackdriver Monitoring