You need to enable JavaScript to run this app. Python2.7 Google Cloud Shell调用Monitoring API报AttributeError
优惠活动
大模型
产品
解决方案
定价
更多

Python2.7 Google Cloud Shell调用Monitoring API报AttributeError

问题场景
  • 运行环境:Google Cloud Shell
  • Python版本:2.7
  • 前置操作:执行 pip install --upgrade google-api-python-client 完成 google-api-python-client 依赖升级安装
  • 操作目标:通过Python命令行调用Google Monitoring API获取监控时序数据
  • 报错触发位置:执行 service = build('monitoring', 'v3', cache_discovery=True) 初始化监控API服务客户端时

待运行业务代码

from googleapiclient.discovery import build

service = build('monitoring', 'v3', cache_discovery=True)
timeseries = service.projects().timeSeries().list(
            name="my-project",
            filter="metric.type=compute.googleapis.com/instance/cpu/utilization"
            aggregation_alignmentPeriod=86400s,
            # aggregation_crossSeriesReducer=api_args["crossSeriesReducer"],
            aggregation_perSeriesAligner="ALIGN_MEAN",
            aggregation_groupByFields="metric.labels.key",
            interval_endTime="2020-08-23T17:30:04.707Z",
            interval_startTime="2020-08-22T17:30:05.603000Z",
            pageSize=100
        ).execute()

完整运行报错栈

>>>from googleapiclient.discovery import build
>>> service = build('monitoring', 'v3', cache_discovery=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mugham/.local/lib/python2.7/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/mugham/.local/lib/python2.7/site-packages/googleapiclient/discovery.py", line 282, in build
    adc_key_path=adc_key_path,
  File "/home/mugham/.local/lib/python2.7/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/mugham/.local/lib/python2.7/site-packages/googleapiclient/discovery.py", line 489, in build_from_document
    credentials = _auth.default_credentials()
  File "/home/mugham/.local/lib/python2.7/site-packages/googleapiclient/_auth.py", line 44, in default_credentials
    credentials, _ = google.auth.default()
  File "/usr/local/lib/python2.7/dist-packages/google/auth/_default.py", line 338, in default
    credentials, project_id = checker()
  File "/usr/local/lib/python2.7/dist-packages/google/auth/_default.py", line 208, in _get_gae_credentials
    project_id = app_engine.get_project_id()
  File "/usr/local/lib/python2.7/dist-packages/google/auth/app_engine.py", line 77, in get_project_id
    return app_identity.get_application_id()
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/api/app_identity/app_identity.py", line 455, in get_application_id
    _, domain_name, display_app_id = _ParseFullAppId(full_app_id)
  File "/usr/lib/google-cloud-sdk/platform/google_appengine/google/appengine/api/app_identity/app_identity.py", line 436, in _ParseFullAppId
    psep = app_id.find(_PARTITION_SEPARATOR)
AttributeError: 'NoneType' object has no attribute 'find'

报错根因

  • 核心触发原因:google-auth 库的默认凭证探测逻辑,错误识别到Cloud Shell环境中挂载的App Engine SDK路径,强制走App Engine托管环境的凭证获取流程。但Cloud Shell不属于App Engine运行时环境,无法从App Identity服务拿到有效的项目ID,最终返回None值,调用字符串的find方法时触发空类型错误。
  • 代码层附带问题:原业务代码存在两处语法缺陷,即使凭证问题解决后也无法正常执行:
    • filter="metric.type=compute.googleapis.com/instance/cpu/utilization" 行尾缺少逗号分隔参数
    • aggregation_alignmentPeriod=86400s 数值格式错误,Google API要求该参数为字符串格式的时长值,不能直接传入未加引号的字面量

解决步骤

    1. 禁用错误的GAE凭证探测路径
      启动Python解释器前,在终端执行环境变量配置,跳过App Engine metadata服务的探测逻辑:
    export GOOGLE_GAE_METADATA_SERVER_ROOT=http://localhost:0
    
    1. 配置本地应用默认凭证
      在Cloud Shell终端执行登录授权命令,确保当前账号的凭证已被API客户端识别:
    gcloud auth application-default login
    
    按照终端指引完成授权流程即可。
    1. 修正业务代码语法与参数格式
      调整后的可参考代码:
    from googleapiclient.discovery import build
    
    service = build('monitoring', 'v3', cache_discovery=False)
    timeseries = service.projects().timeSeries().list(
                name="projects/my-project",
                filter="metric.type=compute.googleapis.com/instance/cpu/utilization",
                aggregation_alignmentPeriod="86400s",
                aggregation_perSeriesAligner="ALIGN_MEAN",
                aggregation_groupByFields="metric.labels.key",
                interval_endTime="2020-08-23T17:30:04.707Z",
                interval_startTime="2020-08-22T17:30:05.603000Z",
                pageSize=100
            ).execute()
    
    调整说明:
    • 补上filter参数行尾的逗号
    • 将 aggregation_alignmentPeriod 的值改为字符串格式的 "86400s"
    • 将name参数调整为Google API要求的规范资源路径格式 projects/<你的项目ID>
    • 将 cache_discovery 参数设为False,避免Python2环境下的接口文档缓存写入权限问题

内容的提问来源于stack exchange,提问作者user1403505

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.29 17:06:11