按 UserAgent/操作系统分析 Google App Engine 流量

按 UserAgent/操作系统分析 Google App Engine 流量

我有一个在 Google App Engine 中运行的应用程序。他们的生态系统似乎提供了一些用于分析 StackDriver 日志的功能。

我终其一生都无法弄清楚如何获取按用户代理或操作系统细分的 HTTP 请求的简单图表。

我该如何配置这样的图表?

答案1

目前这是不可能的,如下表所示“指标种类”文档的部分,其中说您不能创建具有值类型 STRING 的自定义指标。

我已经通过使用 create_metric_descriptor 函数确认了这一点对应的repo文件并编辑其主体内容:

descriptor.value_type = (monitoring_v3.enums.MetricDescriptor.ValueType.STRING)  

并收到此错误:

InvalidArgument: 400 Field metricDescriptor.valueType had an invalid value of "STRING": When creating metric custom.googleapis.com/my_metric_6365: the value type is not supported for custom metrics.

无论如何,您仍然可以从 Stackdriver Logging 获取有价值的信息,并将其传输到您选择的任何数据科学和可视化方法。您可以尽可能从 protoPayload.userAgent 字段中提取有关操作系统的信息。

只需尝试使用 Cloud SDK,例如,如下所示:

$ project_id= #FILL
$ module_id=default #EDIT
$ version_id= #FILL
$ gcloud logging read "resource.type=gae_app AND resource.labels.module_id=$module_id AND resource.labels.version_id=$version_id AND logName=(projects/$project_id/logs/stderr OR projects/$project_id/logs/appengine.googleapis.com%2Frequest_log)" --format "json(receiveTimestamp, protoPayload.userAgent, protoPayload.ip, protoPayload.resource)"

您将获得如下条目:

[
   {
            "protoPayload": {
                    "ip": "....",
                    "resource": "/",
                    "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"
            },
            "receiveTimestamp": "2019-09-19T15:39:42.236439758Z"
   },
   {
            "protoPayload": {
                    "ip": "...",
                    "resource": "/",
                    "userAgent": "Mozilla/5.0 (Linux; Android 8.0.0; SM-G930F Build/R16NW; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/74.0.3729.157 Mobile Safari/537.36"
            },
            "receiveTimestamp": "2019-09-19T15:39:41.153786772Z"
    },
    {
            "protoPayload": {
                    "ip": "...",
                    "resource": "/",
                    "userAgent": "Mozilla/5.0 (Windows NT 5.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586"
            },
            "receiveTimestamp": "2019-09-19T15:39:33.286861169Z"
   },
   {
            "protoPayload": {
                    "ip": "...",
                    "resource": "/",
                    "userAgent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:21.0) Gecko/20130331 Firefox/21.0"
            },
            "receiveTimestamp": "2019-09-19T15:39:31.259811179Z"
    },
    {
            "protoPayload": {
                    "ip": "...",
                    "resource": "/",
                    "userAgent": "curl/7.66.0"
            },
            "receiveTimestamp": "2019-09-19T15:39:01.134762065Z"
    },
    {
            "protoPayload": {
                    "ip": "...",
                    "resource": "/",
                    "userAgent": "Wget/1.17 (linux-gnu)"
            },
            "receiveTimestamp": "2019-09-19T15:38:54.488718412Z"
    }
]

相关内容