场景描述
共享电动车企业使用函数服务搭建服务端。在函数服务中开启日志,把程序日志写到日志服务,同时在函数代码中使用SDK把运营日志写入到日志服务,使用日志服务进行日志分析。
在日志服务中,用户可以:
- 开发查看日志调试程序,确保程序的逻辑符合逻辑。
- 运维设置监控和报警,监控服务状态是否正常。
- 客服调查用户投诉。
- 运营分析数据,创建漏斗,分析留存率、转化率。
实验步骤
1. 开通服务和上传代码包
首先需要开通函数计算、日志服务,开通步骤和准备工作见文档:函数服务实践文章。
2. 确认日志服务Project和logstore已经被创建
打开日志服务控制台
可以看到华东2已经创建了一个project,以yunqi-demo-开头,这个project是在函数服务的代码里创建的。(注:如果已经在华东2创建了project,会使用用户创建的第一个project)
在logstore列表,我们能看到两个logstore,分别是fc-debug-log程序日志和ebike业务日志:
3. 配置函数服务的日志
打开函数服务控制台,选择刚刚创建的project和logstore, 选择AliyunLogFullAccess权限,点击保存:
进入 lock 这个函数:
点击触发事件,输入事件的json,并且保存。然后点击执行:
事件样例:
{
"Uid" : "abc",
"DeviceId" : "abcdef",
"Mobile" : "18612345678",
"SessionId" : 23456,
"Logitude": 120.0776645,
"Latitude": 30.131852
}
同样可以尝试下unlock方法,事件样例:
{
"Mobile" : "18612345678",
"DeviceId" : 123456,
"Logitude": 120.0776645,
"Latitude": 30.131852
}
4.查询
1. 在日志服务控制台,分别打开 两个标签,一个是fc-log,另一个是ebike:
2. 开发者调查函数调用情况。在 fc-log查询页面:输入lock ,点击查询:
此外,可以在ebike logstore中,查询op:unlock 或op:un* 或者unlock。
3. 客服收到用户投诉,某一次请求异常,那我们先找到userid:xxx, 然后找到requestid进入ebike查询: 查询userid:458 ,找到request id:
可以通过Request到其他系统中去找具体原因
5. 运维同学为了监控线上的异常情况,打开ebike查询页面,查看十分钟内开锁次数最多的用户,输入query:
query No. 1:
op : unlock| select count(1) as pv ,userid group by userid, __time__ - __time__% 600 order by pv desc
点击查询,然后右上角点击『另存为快速查询』:
然后点击『添加到dashboard』,选择新建dashboard,名称为ebike-monitor:
右上角添加报警,选择刚刚保存的快速查询,输入相关参数和手机号码:
查看开锁和关锁的延时情况,修改query为:
query No. 2:
op:lock or op : unlock | select date_format(from_unixtime(__time__ - __time__% 3600),'%m-%d %H:%i') as time, avg(latency)as avg_latency, approx_percentile(latency,0.9) as top_10_percent_latency ,approx_percentile(latency,0.8) as top_20_percent_latency group by time order by time limit 10000
点击保存到dashboard,选择已经存在的dashboard: ebike_monitor:
为了快速查看延时的分布情况,我们把所有的延时均匀的划分到是个桶里,看每个桶中的均值是多少,以及对应的日志条数是多少?
查看函数服务每分钟的请求pv:
query No. 3:
op:lock or op : unlock | select date_format(from_unixtime(__time__ - __time__% 3600),'%m-%d %H:%i') as time,count(1) as pv group by time order by time limit 10000
点击保存到dashboad,图表名称命名为ebike_pv
查看电池使用情况:
query No. 4:
op:lock | select sum(used_battery) as hourly_used_battery , date_format (from_unixtime(__time__ - __time__% 3600),'%m-%d %H:%i') as time group by time order by time limit 25
6. 运营同学需要日志数据做统计分析。在ebike查询页面,输入query:
query No. 5:
* | select sum(case when op = 'lock' then 1 else 0 end) as lock_pv, sum(case when op = 'unlock' then 1 else 0 end) as unlock_pv , date_format(from_unixtime(__time__ - __time__ % 3600),'%m-%d %H:%i') as time group by time order by time limit 100
点击保存到dashboard,参考上文步骤,新建一个dashboard名称为ebike_dashboard:
输入更多query,保存到dashboard中:
查看不同省份的占比:
query No. 6:
* | select count(1) as count, ip_to_province(ip) as province group by province
查看不同城市的占比:
query No.7:
* | select count(1) as count, ip_to_city(ip) as city group by city
查看不同网路运营商的占比:
query No.8:
* | select count(1) as count, ip_to_provider(ip) as provider group by provider
一天内的总收入:
query No. 9:
op : lock | select sum(money) as income
用户的支付手段占比:
query No. 10:
op:lock | select count(1) as pv , pay_channel group by pay_channel
7. 打开保存的dashboard:
左侧选择新建标签:
在右侧弹出窗口中,选择刚刚创建的dashboard:
如果需要修改图表,点击每张图右上角的箭头;如果需要刷新页面,可以点击刷新按钮:
关注我们
欢迎添加VIP钉钉群进行咨询:
附录
函数触发事件:
{
"Uid" : "abc",
"DeviceId" : "abcdef",
"Mobile" : "18612345678",
"SessionId" : 23456,
"Logitude": 120.0776645,
"Latitude": 30.131852
}
本文所使用全部query:
op : unlock| select count(1) as pv ,userid group by userid, __time__ - __time__% 600 order by pv desc
2.
op:lock or op : unlock | select date_format(from_unixtime(__time__ - __time__% 3600),'%m-%d %H:%i') as time, avg(latency)as avg_latency, approx_percentile(latency,0.9) as top_10_percent_latency ,approx_percentile(latency,0.8) as top_20_percent_latency group by time order by time limit 10000
3.
op:lock or op : unlock | select date_format(from_unixtime(__time__ - __time__% 3600),'%m-%d %H:%i') as time,count(1) as pv group by time order by time limit 10000
4.
op:lock | select sum(used_battery) as hourly_used_battery , date_format (from_unixtime(__time__ - __time__% 3600),'%m-%d %H:%i') as time group by time order by time limit 25
5.
* | select sum(case when op = 'lock' then 1 else 0 end) as lock_pv, sum(case when op = 'unlock' then 1 else 0 end) as unlock_pv , date_format(from_unixtime(__time__ - __time__ % 3600),'%m-%d %H:%i') as time group by time order by time limit 100
6.
* | select count(1) as count, ip_to_province(ip) as province group by province
7.
* | select count(1) as count, ip_to_city(ip) as city group by city
8.
* | select count(1) as count, ip_to_provider(ip) as provider group by provider
9.
op : lock | select sum(money) as income
10.
op:lock | select count(1) as pv , pay_channel group by pay_channel