使用Google Cloud Monitoring API监控Geocoding API请求数异常排查
问题描述
我尝试使用Google Cloud Monitoring API监控Geocoding API的请求量,但返回结果显示请求数为0。此前已发起约2000次请求,且该数据在Google Cloud Web端Metrics中可正常查看,但API调用无法获取正确数据。我推测问题出在metric.type参数,但无法确定正确的指标类型。
以下是我的代码实现:
import datetime from google.oauth2 import service_account from googleapiclient.discovery import build # 加载凭据 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'] ) # 创建Monitoring API客户端 service = build('monitoring', 'v3', credentials=credentials) # 定义时间范围(最近2天) end_time = datetime.datetime.now(datetime.UTC) start_time = end_time - datetime.timedelta(days=2) project_id = 'projects/project_name' # 查询使用情况 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() # 计算总请求数 total_requests = 0 for series in response.get('timeSeries', []): for point in series.get('points', []): total_requests += point['value']['int64Value'] print(f"本月总请求数: {total_requests}") # 验证是否超过40000次免费请求限制 if total_requests >= 40000: print("⚠️ 已达到Geocoding API的40000次免费请求上限。") else: print(f"✅ 可继续使用API。剩余请求数: {40000 - total_requests}")
可查看各API请求统计表:
解决方案
问题核心是指标类型与Geocoding API不匹配,serviceruntime.googleapis.com/api/request_count适用于常规GCP服务,而Google Maps类API有专属的监控指标。
修正步骤
- 替换
metric.type为maps.googleapis.com/geocoding/requests,这是Geocoding API专属的请求量统计指标 - 调整资源过滤条件为
resource.type="global",Maps APIs的监控资源类型为全局资源
修正后的代码
import datetime from google.oauth2 import service_account from googleapiclient.discovery import build # 加载凭据 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'] ) # 创建Monitoring API客户端 service = build('monitoring', 'v3', credentials=credentials) # 定义时间范围(最近2天) end_time = datetime.datetime.now(datetime.UTC) start_time = end_time - datetime.timedelta(days=2) project_id = 'projects/project_name' # 查询使用情况 - 使用正确的Maps Geocoding指标 request = service.projects().timeSeries().list( name=project_id, filter=( 'metric.type="maps.googleapis.com/geocoding/requests" ' 'AND resource.type="global"' ), interval_startTime=start_time.isoformat(), interval_endTime=end_time.isoformat(), view='FULL' ) response = request.execute() # 计算总请求数 total_requests = 0 for series in response.get('timeSeries', []): for point in series.get('points', []): # 注意:该指标的数值类型为doubleValue,需转换为整数 total_requests += int(point['value']['doubleValue']) print(f"最近2天总请求数: {total_requests}") # 验证是否超过40000次免费请求限制 if total_requests >= 40000: print("⚠️ 已达到Geocoding API的40000次免费请求上限。") else: print(f"✅ 可继续使用API。剩余请求数: {40000 - total_requests}")
额外说明
- 若需细分请求类型(如正向/反向地理编码),可在过滤条件中添加
metric.labels.method="geocode"或metric.labels.method="reverse_geocode" - 确保所用服务账号已被授予
Monitoring Viewer或更高权限,否则会无法读取监控数据
内容的提问来源于stack exchange,提问作者newDev
相关产品推荐
相关产品推荐

