Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JSON度量过滤器从Lambda筛选CloudWatch日志_Json_Aws Lambda_Amazon Cloudwatch_Amazon Cloudwatch Metrics - Fatal编程技术网

如何使用JSON度量过滤器从Lambda筛选CloudWatch日志

如何使用JSON度量过滤器从Lambda筛选CloudWatch日志,json,aws-lambda,amazon-cloudwatch,amazon-cloudwatch-metrics,Json,Aws Lambda,Amazon Cloudwatch,Amazon Cloudwatch Metrics,使用直接来自的示例,在lambda函数中,我将: console.log( { "eventType": "UpdateTrail", "sourceIPAddress": "111.111.111.111", "arrayKey": [ "value", "another value" ], "objectList"

使用直接来自的示例,在lambda函数中,我将:

console.log(
        {
          "eventType": "UpdateTrail",
          "sourceIPAddress": "111.111.111.111",
          "arrayKey": [
                "value",
                "another value"
          ],
          "objectList": [
               {
                 "name": "a",
                 "id": 1
               },
               {
                 "name": "b",
                 "id": 2
               }
          ],
          "SomeObject": null,
          "ThisFlag": true
        }) 
然后,我在CloudWatch中创建了一个日志度量过滤器,其过滤器模式如文档示例中所述:

{ $.eventType = "UpdateTrail" }
过滤器不会像文档中所说的那样生成度量-以下是输出:

2017-10-23T13:27:19.320Z    1143e2b0-eea6-4225-88c0-efcd79055f7b    { eventType: 'UpdateTrail',
sourceIPAddress: '111.111.111.111',
arrayKey: [ 'value', 'another value' ],
objectList: [ { name: 'a', id: 1 }, { name: 'b', id: 2 } ],
SomeObject: null,
ThisFlag: true }
如您所见,时间戳和标识符在JSON前面


中的一个答案说,这是因为Lambda将日志转换为字符串。说得差不多。在这两种情况下都没有提供解决方案。如何使用JSON度量过滤器从Lambda过滤CloudWatch日志

看看日志行实际上是什么样子。如果您看到类似的内容,则它不是有效的json:

{ eventType: 'UpdateTrail', ... }
您想要的是这样的东西(请注意报价):

要实现这一点,请尝试将对象包装在
JSON.stringify()
中,如下所示:

console.log(
        JSON.stringify(
            {
              "eventType": "UpdateTrail",
              "sourceIPAddress": "111.111.111.111",
              "arrayKey": [
                    "value",
                    "another value"
              ],
              "objectList": [
                   {
                     "name": "a",
                     "id": 1
                   },
                   {
                     "name": "b",
                     "id": 2
                   }
              ],
              "SomeObject": null,
              "ThisFlag": true
            }
        )
    ) 

就这样。我在问题描述中添加了无效的JSON日志输出。我没有意识到它是无效的,需要严格化。早就应该试一下了。谢谢没有stringify的JSON是有效的吗?你知道为什么会有区别吗?但很明显,过滤器并没有生成事件。这个问题已经存在了将近2年,可能是CloudWatch日志或Lambda在此期间发生了变化。
console.log(
        JSON.stringify(
            {
              "eventType": "UpdateTrail",
              "sourceIPAddress": "111.111.111.111",
              "arrayKey": [
                    "value",
                    "another value"
              ],
              "objectList": [
                   {
                     "name": "a",
                     "id": 1
                   },
                   {
                     "name": "b",
                     "id": 2
                   }
              ],
              "SomeObject": null,
              "ThisFlag": true
            }
        )
    )