Reputation: 1
I am trying to use the Google Cloud API to monitor my requests made to the Geocoding API but it gives me 0 requests made even though a made like 2000 requests previusly and it is shown in the Google Cloud Metrics on the Web. I belive the problem is the metric.type but i just can't find which of the metrics are. Requests table of every api used
import datetime
from google.oauth2 import service_account
from googleapiclient.discovery import build
# Cargar credenciales
SERVICE_ACCOUNT_FILE = 'tu_clave.json'
credentials = service_account.Credentials.from_service_account_file(
SERVICE_ACCOUNT_FILE,
scopes=['https://www.googleapis.com/auth/monitoring.read']
)
# Crear cliente para la API de Monitoring
service = build('monitoring', 'v3', credentials=credentials)
# Definir el rango de tiempo (mes actual)
end_time = datetime.datetime.now(datetime.UTC)
start_time = end_time - datetime.timedelta(days=2)
project_id = 'projects/project_name'
# Realizar la consulta de uso
request = service.projects().timeSeries().list(
name=project_id,
filter=(
'metric.type="serviceruntime.googleapis.com/api/request_count" '
'AND resource.labels.service="geocoding.googleapis.com"'
),
interval_startTime=start_time.isoformat(),
interval_endTime=end_time.isoformat() ,
view='FULL'
)
response = request.execute()
# Calcular total de peticiones
total_requests = 0
for series in response.get('timeSeries', []):
for point in series.get('points', []):
total_requests += point['value']['int64Value']
print(f"Total de peticiones este mes: {total_requests}")
# Validar si se superaron las 40,000 peticiones gratuitas
if total_requests >= 40000:
print("⚠️ Has alcanzado el límite de 40,000 peticiones gratuitas de Geocoding.")
else:
print(f"✅ Puedes seguir usando la API. Restantes: {40000 - total_requests}")``
Upvotes: 0
Views: 29