原创

阿里云日志平台的使用

1、相关文档

2、高频使用的场景

关键参数说明

arbitrary(x) 返回x中任意一个非空的值。

count(*) 统计所有的日志条数。

count(x) 统计x中值不为NULL的日志条数。

count_if(boolean expression) 统计满足指定条件的日志条数。

date(x) 返回日期和时间表达式中的日期部分。

date_format(x, format) 将timestamp类型的日期和时间表达式转化为指定格式的日期和时间表达式。

date_trunc(unit, x) 根据您指定的时间单位截断日期和时间表达式,并按照毫秒(millisecond)、秒(second)、分钟(minute),小时(hour)、日(day)、月(month)或年(year)对齐。

avg(x) 计算x的算术平均值。

ceil(x)或 ceiling(x) 对x进行向上取整数。

floor(x) 对x进行向下取整数。

round(x) 对x进行四舍五入取整数。

round(x, n) 对x进行四舍五入且保留n位小数。

truncate(x) 截断x的小数部分。

max(x) 查询x中的最大值。

max(x, n) 查询x中最大的n个值。返回结果为数组。

max_by(x, y) 查询y为最大值时对应的x值。

max_by(x, y, n) 查询最大的n个y值对应的x值,返回结果为数组。

min(x) 查询x中的最小值。

min(x, n) 查询x中最小的n个值。返回结果为数组。

min_by(x, y) 查询y为最小值时对应的x值。

min_by(x, y, n) 查询最小的n个y值对应的x值,返回结果为数组。

arbitrary(x) 返回x中任意一个非空的值。

json_extract_scalar(x, json_path) 从JSON对象或JSON数组中提取一组标量值(字符串、整数或布尔值)。类似于json_extract函数。

json_array_length(x) 计算JSON数组中元素的数量。

json_array_contains(x, value) 判断JSON数组中是否包含某个值。

json_size(x, json_path) 计算JSON对象或数组中元素的数量。

histogram(x) 对查询和分析结果进行分组,返回结果为JSON格式。

histogram_u(x) 对查询和分析结果进行分组,返回结果为多行多列格式。

cardinality(x) 计算Map的大小。

element_at(x, key) 获取Map中目标键的值, 当不存在目标键时, 返回null

cast(x as type) 转换x的数据类型。使用cast函数转换数据类型时,如果某个值转换失败,将终止整个查询与分析操作。

try_cast(x as type) 转换x的数据类型。 使用try_cast函数转换数据类型时,如果某个值转换失败,该值返回NULL,并跳过该值继续处理。

type 可选值为bigint、varchar、double、boolean、timestamp、decimal、array或map, 例如cast(json_parse(key) as array(varchar))。

typeof(x) 返回x的数据类型。

dense_rank() 窗口分区内值的排名。相同值拥有相同的排名,排名是连续的,例如有两个相同值的排名为1,则下一个值的排名为2。

rank() 窗口分区内值的排名。相同值拥有相同的排名,排名不是连续的,例如有两个相同值的排名为1,则下一个值的排名为3。

row_number() 窗口分区内值的排名。每个值拥有唯一的序号,从1开始。三个相同值的排名为1、2、3。

regexp_like(x, regular expression) 判断目标字符串是否符合正则表达式。

2.1、返回异常调用数和traceId

serviceId:footstone-doc and not status: 200 | select api,status,count(*) sl, arbitrary(traceId) as traceId from log group by api,status order by sl desc

2.2、查看指定范围内服务的api调用排行

serviceId: footstone-doc or serviceId: footstone-seal or serviceId: footstone-doc or serviceId:footstone-flow | SELECT api, COUNT(*) as count, arbitrary(serviceId) as serviceId GROUP BY api order by count desc limit 1000

2.3、查看指定接口的每日调用量

serviceId:footstone-doc AND status:200 and (api: '/v2/processes/start' NOT api: '/v2/processes/start/initConfig') | select api, date_format(date_trunc('day', timestamp/1000), '%Y-%m-%d') as date_time, count(*) as date_count group by api, date_time order by date_time desc limit 100

2.4、查询接口平均响应时间超过阈值的


# 统计每个接口的平均耗时,并按耗时时间倒序排列, 取前20条记录
serviceId:footstone-doc AND status:200 | select api, round(avg(duration), 0) as avg_duration GROUP by api HAVING avg_duration > 1000 order by avg_duration desc limit 20

2.5、查询接口的响应时长范围

# 查询每个接口的最大耗时及最小耗时, 并返回对应的traceId
serviceId:footstone-doc AND status:200 | select api, max(duration) as max_duration, max_by(traceId, duration) as max_traceId, min(duration) as min_duration, min_by(traceId, duration) as min_traceId where json_extract_scalar(responseBody, '$.code') = '0' GROUP by api order by max_duration desc limit 20

# 接口耗时TOP20
serviceId: footstone-doc or serviceId: footstone-seal or serviceId: footstone-doc or serviceId: footstone-flow | select api, arbitrary(traceId) as traceId, arbitrary(duration) as duration, arbitrary(serviceId) as serviceId group by api order by duration desc limit 20

2.6、提取json字符串中的内容

# 查询指定端的请求量:
serviceId: webserver | select clientName,count(clientName) from (SELECT  json_extract_scalar(replace(requestHeader, '-', ''), '$.xtsignclientid') as clientName from log) where clientName is not null GROUP by clientName order by clientName LIMIT 100


# 查询指定时间段内发起的企业列表
serviceId:footstone-doc and (api:"/v2/processes/start" or api:"/v2/processes/startByFlowTemplate") | select json_extract_scalar(replace(requestHeader, '-', ''), '$.xtsignopentenantid') AS tenantId,  count(1) as startCount where json_extract_scalar(replace(requestHeader, '-', ''), '$.xtsignopentenantid') != json_extract_scalar(replace(requestHeader, '-', ''), '$.xtsignopenoperatorid') group by tenantId limit 1000

2.7、查询指定的返回值中的数量

# 查询主体指定意愿方式中包含短信意愿的统计数
serviceId:footstone-doc AND status:200 and (api: '/v2/processes/start/initConfig') | select json_array_contains(json_extract(responseBody, '$.data.willTypes'), 'CODE_SMS') as will_size, count(*) as count group by will_size

2.8、查询接口的平均耗时

# 计算startCooperationFlow接口平均耗时
app:footstone-doc and Content: startCooperationFlow | select round(avg(try_cast(rpc_cost as bigint))) from (select regexp_extract(Content, 'Time : (.*) ms', 1) as rpc_cost from log where regexp_like(Content, 'Time :'))

2.9、查询每个接口的调用量

# 查询footstone-doc在用接口总数,并统计每个接口调用量(json格式),从中单独统计/v2/processes/start的调用情况
serviceId:footstone-doc AND status:200 | SELECT histogram(api) as apiCountMap, cardinality(histogram(api)) as apiCount, element_at(histogram(api), '/v2/processes/start') as startCount

# 统计footstone-doc每个接口调用量(多行多列格式)
serviceId:footstone-doc AND status:200 | SELECT histogram_u(api)

2.10、查询每个端的接口的调用量

# 通过map函数统计每个端接口调用情况
serviceId: webserver | SELECT element_at(headerMap, 'x-tsign-client-id') as clientId, count(*) as count from (select try_cast(json_parse(requestHeader) AS map(varchar, varchar)) as headerMap from log) where contains(map_keys(headerMap), 'x-tsign-client-id') group by clientId order by clientId limit 100

2.11、查询各个端不同发起方式的情况

serviceId: webserver not method: options and (api: '/v2/processes/start' or api:'/v2/processes/startByFlowTemplate') not api: 'initConfig' | select client, startWay, startWayCount, round ( startWayCount * 1.0 / sum(startWayCount) over(partition by client), 3) AS startWayCount_percentage, dense_rank() over(partition by client ORDER by startWayCount desc) AS startWayCount_rank from (select json_extract_scalar(upper(replace(requestHeader, '-', '')), '$.XTSIGNCLIENTID') as client, CASE WHEN json_extract_scalar(requestBody, '$.scene') = '2' and regexp_like(api, 'startByFlowTemplate')  THEN '预览模板发起' WHEN json_extract_scalar(requestBody, '$.scene') = '2' THEN '模板发起' WHEN json_extract_scalar(requestBody, '$.scene') = '1' and regexp_like(api, 'startByFlowTemplate') THEN '指定位置直接发起' ELSE '直接发起' END as startWay, count(1) as startWayCount from log group by client, startWay) where client is not null

2.12、查询不同地区的ip调用情况

ip_to_city(x) 分析目标IP地址所属城市, 返回结果为城市的中文名称。

ip_to_country(x) 分析目标IP地址所属国家或地区, 返回结果为国家或地区的中文名称。

ip_to_province(x) 分析目标IP地址所属省份, 返回结果为省份的中文名称。

serviceId: webserver not status: 200 | select ip_to_country(clientIp) as country, ip_to_province(clientIp) as province, ip_to_city(clientIp) as city, count(1) as count group by country, province, city order by count desc

2.13、url中提取参数

url_extract_parameter(x, parameter name) 从URL的查询部分中提取指定参数的值。

url_extract_path(x) 从URL中提取访问路径信息。

serviceId: webserver and method: get not status: 200 and api: '/athenaContract/pdf2TextByFileKey' | select url_extract_path(url), url_extract_parameter(url, 'fileKey')

2.14、电话号码

mobile_carrier(x) 分析电话号码所属运营商。
mobile_city(x) 分析电话号码所属城市。
mobile_province(x) 分析电话号码所属省份。

serviceId: webserver and url:uniqueId | select uniqueId, mobile_province(try_cast(uniqueId as bigint)), mobile_city(try_cast(uniqueId as bigint)), mobile_carrier(try_cast(uniqueId as bigint)) from (select DISTINCT  url_extract_parameter(url, 'uniqueId') as uniqueId from log) where regexp_like(uniqueId, '\d{11}')
正文到此结束
本文目录