Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Google cloud Metrics

I'm new to google cloud, I want the metrics for each instance I have like cpu usage and network sent and received, I have this code

from typing import Iterable

from google.cloud import compute_v1

def list_instances(project_id: str, zone: str) -> Iterable[compute_v1.Instance]:
"""
List all instances in the given zone in the specified project.

Args:
project_id: project ID or project number of the Cloud project you want to use.
zone: name of the zone you want to use. For example: “us-west3-b”
Returns:
An iterable collection of Instance objects.
"""
instance_client = compute_v1.InstancesClient()
instance_list = instance_client.list(project=project_id, zone=zone)

return instance_list
and this, from https://cloud.google.com/monitoring/docs/reference/libraries?hl=pt-br#client-libraries-usage-python

from google.cloud import monitoring

client = monitoring.Client()

resource = client.resource(
type_='gce_instance',
labels={
'instance_id': '0000000000000000',
'zone': 'europe-west1-b',
}
)

metric = client.metric(
type_='custom.googleapis.com/my_metric',
labels={}
)

# Default arguments use endtime datetime.utcnow()
client.write_point(metric, resource, 3.14)
print('Successfully wrote time series.')

but when I do this, I get

module 'google.cloud.monitoring' has no attribute 'Client'
So google docs are not the best how can I access metrics from the list of instances I have?

0 1 976
1 REPLY 1

Hi @ls-1000000 ,

This error sometimes happens to other google.cloud modules. One cause of this is a broken installation of the library.

Try to install the google-cloud-monitoring library again, hope this will fix the issue.