JSON 数据作为一种通用类型的数据类型, 其自解析灵活的特性, 使其能够很好满足复杂场景下数据的记录需求, 在很多日志内容中格式不固定的部分往往都是以 json 的形式进行记录, 如将一次 http 请求的 request 参数和 response 内容以 json 的形式记录在一条日志中
为了更好支持 json 格式日志的查询和分析功能, 日志服务加入了对 json 类型格式的支持
核心功能 :
支持 json 格式解析, 所有 textbool 类型自动索引
- json_string.key_map.key_text : test_value
- json_string.key_map.key_bool : true
非 json array 中的 doublelong 类型数据, 可通过配置指定 json 路径后进行查询
配置 key_map.key_long 这个字段的类型为 long
查询 : json_string.key_map.key_long > 50
非 json array 中的 textdoublelong 类型字段, 可开启统计分析功能, 进行 sql 分析
- json_string.key_map.key_long > 10 | select count(*) as c ,
- "json_string.key_map.key_text" group by
- "json_string.key_map.key_text"
使用限制:
不支持 json objectjson array 类型
字段不能在 json array 中
bool 类型字段可以转成 text 类型
支持非完全合法 json 数据解析
日志服务会尽可能解析有效内容, 直到遇到非法部分结束, 如
- "json_string":
- {
- "key_1" : "value_1",
- "key_map" :
- {
- "key_2" : "value_2",
- "key_3" : "valu
在 key_3 之后的数据被截断丢失, 对于这种缺失的日志, 日志服务可正确解析到 json_string.key_map.key_2 这个字段
使用参考
对于以下日志样例, 其 "message" 字段是 json 格式
- "message": {
- "traceInfo": {
- "traceType": "dubbo_provider",
- "stepIndex": "0",
- "requestId": "92.137_1518139699935_5599"
- },
- "methodName": "getProjectInfo",
- "success": true,
- "remoteAddress": "1.1.1.1:11111",
- "param": [{
- "projectName": "ali-log-test-project",
- "requestId": "d3f0c96a-51b0-4166-a850-f4175dde7323"
- }],
- "localAddress": "2.2.2.2:22222",
- "errorCode": null,
- "serviceName": "com.aliyun.csc.sls.service.IProjectService",
- "usedTime": 48,
- "exceptionMsg": null,
- "startTime": 1518139699935,
- "result": {
- "message": "successful",
- "code": "200",
- "data": {
- "clusterRegion": "ap-southeast-1",
- "ProjectName": "ali-log-test-project",
- "ProjectDesc": "","ProjectOwner":"11111111111","LastModifyTime":"2017-06-08 20:22:41","ProjectStatus":"Normal","CreateTime":"2017-06-08 20:22:41"},"success": true
- }
- }
通过以下设置, 可以对该 json 字段进行查询和分析:
stringbool 类型数据查询
- message.traceInfo.requestId : 92.137_1518139699935_5599
- message.param.projectName : ali-log-test-project
- message.success : true
- message.result.data.ProjectStatus : Normal
注:
json 内字段无需配置
json maparray 自动展开, 支持多层嵌套, 每一层以 "." 进行分割
longdouble 类型数据查询
message.usedTime > 40
注:
需要对 json 内字段独立配置, 字段必须不在 array
Sql 统计分析
- * |select avg("message.usedTime") as avg_time,
- "message.methodName"group by "message.methodName"
注:
需要对 json 内字段独立配置, 字段必须不在 array
查询字段需要使用引号 或者设置 别名
来源: https://yq.aliyun.com/articles/459590