【问题标题】:Setting up Stackdriver Error Reporting to show detailed information设置 Stackdriver 错误报告以显示详细信息
【发布时间】:2026-02-22 04:10:01
【问题描述】:

我有一个基本设置工作,我从 GKE 发送日志并根据 Stackdriver 错误报告中的规则对其进行格式化。

我可以看到堆栈跟踪,但缺少用户、版本等附加信息。这就是我所看到的: error reporting detail view

当我点击显示日志时,我可以看到格式化的日志条目,在我看来它的格式是正确的...示例中 Stackdriver 日志记录中的日志条目如下所示:

{
insertId: "vd0zy9g5mw3iof"
jsonPayload: {
    message: {
        context: {
            reportLocation: {
                functionName: "error_router"
                filePath: "/usr/local/lib/python2.7/site-packages/flask_restplus/api.py"
                lineNumber: 554
            }
            httpRequest: {
            method: "POST"
            remoteIp: "213.47.170.171"
            referrer: "https://api-staging.smaxtec.com/api/v1/"
            userAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36"
            url: "https://api-staging.smaxtec.com/api/v1/device/update_name"
            responseStatusCode: 404
            }
            user: "569e0bcda80a5f1c07b542be"
        }
        message: "Traceback (most recent call last):
        File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
            rv = self.dispatch_request()
        File "/usr/local/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request
            return self.view_functions[rule.endpoint](**req.view_args)
        File "/usr/local/lib/python2.7/site-packages/flask_restplus/api.py", line 313, in wrapper
            resp = resource(*args, **kwargs)
        File "/usr/local/lib/python2.7/site-packages/flask/views.py", line 84, in view
            return self.dispatch_request(*args, **kwargs)
        File "/usr/local/lib/python2.7/site-packages/flask_restplus/resource.py", line 44, in dispatch_request
            resp = meth(*args, **kwargs)
        File "/usr/local/lib/python2.7/site-packages/webargs/core.py", line 445, in wrapper
            return func(*new_args, **kwargs)
        File "/app/anthilldata/core/restapi/namespace.py", line 154, in dump_wrapper
            response = func(*args, **kwargs)
        File "/app/anthilldata/api/publicv1/base.py", line 144, in decorated
            return f(*args, auth=auth, **kwargs)
        File "/app/anthilldata/api/publicv1/device.py", line 98, in post
            dev = devices.getById(device_id)
        File "/app/anthilldata/devices/__init__.py", line 102, in getById
            device = self._getById(id)
        File "/app/anthilldata/core/__init__.py", line 171, in _getById
            self.__model__.__name__, id))
        NotFoundError: Device(strisdfsdfsdfsdfsdfsdfng) not found
        "
        serviceContext: {
            version: "v1.20-25-gf94b2b7"
            service: "anthilldata"
        }
        }
        thread: 140663840671488
}
resource: {
    type: "container"
    labels: {
    pod_id: "api-staging-deployment-3325000162-npq9r"
    zone: "europe-west1-d"
    project_id: "smaxtec-system"
    cluster_name: "kuhbernetes1"
    container_name: "anthilldata"
    namespace_id: "default"
    instance_id: "4882784730069069317"
    }
}
timestamp: "2017-04-07T08:06:30.247392892Z"
severity: "ERROR"
labels: {
    container.googleapis.com/container_name: "anthilldata"
    compute.googleapis.com/resource_name: "fluentd-cloud-logging-gke-kuhbernetes1-default-pool-b875e508-7x"
    container.googleapis.com/instance_id: "4882784730069069317"
    container.googleapis.com/pod_name: "api-staging-deployment-3325000162-npq9r"
    container.googleapis.com/stream: "stderr"
    container.googleapis.com/namespace_name: "default"
    compute.googleapis.com/resource_type: "instance"
    compute.googleapis.com/resource_id: "4882784730069069317"
    container.googleapis.com/cluster_name: "kuhbernetes1"
}
logName: "projects/smaxtec-system/logs/anthilldata"
}

用户、httpContext 和其他上下文属性都在 json 日志中。我缺少什么来获得example 中显示的结果,您可以在其中看到受影响的用户和 http 请求?

【问题讨论】:

    标签: stackdriver google-compute-engine google-cloud-error-reporting


    【解决方案1】:

    您示例中的条目有效负载具有如下结构

    jsonPayload: {
        message: {
            context: {...},
            message: "...",
            serviceContext: {...}
        }
    }
    

    中间的message 结构会导致问题。你想要类似的东西

    jsonPayload: {
        context: {...},
        message: "...",
        serviceContext: {...}
    }
    

    错误报告仍然能够解析出异常消息,但需要特定 JSON 路径中的其他字段。

    我尝试向 Stackdriver Logging 写入两条消息,有无额外的 message 嵌套。两者都出现在错误报告中,但样本中只有一个没有包含服务、版本、用户等。

    【讨论】:

    • 非常感谢,没看到就傻眼了。彻底解决了我的问题。
    最近更新 更多