When visiting console.cloud.google.com, I can access the following information for a particular API:
- Requests
- Errors
- Latency
- and more
by:
- Select the project running the API I want to know more about
- Clicking on "APIs & Services" in the "Navigation Menu"
- Clicking on "Enabled APIs & services"
On the "Enabled APIs & services" page, it shows which APIs I am using and the values I listed above. I can also select a particular API on that page to see a more detailed view with graphs and other information.
I would like to
- Extract the above information, such as requests, latency, and errors
- Extract the Methods for an API and their Requests, Errors, Latency
I would like to obtain this information over an API using python
. How can I do this?
Also, is it possible to:
- Embed the graphs show on the "API/Service Details" page for a particular API using IFrames or something similar in another website?
CodePudding user response:
The commenters are correct and you should (!) provide at least a preliminary attempt at a solution in code.
However, I think your question is asked in good faith and it isn't obvious how the metrics are generated for the APIs metrics pages.
The answer is that the pages use
Code is also filtered by method (resource.labels.method = "compute.v1.ZonesService.List"
) just so few entries will be returned. It is up to you if you would like to filter using methods. See serviceruntime metric documentation for more metrics (errors, latency, etc.) and filtering guide for more details.
from google.cloud import monitoring_v3
import time
client = monitoring_v3.MetricServiceClient()
project_name = f"projects/tiph-ricconoel-batch8"
now = time.time()
seconds = int(now)
nanos = int((now - seconds) * 10 ** 9)
interval = monitoring_v3.TimeInterval(
{
"end_time": {"seconds": seconds, "nanos": nanos},
"start_time": {"seconds": (seconds - 1200), "nanos": nanos},
}
)
results = client.list_time_series(
request={
"name": project_name,
"filter": 'metric.type = "serviceruntime.googleapis.com/api/request_count" AND resource.labels.method = "compute.v1.ZonesService.List"',
"interval": interval,
"view": monitoring_v3.ListTimeSeriesRequest.TimeSeriesView.FULL,
}
)
print(results)