-1

I want to log all the log-based metrics I have in my gcp project with a cloud function in python. I'm trying to run the following code sample:

for metric in client.list_metrics():  # API call(s)
    #do_something_with(metric)
    print(metric)

I have the following log output:

"<google.cloud.logging_v2.metric.Metric object at 0x3e9bfe2fb4c0>"

How do I read this? I tried list(), json.loads(), json.dumps() but nothing seems to work.

Simon Breton
  • 2,638
  • 7
  • 50
  • 105
  • 1
    I just noticed this part of your question --> "I get down voted on this question and I don't understand why.". I'm not one of the downvoters so I'm not sure on their reasoning but I think it's because your question is about the fundamentals of Python in general. What objects are and how you should handle them is explained in every basic course. Combined with the fact that it can be easily googled I think it might be the reason for downvotes. – JustLudo Sep 15 '21 at 07:06

1 Answers1

3

You are getting back a python object:

"<google.cloud.logging_v2.metric.Metric object at 0x3e9bfe2fb4c0>"

According to this documentation it has some properties.

So something like

print(metric.name)

should work for you.

JustLudo
  • 1,690
  • 12
  • 29
  • Is there a way to explore an object like this without having access to the documentation? Or do you need to have access to the doc? How can I know the key to get the global object for example? If I want to print the entire list directly? – Simon Breton Sep 13 '21 at 07:49
  • Sorry adding another comment; `metric.name` is working but not `metric.filter`... – Simon Breton Sep 13 '21 at 07:52
  • You can always use print(dir(metric)) but that will not always be fully explained. Most of the time I would just trust documentation though, especially with suppliers the size of google. – JustLudo Sep 13 '21 at 12:51
  • As a sidenote: It's imho always better in these kind of scenario's to debug and step through code where possible. Dir will get you far but does not for example make distinction between properties and methods, which you have to figure out for yourself. – JustLudo Sep 13 '21 at 12:53