Home > OS >  API for accessing API/Service Details for number of requests, errors, latency, and more?
API for accessing API/Service Details for number of requests, errors, latency, and more?

Time:07-12

When visiting console.cloud.google.com, I can access the following information for a particular API:

  • Requests
  • Errors
  • Latency
  • and more

by:

  1. Select the project running the API I want to know more about
  2. Clicking on "APIs & Services" in the "Navigation Menu"
  3. 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

  1. Extract the above information, such as requests, latency, and errors
  2. 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 enter image description here

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)
  • Related